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