The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
public class PairsTradingAlgorithm : QCAlgorithm
{
decimal _diff;
MovingAverageConvergenceDivergence _macd;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Start and End Date range for the backtest:
SetStartDate(2013, 1, 1);
SetEndDate(2014, 1, 1);
//Cash allocation
SetCash(25000);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, "AAPL", Resolution.Minute);
AddSecurity(SecurityType.Equity, "MSFT", Resolution.Minute);
//Initialise MACD
_macd = new MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Simple);
}
//Data Event Handler: All data arrives here in slices.
public override void OnData(Slice slice)
{
if (!slice.ContainsKey("MSFT") || !slice.ContainsKey("AAPL")) return;
//Calculate difference MSFT and AAPL
_diff = slice["MSFT"].Close - slice["AAPL"].Close;
//Update MACD
_macd.Update(slice.Time, _diff);
//Calculate MACD difference
decimal signalDeltaPercent = (_macd - _macd.Signal)/_macd.Fast;
var tolerance = 0.0025m;
//If MACD gives signal and we're not in a position right now
if (!Portfolio.HoldStock && signalDeltaPercent > tolerance)
{
//Calculate quantities of individual positions
int quantityA = (int)Math.Floor(0.5M*Portfolio.Cash/slice["MSFT"].Close);
int quantityB = (int)Math.Floor(0.5M*Portfolio.Cash/slice["AAPL"].Close);
//Go long on first, go short on second
Order("MSFT", quantityA);
Order("AAPL", -quantityB);
} else if (signalDeltaPercent < -tolerance) {
//If MACD gives opposite signal
Liquidate("MSFT");
Liquidate("AAPL");
}
//Plot MACD lines
Plot("MACD", "Difference", _diff);
Plot("MACD", _macd.Fast, _macd.Slow);
}
}
var msft = Identity("MSFT");
var aapl = Identity("AAPL");
We can then take these and form more complex indicators from them, such as a difference:var diff = msft.Minus(aapl);
We can then define a MACD on the difference using:var macdDiff = new MovingAverageConvergenceDivergence(12, 26, 9).Of(diff);
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can
continue your Boot Camp training progress from the terminal. We
hope to see you in the community soon!