I am fairly new into programming and my aim is to make a trading bot. I started a few months ago on codecademy and would like to share my story on the road so that I can receive feedback to improve my trading bot.
I will try to give an insight into my thinking when I wrote the piece of code you see below. I hope you guys would like to give me some feedback and tell me which course I can follow on Codecademy to understand what I am doing on a specific subject.
For backtesting the strategy I found a structure I would like to follow because it is quite clear. This model exists of 3 core functions:
- A trading strategy
- A chart where I can see buy and sell signals
So my class is Trading Model and the init function has two methods, namely the currency pair I want to trade and the DataFrame with the data.
from binance.client import Client
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import plot
next is the function to get data from the API:
def getData(self): client=Client(api_key=keys.RKey,api_secret=keys.RSecretKey) btctrade=client.get_klines(symbol=self.symbol,interval=Client.KLINE_INTERVAL_1HOUR) # put in dataframe and clean-up df = pd.DataFrame.from_dict(btctrade) df = df.drop(range(6, 12), axis=1) # rename columns col_names = ['time', 'open', 'high', 'low', 'close', 'volume'] df.columns = col_names # transform values from strings to floats for col in col_names: df[col] = df[col].astype(float) return df
I think the above code is pretty clear, get data and clean it up in order to get only the useful data.
Next I make the strategy I want to backtest. The strategy I want to backtest is as follows:
- When the price of the nth element is higher that the previous 7 elements, we have a buy signal
- A buy signal automatically means a sell_signal when the price is going 3% lower
- When the price is going up 10%, the sell signal must go up 10% as well the price is going up 10%, the sell signal has to go up 10% as well
- With every 10% increase of the price, sell_signal must move up 10% as well
For example, buy_signal is at 100, sell signal is at 97, but when price is going up 10% sell signal is going to 106.7. When price hits 106.7 it’s a sell.
- A buy_signal can only appear when the previous buy has been sold, so one trade at a time.
So my first step was to define a function which makes another column on the dataframe where buy signals appear. Only the prices that are the highest of the previous 7 elements must get in the column. I made a variable for the rolling max and then tried to make a for loop which gets rolling max as buy_signal.
df['rollingmax']=df.rolling(7)['high'].max().diff().gt(0) buy_signals= sell_signals= stoploss=None for row in range(1, len(df['high'])): if df['rollingmax'][row]==True: buy_signals.append([df['time'][row], df['high'][row]]) for row in range(1,len(df['low'])): if df['low'][row]>0,97*buysignals
For now I am struggling to fill the sell_signals column, especially to compare [‘low’] prices with the last buy_signal and see if it is lower than the 3% decline in buy_signal I want. Then I also need to figure out how I can make the sell price go higher 10% when buy_signal is going up 10% higher.
I hope you guys will help me with some feedback on my piece of code, I will post regular updates so anybody who is interested can see how you can code your own tradingbot