The Nasdaq 100 (QQQ) index is fairly volatile because it contains so many technology stocks whose prices move around so much more than average non-technology stocks. The volatility of the QQQ has many sources, including rapid technological advances, competition, and sudden news that can affect stock prices. The strategy below waits for a big “crash” in the QQQ, where a crash is defined as QQQ prices moving below a lower Bollinger band set 1.5 standard deviations away from a 10-day moving average. While using 1.5 standard deviations is not unusual for Bollinger bands, the use of 1.5 with a short 10-day moving average is an unusual combination. To move below a lower Bollinger band that is based on a 10 day average requires a pretty drastic price move, because a fast 10-day average will stick closely to price movements. So this strategy looks for drastic drops in QQQ, buys long the morning after the crash (in OnBarOpen), and then trades the gap back up as usual. It exits when a profit is made, or after 30 days. We put this strategy under the volatility section because it uses methods that deliberately select volatile moves (1.5 standard deviations, 10 day average). In contrast, our other Bollinger band examples are listed under the range trading section, because they focus on slower moving averages and less volatile price moves.

using OpenQuant.API;
using OpenQuant.API.Indicators;

using System.Drawing;

public class MyStrategy : Strategy
{
	[Parameter("Order quantity (number of contracts to trade)")]
	double Qty = 100;
	
	[Parameter("Length of BBL")]
	int BBLLength = 10;
	
	[Parameter("Order of BBL")]
	double BBLOrder = 1.5;	
	
	[Parameter("Max number of bars, while position is active")]
	int MaxDuration = 20;
	
	// bollinger band, 10 days, 1.5 std deviation
	BBL bbl;
	// remember what you paid on entry
	double entryPrice;
	// exit after 20 days
	int barsFromEntry = 0;	
	// orders and quantity
	Order buyOrder;	

	public override void OnStrategyStart()
	{
		// set up bollinger bands
		bbl = new BBL(Bars, BBLLength, BBLOrder);
		bbl.Color = Color.Yellow;
		Draw(bbl, 0);
	}

	public override void OnBar(Bar bar)
	{
		// if we don't have a position and we have some bars
		// in the bollinger series, try to enter a new trade
		if (!HasPosition)
		{
			if (bbl.Count > 0)
			{
				// if the current bar is below the lower bollinger band
				// buy long to close the gap
				if (Bars.Crosses(bbl, bar) == Cross.Below)
				{
					buyOrder = MarketOrder(OrderSide.Buy, Qty, "Entry");					
					buyOrder.Send();
				}
			}
		}
		else
		{
			// else if we DO have an existing position, and if
			// today's bar is above our entry price (profitable),
			// then close the position with a market order
			if (entryPrice < bar.Close)
			{
				barsFromEntry = 0;
				
				Sell(Qty, "Exit (Take Profit)");
			}
			else
				barsFromEntry++;
		}
	}

	public override void OnBarOpen(Bar bar)
	{
		if (barsFromEntry == MaxDuration)
			Sell(Qty, "Sell");
	}

	public override void OnPositionOpened()
	{
		// when a position is opened, remember what we paid for
		// the instrument. Notice we use the average price of 
		// all the partial fills that we might have received.
		entryPrice = buyOrder.AvgPrice;
	}

	public override void OnPositionClosed()
	{
		barsFromEntry = 0;
	}
}