|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import pickle, pathlib, os\n", |
| 10 | + "import pandas as pd" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 2, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "file_name = 'TRXBTC_1h_EMA25.bin'\n", |
| 20 | + "home_path = str(pathlib.Path.home())\n", |
| 21 | + "data_path = os.path.join(home_path, file_name)\n", |
| 22 | + "\n", |
| 23 | + "df = pickle.load(open(data_path, 'rb'))" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": 5, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "pd.set_option('precision', 10) # output 10 decimal places\n", |
| 33 | + "\n", |
| 34 | + "# copy value from df\n", |
| 35 | + "ema_n_0 = df.iloc[-1].at['ema-25'] # latest ema value\n", |
| 36 | + "ema_n_1 = df.iloc[-2].at['ema-25'] # last ema value\n", |
| 37 | + "\n", |
| 38 | + "# trade_factor > 0 = price is rising / bullish trend\n", |
| 39 | + "# trade_factor < 0 = price is falling / bearish trend\n", |
| 40 | + "trade_factor = (ema_n_0 / ema_n_1) -1" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 8, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [ |
| 48 | + { |
| 49 | + "name": "stdout", |
| 50 | + "output_type": "stream", |
| 51 | + "text": [ |
| 52 | + "Profit so far: -0.23\n" |
| 53 | + ] |
| 54 | + } |
| 55 | + ], |
| 56 | + "source": [ |
| 57 | + "# Validation Stage 2\n", |
| 58 | + "\n", |
| 59 | + "n_cnt = 0\n", |
| 60 | + "\n", |
| 61 | + "\n", |
| 62 | + "buy_price = 0.0\n", |
| 63 | + "sell_price = 0.0\n", |
| 64 | + "\n", |
| 65 | + "buy_factor = 0.004\n", |
| 66 | + "sell_factor = -0.002\n", |
| 67 | + "\n", |
| 68 | + "\n", |
| 69 | + "def validate(_buy_factor, _sell_factor):\n", |
| 70 | + " \n", |
| 71 | + " b_bought = False\n", |
| 72 | + " \n", |
| 73 | + " profit = 0.0\n", |
| 74 | + " \n", |
| 75 | + " for index, row in df[1:].iterrows(): # iterate over rows\n", |
| 76 | + " # start at second row and stop at penultimate row\n", |
| 77 | + "\n", |
| 78 | + " ema_n_1 = df.iloc[index][['ema-25']].values[0] # latest ema value\n", |
| 79 | + " ema_n_2 = df.iloc[index-1][['ema-25']].values[0] # latest ema value\n", |
| 80 | + " trade_factor = (ema_n_1 / ema_n_2) -1\n", |
| 81 | + "\n", |
| 82 | + " if trade_factor > _buy_factor and not b_bought: # ema-25 rises\n", |
| 83 | + " buy_price = df.iloc[index][['close']].values[0]\n", |
| 84 | + " b_bought = True\n", |
| 85 | + "\n", |
| 86 | + " elif trade_factor < _sell_factor and b_bought: # ema-25 is falling\n", |
| 87 | + " sell_price = df.iloc[index][['close']].values[0]\n", |
| 88 | + " b_bought = False\n", |
| 89 | + "\n", |
| 90 | + " profit += (sell_price / buy_price) -1\n", |
| 91 | + "\n", |
| 92 | + " \n", |
| 93 | + " return profit\n", |
| 94 | + "\n", |
| 95 | + "\n", |
| 96 | + "\n", |
| 97 | + " \n", |
| 98 | + "profit = validate(buy_factor, sell_factor)\n", |
| 99 | + "\n", |
| 100 | + "print(\"Profit so far: {:.2f}\".format(profit))" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 9, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "trading_factors = []\n", |
| 110 | + "\n", |
| 111 | + "#iterate over buy factor from 0.001 to 0.009\n", |
| 112 | + "for buy_factor in range(1, 9, 1): \n", |
| 113 | + " buy_factor = buy_factor * 10**(-3)\n", |
| 114 | + " \n", |
| 115 | + " #iterate over buy factor from -0.001 to -0.009\n", |
| 116 | + " for sell_factor in range(1, 9, 1): \n", |
| 117 | + " sell_factor = sell_factor * -10**(-3) \n", |
| 118 | + " \n", |
| 119 | + " profit = validate(buy_factor, sell_factor)\n", |
| 120 | + " \n", |
| 121 | + " trading_factors.append((buy_factor, sell_factor, profit))" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 10, |
| 127 | + "metadata": { |
| 128 | + "scrolled": true |
| 129 | + }, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "def select_profit(record):\n", |
| 133 | + " return record[2]\n", |
| 134 | + "\n", |
| 135 | + "trading_factors.sort(key=select_profit, reverse=True)" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": 11, |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "data": { |
| 145 | + "text/plain": [ |
| 146 | + "[(0.002, -0.002, 0.03436033979967046),\n", |
| 147 | + " (0.002, -0.003, 0.014158319597650126),\n", |
| 148 | + " (0.006, -0.007, 0.0),\n", |
| 149 | + " (0.006, -0.008, 0.0),\n", |
| 150 | + " (0.007, -0.007, 0.0),\n", |
| 151 | + " (0.007, -0.008, 0.0),\n", |
| 152 | + " (0.008, -0.007, 0.0),\n", |
| 153 | + " (0.008, -0.008, 0.0),\n", |
| 154 | + " (0.001, -0.006, -0.00834652156004101),\n", |
| 155 | + " (0.002, -0.001, -0.031875007204029915),\n", |
| 156 | + " (0.002, -0.004, -0.03634673090740048),\n", |
| 157 | + " (0.002, -0.005, -0.03634673090740048),\n", |
| 158 | + " (0.002, -0.006, -0.03634673090740048),\n", |
| 159 | + " (0.001, -0.002, -0.04735133105695555),\n", |
| 160 | + " (0.006, -0.001, -0.07200000000000017),\n", |
| 161 | + " (0.007, -0.001, -0.07200000000000017),\n", |
| 162 | + " (0.008, -0.001, -0.07200000000000017),\n", |
| 163 | + " (0.006, -0.002, -0.08799999999999997),\n", |
| 164 | + " (0.007, -0.002, -0.08799999999999997),\n", |
| 165 | + " (0.008, -0.002, -0.08799999999999997),\n", |
| 166 | + " (0.006, -0.003, -0.10399999999999998),\n", |
| 167 | + " (0.007, -0.003, -0.10399999999999998),\n", |
| 168 | + " (0.008, -0.003, -0.10399999999999998),\n", |
| 169 | + " (0.001, -0.007, -0.11715481171548114),\n", |
| 170 | + " (0.001, -0.008, -0.11715481171548114),\n", |
| 171 | + " (0.002, -0.007, -0.11715481171548114),\n", |
| 172 | + " (0.002, -0.008, -0.11715481171548114),\n", |
| 173 | + " (0.001, -0.003, -0.1216481482294206),\n", |
| 174 | + " (0.001, -0.004, -0.13781080727432682),\n", |
| 175 | + " (0.001, -0.005, -0.13781080727432682),\n", |
| 176 | + " (0.003, -0.007, -0.1422764227642277),\n", |
| 177 | + " (0.003, -0.008, -0.1422764227642277),\n", |
| 178 | + " (0.004, -0.007, -0.1422764227642277),\n", |
| 179 | + " (0.004, -0.008, -0.1422764227642277),\n", |
| 180 | + " (0.006, -0.004, -0.14400000000000013),\n", |
| 181 | + " (0.006, -0.005, -0.14400000000000013),\n", |
| 182 | + " (0.006, -0.006, -0.14400000000000013),\n", |
| 183 | + " (0.007, -0.004, -0.14400000000000013),\n", |
| 184 | + " (0.007, -0.005, -0.14400000000000013),\n", |
| 185 | + " (0.007, -0.006, -0.14400000000000013),\n", |
| 186 | + " (0.008, -0.004, -0.14400000000000013),\n", |
| 187 | + " (0.008, -0.005, -0.14400000000000013),\n", |
| 188 | + " (0.008, -0.006, -0.14400000000000013),\n", |
| 189 | + " (0.001, -0.001, -0.16570250554082178),\n", |
| 190 | + " (0.005, -0.007, -0.16600790513833985),\n", |
| 191 | + " (0.005, -0.008, -0.16600790513833985),\n", |
| 192 | + " (0.003, -0.002, -0.23027642276422766),\n", |
| 193 | + " (0.004, -0.002, -0.23027642276422766),\n", |
| 194 | + " (0.005, -0.001, -0.23800790513834003),\n", |
| 195 | + " (0.003, -0.003, -0.24627642276422768),\n", |
| 196 | + " (0.004, -0.003, -0.24627642276422768),\n", |
| 197 | + " (0.005, -0.002, -0.2540079051383398),\n", |
| 198 | + " (0.005, -0.003, -0.27000790513833983),\n", |
| 199 | + " (0.003, -0.004, -0.2862764227642278),\n", |
| 200 | + " (0.003, -0.005, -0.2862764227642278),\n", |
| 201 | + " (0.003, -0.006, -0.2862764227642278),\n", |
| 202 | + " (0.004, -0.004, -0.2862764227642278),\n", |
| 203 | + " (0.004, -0.005, -0.2862764227642278),\n", |
| 204 | + " (0.004, -0.006, -0.2862764227642278),\n", |
| 205 | + " (0.003, -0.001, -0.29898351489443764),\n", |
| 206 | + " (0.004, -0.001, -0.29898351489443764),\n", |
| 207 | + " (0.005, -0.004, -0.31000790513834),\n", |
| 208 | + " (0.005, -0.005, -0.31000790513834),\n", |
| 209 | + " (0.005, -0.006, -0.31000790513834)]" |
| 210 | + ] |
| 211 | + }, |
| 212 | + "execution_count": 11, |
| 213 | + "metadata": {}, |
| 214 | + "output_type": "execute_result" |
| 215 | + } |
| 216 | + ], |
| 217 | + "source": [ |
| 218 | + "trading_factors" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": null, |
| 224 | + "metadata": {}, |
| 225 | + "outputs": [], |
| 226 | + "source": [] |
| 227 | + } |
| 228 | + ], |
| 229 | + "metadata": { |
| 230 | + "hide_input": false, |
| 231 | + "kernelspec": { |
| 232 | + "display_name": "Python 3", |
| 233 | + "language": "python", |
| 234 | + "name": "python3" |
| 235 | + }, |
| 236 | + "language_info": { |
| 237 | + "codemirror_mode": { |
| 238 | + "name": "ipython", |
| 239 | + "version": 3 |
| 240 | + }, |
| 241 | + "file_extension": ".py", |
| 242 | + "mimetype": "text/x-python", |
| 243 | + "name": "python", |
| 244 | + "nbconvert_exporter": "python", |
| 245 | + "pygments_lexer": "ipython3", |
| 246 | + "version": "3.7.3" |
| 247 | + } |
| 248 | + }, |
| 249 | + "nbformat": 4, |
| 250 | + "nbformat_minor": 2 |
| 251 | +} |
0 commit comments