Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Inconsistent Trail/General Stoploss

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Inconsistent Trail/General Stoploss

    Hey NinjaTrader folks!
    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)
    ​- Yes, this didn't necessarily hit, but that is what development is about. Trial and error.
    - 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:
    Code:
    SetTrailStop(POSITION_NAME, CalculationMode.Ticks, desiredPosition.StopLossDifference, true);
    Note that desiredPosition.StopLossDifference does not change throughout execution of these orders. Consider this constant while reading.

    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.
    Last edited by caderdev; 09-04-2024, 11:45 PM.

    #2
    Hello caderdev,

    In this case I would suggest to make a sample strategy and post that instead of the description, I am not able to follow what may have happened in that case without also knowing how the code was evaluated. The easiest way to debug this situation is to make a new strategy that has a return for historical bars meaning it will only place 1 trade in realtime. Then use a small timeframe chart like 10 seconds and have itt enter a position and submit targets. Check that the targets are placed correctly and if any other actions are needed beyond that you could also do those actions once to simulate the problem. Based on what code is made for that we can see if that would be expected when testing it. I would highly suggest to keep the code as simple as possible, for example like the SampleMACrossOver so we can isolate just what is required to see the problem.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    52 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    48 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X