using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
namespace QuantConnect
{
// Name your algorithm class anything, as long as it inherits QCAlgorithm
public class BarbellStrategy : QCAlgorithm
{
public string symbol = "XIV";
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
SetStartDate(2012, 1, 1);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(100000); //only going to trade 20%
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
}
//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
{
//only get daily close
if (data[symbol].Time.ToString().Contains("3:59:00")) {
Debug(data[symbol].Time.ToString());
decimal currentPercentage = (Portfolio.TotalHoldingsValue / Portfolio.TotalPortfolioValue);
Debug("CurrentPercentage: " + currentPercentage);
//reconfigure for 20/80 holding; but give a little room (15%)
if (currentPercentage < (decimal).15 ){
//buy
decimal amtToBuy = ((((decimal).2 - (currentPercentage)) * Portfolio.TotalPortfolioValue)/data[symbol].Close);
amtToBuy = Math.Round(amtToBuy);
Order(symbol, amtToBuy);
Debug("Puchased " + amtToBuy + "of " + symbol);
return;
}
//reconfigure for 20/80; but give a little room (25%)
if (currentPercentage > (decimal).25) {
//sell
decimal amtToSell = (((currentPercentage - (decimal).2) * Portfolio.TotalPortfolioValue)/data[symbol].Close);
amtToSell = Math.Round(amtToSell);
Debug("Sold " + amtToSell + "of " + symbol);
Order(symbol, -1* amtToSell);
return;
}
} else {
return;
}
/*
Debug(data[symbol].Time.ToString());
if (!Portfolio.HoldStock)
{
Order(symbol, (int)Math.Floor(Portfolio.Cash / data[symbol].Close) );
Debug("Debug Purchased XIV");
}
*/
}
}
}