Thus, in this strategy, a buy order is issued at the market open on day 5 after 4 down days. Then, it just automatically issues a market sell order at the end of day 6 (when day 6 bar is received). The sell order will actually be executed by the exchange when the market opens on day 7.
In this daily bars trading strategy, the daily bar is received at the end of each trading day, in the OnBar event handler. The OnBar event handler is executed on the trailing edge of each bar, at time that corresponds to the end of the trading day for daily bars.
If you want to customize the strategy to do something at the market open before the daily bar is constructed then you should put your code in an OnBarOpen event handler.
- Code: Select all
#region usings
/*
* Auto generated by Quant Solution - Quant Edge Corp
* Do not remove any items in this region
*/
using System;
using System.Drawing;
using QuantEdge.BackTest;
using QuantEdge.BackTest.Base;
using QuantEdge.BackTest.Common;
using QuantEdge.BackTest.Common.Enums;
using QuantEdge.BackTest.Common.Attributes;
using QuantEdge.BackTest.Common.CurveOperator;
using QuantEdge.IDE.Entities.Solution;
using QuantEdge.TradeEdge.ChartLibrary.TechnicalAnalysis;
using QuantEdge.TradeEdge.ChartLibrary.TechnicalAnalysis.Indicator;
#endregion
public class Four_Down_Day_And_Long : BaseStrategy
{
[Parameter("Order quantity (number of contracts to trade)")]
int quantity = 100;
[Parameter("Number of consecutive down closes")]
int consClosesCount = 4;
int count;
Order buyOrder;
Order sellOrder;
public override void OnStrategyStart()
{
count = 0;
}
public override void OnBar()
{
if (PrevClose != -1)
{
// if we don't have a position open, increment the count of down days (or reset it to zero)
if (!HasPosition)
{
if (PrevClose > bar.Close)
count++;
else
count = 0;
// if this is the fourth (consClosesCount is equal to 4 by default) down day,
// issue a market order to open a long position tomorrow morning, on day 5
if (count == consClosesCount)
{
Buy(quantity, "Entry");
}
}
else
Sell(quantity, "Exit");
}
}
}
