Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

stop loss not executing

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

    stop loss not executing

    i have a strategy that enters short and has a stop loss 1 tick above the high of the entry bar for the position with a profit target order of 4 ticks. it seems to have arbitrary orders where the stop loss order does not execute and the position is held until "Exit on session close" for a massive loss. is there a way to prevent this? any help is greatly appreciated!

    code that creates the orders:
    ====
    string signalName = "{0}|stop|market|" + barIndex;
    priceStop -= TickSize;
    signalName = string.Format(signalName, "short");
    SetStopLoss(signalName, CalculationMode.Ticks, (High[0] + TickSize), false);
    SetProfitTarget(signalName, CalculationMode.Ticks, 4);
    EnterShortStopMarket(1, priceStop, signalName);
    ====

    output from traceorders:
    ====
    VALID Trend -- Direction: Down, Bar Index: 2009
    1/6/2023 9:48:27 AM Strategy 'MyStrategy/-1': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='short|stop|market|2009' Mode=Ticks Value=3914.75 IsSimulatedStop=False IsMarketIfTouched=False
    1/6/2023 9:48:27 AM Strategy 'MyStrategy/-1': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='short|stop|market|2009' Mode=Ticks Value=4 IsSimulatedStop=False IsMarketIfTouched=False
    1/6/2023 9:48:27 AM Strategy 'MyStrategy/-1': Entered internal SubmitOrderManaged() method at 1/6/2023 9:48:27 AM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=3912.00 SignalName='short|stop|market|2009' FromEntrySignal=''
    bar index: 2009
    short: price 3912.25 price - ticksize 3912​
    ====

    Click image for larger version

Name:	stoploss_not_triggered_chart.png
Views:	236
Size:	544.3 KB
ID:	1265757

    #2
    Hello im_an_algo_trader,

    Thank you for your post and for your patience.

    I see this is how you have set up your stop loss:
    SetStopLoss(signalName, CalculationMode.Ticks, (High[0] + TickSize), false);

    WIth CalculationMode.TIcks, the stop loss will be placed a number of ticks away from the entry price. High[0] + TickSize as a value would be trying to place the Stop Loss over 3900 ticks away from the entry price, which is not valid.

    In this case, it seems you are trying to calculate a price as a value so you should use CalculationMode.Price and call SetStopLoss() dynamically in OnBarUpdate() prior to the entry being executed. Here is the help guide page for SetStopLoss() which describes how each calculation mode works:


    The SamplePriceModification reference sample demonstrates how to reset the stop loss when the position is flat when using it dynamically in OnBarUpdate():


    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi Emily,

      Thanks for your reply! I am putting my stoploss one tick above the high of my signal bar and for shorts my entry is one tick below the low of my signal bar. If I understand you correctly, if I set the CalculationMode to Price, the statement SetStopLoss(signalName, CalculationMode.Price, (High[0] + TickSize), false) would be valid i.e. High[0] = 3900 and TickSize = 0.25 then the stoploss price would be 3900.25. Would the SetProfitTarget(signalName, CalculationMode.Ticks, 4) have to change as well or is it fine? I want it to take profits 4 ticks lower than my entry price for a short.

      Also, if I kept it to Ticks in the SetStopLoss method, would I have to determine the total number of ticks that comprised the price range from my entry price and the protective stop for that bar? For example, entry price is $10 and the High[0] is $12 and protective stop High[0] + TickSize is $12.25, so the total number of ticks for the stoploss would be 9 i.e. (12.25 - 10)/0.25. Therefore the code would be SetStopLoss(signalName, CalculationMode.Ticks, 9, false)?

      Your help is greatly appreciated!
      Last edited by love2code2trade; 08-27-2023, 12:58 PM.

      Comment


        #4
        Hello love2code2trade,

        Thank you for your reply.
        • If I understand you correctly, if I set the CalculationMode to Price, the statement SetStopLoss(signalName, CalculationMode.Price, (High[0] + TickSize), false) would be valid i.e. High[0] = 3900 and TickSize = 0.25 then the stoploss price would be 3900.25.
          • Yes, that is correct. You will need to call this dynamically in OnBarUpdate() and reset the price when your strategy is flat, as mentioned in the tips on the SetStopLoss() page:
            • "Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position​"
        • Would the SetProfitTarget(signalName, CalculationMode.Ticks, 4) have to change as well or is it fine? I want it to take profits 4 ticks lower than my entry price for a short.
          • No, this would not need to change. Having it set to calculate based on ticks with a value of 4 will set your profit target 4 ticks away from your entry price. This is not considered a dynamic price, so you could even call SetProfitTarget() in OnStateChange during State.Configure if you'd like.
        If you kept SetStopLoss() in ticks for the calculation mode, that is correct. You would have to figure out how many ticks there are between your entry price and where you would like to set the stop loss. In either scenario, whether you choose to use Price or Ticks for calculation mode, you will need to be sure you are calling SetStopLoss() prior to calling the entry method.

        Please let us know if we may be of further assistance.

        Comment


          #5
          Emily, Thankyou for this clarification! I have a follow up question vis-a-vis setting the SetStopLoss method. If I use CalculationMode.Ticks (Ticks away from entry average entry) setting, that only has to be done once for the strategy, right? All subsequent calls to EnterShortStopMarket/EnterLongStopMarket will adhere to the default stoploss and adjust dynamically with the entry price? Also, how does "average entry" differ than my concept of an entry at a specific price as entered into the EnterShortStopMarket/EnterLongStopMarket?

          Comment


            #6
            Hello love2code2trade,

            Thank you for your reply.

            Correct; if you use CalculationMode.Ticks and you want to keep that number to the same amount of ticks for every stop loss that is set, you only need to do that once for the strategy and that is why you could even set it in State.Configure rather than updating the value in OnBarUpdate().

            Average entry is the average entry for a position; for example, if you enter long with one contract and then "scale in" to the position with another buy order for one contract at a different price, the average entry for the position would be both of those entry prices divided by two (the quantity. Average entry is the sum of all entries for the position divided by the number of contracts.
            • how does "average entry" differ than my concept of an entry at a specific price as entered into the EnterShortStopMarket/EnterLongStopMarket?
              • If your entry only consists of one entry price for the full quantity of the entry, the average entry will be equal to the price that the EnterShortStopMarket/EnterLongStopMarket was filled at. If you are using a quantity greater than 1 and there is a partial fill at one price then the rest of the quantity is filled at a different price, the average entry price will be the average of those fill prices based on the quantities filled.
            Please feel free to reach out with any additional questions or concerns.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            50 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            126 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            69 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X