| Overall Statistics |
|
Total Trades 160 Average Win 0% Average Loss 0% Compounding Annual Return 12.642% Drawdown 21.000% Expectancy 0 Net Profit 0% Sharpe Ratio 0.938 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0.124 Beta 0.035 Annual Standard Deviation 0.137 Annual Variance 0.019 Information Ratio -0.083 Tracking Error 0.203 Treynor Ratio 3.702 Total Fees $160.00 |
///<summary>
/// QCU - S&P 500 Dollar Averaging Over Time.
/// Typical, simple investment strategy - invest a fixed amount each month regardless of market conditions.
/// How would this have performed over the last few years?
///</summary>
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
namespace QuantConnect
{
public class QCUDollarCostAverage : QCAlgorithm, IAlgorithm
{
private string symbol = "ZIV";
private DateTime startDate = new DateTime(2010, 01, 01);
private DateTime endDate = new DateTime(2015, 07, 01);
private decimal monthlyDollarValue = 1000;
private DateTime nextTradeDate = DateTime.MinValue;
//Initialize the Code
public override void Initialize()
{
//Dynamic start and end dates configured above.
SetStartDate(startDate);
SetEndDate(endDate);
//Set the cash as a function of the number of months we're investing
decimal investments = Convert.ToDecimal((endDate - startDate).TotalDays / 10);
SetCash(investments * monthlyDollarValue);
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute, true, false);
}
//Handle the Data Event:
public void OnData(TradeBars data)
{
//Its good practice to wrap our code in "Try-Catch" to handle errors:
try
{
decimal price = data[symbol].Price;
DateTime today = data[symbol].Time;
int quantity = (int)(monthlyDollarValue / price);
//Check we've past the required date of our next investment
if (today.Date >= nextTradeDate.Date && Time.Hour >= 12)
{
//Now place the order to purchase more SPY stock.
Order(symbol, quantity); //symbol, quantity
// Set the next date we'll place an order:
nextTradeDate = today.AddDays(10);
}
return;
}
catch (Exception err)
{
//Errors will be logged to the console
Error("Error in Data Event:" + err.Message);
}
}
}
}