|
| 1 | +# Pseudo code |
| 2 | + |
| 3 | +# Import data from "data" folder |
| 4 | +import json |
| 5 | +from pprint import pprint |
| 6 | +from datetime import datetime |
| 7 | +import pandas as pd |
| 8 | +from ta.trend import EMAIndicator |
| 9 | +import matplotlib.pyplot as plt |
| 10 | + |
| 11 | +# Load json file |
| 12 | +with open('data\BTCUSDT_1m_future.json') as json_file: |
| 13 | + data = json.load(json_file) |
| 14 | + # pprint(data[-10:]) # Explore data |
| 15 | + # pprint(data[:10]) |
| 16 | + |
| 17 | +print( |
| 18 | + 'Start date: ', datetime.fromtimestamp( |
| 19 | + float(data[0][0]) / 1000 |
| 20 | + ), |
| 21 | + '\nEnd date: ', datetime.fromtimestamp( |
| 22 | + float(data[-1][0]) / 1000 |
| 23 | + ), |
| 24 | + '\nLength: ', len(data) |
| 25 | +) |
| 26 | + |
| 27 | +# Process json file |
| 28 | +closes = [] |
| 29 | +for candle in data: |
| 30 | + closes.append(float(candle[4])) |
| 31 | + |
| 32 | +print( |
| 33 | + 'Max: ', max(closes), |
| 34 | + '\nMin: ', min(closes) |
| 35 | +) |
| 36 | + |
| 37 | +df = pd.DataFrame( |
| 38 | + data = closes, |
| 39 | + columns = ['close'] |
| 40 | +) |
| 41 | + |
| 42 | +# Build technical indicators |
| 43 | +emas_Used = [3, 5, 8, 10, 12, 15, 30, 35, 40, 45, 50, 60] |
| 44 | + |
| 45 | +for x in emas_Used: |
| 46 | + ema = x |
| 47 | + indicator_ema = EMAIndicator( |
| 48 | + close=df['close'], |
| 49 | + window=x, |
| 50 | + fillna=False |
| 51 | + ) |
| 52 | + df['ema_' + str(ema)] = indicator_ema.ema_indicator() |
| 53 | +# print(df.tail()) |
| 54 | + |
| 55 | +# Strategy |
| 56 | +position = 0 |
| 57 | +open_long = [] |
| 58 | +open_short = [] |
| 59 | +close_long = [] |
| 60 | +close_short = [] |
| 61 | +pnl = [] |
| 62 | +fees = [] |
| 63 | + |
| 64 | +for i in df.index: |
| 65 | + c_min = min( |
| 66 | + df['ema_3'][i], |
| 67 | + df['ema_5'][i], |
| 68 | + df['ema_8'][i], |
| 69 | + df['ema_10'][i], |
| 70 | + df['ema_12'][i], |
| 71 | + df['ema_15'][i], |
| 72 | + ) |
| 73 | + c_max = max( |
| 74 | + df['ema_30'][i], |
| 75 | + df['ema_35'][i], |
| 76 | + df['ema_40'][i], |
| 77 | + df['ema_45'][i], |
| 78 | + df['ema_50'][i], |
| 79 | + df['ema_60'][i], |
| 80 | + ) |
| 81 | + price = df['close'][i] |
| 82 | + |
| 83 | + if position == 0: |
| 84 | + |
| 85 | + # Entry point when short term emas > long term emas |
| 86 | + if c_min > c_max: |
| 87 | + print(i, ': GO LONG') |
| 88 | + position = 1 |
| 89 | + open_long.append(price) |
| 90 | + |
| 91 | + # Short condition is met |
| 92 | + elif c_max > c_min: |
| 93 | + print(i, ': GO SHORT') |
| 94 | + position = -1 |
| 95 | + open_short.append(price) |
| 96 | + |
| 97 | + else: |
| 98 | + pass |
| 99 | + |
| 100 | + elif position == 1: |
| 101 | + |
| 102 | + # Short condition is met |
| 103 | + if c_max > c_min: |
| 104 | + print(i, ': CLOSE LONG AND GO SHORT') |
| 105 | + position = -1 |
| 106 | + close_long.append(price) |
| 107 | + open_short.append(price) |
| 108 | + fees.append(close_long[-1] * 0.01) |
| 109 | + pnl.append(open_long[-1] - close_long[-1]) |
| 110 | + |
| 111 | + else: |
| 112 | + pass |
| 113 | + |
| 114 | + elif position == -1: |
| 115 | + |
| 116 | + # Long condition is met |
| 117 | + if c_min > c_max: |
| 118 | + print(i, ': CLOSE SHORT AND GO LONG') |
| 119 | + position = 1 |
| 120 | + close_short.append(price) |
| 121 | + open_long.append(price) |
| 122 | + pnl.append(close_short[-1] - open_short[-1]) |
| 123 | + fees.append(close_short[-1] * 0.01) |
| 124 | + |
| 125 | + else: |
| 126 | + pass |
| 127 | + |
| 128 | + else: |
| 129 | + print('Error') |
| 130 | + |
| 131 | +print( |
| 132 | + 'net pnl: {} \nfees: {}'.format( |
| 133 | + round(sum(pnl) - sum(fees), 4), |
| 134 | + round(sum(fees), 4) |
| 135 | + ) |
| 136 | +) |
| 137 | + |
| 138 | +# Visualize data (last 24 hours) |
| 139 | +# df.iloc[-1440:].plot( |
| 140 | +# color = [ |
| 141 | +# '#1ab1cd', |
| 142 | +# '#6fff00', |
| 143 | +# '#7cff00', |
| 144 | +# '#a3ff00', |
| 145 | +# '#d6ff00', |
| 146 | +# '#f0ff00', |
| 147 | +# '#ffdb00', |
| 148 | +# '#ffb400', |
| 149 | +# '#ff9a00', |
| 150 | +# '#ff6700', |
| 151 | +# '#ff4000', |
| 152 | +# '#ff2700', |
| 153 | +# '#ff0000' |
| 154 | +# ] |
| 155 | +# ) |
| 156 | +# plt.show() |
0 commit comments