View Full Version : Strategy in real time problem
nybangali
08-21-2007, 10:42 AM
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 ?
NinjaTrader_Josh
08-21-2007, 11:01 AM
Have you tried using the Historical bool to stop the strategy from running in the past?
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.
nybangali
08-21-2007, 06:31 PM
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");
}
}
Have you tried using the Historical bool to stop the strategy from running in the past?
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.
NinjaTrader_Josh
08-21-2007, 07:05 PM
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:
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 PMAre 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:
// 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 int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 1, 10);because of the syntax being MRO(Condition condition, int instance, int lookBackPeriod)
nybangali
08-21-2007, 10:17 PM
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.
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:
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 PMAre 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:
// 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 int barsAgo = MRO(delegate {return Close[0] > Open[0];}, 1, 10);because of the syntax being MRO(Condition condition, int instance, int lookBackPeriod)
NinjaTrader_Josh
08-22-2007, 12:03 AM
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.
nybangali
08-22-2007, 12:18 AM
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.
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.
NinjaTrader_Josh
08-22-2007, 12:43 AM
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.
nybangali
08-22-2007, 12:48 AM
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.
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.
NinjaTrader_Josh
08-22-2007, 12:54 AM
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/HelpGuideV6/helpguide.html?SimulatorTab