Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 bortz, 11-06-2023, 08:04 AM
      47 responses
      1,607 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      9 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      19 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      6 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      15 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X