Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using variables set in OnPositionUpdate

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

    Using variables set in OnPositionUpdate

    I am setting a variable within OnPositionUpdate which seems to not be available in the OnBarupdate method. I do not know C# very well (obviously). How is this done? Here is the relevant sections of my code:

    protected override void OnBarUpdate()
    {Print(Time[0].ToString() + " lngstop " + lngstop);
    }

    protected override void OnPositionUpdate(IPosition position)
    {
    if (position.MarketPosition == MarketPosition.Long)
    {double lngstop = swing(swlength).SwingLow[Swing(swlength).SwingLowBar(0,1,200)];
    Print(Time[0].ToString() + position.ToString() + " " + lngstop);
    }
    }

    The print in the OnBarUpdate method shows lngstop = 0 while the print in the OnPositionUpdate shows the correct value.

    I want to use the lngstop value in the OnBarUpdate method to exit a trade. I declare the variable as follows in the variable section : private double lngstop; - is this what should change?

    #2
    Hello Verge,

    Thank you for your post.

    You are declaring your variable an additional time within OnPositionUpdate()
    Code:
     
     
    protected override void OnPositionUpdate(IPosition position) 
    { 
    if (position.MarketPosition == MarketPosition.Long) 
    {[COLOR=red][B]double[/B][/COLOR] lngstop = swing(swlength).SwingLow[Swing(swlength).SwingLowBar(0,1,200)];
    Print(Time[0].ToString() + position.ToString() + " " + lngstop);
    } 
    }
    Remove the word "double" and then recheck your values.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Duh. Thank you

      Thank you Ryan - it works now!

      Regards

      Verge

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      60 views
      0 likes
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      145 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      161 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      97 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      283 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Working...
      X