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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      67 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      36 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      59 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      62 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      53 views
      0 likes
      Last Post CarlTrading  
      Working...
      X