{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Combination" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sklearn.cluster import KMeans\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import mplfinance as mpf\n", "from mplfinance.original_flavor import candlestick_ohlc\n", "import finnhub\n", "import matplotlib.dates as mpl_dates\n", "import numpy as np\n", "from datetime import *" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from sklearn.cluster import KMeans\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def get_optimum_clusters(df, saturation_point = 0.05):\n", " '''\n", " :param df: dataframe\n", " :param saturation_point: The amount of difference we are willing to detect\n", " :return: clusters with optimum K centers\n", " This method uses elbow method to find the optimum number of K clusters\n", " We initialize different K-means with 1..10 centers and compare the inertias\n", " If the difference is no more than saturation_point, we choose that as K and move on\n", " '''\n", "\n", " wcss = []\n", " k_models = []\n", " labels = []\n", "\n", " size = min(21, df.shape[0])\n", " for i in range(1, size):\n", " # kmeans = KMeans(n_clusters = i, init='k-means++', max_iter=300, n_init=10, random_state=0)random\n", " kmeans = KMeans(n_clusters = i, init='k-means++', max_iter=300, n_init=10,random_state =0)\n", " kmeans.fit(df)\n", " wcss.append(kmeans.inertia_) # Sum of squared distances of samples to their Closest cluster center\n", " k_models.append(kmeans)\n", " labels.append(kmeans.labels_)\n", "\n", " # Compare differences in inertias until it's no more than saturation_point\n", " optimum_k = len(wcss)-1\n", " for i in range(0, len(wcss)-1):\n", " diff = abs(wcss[i+1] - wcss[i])\n", " if diff < saturation_point:\n", " optimum_k = i\n", " break\n", "\n", " # print(\"Optimum K is \" + str(optimum_k + 1))\n", " optimum_clusters = k_models[optimum_k]\n", " labels = labels[optimum_k]\n", "\n", " return (optimum_clusters.cluster_centers_, labels)\n", "\n", "def isSupport(df,i):\n", " support = df['Low'][i] < df['Low'][i-1] and df['Low'][i] < df['Low'][i+1] \\\n", " and df['Low'][i+1] < df['Low'][i+2] and df['Low'][i-1] < df['Low'][i-2]\n", " return support\n", "\n", "def isResistance(df,i):\n", " resistance = df['High'][i] > df['High'][i-1] and df['High'][i] > df['High'][i+1] \\\n", " and df['High'][i+1] > df['High'][i+2] and df['High'][i-1] > df['High'][i-2] \n", " return resistance" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# Setup client\n", "finnhub_client = finnhub.Client(api_key = \"bt3efpf48v6tfcs816eg\")\n", "\n", "# Stock candles\n", "ticker = 'AAPL'\n", "start_time = int(datetime(2021, 5, 18, 0, 0).replace(tzinfo = timezone.utc).timestamp())\n", "end_time = int(datetime(2021, 6, 18, 0, 0).replace(tzinfo = timezone.utc).timestamp())\n", "res = finnhub_client.stock_candles(ticker, 'D', start_time, end_time)\n", "\n", "stock = pd.DataFrame(res)\n", "stock = stock.rename(columns = {'t':'Date', 'o':'Open', 'h':'High', 'l':'Low', 'c':'Close', 's':'status', 'v':'volumn'})\n", "stock['Date'] = pd.to_datetime(stock['Date'], unit = 's')\n", "stock = stock.set_index('Date')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Close | \n", "High | \n", "Low | \n", "Open | \n", "status | \n", "volumn | \n", "
---|---|---|---|---|---|---|
Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
2021-05-18 | \n", "124.85 | \n", "126.990 | \n", "124.7800 | \n", "126.560 | \n", "ok | \n", "63342929 | \n", "
2021-05-19 | \n", "124.69 | \n", "124.915 | \n", "122.8600 | \n", "123.160 | \n", "ok | \n", "92611989 | \n", "
2021-05-20 | \n", "127.31 | \n", "127.720 | \n", "125.1000 | \n", "125.230 | \n", "ok | \n", "76857123 | \n", "
2021-05-21 | \n", "125.43 | \n", "128.000 | \n", "125.2100 | \n", "127.820 | \n", "ok | \n", "79295436 | \n", "
2021-05-24 | \n", "127.10 | \n", "127.940 | \n", "125.9400 | \n", "126.010 | \n", "ok | \n", "63092945 | \n", "
2021-05-25 | \n", "126.90 | \n", "128.320 | \n", "126.3200 | \n", "127.820 | \n", "ok | \n", "72009482 | \n", "
2021-05-26 | \n", "126.85 | \n", "127.390 | \n", "126.4200 | \n", "126.955 | \n", "ok | \n", "56575920 | \n", "
2021-05-27 | \n", "125.28 | \n", "127.640 | \n", "125.0800 | \n", "126.440 | \n", "ok | \n", "94625601 | \n", "
2021-05-28 | \n", "124.61 | \n", "125.800 | \n", "124.5500 | \n", "125.570 | \n", "ok | \n", "71311109 | \n", "
2021-06-01 | \n", "124.28 | \n", "125.350 | \n", "123.9400 | \n", "125.080 | \n", "ok | \n", "67637118 | \n", "
2021-06-02 | \n", "125.06 | \n", "125.240 | \n", "124.0500 | \n", "124.280 | \n", "ok | \n", "59278862 | \n", "
2021-06-03 | \n", "123.54 | \n", "124.850 | \n", "123.1300 | \n", "124.680 | \n", "ok | \n", "76229170 | \n", "
2021-06-04 | \n", "125.89 | \n", "126.160 | \n", "123.8500 | \n", "124.070 | \n", "ok | \n", "75169343 | \n", "
2021-06-07 | \n", "125.90 | \n", "126.320 | \n", "124.8321 | \n", "126.170 | \n", "ok | \n", "71057550 | \n", "
2021-06-08 | \n", "126.74 | \n", "128.460 | \n", "126.2101 | \n", "126.600 | \n", "ok | \n", "74403774 | \n", "
2021-06-09 | \n", "127.13 | \n", "127.750 | \n", "126.5200 | \n", "127.210 | \n", "ok | \n", "56877937 | \n", "
2021-06-10 | \n", "126.11 | \n", "128.190 | \n", "125.9400 | \n", "127.020 | \n", "ok | \n", "71186421 | \n", "
2021-06-11 | \n", "127.35 | \n", "127.440 | \n", "126.1000 | \n", "126.530 | \n", "ok | \n", "53522373 | \n", "
2021-06-14 | \n", "130.48 | \n", "130.540 | \n", "127.0700 | \n", "127.820 | \n", "ok | \n", "96906490 | \n", "
2021-06-15 | \n", "129.64 | \n", "130.600 | \n", "129.3900 | \n", "129.940 | \n", "ok | \n", "62746332 | \n", "
2021-06-16 | \n", "130.15 | \n", "130.890 | \n", "128.4610 | \n", "130.370 | \n", "ok | \n", "91815026 | \n", "
2021-06-17 | \n", "131.79 | \n", "132.550 | \n", "129.6500 | \n", "129.800 | \n", "ok | \n", "96721669 | \n", "
2021-06-18 | \n", "130.46 | \n", "131.510 | \n", "130.2400 | \n", "130.710 | \n", "ok | \n", "108953309 | \n", "