View Full Version : How can strategy enter/exit at current day's close price
rnhoo
10-04-2009, 03:35 PM
Hi,
I am backtesting an indicator. But I found my strategy can only enter/exit at the open price of the next day after the signal is triaggered. Actually I want to enter/exit at the close price of the day when the signal is triaggered.
Please see my code below.
Thanks,
Jing
protected override void OnBarUpdate()
{
if( Position.MarketPosition == MarketPosition.Flat && MyIndicator().BuySignal[0])
EnterLong("L");
if (Position.MarketPosition == MarketPosition.Long && MyIndicator().SellSignal[0])
ExitLong("L");
}
NinjaTrader_Bertrand
10-05-2009, 05:02 AM
Jing, this is expected for backtesting per default - you can add a finer granularity to simulate intrabar executions with the concept of this sample - http://www.ninjatrader-support2.com/vb/showthread.php?t=6652
rnhoo
10-06-2009, 10:33 AM
When I use Add(PeriodType.Minute, 1), during the backtest, it first tries to load the data, and throw a dialog "ERROR: BACKFILL "SPY" Hdr: 400 Validation failed for 1 of 1 requests. Your request is not valid".
If I use Add(PeriodType.Day, 1), it does not report failure but I still enter/exit at the open of next day.
Is the failure because it can not find minutes data? Actually I want to backtest 10-year daily data but my broker does not provider 10-year minutes data.
Thanks,
Jing
NinjaTrader_Bertrand
10-06-2009, 10:37 AM
Yes, this looks like an error related to data availability on your connection. To simulate the intrabar fills concept however you would need this access. Can you try a shorter data range back?
rnhoo
10-06-2009, 11:08 PM
It works for start date from 2007. And I am sure my broker does not provide interday data older than 2 month.
NinjaTrader_Bertrand
10-07-2009, 04:47 AM
Ok, thanks for reporting back - unfortunately you would need access to this smaller granularity for task at hand - have you considered connecting to a third party data provider first that would offer this data?
http://www.ninjatrader-support.com/HelpGuideV6/HistoricalData.html
pmorissette
06-07-2010, 08:39 AM
Is there any way to do this without using a shorter time-frame?? For example, I want to test a daily strategy over the long-term with yahoo finance...therefore there is no intraday data available.
There must be a way of doing this - the gap between open/close is too big for backtest results to make sense.
NinjaTrader_Bertrand
06-07-2010, 09:17 AM
pmorissette, unfortunately not - to simulate intrabar fills you need smaller than daily granularity available, as in backtesting only the OHLCV values would be used.
bill897
12-09-2010, 12:05 PM
How do I use the CloseStrategy and the flatteneverything commands in my strategy?
What is the syntax. I'm getting an error for CLOSESTRATEGY(atm1); I'm also getting
an error "Flatteneverything" does not exist in the current context.
NinjaTrader_Bertrand
12-09-2010, 12:17 PM
Bill, you would need to use the AtmStrategyClose() as documented here - http://www.ninjatrader.com/support/helpGuides/nt7/atmstrategyclose.htm
'FlattenEverything' from NinjaScript is unfortunately not supported.
luxurious_04
02-10-2011, 02:38 AM
How to close the ATM strategy at the market price? Or how to move the stop to the current market price? How to get the current market price?:confused:
NinjaTrader_Bertrand
02-10-2011, 03:00 AM
To close the ATM just use the AtmStrategyClose command available, the current price is Close[0] if CalculateOnBarClose is false.
luxurious_04
02-11-2011, 01:18 AM
Bertran please help me with the code to automatically close all the orders and unchecked or disable all the strategies in the strategy tab in Ninja just like the function of CLOSE button of the DOM. Or just a code to activate or trigger the CLOSE button of the DOM. Please.....:confused::confused::confused:
NinjaTrader_Bertrand
02-11-2011, 03:41 AM
Unfortunately there's no supported method for this purpose - as a hint you can look into this thread though - http://www.ninjatrader.com/support/forum/showthread.php?t=22435&highlight=unsupported&page=6