Hopefully I'm just overlooking something silly here.
So, I have a strategy that can use either a trail stop, or normal stoploss. I'm attempting to implement a trail stop as an alternate option for this, considering we have a target price.
However, orders I'm seeing submitted vary from what I'm logging for development.
For example, today I had the following trade targeted (pulled from my logs):
| 9/4/2024 7:26:15 AM | NinjaScript | Playing to target 19283.125 from current ask 18967.25 with stop loss 126.35 (18840.9) and target of 315.875 (19283.125) |
- Note that stop loss values (126.35) were provided directly to SetTrailStop() with ticks as the CalculationMode.
Now, what is the issue?
Upon entering a long position, the resulting order is filled:
| 9/4/2024 7:26:15 AM | Order | Order=<redacted>' New state='Filled' Instrument='NQ SEP24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=1 Fill price=18972.75 Error='No error' Native error='' |
We have a logged stoploss of 126.35 ticks. Or, security value of 18840.9, based off of our fill price thisstop loss would be 18800.
Prior to this, we set the trailing stop loss via SetTrailStop(). Here is a simulated snippet of the code, where desiredPosition.StopLossDifference = 126.35:
SetTrailStop(POSITION_NAME, CalculationMode.Ticks, desiredPosition.StopLossDifference, true);
Resulting is the following trail stop position:
| 9/4/2024 7:26:15 AM | Order | Order=<redacted> New state='Trigger pending' Instrument='NQ SEP24' Action='Sell' Limit price=0 Stop price=18941.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco=<redacted> |
Now, as the position is being processed, this value changes a bit, since we are "simulating" the trail stop until our order is processed. For our purposes, lets just call the stop price fluctuating by +-5 ticks. Note that the filled stop price varies by ~100 ticks.
After a minute or so, our trail stop is filled, indicated by the following the log:
| 9/4/2024 7:27:03 AM | Order | Order=<redacted> Name='Trail stop' New state='Filled' Instrument='NQ SEP24' Action='Sell' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco=<redacted> |
If our saved variables are indicating a 100+ tick stop price, why is the stop price differing so heavily?
Is there any information that I'm missing on why my trailing stop loss is being filled at a massive difference from what we intended?
When running in strategy analyzer, we see profits on this day (1 minute to one tick data series), however we see losses when running in real time.

Comment