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 07-07-2010, 07:13 AM   #1
brooksrimes
Senior Member
 
Join Date: Apr 2005
Location: , ,
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
Default How to enter on the open and exit on the close of a bar

I'm doing some backtesting using some of the Period features for analysis purposes and want to cause trades to consist of one bar, with the entry on the open and the exit on the close. How do I do this?

I tried:

protected override void OnBarUpdate()
{
EnterLong(DefaultQuantity, "Long1");
ExitLong("Exit Long1", "Long1");
}

but this had two problems:
1. The exit occurred on the open of the next bar instead of on the close of the entry bar.
2. NT skipped every other bar. It would not do an entry on bars 1, 2, 3, etc. It entered on bar 1, 3, etc.

Thanks
brooksrimes is offline  
Reply With Quote
Old 07-07-2010, 09:00 AM   #2
NinjaTrader_Tim
NinjaTrader Customer Service
 
NinjaTrader_Tim's Avatar
 
Join Date: Nov 2009
Location: Denver, CO
Posts: 1,424
Thanks: 0
Thanked 3 times in 3 posts
Default

Hi brooksrimes,

In backtesting, that behavior is expected as each bar is treated as a single instance.

If you want to accomplish this on live data, you can use CalculateOnBarClose=false; in combination with FirstTickOfBar, or perhaps use a multi-series strategy.

FirstTickOfBar - http://www.ninjatrader-support.com/H...FirstTickOfBar

Multi-Series - http://www.ninjatrader-support.com/H...ameInstruments
NinjaTrader_Tim is offline  
Reply With Quote
Old 07-07-2010, 09:45 AM   #3
brooksrimes
Senior Member
 
Join Date: Apr 2005
Location: , ,
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
Default

<In backtesting, that behavior is expected as each bar is treated as a single instance.>

"It's not a bug, it's a feature." - Microsoft

I need this for backtesting, not real time.

It's very basic stuff to be able to enter and exit on the same bar. It's easy in Tradestation or Amibroker. If NT can't do it, IMHO someone should be working on it.

I believe in the early versions of Amibroker, they couldn't do this either but got requests for it, and created system variables for BuyPrice, SellPrice, ShortPrice & CoverPrice and gave users control.

If I can't get the closing price in Ninja Trader, can you at least suggest code and settings to enter on the open of every bar and exit on the open of the next bar? In other words:

Trade 1 - entry at bar 1 open price, exit at bar 2 open price
Trade 2 - entry at bar 2 open price, exit at bar 3 open price
etc.

I tried setting "Entries Per Direction" from 1 to 2, but that didn't work. It made some trades last 1 bar and some last 2 bars.

Thanks
brooksrimes is offline  
Reply With Quote
Old 07-07-2010, 10:28 AM   #4
NinjaTrader_Tim
NinjaTrader Customer Service
 
NinjaTrader_Tim's Avatar
 
Join Date: Nov 2009
Location: Denver, CO
Posts: 1,424
Thanks: 0
Thanked 3 times in 3 posts
Default

Hi brooksrimes,

To clarify, the reason this is done is so that NinjaTrader can simulate fills in a conservative way. The tick by tick sequence is not available when looking at a, for example, historical 5min bar. So there is no guarantee that certain prices were followed by others. For example, there's no way to know when the High of a bar was accomplished, in comparison to it's Low.

Adding granularity to the strategy could be helpful in backtesting.
The details of doing so are detailed here - http://www.ninjatrader.com/support/f...ead.php?t=6652

You could add a smaller smaller series, and use limit orders for the Open[0] or Close[0] of the larger series.
NinjaTrader_Tim is offline  
Reply With Quote
Old 07-07-2010, 12:00 PM   #5
brooksrimes
Senior Member
 
Join Date: Apr 2005
Location: , ,
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
Default

I understand the desire to be conservative and agree completely with not allowing entry or exit on high or low, but the restrictions in place on open, close and entering and exiting on the same bar are unnecessarily restrictive, IMHO.

Using a 2nd timeframe is not a good solution as it introduces unneeded complexity.

It's really hard to believe that there is no easy way to code:

Trade 1 - entry at bar 1 open price, exit at bar 2 open price
Trade 2 - entry at bar 2 open price, exit at bar 3 open price
etc.

Could you confer with your colleagues?

Quote:
Originally Posted by NinjaTrader_Tim View Post
Hi brooksrimes,

To clarify, the reason this is done is so that NinjaTrader can simulate fills in a conservative way. The tick by tick sequence is not available when looking at a, for example, historical 5min bar. So there is no guarantee that certain prices were followed by others. For example, there's no way to know when the High of a bar was accomplished, in comparison to it's Low.

Adding granularity to the strategy could be helpful in backtesting.
The details of doing so are detailed here - http://www.ninjatrader.com/support/f...ead.php?t=6652

You could add a smaller smaller series, and use limit orders for the Open[0] or Close[0] of the larger series.
brooksrimes is offline  
Reply With Quote
Old 07-07-2010, 12:28 PM   #6
NinjaTrader_Tim
NinjaTrader Customer Service
 
NinjaTrader_Tim's Avatar
 
Join Date: Nov 2009
Location: Denver, CO
Posts: 1,424
Thanks: 0
Thanked 3 times in 3 posts
Default

Hi brooksrimes,

The method mentioned before is the supported way of achieving what you are seeking. Unfortunately, there is not another way to do this in both backtesting and live.
NinjaTrader_Tim is offline  
Reply With Quote
Reply

Tags
close, enter, exit, open, same bar

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
How can strategy enter/exit at current day's close price rnhoo ATM Strategies (Discretionary Trading) 13 02-11-2011 03:41 AM
Automatically enter position on Market Open, Close position on Market Close kevendickenson SuperDOM and other Order Entry Windows 1 04-02-2010 04:30 AM
Enter trade at open and exit trade at close d.allen101 Strategy Development 3 11-11-2009 10:02 AM
Exit at close and enter at open Andy_Mil Strategy Development 5 06-24-2009 12:17 PM
Enter at Close, Exit next day Close tjendra Strategy Development 3 04-08-2009 11:33 AM


All times are GMT -6. The time now is 09:43 AM.