This example watches for a stock to gap down 2%, then buys the gap (long) to close the position when the gap closes, or at the end of the next bar after the gap bar. So this strategy only holds a position open for 1 bar length of time. But we present the example here using daily bars, to make the explanation more intuitive. Here is the code for the entire component, including the boilerplate sections of (1) assembly references and (2) class headers. To save space, later examples will omit the boilerplate assembly references, strategy component attributes, and class headers. These bits of code are all essentially the same, for all examples.

using OpenQuant.API;

public class MyStrategy : Strategy
{
	[Parameter("Gap percent")]
	double Percent = 2;

	[Parameter("Order quantity (number of contracts to trade)")]
	double Qty = 100;

	double prevClose;
	Order sellOrder;

	public override void OnBar(Bar bar)
	{
		prevClose = bar.Close;

		if (HasPosition)
		{
			sellOrder.Cancel();

			Sell(Qty, "Exit");
		}
	}

	public override void OnBarOpen(Bar bar)
	{
		if ((prevClose - bar.Open) / prevClose > Percent / 100)
			Buy(Qty, "Entry");
	}

	public override void OnPositionOpened()
	{
		sellOrder = SellLimitOrder(Qty, prevClose, "Limit Exit");
		sellOrder.Send();
	}
}