Why did Strategy take this trade?

I use an SMA/EMA based Strategy that I wrote myself with Strategy Builder, and yesterday, it took a trade that it shouldn’t have and I’m very confused. When I enable the strategy to see what trade it would have taken yesterday, it showed exactly the trade it would have done based on my conditions, which on the left. Meanwhile, the trade it actually took when I enabled the strategy in real time is on the right.

This strategy only has one Short condition, and all three MA’s pictured here should be in the exact opposite order, and price action should have closed a bar above the pink EMA to trigger the short condition. There should have been no reason for a short here whatsoever. Why did this happen in real time and how can I prevent this from happening again?

You would need to look at the logic of your code and how it updates to figure out why it possibly isn’t working as intended.

Thanks, I figured as much, but where do I start? What do I need to look at?

I’m not too sure about how you would do it with strategy builder, but unlocking the code and adding print statements to determine how the values you are using are evaluating at the time of the trade would be my first step. Also, if you are using OnBarClose, vs OnPriceChange/OnEachTick makes a difference.

I could certainly create an unlocked version where I can add print statements. I am currently using OnBarClose. I attempted to use ChatGPT to assist me with this issue and it created a code with OnEachTick, but it ended up taking multiple trades per single candle when I used it’s code in Playback, which I didn’t want. Also attempted to fix this, and it made a code that only triggered one condition per candle, and ultimately the results were worse than before. I’ll attempt print statements first, probably.

Depending on what you use for OnBarUpdate would change how the logic works. So it depends on how you want the logic to work. For example, if you want something that triggers on the break of the high of the previous candle, then you would need something like OnPriceChange or OnEachTick, otherwise you wouldn’t enter until the bar is done closing. So it depends on what you want the logic to be. Depending on what the logic is, it could enter differently historically vs in realtime.

In this specific instance in the screenshot, it was supposed to trigger a long as per the left side of the screenshot, as the close of the bar broke that yellow EMA. The only reason a short would trigger is if price action broke the pink EMA. That’s why I don’t understand why this short was triggered at all, the condition wasn’t anywhere close to being met.

Looking at the screenshot won’t tell you why the code isn’t working. You need to look at the code, although the screenshot will be the first indication that something is wrong. The likely explanation is there is some loop hole in your code that is causing it to take that trade it isn’t supposed to. You’ll have to look at the code, possibly use prints, for instance, print the values that cause the long and short trades to occur to find out which is active/true. Perhaps there is a value that you need to reset to false, but haven’t done so in this case, allowing the other conditions to complete the if statement and take the short trade. We won’t be able to tell until we find out what your code is doing.