Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting a Variable

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

    Setting a Variable

    Hi Support,
    if I set a variable (previously declared as private double variable1 = 0; in the Variable section) to a fixed value at the time I enter a trade in the onBarUpdate() method as described below
    Code:
    if(Close[0] > Close[1])
    {
    [B]double variable1 = High[0] - Low[0]; [/B]
    entryOrder = EnterLongLimit(0,false,1,Close[0],"Buy"); 
    }
    for how long does this variable maintain the value stored (e.i. High[0] - Low[0] of the bar i entered)?

    Assuming after 10 bars in the trade I call this variable to adjust my stop (code below), would this be still the same value set at the time I entered the trade?

    Code:
    if(Close[0] > High[1] + [B]variable1[/B])
    stopOrder = ExitLongStop(0,true,1,Close[0]+variable1,"TrailingStop",Buy");
    Last edited by bendjo; 08-23-2013, 06:30 AM.

    #2
    Hi bendjo,

    The value of the variable will be retained until it is reset. In your case, it will be reset for each bar where Close[0] > Close[1]. So, if you want to retain the original value and NOT reset it every time the condition is true, then you'll need to add some additional logic to prevent that block of code from executing. For example:

    Close[0] > Close[1] && position.MarketPosition == MarketPosition.Flat

    which will prevent the variable from being recalculated when you're currently in a position.

    Comment


      #3
      well received thanks. I guess it work the same with something like this:
      Code:
      if(Position.MarketPosition == MarketPosition.Flat)
      { if(Close[0] > Close[1])
      {
      ...
      Originally posted by coolmoss View Post
      Hi bendjo,

      The value of the variable will be retained until it is reset. In your case, it will be reset for each bar where Close[0] > Close[1]. So, if you want to retain the original value and NOT reset it every time the condition is true, then you'll need to add some additional logic to prevent that block of code from executing. For example:

      Close[0] > Close[1] && position.MarketPosition == MarketPosition.Flat

      which will prevent the variable from being recalculated when you're currently in a position.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      157 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      91 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      143 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      131 views
      1 like
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      107 views
      0 likes
      Last Post CarlTrading  
      Working...
      X