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 Hwop38, 05-04-2026, 07:02 PM
      0 responses
      161 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Started by CaptainJack, 04-24-2026, 11:07 PM
      0 responses
      309 views
      0 likes
      Last Post CaptainJack  
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      245 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      349 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      179 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Working...
      X