Announcement

Collapse
No announcement yet.

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:	162
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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      72 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X