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 02-06-2009, 08:53 AM   #1
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default Strategy Trades One Candle Late

I built an indicator that has characteristics of a strategy in that buy-sell situations are created. I ported the "indicator" relevant trading code to a strategy. It's identical code with the draw and print code removed. The strategy code enters trades one candle after the indicator code enters trades. A synopsis of the code is below:

Indicator code:
protected override void OnBarUpdate()
{

if (FirstTickOfBar)
{
Coded Logic

//Trying to enter an UP trade
if (X=Y) //redundant
if (X=Y)
if (X=Y)
if ((X=Y) | (X=Y))
{
upentry = true; //for the indicator
EnterLong(); //for the strategy
}
}

}

If the indicator calls the trade, say, on the first tick of bar 100, the strategy enters the trade on the first tick of bar 101. If it makes a difference, I'm using 15 minute candles.

The is a lot of logic based on bar number like this:

if ((Close[1] - Open[1]) < 0.0)
{lastred =
true;}
else
{lastred = false;}


volatility = (High[2] + High[3] + High[4] - Low[2] - Low[3] - Low[4])/3.0;

Can anyone tell me what to do? This variance makes a hugh difference as one might suspect.

Thanks!
Last edited by epcompany; 02-06-2009 at 08:57 AM.
epcompany is offline  
Reply With Quote
Old 02-06-2009, 09:05 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 977 times in 960 posts
Default

Hi epcompany, welcome to the NinjaTrader support forums!

If you run the strategy on CalculateOnBarClose = true then you will wait for the 15 min bar to close until it can place an order to be executed on the next bar, unfortunately this is the nature of things when backtesting.

For more finetuning, you can use this approach - http://www.ninjatrader-support2.com/...ead.php?t=6652
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 02-06-2009, 11:35 AM   #3
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks for the response!

Unfortunately, this is the code used:

protectedoverridevoid Initialize()
{
CalculateOnBarClose =
false;
ClearOutputWindow();}

