0

As the subject says, I have some live trading code, in Python. I had been using it successfully for well over a week before my issue began. Suddenly, yesterday morning, when my code began running, it would open trades and close them almost as soon as they had been opened [as opposed to waiting until a certain amount of time (ExitThreshold)] had been reached. Here is the errant excerpt of code:

p = trades.OpenTrades(accountID= accountID); pv = client.request(p)
for i in range(0, len(pv["trades"])):
   openTime = datetime.datetime.strptime(pv["trades"][i]["openTime"][:19], '%Y-%m-%dT%H:%M:%S')
   if (datetime.datetime.utcnow() - openTime).seconds >= ExitThreshold:
     closeID = pv["trades"][i]["id"]
     r = trades.TradeClose(accountID, tradeID= closeID, data= TradeCloseRequest(units= "ALL").data); rv = client.request(r)

I have done some dubugging, stepped into the execution and repeatedly confirmed that the if condition is False. Nonetheless, the trade is closed regardless. As I had said, this code was previously performing as per expectations for well over a week and there have been no changes to it. So why this sudden change in behaviour???

  • 1
    Basically, the condition is true. You could add `print((datetime.datetime.utcnow() - openTime).seconds, ExitThreshold)` at the beginning of the `if` statements body to see that it's the case. – ForceBru May 18 '21 at 13:01
  • But the condition is unequivocally and confirmed False. As mentioned in the post, my debugging attempts confirm this. And, even aside from that, the ExitThreshold is at least 60 minutes (3,600 seconds) long. Nonetheless, the if statement executes regardless and closes trades which are not even 1 second old. – Indeterminate May 18 '21 at 13:10
  • However, this is impossible: no sane Python implementation would execute an `if` statement whose condition is false. How exactly and when did you "step into the execution"? How did you check that the condition was false, _but_ the body of the `if` statement was being executed? IMO, it's easier to use that `print` statement and see what will be printed – ForceBru May 18 '21 at 13:15
  • I agree with you that this should be "impossible". But my experience says otherwise... I stepped in using ipdb. I stepped into the if statement and confirmed the two values of datetime.datetime.utcnow() and openTime, as well as the >= inequality, seperately. Because the code was executing via a demo account, I was able to view, in real time, the trades being opened and closed less than a second afterwards. I've come up with a temporary fix of adding an else to the if statement and everything is now working as expected. Nonetheless, I posted this hoping someone might know what would cause this. – Indeterminate May 18 '21 at 13:56
  • Is it possible this is a time zone issue? According to Google, for example, in Morocco Daylight Savings Time was suspended for the month of Ramadan and resumed on May 16, 2021. Possibly other locations also changed. Just a guess... – evergreen May 18 '21 at 14:22

1 Answers1

1

I managed to solve the issue quite some time ago, but I seem to have neglected to post and update. Oh well, better late than never.

The issue was remedied simply by replacing the if statement with the one below:

datetime.datetime.utcnow() >= (openTime + datetime.timedelta(seconds= ExitThreshold))

The two statements/conditions mean different things and the above is the correct way to formulate my intention.