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 04-18-2011, 12:00 PM   #1
sgordet
Senior Member
 
Join Date: Feb 2010
Posts: 150
Thanks: 1
Thanked 1 time in 1 post
Default Testing first tick of day bar

To test for the value of the first tick of the day when charting 5 minute bars, I set COBC = false and use:
ToTime(Time[0]) == 93500 && FirstTickOfBar

What would I replace the ToTime(Time[0]) function with when using days bars as I don't think that I can use 161500 because the bars are marked with days instead of minutes. If it's some kind of date function that I need, I'd be appreciative if you could also give me the exact format that the date has to be in.

Thanks,
Stephen
sgordet is offline  
Reply With Quote
Old 04-18-2011, 12:16 PM   #2
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello Stephen,

Thank you for your post.

If you are working with daily bars, you only need to check FirstTickOfBar since there is only one daily bar in the session. No additional date checking should be needed.

Give this a try and let me know if I can assist with anything else.
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-18-2011, 01:03 PM   #3
sgordet
Senior Member
 
Join Date: Feb 2010
Posts: 150
Thanks: 1
Thanked 1 time in 1 post
Default

Thanks.
My strategy executes on day bars and depend on the opening price of the day (which you've shown me how to get). For backtesting, I'm going to need 1 minute bars (I don't have tick data going back far enough). I read the SampleIntrabarBacktest program. I realize that I have to execute buys and sells on the 1 minute bars, but do I have to explicitly reference the one minute bars when I need intraday data, e.g., for FirstTickOfBar when I'm doing my logical tests as they relate to day bars or will the backtesting program just have access to that 1 minute data and use it as appropriate. On the day bars, I just need to test the first tick of the day relative to the previous day's high and low and test prices during the day for their relationship to the previous day's high and low.
sgordet is offline  
Reply With Quote
Old 04-18-2011, 01:26 PM   #4
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello,

Thank you for your reply.

Yes, when you need to reference data from the one minute bars, you need to specify such. This can be done in an if(BarsInProgress == 1) section. You could also use BarsArray[1], this is assuming your 1 minute series is the first additional timeframe added. OnBarUpdate is called for each timeframe added, with the primary being index 0.

Since you are working with current day open high & low and previous day's high and low these two indicators may be of help:

CurrentdayOHL - http://www.ninjatrader.com/support/h...nt_day_ohl.htm
PriorDayOHLC - http://www.ninjatrader.com/support/h...r_day_ohlc.htm

You can access their calculated values so this may save you some effort. You may be able to just use a single timeframe in your code, and use these indicators for the day data.

Just let me know if I can assist with anything else
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-18-2011, 01:51 PM   #5
sgordet
Senior Member
 
Join Date: Feb 2010
Posts: 150
Thanks: 1
Thanked 1 time in 1 post
Default

Just to check that I understand this. Are these equivalent:

EnterLong(1, 1, "Long: 1min");

if (BarsInProgress == 1)
EnterLong(1,"Long: 1min");
sgordet is offline  
Reply With Quote
Old 04-18-2011, 02:20 PM   #6
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Stephen,

They are different, the first one would run on both timeframes added. For example, if you are running on a 5 minute chart, and have added a 1 minute period via Add(), OnBarUpdate will be called for each time frame, on each tick if COBC = false. Checking BarsInProgress will make the logic only apply to a certain timeframe.

Please let me know if you have any other questions.
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-18-2011, 07:41 PM   #7
sgordet
Senior Member
 
Join Date: Feb 2010
Posts: 150
Thanks: 1
Thanked 1 time in 1 post
Default

I took your advice and went with all 1 minute bars, using PriorDayOHLC. The code uses SetStopLoss in Initialize for stops and ExitLong and ExitShort for target exits. The targets are set for the open of the first profitable day:
if (ToTime(Time[0]) == 93100 && FirstTickOfBar)

In the backtest I have ExitOnClose set to false. I don't understand why some profits are being taken during the day as opposed to 0931 and why some of my losses are substantially less than the $750 stop.

I'm running the strategy on ES. If you could take a look at the attached code, I would be appreciative.
Attached Files
File Type: zip LwOOPS.zip (3.7 KB, 3 views)
sgordet is offline  
Reply With Quote
Old 04-19-2011, 08:27 AM   #8
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello Stephen,

I gave it a try on the ES. With Exit on Close set to false in the backtest options, it never closed on exit except for closing out of the strategy's final position. You may want to double check Exit On close is set correctly in the backtest UI, I do not see it set in the code anywhere.

The stops were correctly being placed at the $750 loss price as well, so it's probably related to the exit on close setting as well.

Double check that and let me know if you are still experiencing discrepancies.
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-19-2011, 10:16 AM   #9
sgordet
Senior Member
 
Join Date: Feb 2010
Posts: 150
Thanks: 1
Thanked 1 time in 1 post
Default

Dexter, thanks for going the extra mile and running the code.
I do have the ExitOnClose set to false in the strategy analyzer. I'm trading ES using the ETH index hours.
I've enclosed the Excel sheet of the trades. Several of the trades get closed out with a profit during the day and this should happen only at the first tick after 9:30 (I'm running 1 minute bars). Also, some of the losses get taken at 9:30 and for less than the $750 stop loss.
Attached Files
File Type: zip LwOOPS Trades.zip (9.9 KB, 1 views)
sgordet is offline  
Reply With Quote
Old 04-19-2011, 10:31 AM   #10
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Stephen,

Thank you for that spreadsheet of trades.

To compare apples to apples, I'm trying to reproduce on my end using the code attached and am getting completely different results. Attached is a screenshot of the settings I'm using in strategy analyzer and the trades it is taking. 1 minute bars, 4/19/2010 to 4/19/2011, Exit on close false. They all look correct per your specification. Let me know if I'm missing a setting.

Also, If you have updated the strategy, please send me the latest copy and I will give that a try.
Attached Images
File Type: png LwOOPSTrades1.png (101.7 KB, 5 views)
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-19-2011, 11:03 AM   #11
sgordet
Senior Member
 
Join Date: Feb 2010
Posts: 150
Thanks: 1
Thanked 1 time in 1 post
Default

I just re-ran the Strategy Analyzer again and compared all of the settings; everything is identical except for our hugely different results.
It seems to me that the only pssibility is the file. Here is a new zip of it.
Thanks.
Attached Files
File Type: zip LwOOPS.zip (3.7 KB, 6 views)
sgordet is offline  
Reply With Quote
Old 04-19-2011, 11:49 AM   #12
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you for your patience, and thank you for getting that for me. Re-imported and am now getting the same output.

First, some key things regarding backtesting. In backtests, no tick data is available, so FirstTickOfBar will not work correctly. Also, because of this, CalculateOnBarClose is always true. Entry and exit signals will be placed on the following bar. The workaround for this is back testing using market replay, it will work on tick conditions correctly.

If you enable TraceOrders in your Initialize(), it will output helpful info in the output window (Control Center, Tools > Output Window). Please see the helpguide article on TraceOrder here: http://www.ninjatrader.com/support/h...sub=TraceOrder

With this enabled, I am seeing entries like this:

4/23/2010 9:31:00 AM Ignored PlaceOrder() method: Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'

12/22/2010 9:31:00 AM Ignored PlaceOrder() method at 12/22/2010 9:31:00 AM: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Short' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'

It looks like your logic is losing track of position entry/exits, so I would start there - When using ExitShort() and ExitLong(), you will need pass them the same signal you opened with, such as "Short" and "Long" in your case.

Please give this a shot and let me know if it does the trick.
NinjaTrader_Dexter 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
Last Day in testing range not accessbile? Beauregard Strategy Development 3 01-14-2011 07:37 AM
Back-testing difference (single day vs multiple days) Mikefra Strategy Analyzer 5 12-27-2010 11:46 AM
Testing Slope of 21-Day EMA StarBright2009 General Programming 1 09-24-2009 06:42 AM
Visual testing tick by tick on chart peterdias77 Strategy Analyzer 1 01-28-2009 06:08 AM
backtesting vs. forward testing like night vs. day andrewbee Strategy Development 15 01-04-2008 10:48 PM


All times are GMT -6. The time now is 10:14 AM.