Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why is setstop resetting when I don't think it should be?

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

    Why is setstop resetting when I don't think it should be?

    I'm trying to understand why a stop loss order (set via SetStopLoss) is getting reset twice on the same bar (I'm printing times out to the output.txt file). Strategy is calc on bar close. The only place in which the stop is modified is in this block of code:

    Code:
    if(Position.MarketPosition == MarketPosition.Short)
    						{
    							if (Position.MarketPosition == MarketPosition.Short && Low[0] <= theoEntry - oneDone*TickSize && stopControl)
    								{
    								Print("stop updated at One & Done tick increment in the money"+ Time[0].ToString());
    								stopPrice = theoEntry + entryOffset*TickSize;
    								SetStopLoss(CalculationMode.Price,stopPrice);// one & done stop move
    								stopControl = false; //this can only be reset to true if flat at end of bar on primary series
    								}
    							// MomDot stop loss adjustment	
    							if (Position.MarketPosition == MarketPosition.Short && Close[0] > rhMomDot().Plot0[0] && High[0] + entryOffset*TickSize < stopPrice && Close[0] > Open[0])
    								{
    								Print("stop updated at momdot"+ Time[0].ToString());
    								SetStopLoss(CalculationMode.Price,High[0] + entryOffset*TickSize);//momdot stop move
    								//stopPrice = High[0] + entryOffset*TickSize;
    								}
    							else
    								{
    								SetStopLoss(CalculationMode.Price,stopPrice);//reset stop loss price to One&Done or Initial Stop
    								Print("stop RESET to stopPrice after momdot exit not elected"+ Time[0].ToString());
    								}
    }
    The strategy is in a state where the position is short, and the above code is the only code that runs when strategy is short. The output.txt shows the stop being reset to a new price, followed by the "Amended stop order" lines, then immediately resets again to the price it was previously at.

    I'm totally baffled.

    #2
    coolmoss, the strategy runs on historical and realtime data, correct?

    Do you reset the stoploss then to a tick default setting for example when you're flat with the strategy? This would be needed as otherwise an historically used / set stop value could be reused for a subsequent entry.

    Comment


      #3
      I do reset stopPrice to a new value when flat AND a new entry signal appears. And that works okay.

      What is happening in this instance, is I'm changing the stop loss (via SetStopLoss) to a new value, and output.txt shows that new value in amended order. But then, on the same bar, with the position unaltered since beginning of bar, and calc'd on bar close, the stop gets changed again. It looks like this:

      stop updated at momdot3/27/2012 9:14:55 AM
      3/27/2012 9:14:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.28 Currency=0 Simulated=False
      3/27/2012 9:14:55 AM Amended stop order: Order='NT-00177/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00116-746' Filled=0 Fill price=0 Token='a7b113f0dc2946b280b9f1e7facdca85' Gtd='12/1/2099 12:00:00 AM'
      3/27/2012 9:14:55 AM Amended stop order: Order='NT-00179/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00117-746' Filled=0 Fill price=0 Token='1bc5252517c04ccdad658c4fb2a0bb8e' Gtd='12/1/2099 12:00:00 AM'
      3/27/2012 9:14:55 AM Amended stop order: Order='NT-00181/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.28 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00118-746' Filled=0 Fill price=0 Token='a4f4756b46784ab295915bc7ea69acc2' Gtd='12/1/2099 12:00:00 AM'
      3/27/2012 9:14:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.34 Currency=0 Simulated=False
      3/27/2012 9:14:55 AM Amended stop order: Order='NT-00177/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00116-746' Filled=0 Fill price=0 Token='a7b113f0dc2946b280b9f1e7facdca85' Gtd='12/1/2099 12:00:00 AM'
      3/27/2012 9:14:55 AM Amended stop order: Order='NT-00179/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00117-746' Filled=0 Fill price=0 Token='1bc5252517c04ccdad658c4fb2a0bb8e' Gtd='12/1/2099 12:00:00 AM'
      3/27/2012 9:14:55 AM Amended stop order: Order='NT-00181/Sim101' Name='Stop loss' State=Working Instrument='JNPR' Action=BuyToCover Limit price=0 Stop price=22.34 Quantity=1 Strategy='jugKCIhistMarch27continuedRevision' Type=Stop Tif=Gtc Oco='NT-00118-746' Filled=0 Fill price=0 Token='a4f4756b46784ab295915bc7ea69acc2' Gtd='12/1/2099 12:00:00 AM'
      3/27/2012 9:16:23 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=22.34 Currency=0 Simulated=False

      You can see at first the price is changed to 22.28, then in the same bar it's changed back to 22.34 (what it had just been). No where else in the code is there is SetStopLoss statement except where a condition exists that position is flat.

      This is only running on historical data, I'm not using this strategy for real time.

      Comment


        #4
        coolmoss, looks like you have 2 conditions for the bar update that would be called sequentially allowing for a SetStopLoss modification - the first one leasing to 22.28 as stoploss and second one to the 22.34.

        I would suggest debugging your code to ensure only the intended condition is hitting and allowing the stop loss to be moved, this could be achieved for example via bool flags to streamline the code / flow control as you desire.

        Comment


          #5
          Bertrand,

          I thought that's what I was doing with the Print statements. If you note the section with an IF and an Else, they both have Print statements, and in the output.txt window, only one of those statements prints for each bar. I thought that meant only one or the other of the IF/Else was executing.

          I have Print statements at every location in the code where I have a SetStopLoss, yet not one of those Print statements executes at the portion of the output.txt window where the stop loss is getting reset incorrectly.

          I guess I would have this question then: if the stop loss is not being reset by one of my SetStopLoss, what else could be resetting it?

          Thanks much for your assistance.

          Comment


            #6
            coolmoss, I would recommend you remove code sections until you experience first of all a state that aligns with your expectations when running the script, only then include more complex logic. The stop loss would only be moved again if you call this in your OnBarUpdate() code, there's no way around. In the print you set please also incluce a timestamp and CurrentBar, so you can easily align the prints triggered with the corresponding trades.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X