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

Strategy in real time problem

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

    Strategy in real time problem

    I have a strategy that considers a basic condition in the past 100 bars using the MRO function, and then triggers a trade.
    When I test the Strategy it works fine.

    However, when I run the strategy in real time, it seems to run all the trades from the beginning of time (based on Print statements in my Output window)

    Also when running the strategy in real time, the MRO function seems to pick up the condition near about the first bar of the chart, as opposed to the most recent condition trigger.

    Am I missing something simple ?

    #2
    Have you tried using the Historical bool to stop the strategy from running in the past?
    Code:
    if(Historical)
         return;
    Check to see if your condition triggers are resetting after every time it fires off. It should be fine because you said your strategy works in tests, but try putting Print statements all along your code to see where it is getting stuck on.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Strategy in real time problem

      Can't get it work even after adding Historical.
      I am attaching some sample code with a simple pattern match in the MRO function.

      The MRO condition detects the last 3 bar pattern with the middle bar being the longer than the surrounding bars... over the last 100 bars.

      The buy trigger is : if High of current bar exceeds high of last pattern match
      That's it.
      It runs fine in Strategy backtest.
      When run in real time it fires of all tests from the beginning of time.
      Adding Historical had no effect.
      Here is the code:
      if (Historical) { return;}
      int barsAgoH = MRO(delegate {return (High[0] < High[1]) && (High[2] < High[1]);}, 1, 100);
      if (barsAgoH > -1) {
      if ((Position.MarketPosition != MarketPosition.Long) && (High[0] > High[barsAgoH+1]) ) {
      Print(Time[0].ToString("") + ": Going Long. Current High Price of "+High[0].ToString()+" is higher than pattern match of " + High[barsAgoH+1] + " on : " + Time[barsAgoH+1].ToString(""));
      EnterLong("Long");
      }
      }


      Originally posted by Josh View Post
      Have you tried using the Historical bool to stop the strategy from running in the past?
      Code:
      if(Historical)
           return;
      Check to see if your condition triggers are resetting after every time it fires off. It should be fine because you said your strategy works in tests, but try putting Print statements all along your code to see where it is getting stuck on.

      Comment


        #4
        I tried using the sample code you provided, but all seems to be in order. In backtesting the print occurs every time the condition was satisfied (when I remove the Historical check). In real-time the print would only occur whenever the condition became satisfied (when the Historical check was in place). I tested it with a replay of AAPL today. The output was:
        Code:
        8/21/2007 4:45:00 AM: Going Long. Current High Price of 122.67 is higher than pattern match of 122.21 on : 8/20/2007 3:45:00 PM
        Are you putting the code in the right section? The code should be under the OnBarUpdate() section. Please upload your strategy so I can take a look at the exact problem you are facing because I can't seem to reproduce it at the moment.

        Also, NinjaTrader please note that the documentation on MRO is erroneous in the Examples section. The example:
        Code:
        // Prints the high price of the most recent up  bar over the last 10 bars 
        int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 10,  1); 
        if (barsAgo >  -1) 
            Print("The bar high was " +  High[barsAgo]);
        should be
        Code:
        int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 1,  10);
        because of the syntax being
        Code:
        MRO(Condition condition, int instance,  int lookBackPeriod)
        Attached Files
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Strategy in real time problem

          Hey Josh:
          It works now. Interesting use of the Historical function. I would never had guessed. Important to know that you need to use it as a toggle function when backtesting vs live (when using MRO only ??).

          By the way, it didn't work when I tested it live earlier today. But after disconnecting the datasource and reconnecting it worked as you described. There is probably some caching happening somewhere.

          Anyway, thanks.

          Originally posted by Josh View Post
          I tried using the sample code you provided, but all seems to be in order. In backtesting the print occurs every time the condition was satisfied (when I remove the Historical check). In real-time the print would only occur whenever the condition became satisfied (when the Historical check was in place). I tested it with a replay of AAPL today. The output was:
          Code:
          8/21/2007 4:45:00 AM: Going Long. Current High Price of 122.67 is higher than pattern match of 122.21 on : 8/20/2007 3:45:00 PM
          Are you putting the code in the right section? The code should be under the OnBarUpdate() section. Please upload your strategy so I can take a look at the exact problem you are facing because I can't seem to reproduce it at the moment.

          Also, NinjaTrader please note that the documentation on MRO is erroneous in the Examples section. The example:
          Code:
          // Prints the high price of the most recent up  bar over the last 10 bars 
          int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 10,  1); 
          if (barsAgo >  -1) 
              Print("The bar high was " +  High[barsAgo]);
          should be
          Code:
          int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 1,  10);
          because of the syntax being
          Code:
          MRO(Condition condition, int instance,  int lookBackPeriod)

          Comment


            #6
            You can use the Historical thing anytime you don't want the indicator/strategy to run on historical bars (aka real-time only). Just put it at the start of wherever you need it to not run on old bars. Hope that makes sense.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Strategy in real time problem

              Understood.
              However, this does seem like a quirk. By definition, real time execution of a strategy should imply on new data only. OnBarUpdate in real time should mean OnNewBarUpdate.
              Backtesting is a simulation of sorts so of course would be historical in nature.

              I'm thankful you found the answer for me. However, the workaround is a pain. Everytime I make a change to the code and want to backtest, I need to comment out the line. Everytime I run it live, I need to remember to uncomment it. Unless I'm misunderstanding something.

              Originally posted by Josh View Post
              You can use the Historical thing anytime you don't want the indicator/strategy to run on historical bars (aka real-time only). Just put it at the start of wherever you need it to not run on old bars. Hope that makes sense.

              Comment


                #8
                You don't necessarily have to have the Historical check. When you run a strategy in real-time it will run through all the historical bars and do a sort of "mini backtest". In this "mini backtest" it will just show you where your trades would have been, but it does not affect your account sizes and such. I personally do not use the Historical check except for occasions in which my indicator/strategy cannot calculate accurately on historical bars (e.g. cumulative bid/ask volume).

                I believe in your case you can just leave the Historical check completely out. Every time you load the strategy you will have some additional output Print lines reflecting historical trade entries and it will perform the way you want it to during real-time without a problem.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Strategy in real time problem

                  Interesting...
                  This morning when I started the strategy with live data, I couldn't figure out why it was showing a number in the Unrealized column even though the condition wasn't met. That's when I put the print statements and realized it was looking at historical data. Or so I thought.

                  I will try tomorrow with fresh data and see what it does. With all the changes I've made, I'm not sure anymore.

                  Originally posted by Josh View Post
                  You don't necessarily have to have the Historical check. When you run a strategy in real-time it will run through all the historical bars and do a sort of "mini backtest". In this "mini backtest" it will just show you where your trades would have been, but it does not affect your account sizes and such. I personally do not use the Historical check except for occasions in which my indicator/strategy cannot calculate accurately on historical bars (e.g. cumulative bid/ask volume).

                  I believe in your case you can just leave the Historical check completely out. Every time you load the strategy you will have some additional output Print lines reflecting historical trade entries and it will perform the way you want it to during real-time without a problem.

                  Comment


                    #10
                    If you loaded the strategy multiple times it could have potentially entered a position and never exited. Hence the unrealized profit value. What you could do to ensure that this is not the case is to reset your Sim101 account before adding the strategy (Warning: this will clear all your performance results).

                    To clear Sim101 account refer to the end of this page: http://www.ninjatrader-support.com/H...l?SimulatorTab
                    Josh P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ScottWalsh, Today, 04:29 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post ScottWalsh  
                    Started by rtwave, 04-12-2024, 09:30 AM
                    2 responses
                    21 views
                    0 likes
                    Last Post rtwave
                    by rtwave
                     
                    Started by tsantospinto, 04-12-2024, 07:04 PM
                    5 responses
                    69 views
                    0 likes
                    Last Post tsantospinto  
                    Started by cre8able, Today, 03:20 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post cre8able  
                    Started by Fran888, 02-16-2024, 10:48 AM
                    3 responses
                    49 views
                    0 likes
                    Last Post Sam2515
                    by Sam2515
                     
                    Working...
                    X