Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plot Dynamic Stop Loss

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

    Plot Dynamic Stop Loss

    This seems like it should be easy, but I am stuck. My strategy has stop loss orders change dynamically based on price distance and time from order entry. When I use AddPlot such that

    Code:
                if (State == State.SetDefaults)
                {......
                    AddPlot(new Stroke(Brushes.Red,2), PlotStyle.Hash, "Stop loss trigger");
    and
    Code:
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
            {
                if (order.OrderState == OrderState.Working && order.Name == "Stop loss"
                    )
                {
                    Value[0] = order.StopPrice;
                }
            }
    I get plots for the stop loss level each time the stop loss is updated. But I can't seem to figure out how to get the plots to extend to bars where the position is still open, but the stop loss was not updated. Everything I try leads to stop loss level being plotted on all bars, and where there is no position it is zero. This is even the case if I try to limit the series being printed by means of:
    Code:
    protected override void OnBarUpdate()
    ........
               if (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)
                {
                Value[0] = Value[0];
                }
    I feel like there is something obvious I am missing so I am asking here, is there a shortcut to accomplishing this? I can't null the Value[0] double...so I am stuck.

    #2
    Hello liquid150,

    Thanks for your post.

    I recommend creating a double variable at the class level and assigning it the order.StopPrice in OnOrderUpdate().

    Then in OnBarUpdate(), you can assign the double variable to the plot.

    Comment


      #3
      Originally posted by NinjaTrader_PaulH View Post
      Hello liquid150,

      Thanks for your post.

      I recommend creating a double variable at the class level and assigning it the order.StopPrice in OnOrderUpdate().

      Then in OnBarUpdate(), you can assign the double variable to the plot.
      Thank you, sir. That did the trick. I had considered doing this but hadn't tried it, thinking it wouldn't solve the "printing on inactive position bars" problem. But it did.

      Comment


        #4
        Hello liquid150,

        Thanks for your reply.

        The main thing to keep in mind is that the plot is sync'd to the OnBarUpdate of the charts data series. The OnOrderUpdate() happens asynchronously to OnBarUpdate().

        Comment


          #5
          Originally posted by NinjaTrader_PaulH View Post
          Hello liquid150,

          Thanks for your reply.

          The main thing to keep in mind is that the plot is sync'd to the OnBarUpdate of the charts data series. The OnOrderUpdate() happens asynchronously to OnBarUpdate().
          Indeed. I didn't realize just how early it occurred until adding this plot, which will help me fix an extremely rare situation where I get stopped out immediately on an order entry caused by this asynchronicity. So two birds with one stone I guess?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          64 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          139 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X