![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 977 times in 960 posts
|
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
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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! |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
|
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.
|
|
|
|
|
|
#5 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 977 times in 960 posts
|
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.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#7 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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! |
|
|
|
|
|
#9 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#10 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 977 times in 960 posts
|
Hi, do you see this in backtest or in realtime? The screenshot did not show properly, can you please repost it? Thanks!
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
Pending the screenshot, you also need to move SetStopLoss() before the EnterShort() for it to apply to the short position you just acquired.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
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?
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#14 |
|
Member
Join Date: Dec 2008
Posts: 59
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#15 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
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
Ben
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |