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

how to stop stop resetting a variable

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

    how to stop stop resetting a variable

    Click image for larger version

Name:	CustomCalculations1.jpg
Views:	106
Size:	5.3 KB
ID:	1187529

    Hello, I'm currently using this custom calculation to simulate the current gross realized for a back-test since the strategy analyzer cannot take "Account.Get(AccountItem.GrossRealizedProfitLo ss, Currency.UsDollar)".

    The variable "CurrentGrossRealizedPnL" should only be 0 at the beginning of the strategy then keep updating to a new value after every trade, but since I have it set to CalculateOnPriceChange it resets to 0 each time the method is called.

    How can I avoid resetting the variable each time the method is called?

    #2
    Hi Don22Trader1, I don't think this will answer your question, but in my strategies, setting some values at the start of the strategy (and only once) is something I do a lot, maybe it can help you with this and in these situations in general.


    So what I do is to create a variable ("StrategyFirstLoop") that indicates that the strategy is starting and assign it a value:

    Code:
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MyStrategy: Strategy
    {
    private int StrategyFirstLoop = 1;
    And then, in the section of OnBarUpdate(), I dedicate a section to anything I want to do only once, and finish that section by assigning a new value to "StrategyFirstLoop", so that this section is not read anymore after:

    Code:
    // First Loop, when OnBarUpdate() is running
    if (StrategyFirstLoop== 1)
    {
    // do everything I want to do at the beginning of the strategy
    
    StrategyFirstLoop = 0;  // this section will not run again
    }
    I hope it's not too much off-topic from your question and that it's helping a bit.

    Comment


      #3
      Hello Don22Trader1,

      Set CurrentGrossRealizedPnL to 0 in State.DataLoaded or State.Historical if you only want this set once.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by alifarahani, Today, 09:40 AM
      4 responses
      20 views
      0 likes
      Last Post alifarahani  
      Started by gentlebenthebear, Today, 01:30 AM
      3 responses
      16 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by PhillT, Today, 02:16 PM
      2 responses
      7 views
      0 likes
      Last Post PhillT
      by PhillT
       
      Started by Kaledus, Today, 01:29 PM
      3 responses
      11 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by frankthearm, Yesterday, 09:08 AM
      14 responses
      47 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Working...
      X