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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      647 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      369 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      572 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X