protectedoverridevoid OnBarUpdate()
{
if (CurrentBar <40) return;

if (FirstTickOfBar)
{

place order

etc....
}

So, refering to the first post, everything I'm doing to get into a trade is done off the first tick, or so I think. My indicator gets into the trade state one bar and the strategy waits until the first tick of the next bar, or so it looks.

Thanks again!
epcompany is offline  
Reply With Quote
Old 02-06-2009, 12:06 PM   #4
eDanny
Senior Member
 
eDanny's Avatar
 
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
Default

Try checking the condition of the last bar at FirstTickOfBar.

if(FirstTickOfBar)
if (orderId1.Length == 0 && atmStrategyId1.Length == 0 && ShortSignal[1] == 1)
{Do your thing;}

It looks like some your logic is doing this already:
if ((Close[1] - Open[1]) < 0.0)
{lastred =
true;}
else
{lastred = false;}

but basically all of your logic has to reference the last bar since there is really nothing to check on the current bar at first tick.
Last edited by eDanny; 02-06-2009 at 12:24 PM.
eDanny is offline  
Reply With Quote
Old 02-06-2009, 12:27 PM   #5
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 977 times in 960 posts
Default

Are you using any embedded indicators with CalculateOnBarClose set to true? If yes, remove those references and set none in your indicators and control this behavior from the strategy.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 02-06-2009, 02:41 PM   #6
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default

eDanny and Bertrand,
Thanks very much for the interest! I appreciate it.

eDanny: I will implement your test this weekend. Thanks!

Bertrand: The only reference to CalculateOnBarClose is the " = false" one I showed. It is never referenced as true unless it picks it up in one of the called NT indicators I use. I haven't traced into them.

I do use references to other indicators in the strategy such as VMA.
I use : VMA, Math, Atan, Open, Close, High, Low, CurrentBar, ClearOutputWindow, CalculateOnBarClose and that's about it.

When running, I have a 15 minute ES chart running my custom indicator. Then I start the strategy built from the indicator from the strategy tab on the chart.

While working from market data, the indicator shows entries by drawing entry points on the chart at precisely the points I programmed as entry. The strategy trades in the sim account one candle late.

Interestingly, when running the indicator and strategy from historical data, the strategy is still one candle late.

I'm still at a loss but thanks again for the help! I'll ponder the problem using recorded data this weekend.
epcompany is offline  
Reply With Quote
Old 02-06-2009, 02:44 PM   #7
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 are calling indicators from a strategy you CANNOT have any CalculateOnBarClose line at all in the indicator. Please remove these lines from the indicator code. Thank you.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-06-2009, 03:43 PM   #8
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default

Josh,
That was VERY clear.

I shall post haste remove the statement.

As Arnold says "I'll be back" later and give the result.

I appreciate all of the help and advice!
epcompany is offline  
Reply With Quote
Old 02-17-2009, 11:17 PM   #9
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default Same problem with one bar late trading

Josh,
I'm back and have not solved the problem. I have made changes.

I attached a screen shot to show what is happening.

The strategy is now self contained and only calls NT provided indicators like VMA.

The flow is still:

publicclass ATickTrader : Strategy
{
protectedoverridevoid Initialize()
{
CalculateOnBarClose = false;}
protectedoverridevoid OnBarUpdate()
Logic
Logic
if (FirstTickOfBar)
{
enter = down_start_price
stop = down_start_price + initial_stop;
EnterShort();
SetStopLoss(CalculationMode.Price, stop);
}

When the "enter =" instruction is processed, my strategy considers itself to have entered a trade. The next two instructions are for NT to enter a trade from the Strategy Tab.

Viewing the screen shot, you see that my strategy enters on the first tick of candle #1 and exits on the first tick of candle #3. NT executes the strategy's trade on the first tick of candle #2 and exits on the first tick of candle #4.

This happens on a 15 minute chart so it's not like something hiccuped.

I'm at a loss. Thanks very much for the help!

Larry
epcompany is offline  
Reply With Quote
Old 02-18-2009, 06:27 AM   #10
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 977 times in 960 posts
Default

Hi, do you see this in backtest or in realtime? The screenshot did not show properly, can you please repost it? Thanks!
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 02-18-2009, 07:37 AM   #11
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

Pending the screenshot, you also need to move SetStopLoss() before the EnterShort() for it to apply to the short position you just acquired.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-21-2009, 12:49 PM   #12
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default

I apologize for the delay. I wanted to get a concrete answer on real time data this week and did not.

The answer to the question is that the strategy trades one candle late on both historical and recorded data regardless of the timeframe. I moved the stop to a position in front of the "Enter" instruction and it made no difference.

I've made another attempt to upload the pic. It looks like it worked.

Thanks for the help! Entering on the right candle makes a big difference as you know.

Larry
epcompany is offline  
Reply With Quote
Old 02-21-2009, 08:18 PM   #13
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

I am sorry, there was no screen shot attached.

I did not read this entire thread, but is this occuring in backtesting only? Do you have CalculateOnBarClose sent to true?
NinjaTrader_Ben is offline  
Reply With Quote
Old 02-22-2009, 07:49 AM   #14
epcompany
Member
 
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks for the response.

I have had Calculate on Bar Close set to true and set to false and commented out. It makes no difference. My strategy trades on the first tick of bar one and NT trades on the first tick of the next bar.

The pic didn't upload apparently because it was larger than the 97KB limit of the forum. I cropped it and hopefully it is attached this time.

Later today I intend to have my strategy produce trades from recorded data at the end of the current bar. If NT trades at the first tick of the next bar, that will work out but will seem like a force fit. I'll get back with the result of this experiment.

Thanks,

Larry
Attached Images
File Type: jpg ScreenShot2.jpg (75.1 KB, 28 views)
epcompany is offline  
Reply With Quote
Old 02-22-2009, 10:54 AM   #15
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

Yes, this is expected behavior. This is the nature of backtesting using OHLCV data. The criteria is met on one bar and the exectution happens on the next available price data, which is the next bar since we are only using OHLCV data. This link may help:
http://www.ninjatrader-support.com/H...AStrategy.html
NinjaTrader_Ben 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
Strategy not triggering trades all the time ju1234 Strategy Development 6 11-22-2008 05:06 PM
Strategy to execute trades only once per day dendy Strategy Development 6 10-21-2008 02:37 PM
Strategy + discretionary trades combined bluelou Automated Trading 1 09-04-2008 11:12 AM
if ( BarsInProgress == 1 ) ...starts 20 mins late ATI user Strategy Development 3 08-22-2008 06:56 AM
order filled one bar late sub5mango Strategy Development 10 06-25-2008 06:38 AM


All times are GMT -6. The time now is 08:49 AM.