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 StockTrader88, 03-06-2021, 08:58 AM
      44 responses
      3,966 views
      3 likes
      Last Post jhudas88  
      Started by rbeckmann05, Today, 06:48 PM
      0 responses
      4 views
      0 likes
      Last Post rbeckmann05  
      Started by rhyminkevin, Today, 04:58 PM
      4 responses
      54 views
      0 likes
      Last Post dp8282
      by dp8282
       
      Started by iceman2018, Today, 05:07 PM
      0 responses
      5 views
      0 likes
      Last Post iceman2018  
      Started by lightsun47, Today, 03:51 PM
      0 responses
      8 views
      0 likes
      Last Post lightsun47  
      Working...
      X