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 SalmaTrader, 07-07-2026, 10:26 PM
      0 responses
      50 views
      0 likes
      Last Post SalmaTrader  
      Started by CarlTrading, 07-05-2026, 01:16 PM
      0 responses
      22 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 06-17-2026, 10:32 AM
      0 responses
      16 views
      0 likes
      Last Post CaptainJack  
      Started by kinfxhk, 06-17-2026, 04:15 AM
      0 responses
      22 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 06-17-2026, 04:06 AM
      0 responses
      23 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Working...
      X