1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import pandas as pd
from binance.client import Client
import ta
import pandas_ta as pda
import matplotlib.pyplot as plt
import numpy as np
from termcolor import colored
client = Client()
klinesT = client.get_historical_klines("ETHUSDT", Client.KLINE_INTERVAL_1HOUR, "01 january 2021")
df = pd.DataFrame(klinesT, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ])
df['close'] = pd.to_numeric(df['close'])
df['high'] = pd.to_numeric(df['high'])
df['low'] = pd.to_numeric(df['low'])
df['open'] = pd.to_numeric(df['open'])
df = df.set_index(df['timestamp'])
df.index = pd.to_datetime(df.index, unit='ms')
del df['timestamp']
print(df)
#Backtest Execution
df.drop(df.columns.difference(['open','high','low','close','volume']), 1, inplace=True)
#Relative Strength Index (RSI)
df['RSI'] =ta.momentum.rsi(close=df['close'], window=14)
df['SMA_RSI']=ta.trend.sma_indicator(close=df['RSI'], window=14)
df
#Spot Backtest
dfTest = df.copy()
# dfTest = df['2021-01-01':]
dt = None
dt = pd.DataFrame(columns = ['date','position', 'reason', 'price', 'frais' ,'fiat', 'coins', 'wallet', 'drawBack'])
def buyCondition(row, previousRow):
if df['pente_SMA_RSI']>0:
return True
else:
return False
def sellCondition(row, previousRow):
if df['pente_SMA_RSI']<0:
return True
else:
return False
for index, row in dfTest.iterrows():
#Buy market order
pente_SMA_RSI= ((df['SMA_RSI'].loc[index+1])-(df['SMA_RSI'].loc[index]))/((index+1)-index)
if buyCondition(row, previousRow) == True and usdt > 0 and buyReady == True:
#You can define here at what price you buy
buyPrice = row['close'] |
Partager