Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit code causes extreme RAM usage

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

    Exit code causes extreme RAM usage

    The piece of code below uses about 7 GB of RAM in 60 seconds when trying to backtest or optimize a six month period of 1 minute bars on ES. It is located in "OnBarUpdate". Any suggestions on how I can achieve a similar break even strategy without using that code that freezes the NT and the computer every time I try to optimize? Thank you in advance.

    Code:
    if (BarsSinceEntryExecution(0, "", 0) > 0
    
    && Swing1.SwingHigh[0] > Position.AveragePrice
    
    && MAX(Highs[0], BarsSinceEntryExecution(0, "", 0))[0] > Swing1.SwingHigh[0])
    
    
    
    {
    
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    
    }
    
    else
    
    {
    
    SetStopLoss(CalculationMode.Price, Lows[0][1] - (StopLoss * TickSize));
    
    }
    ​

    #2
    It's specifically the MAX(Highs[0], BarsSinceEntryExecution.... bit that causes the problem. I even tried setting a variable to BarsSinceEntry so that the calculation would be done by the time it got to MAX, but it still makes the RAM skyrocket on even a backtest. Any suggestions would be greatly appreciated. Thanks

    Comment


      #3
      Hello zrobfrank,

      I would suggest trying to make a test script that only shows the problem and then attach that script. I would suggest to first print the BarsSinceEntryExecution(0, "", 0) value to see what values are being used when you see the problem. Based on that observation you could hard code that value into max to simplify the code further. That is to make sure its not related to BarsSinceEntryExecution(0, "", 0). Once you have simplified to remove BarsSinceEntryExecution and hard coded a value the trading logic could also be removed to confirm the memory use is with MAX.

      Comment


        #4
        Jesse,

        I added some prints and modified the code based on the findings. It still sends the RAM through the roof, even though the data displayed in the output was drastically reduced. I can run this script on a chart, but if I try to optimize over any reasonable period of time it will max out RAM and shut down. I'm not sure what else to modify. I limited the number of bars worth of "Highs" it has to store. And I double checked, if I replace the "BarsSinceEntry" with a hard number, the RAM usage goes way down. Any advice would be greatly appreciated. Thanks.

        if (BarsSinceEntryExecution() > 0 )

        {

        BarsSince = Math.Min(BarsSinceEntryExecution(), 60);

        Print(BarsSinceEntryExecution());

        }

        SetStopLoss(CalculationMode.Ticks, StopLoss);




        if (Position.MarketPosition == MarketPosition.Long

        && IsFirstTickOfBar == true


        && BarsSinceEntryExecution() > 0


        && MAX(High, BarsSince)[0] >= (Position.AveragePrice + (BreakEven * TickSize)))



        {

        StopLoss = 0;

        }

        else

        {

        StopLoss = Loss;

        }


        Last edited by zrobfrank; 08-12-2023, 02:05 PM.

        Comment


          #5
          And now NT, with a blank workspace, will eat up 2.5 GB before I can even sign into the platform; even afer I cleaned the historical data. I followed the suggestions of another post and opened in safe mode, I've deleted all workspaces, and several times used the toolkit to clear historical data, and I'm still close to 3 GB before I even sign in. Any help would be greatly appreciated. Thanks
          Last edited by zrobfrank; 08-12-2023, 10:29 PM.

          Comment


            #6
            Hello zrobfrank,

            If you are still seeing high memory usage with that script I would still suggest to continue to reduce it to find the specific line that causes that. The latest sample you provided still uses variables and other logic to drive that code. I would suggest removing all user inputs and variables and hard code values as a test. If you still see a problem with that include a full script as a sample so that we can test it. The variables that I am referring to are StopLoss, BarsSince, BreakEven.

            If your platform has high usage by its self that may be caused by something imported. I would likely suggest doing a clean install to check how that works without any customizations. You can do hat by using the following steps
            1. Exit NinjaTrader
            2. Open the documents folder
            3. Rename the NinjaTrader 8 folder to NinjaTrader 8 Old.
            4. Reinstall NinjaTrader.

            This removes all data so you can start in a fresh environment for testing.


            Comment


              #7
              Jesse,

              I followed your instructions and have not had the issue with high RAM usage since. I also had changed my code so that the break even code only ran on the first tick of every bar. I've noticed when watching it run live that it will only adjust the stop loss at the beginning of each bar, and will change the stop to break even, but if the price keeps moving towards the stop it will eventually readjust it to the original loss amount. Is there some language I am overlooking or am not familiar with that will trigger a real time break even? I've included the code I'm running below. Thank you.

              Code:
              if (Position.MarketPosition == MarketPosition.Long
              
              && IsFirstTickOfBar == true
              
              // && BarsInProgress == 0
              
              && BarsSinceEntryExecution() > 0
              
              // // && Swing1.SwingHigh[0] > Position.AveragePrice
              
              && MAX(High, BarsSince)[0] >= (Position.AveragePrice + (BreakEven * TickSize)))
              
              
              
              {
              
              StopLoss = 0;
              
              }
              
              else
              
              {
              
              StopLoss = Loss;
              
              }
              
              
              
              if (Position.MarketPosition == MarketPosition.Short
              
              && IsFirstTickOfBar == true
              
              // && BarsInProgress == 0
              
              && BarsSinceEntryExecution() > 0
              
              // && Swing1.SwingLow[0] < Position.AveragePrice
              
              && MIN(Low, BarsSince)[0] <= Position.AveragePrice - (BreakEven * TickSize))
              
              
              
              {
              
              StopLoss = 0;
              
              }
              
              else
              
              {
              
              StopLoss = Loss;
              
              }
              ​

              Comment


                #8
                Hello zrobfrank,

                I wouldn't be able to say what may be happening based only on the code. You would have to use prints to find out how your logic executes in that situation so you know what part needs to be adjusted. I would suggest adding prints into each condition body, the area between the curly braces so you can see when each condition becomes true. That would let you monitor how the logic works in that use case.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                71 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                143 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                76 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                47 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                51 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X