EMA crossover system

Kaushik_Sanghani
I am not able to run the below function, it is running but sometimes not able to square off or take orders early? so can someone help me?

def system_order(symbol):
start_time = time.time()
end_time = start_time + 60*60*5
first_trade = True
while time.time() < end_time:
ohlc = fetchOHLC('5minute',3)
ema = EMA(ohlc,symbol,5,20)
df = ema_system(ema,symbol)
try:
if first_trade == True:
if(df['signal'][-1] == 1):
print(df)
print(df['signal'][-1])
#placeMarketOrder(symbol, 'buy',1)
print('long')
elif(df['signal'][-1] == -1):
#placeMarketOrder(symbol, 'sell',1)
print(df)
print(df['signal'][-1])
print('short')
first_trade = False
else:
print(df['signal'][-1])
print(df)
print('continue')
else:
if(df['signal'][-1] == 1):
print(df)
print(df['signal'][-1])
#placeMarketOrder(symbol, 'buy',2)
print('long')
elif(df['signal'][-1] == -1):
print(df)
print(df['signal'][-1])
#placeMarketOrder(symbol, 'sell',2)
print('short')
else:
print(df)
print(df['signal'][-1])
print('continue')
time.sleep(60*5 - ((time.time() - start_time)%(60*5)))

except KeyboardInterrupt:
print('Key board inturept')
  • SRIJAN
    If orders take too long to execute,then it means your code is still processing the strategy part. You maybe doing some heavy computations. How much time it takes to fire order once buy/sell signal is generated??
  • Kaushik_Sanghani
    Thank you for replying Srijan. It was not much I think 10-sec max. Have you seen any bug or logical error in my code?
  • SRIJAN
    SRIJAN edited December 2021
    I don't know the logic you are using. How can i say if there is any logical error?? If there would be any syntax error, python would show error. Actually,if you are doing algo trading,first you should try 2-3 trades manually according to your strategy,and then check if the results from algo are accurate. This is beacuse sometimes,there would be no syntax error or anything which would directly show up,but there could be that some lines of your code might not be intended properly. Like,if you're using a while loop and if else statement inside it. Its possible that you might write the statement to be executed by the if else statement in the while loop indentation. The code will still work,no direct errors,but false signals can be generated. So, always check your indentations. And 10 sec is not the time to fire order,once buy/sell signal is generated. Its the time of your whole code to run. Orders take less than a second,they are as fast as placing from kite web or app unless there are some network issues. To check the time to fire order,just put a variable start_time=dt.now() just before the order_placing line. And right after the order_placing line,print(dt.now()-start_time). That's the real time to place order,it should be around 1 second or lesser.
    And if 10 sec is too much for you,you can try upgrading your system,like processor,ram etc.
  • tarun07
    I have a few doubts regarding your above strategy.
  • tarun07
    @SRIJAN would like to connect with you
Sign In or Register to comment.