using System;
using System.Collections;
using System.Collections.Generic;
namespace QuantConnect
{
using QuantConnect.Securities;
using QuantConnect.Models;
//Sell in May Algorithm Example:
public partial class QCUSellInMay : QCAlgorithm, IAlgorithm {
//Algorithm Variables
//int quantity = 400;
int ownbond = 0;
int owndia = 0;
int season = 0;
// private string symbol = "USMV"; // US low volatility etf oct 2011
// private string symbol = "DDM"; // dow 2x ETF 6/2006
// private string symbol = "DIA";
// private string symbol = "UMDD"; // 3x mid cap etf 2/22/2010
private string symbol = "UDOW"; // 3x DOW
//private string symbol = "IJR"; // ishares core S&P small cap etf
private string sbond = "AGG"; // agg is agreggate bond etf ishares us
//private string sbond = "PONDX";
private decimal cash = 100000;
public MovingAverageConvergenceDivergence _macd;
//Initialize the Strategy
public override void Initialize() {
SetCash(cash);
SetStartDate(2010, 10, 10);
SetEndDate(2017, 10, 10);
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
AddSecurity(SecurityType.Equity, sbond, Resolution.Minute);
_macd = MACD(symbol,12, 26, 9,MovingAverageType.Simple,Resolution.Daily);
}
//Handle the data events:
public void OnData(TradeBars data) {
if (Time.ToString("MMM") == "May") { season = 1; }
if (Time.ToString("MMM") == "Oct") { season = 2; }
if (data.ContainsKey(symbol)==false) return;
if (_macd<0 && season == 1) {
if (owndia == 1) {
Order(symbol, -Portfolio[symbol].Quantity); // sell DIA and then buy bond
owndia = 0;}
if (data.ContainsKey(sbond)==true)
{
int quantity = (int)Math.Floor(Portfolio.Cash / data[sbond].Close);
Order(sbond, quantity);
ownbond = 1;}
Debug("QCU Sell In May: Flat " + Time.ToString("Y"));
}
else {
if (_macd>0 && season == 2)
{
if (ownbond ==1) {Order(sbond, -Portfolio[sbond].Quantity); ownbond = 0;}// sell the bond fund and buy DIA
int quantity = (int)Math.Floor(Portfolio.Cash / data[symbol].Close);
Order(symbol, quantity);
owndia = 1;
Debug("QCU Sell In May: Long " + Time.ToString("Y"));
}
}
}
}
}