Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple SMA strategy crashing

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

    Simple SMA strategy crashing

    I have been out of the loop for a while regarding NT, so if I missed something obvious forgive me.

    Here is the code below this is my question:
    Code:
            protected override void OnBarUpdate()
            {
                // Only trade between 7:45 AM and 1:45 PM
            if (ToTime(Time[0]) >= 104500 && ToTime(Time[0]) <= 124500)
            {
                if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                {
                    EnterLong();
                    //Print("Enter Long"); 
                }
                else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
                {
                    EnterShort();
                    //Print("Enter Short");
                }
                //Print("Enter Short"); // If I only allow this line to run after time range is entered, then we don't crash.
            }
                
    
            }
    Ok, the above code crashes when I do the following. By the way, this is nothing more than the SMA dilio wrapped in a time range if-then. I used that as a base to reduce the equation.


    1. Market replay, doesn't really matter the data, but June 1 is what I used.

    2. Dom was open, Strategy tab from control panel was in focus, and of course market replay. Nothing else open.

    3. I started the replay at 9:16am, and at about 10:55 or so, the program froze. It is around the 10:45am mark or near the Begin of my time range. So in other words, when we branch into the loop, boink.

    4. I tried to get rid of the indicators and just said Print blah blah and that loop worked fine. So it has something to do with putting the indicators inside the time frame.

    Am I just doing something really wrong like should I instantiate the SMA outside of the loop or something or does that not matter? I forgot, so forgive me if I am asking a noob question. But I don't think it should be crashing.


    Thanks,


    r2kTrader

    #2
    I made sure the object was valid and otherwise I exited the branch.

    Code:
            protected override void OnBarUpdate()
            {
                SMA FastSMA = SMA(Fast);
                SMA SlowSMA = SMA(Slow);
                
                // Only trade between 10:45 AM and 12:45 PM
            if (ToTime(Time[0]) >= 104500 && ToTime(Time[0]) <= 124500)
            {
                if (FastSMA == null || SlowSMA == null)
                    return;
                
                if (CrossAbove(FastSMA, SlowSMA, 1))
                {
                    EnterLong();
                }
                else if (CrossBelow(FastSMA, SlowSMA, 1))
                {
                    EnterShort();
                }
                //Print("Enter Short");
            }
    I need to brush up. I forgot this was c# lol.

    Comment


      #3
      Hi r2kTrader,

      To clarify, did the adjustments in your latest post resolve the behavior you were previously seeing.
      TimNinjaTrader Customer Service

      Comment


        #4
        Tim,

        Well I thought it did, but not 100%. Here is the thing. I am running a high amount of tick data just to test and doing like 100 ticks or 50 ticks. What happens is this. When it looks like I have enough bars to start firing, the whole system freezes and doesn't respond. If I wait like 3 - 5 minutes, it stops coughing and resumes. So I don't know exactly what the issue is. By checking for null, I think I just kept it from doing it more.

        I was running at 500x btw.

        Thanks for the reply.

        Comment


          #5
          r2kTrader, would you mind posting the complete script so I can test this out on my end?
          AustinNinjaTrader Customer Service

          Comment


            #6
            Austin,

            After careful thought, and because I am working with tick data, and because each set of tick data is in a flat file, I decided a simple defrag may be the key to cracking the case.

            My earlier findings are this:

            1. I am not locking up.
            2. Replay is more responsive.


            You might want to experiment with this or offer it up as a potential suggestion.

            So:

            DEFRAG YOUR TICK DATA AND YOUR REPLAY WILL NOT ONLY BE MORE STABLE, BUT REWINDING AND FAST FORWARDING ON THE REPLAY PANEL WILL BE WAY FASTER WITH LESS HOURGLASS ANNOYANCE AND PAIN.

            Hope this helps others.


            r2kTrader

            Comment


              #7
              Hi r2kTrader,

              I see, that makes sense, a defrag would certainly be valuable there. Thanks for reminding us of that.
              TimNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Tim View Post
                Hi r2kTrader,

                I see, that makes sense, a defrag would certainly be valuable there. Thanks for reminding us of that.
                Tim,

                Would it hurt to throw a defrag routine into the replay feature? Or at a minimum, recommend defragging based on the response time (aka hourglass delay during a rewind)?

                Seems to me, this is important information for people to have and they shouldn't have to dig for it. Furthermore, if people are not asking the question, they won't find this anyway.

                Please think about it and the value of education those who use Replay.


                Thank you,


                r2kTrader

                Comment


                  #9
                  Hello r2kTrader,

                  Thank you for your comments and suggestions on this.
                  Ryan M.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  607 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  353 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  105 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  560 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  561 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X