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

clearing embeded indicators from memory

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

    clearing embeded indicators from memory

    I have a question.

    Let's say somewhere in my strategy, I call for the values of a MACD.

    double MyDiff = MACD(12,26,9).Diff[0];

    It is my understanding, that by calling up the MACD(12,26,9.# dataseries, that NT will build that dataseries, and it becomes available from then on in my strategy. Great.

    But then, let's say my strategy decides to no longer use MACD(12,26,9) and begins to use MACD(10,20,5), can I CLEAR THE MEMORY used by MACD(12,26,9)??? The reason is because I have developed my strategy to frequently switch to a different setting of the MACD, and when I run my strategy overnight (NOT in the backtester/optimizer - just normal running of my strategy) it seems that when I get up in the morning NT has crashed from out of memory.

    So to avoid this crash I need to add code to clear indicators that I called in my code but are no longer needed.

    Can I do this: MACD(12,26,9) = new MACD(); ??

    Or if not, is there ANY way to clear it. Please don't tell me there is no way to clear it and that I'm doomed to use up all my memory if I tell my strategy to frequently modify its variables like this.

    #2
    >> can I CLEAR THE MEMORY used by MACD(12,26,9)
    no

    Comment


      #3
      Ok, so... alternatives? if MACD(12,26,9) can't be cleared then my plan is...

      I'll take the MACD internal code, and copy it into my strategy, so that I can modify it within the strategy without a new instance of MACD() being opened every time I change a MACD variable. Plus doing that would give me control over all of the arrays used by the MACD. I'm assuming that declaring a variable with a type of DataSeries is dangerous since you are telling me that the DataSeries type can never be cleared from memory until you shut down ninjatrader? So I'll have to re-build the MACD indicator into my strategy using only double[] arrays to hold the MACD's data, thereby I can clear those variables whenever I need to change my strategy to use a different MACD.

      Anyway that's my plan of attack, if anyone has a better suggestion for clearing old indicatiors embeded in a strategy, let me know.

      One question though, in the backtester/optimizer, how does it clear each of the previous tests, when different indicator settings are used each time? Or does it not clear anything (eek).

      Comment


        #4
        Indicators hosted by a strategy are cleared out as the strategy itself is "cleared" after a backtest.

        NT7 will comes with improvements regarding memory consumption of hosted indicators. I suggested trying again after NT7 public beta is out (no ETA yet).

        Comment


          #5
          solution?

          How about MACD(12,26,9).Dispose();?

          I see that it is re-running the initialize() function after I called .Dispose(), so this is telling me that it is completely clearing the hosted indicator from the memory?

          Comment


            #6
            Unfortunately this is not supported. It may or may not work. You need to try on your own.

            Comment


              #7
              behealed,

              You are on the right track with taking the MACD internal. Depending which part of the MACD you need to use, you will need up to 4 DataSeries objects (fastEma, slowEma, macd, and macdAvg). You then need a routine to call from OnBarUpdate() that calculates the MACD in your data series objects. Having four DataSeries objects is not a big deal. While you could make it tighter with double arrays, using DataSeries objects ensures that you always have enough entries to do your calc.

              Finally, when you decide to change settings on the fly, you need to go back and recalculate the MACD far enough back based on your new parameter values (slow period + smoothing period + N bars) and how many values of the MACD you actually need to use. This recalc should be done within your current DataSeries objects, so you don't need to clear and reallocate memory.

              The following is an example routine you can call from OnBarUpdate() that shows how you could calc the MACD:

              Code:
              public void CalcMacd(IDataSeries data, // what is macd being calced on - typically the close
              		     DataSeries fastEma, DataSeries slowEma, 
              		     DataSeries macd,    DataSeries macdAvg,
              		     int fastLen, int slowLen, int macdLen) {
                  if (CurrentBar == 0) {
                      fastEma.Set(data[0]);
                      slowEma.Set(data[0]);
                      macd.Set(0);
                      macdAvg.Set(0);
                  }
                  else {
                      fastEma.Set((2.0 / (1 + fastLen)) * data[0] + (1 - (2.0 / (1 + fastLen))) * fastEma[1]);
                      slowEma.Set((2.0 / (1 + slowLen)) * data[0] + (1 - (2.0 / (1 + slowLen))) * slowEma[1]);
              
                      macd.Set(fastEma[0] - slowEma[0]);
                      macdAvg.Set((2.0 / (1 + macdLen)) * macd[0] + (1 - (2.0 / (1 + macdLen))) * macdAvg[1]);
                      // calc diff if you want it:  macd[0] - macdAvg[0];
                  }
              }
              Note, it does not calc the diff line, but that is easily added if you need it. Also the EMA is internalized as well.

              You could adapt the above routine to do your recalc, but that is an exercise for you.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by swestendorf, Today, 11:14 AM
              2 responses
              5 views
              0 likes
              Last Post NinjaTrader_Kimberly  
              Started by xiinteractive, 04-09-2024, 08:08 AM
              4 responses
              12 views
              0 likes
              Last Post xiinteractive  
              Started by Mupulen, Today, 11:26 AM
              0 responses
              1 view
              0 likes
              Last Post Mupulen
              by Mupulen
               
              Started by Sparkyboy, Today, 10:57 AM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by TheMarlin801, 10-13-2020, 01:40 AM
              21 responses
              3,917 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Working...
              X