I am trying to test a simple strategy where the rules are:
1. If the average 2 day close is less than the average 5 day close, then buy the next day on the opening.
2. If the average 2 day close is greater than the average 5 day close, then sell short the next day on the opening.
3. Exit all positions on the close on the same day as the trade entry.
Having read the multi-time frame & instruments section of the help manual and I made an attempt at scripting the above strategy for testing. Please see below for the ninja 7 script of the above strategy in BOLD but it did not produce the desired output. Specifically, the issues are:
1. Not all trades were exited on the same day as the entry date
2. Most entries were between 9:30 am and 9:35 am EST (as per my script) but I noticed some trade entries were executed after 9:35 AM EST, not sure why.
protected override void Initialize()
{
Add(PeriodType.Tick, 1);
CalculateOnBarClose = false;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Checks to ensure all Bars objects contain enough bars before beginning
if ((CurrentBars[0] <= BarsRequired) || (CurrentBars[1] <= BarsRequired))
return;
var sma1 = SMA(this.sma1Val); // fast SMA
var sma2 = SMA(this.sma2Val); // slow SMA
if (BarsInProgress == 0)
{
if (sma1[1] < sma2[1])
{
if (ToTime(Times[1][0]) >= 93000 && ToTime(Times[1][0]) < 93500 && Positions[1].MarketPosition == MarketPosition.Flat)
{
//EnterLongLimit(1, Open[0]);
//EnterLong();
EnterLong(1, 1, "Long: 1min");
Print("Long Entry: " + Times[1][0]);
}
}
if (sma1[1] > sma2[1])
{
if (ToTime(Times[1][0]) >= 93000 && ToTime(Times[1][0]) < 93500 && Positions[1].MarketPosition == MarketPosition.Flat)
{
//EnterShortLimit(1, Open[0]);
//EnterShort();
EnterShort(1, 1, "Short: 1min");
Print("Short Entry: " + Times[1][0]);
}
}
}
// Exit positions
if (BarsInProgress == 1)
{
if (ToTime(Times[1][0]) >= 161000 && ToTime(Times[1][0]) < 161500 && Positions[1].MarketPosition == MarketPosition.Long)
{
ExitLong();
Print("Long Exit: " + Times[1][0]);
}
if (ToTime(Times[1][0]) >= 161000 && ToTime(Times[1][0]) < 161500 && Positions[1].MarketPosition == MarketPosition.Short)
{
ExitShort();
Print("Short Exit: " + Times[1][0]);
}
}
In testing the strategy, I selected the Strategy Analyzer from the File menu in Control Center. Then I right clicked on ES 09-18 and selected Backtest. Please see the values I specified for some of the parameters below:
Data Series
- Price based on: last
- Type: Minute
- Value: 1
Order Handling
- Entries per direction: 1
- Entry handling: All entries
- Exit on Close: False
Can you tell me why my script is not producing the desired output?
Comment