How to create an Ichimoku trading bot using Python (Part 2: Building The Bot)

Ape finance
Coinmonks
Published in
3 min readJul 29, 2021

--

Photo by Clément Hélardot on Unsplash

You can read Part 1 here.

Now let's get to building the Ichimoku trading bot using python.

First, I wanted to store the necessary information into dictionary. Additionally I needed to get around 80days of stock data from yahoo finance just by inputting the Ticker into the function( I’m using yahoo finance API btw). Therefore, here are the functions defined:

def stock(a,b,c,d,e,f):
x = {"exchange":a,"ticker":b,"price":c,"atr":d,"sma_vol30":e,"current_vol":f}
return x
def getinfo(x):
symbol=x
state=yf.Ticker(symbol).history(period="max")
state = state[len(state)-80:len(state)]
Data=state.loc[:,['Open','High','Low','Close']]
Data=Data.iloc[::-1]
return Data

Next I had to calculate all the necessary values, I wrote a function that takes the input of a list of all the tickers from the exchange(in this case the list of tickers is defined as s). I then create an empty buy list to store all the dictionaries of tickers that fulfill the conditions. Next I pass each ticker symbol into a for loop which then calculates the necessary indicators(conversion, base, span A, span B) before checking whether that stock fulfills the given requirements (this is done using if-else statements). If the stock matches all the specified conditions as put forth in the Ichimoku strategy, the stock information will be stored as a dictionary into the buy list.

Following which I wrote a function to determine how much shares I should buy for the tickers picked out by the previous ichimoku_analyser function. This is calculated simply using the amount assigned to each stock divide by the stock price.

def weightage(price,max_per_stock):
weight=[math.floor(num)for num in list(max_per_stock/price)]
return weight

Now that we have all the functions down, we just read the list of tickers and execute all the functions. I then arranged all the relevant information into a dataframe (this includes the buy date and a 20% stoploss). The dataframe is arranged based on the stocks relative volume hence I can easily prioritize the more actively traded stocks. I also filtered out stocks with a recent large increase within one day as it is likely to create false signals. This is because the cloud is caused by that large one-time-off increase rather than an uptrend.

I then append the resulting dataframe into a csv file named portfolio.csv

df.to_csv(‘portfolio.csv’,mode=’a’, index=False)

Lastly I designed the a function to read the portfolio.csv file and identify those stocks that have a sell signal or hit stop loss. I then put all these relevant information of the stocks to be sold into another dataframe.

The dataframe with all the information of the stocks to be sold(sell_df) is appended to a sell_list.csv while the portfolio.csv file is updated to remove the stocks to be sold.

df_port.to_csv(‘portfolio.csv’,mode=’w’, index=True)
sell_df.to_csv(‘sell_list.csv’,mode=’a’, index=False)

And we are done! You have now created a ichimoku cloud trading bot that analyses stocks based on the conditions set out in our previous ichimoku strategy.

Join Coinmonks Telegram Channel and learn about crypto trading and investing

Also, Read

--

--