Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.

    Paul H.NinjaTrader Customer Service

    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().
        Paul H.NinjaTrader Customer Service

        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 kempotrader, Today, 08:56 AM
          0 responses
          6 views
          0 likes
          Last Post kempotrader  
          Started by kempotrader, Today, 08:54 AM
          0 responses
          4 views
          0 likes
          Last Post kempotrader  
          Started by mmenigma, Today, 08:54 AM
          0 responses
          2 views
          0 likes
          Last Post mmenigma  
          Started by halgo_boulder, Today, 08:44 AM
          0 responses
          1 view
          0 likes
          Last Post halgo_boulder  
          Started by drewski1980, Today, 08:24 AM
          0 responses
          4 views
          0 likes
          Last Post drewski1980  
          Working...
          X