NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 08-21-2007, 10:42 AM   #1
nybangali
Junior Member
 
Join Date: Aug 2007
Posts: 23
Thanks: 0
Thanked 0 times in 0 posts
Default 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 ?
nybangali is offline  
Reply With Quote
Old 08-21-2007, 11:01 AM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 08-21-2007, 06:31 PM   #3
nybangali
Junior Member
 
Join Date: Aug 2007
Posts: 23
Thanks: 0
Thanked 0 times in 0 posts
Default 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");
}
}


Quote:
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.
nybangali is offline  
Reply With Quote
Old 08-21-2007, 07:05 PM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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
File Type: zip nybangaliTest.zip (906 Bytes, 8 views)
NinjaTrader_Josh is offline  
Reply With Quote
Old 08-21-2007, 10:17 PM   #5
nybangali
Junior Member
 
Join Date: Aug 2007
Posts: 23
Thanks: 0
Thanked 0 times in 0 posts
Default 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.

Quote:
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)
nybangali is offline  
Reply With Quote
Old 08-22-2007, 12:03 AM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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 is offline  
Reply With Quote
Old 08-22-2007, 12:18 AM   #7
nybangali
Junior Member
 
Join Date: Aug 2007
Posts: 23
Thanks: 0
Thanked 0 times in 0 posts
Default 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.

Quote:
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.
nybangali is offline  
Reply With Quote
Old 08-22-2007, 12:43 AM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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 is offline  
Reply With Quote
Old 08-22-2007, 12:48 AM   #9
nybangali
Junior Member
 
Join Date: Aug 2007
Posts: 23
Thanks: 0
Thanked 0 times in 0 posts
Default 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.

Quote:
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.
nybangali is offline  
Reply With Quote
Old 08-22-2007, 12:54 AM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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
NinjaTrader_Josh is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Real-time Marketh Depth Indicator Reference Sample NinjaTrader_Ray Indicator Development 17 11-29-2011 02:21 AM
TRIN, TICK charts not updating in real time Brightstar Charting 13 09-20-2011 09:20 AM
Once NT6 data becomes delayed it doesn't catch up to be real-time dfogg Connecting 11 08-17-2007 07:41 AM
RemoveDrawObject not working in real time JangoFolly Indicator Development 2 07-12-2007 10:13 AM
Backfilling Forex Real-Time Charts with Historical Data JohnL Connecting 4 05-24-2007 11:42 AM


All times are GMT -6. The time now is 01:09 PM.