The strategy is massively multi-pair but will be mono-window because we will use only the 5M window candlestick market data.
You will find some information about this bot in the introduction in this previous article
Only open and close price of the previous fixed candle will be required
import requests,time
#ACCOUNT CREATION
#https://www.binance.com/?ref=25068557
#OFFICIAL API DOCUMENTATION
# https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#klinecandlestick-data
#parameter
symbol = "SYSBTC"
interval = "5m"
now = time.time()
startTime = int( ( now - ( 4*(5*60.)) ) * 1000.) # we take the start as : now - (4 x 5 Minutes) to be sure to get at least two candles
endTime = int( (now+1.) * 1000. )
#we take the previous last candle
#to be sure that candle data will not change
previous_filled_5m_candle = requests.get(
"https://api.binance.com/api/v3/klines",
params={
"symbol":symbol,
"interval":interval,
"startTime":startTime,
"endTime":endTime
}
).json()[-2]
#GIVE SOMETHING LIKE...
# ~ [
# ~ 1499040000000, // Open time
# ~ "0.01634790", // Open
# ~ "0.80000000", // High
# ~ "0.01575800", // Low
# ~ "0.01577100", // Close
# ~ "148976.11427815", // Volume
# ~ 1499644799999, // Close time
# ~ "2434.19055334", // Quote asset volume
# ~ 308, // Number of trades
# ~ "1756.87402397", // Taker buy base asset volume
# ~ "28.46694368", // Taker buy quote asset volume
# ~ "17928899.62484339" // Ignore.
# ~ ]
current = {
"open": float(previous_filled_5m_candle[1]),
"close": float(previous_filled_5m_candle[4])
}
print ("current",current)
#here we force this data to be sure to detect an action
current={"open": 0.00000254, "close":0.00000297}
Now with the open (0.00000254) and close (0.00000297) price, we can detect if it is a green/bullish candle (close > open) and if yes we check if the price increase during this candle more than X%. (here X=1.10%)
Then if we need to act, we have to define our target: our sell & (re)buy limit price & quantity.
Sell price will be near the close and (re)buy near the open, both of them will be inside the body.
#params
minimize_high_percent=1./10.
minimize_low_percent=1./2.5
min_abs_supported_price_diff = 0.011
#check if bullish (green)
bullish=False
if current['open']<current['close']:
bullish=True
#get the average price
average_supported_price=(current['open']+current['close'])/2
abs_supported_price_diff_open=abs(current['open']-current['close'])/(current['open'])*100.
abs_supported_price_diff_close=abs(current['open']-current['close'])/(current['close'])*100.
#params
sell_qty = 100
round_precision = 8
print ("diff",round(abs_supported_price_diff_open,6),"%","bullish:",bullish)
#>diff 16.929134 % bullish: True
if abs_supported_price_diff_open>min_abs_supported_price_diff and bullish:
#minimize_high_percent=1./10.
sell_price=(current['open']*abs_supported_price_diff_open*(1.-minimize_high_percent)/100.)+current['open']
#minimize_low_percent=1./2.5
buy_price=(current['open']*abs_supported_price_diff_open*(minimize_low_percent)/100.)+current['open']
print ("target sell_at", sell_price)
#>target sell_at 2.927e-06
print ("conditional target buy_at", buy_price)
#>conditional target buy_at 2.712e-06
real_margin_without_fee=((sell_price-buy_price)/sell_price)*100.
real_margin_without_after_fee = real_margin_without_fee-0.5
print ("real margin without fee",real_margin_without_fee)
#>real margin without fee 7.345404851383671
buy_qty=sell_qty*(1+(real_margin_without_fee/100.*1/3))
action_args={
'action':True,
'target_sell':round(sell_price,round_precision),
'target_buy':round(buy_price,round_precision),
}
else:
action_args={
'action':False
}
print (action_args)
#>{'action': True, 'target_sell': 2.93e-06, 'target_buy': 2.71e-06}
Now as we detect an action to do, we need to send an order to the exchange.
I will explain you on my next post, how to setup your API Key & Secret (yes it is mandatory to send an authenticated api requests) then how to send your first order using the API …