samedi 5 décembre 2020

AND statement is not getting executed in Python algotrading

This is a paper trading algo code. For some reason if no. 2 and if no. 4 are not getting executed. I have used the same code for backtesting and it worked perfectly. But while when deployed live I'm getting few problems in the flow of program.Any help is greatly appreciated. Thanks!

position = 'Close'
while True:
    df =pd.DataFrame(kite.historical_data(instrument_id, from_date='2020-12-01', to_date='2020-12-04', interval='minute'))
    i = (len(df.index)-1)
    LTP = kite.ltp(symbol)[symbol]['last_price']

    if (EMACrossover_bull(df, i) or consolidation_bull(df, i)) and (position == 'Close'): #if no. 1
        position = 'Open1'
        entry = LTP
        entry_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
        target = entry*(1+tar)
        stop_loss = entry*(1-stl)
        print('{} Long position initiated at {}. Target: {} Stop Loss:{}'.format(entry_time, entry, target, stop_loss))

    if (position == 'Open1') and (LTP > target or LTP < stop_loss):  #if no. 2
        position = 'Close'
        exitt = LTP
        exit_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
        change = exitt-entry
        print('{} Long position closed at {}. G/L: {} '.format(exit_time, exitt, change))

    if (EMACrossover_bear(df, i) or consolidation_bear(df, i)) and (position == 'Close'):  #if no. 3
        position = 'Open2'
        entry = LTP
        entry_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
        target = entry*(1-tar)
        stop_loss = entry*(1+stl)
        print('{} Short position initiated at {}. Target: {} Stop Loss:{}'.format(entry_time, entry, target, stop_loss))

    if (position == 'Open2') and (LTP < target or LTP > stop_loss):  #if no. 4
        position = 'Close'
        exitt = LTP
        exit_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
        change = entry-exitt
        print('{} Short position closed at {}. G/L: {} '.format(exit_time, exitt, change))

This is the output I'm getting: enter image description here

Aucun commentaire:

Enregistrer un commentaire