| Overall Statistics |
|
Total Trades 65 Average Win 0% Average Loss 0% Compounding Annual Return 7.353% Drawdown 7.600% Expectancy 0 Net Profit 0% Sharpe Ratio 1.039 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0.076 Beta -0.017 Annual Standard Deviation 0.071 Annual Variance 0.005 Information Ratio -0.389 Tracking Error 0.174 Treynor Ratio -4.227 Total Fees $65.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 = "SPY";
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 / 30);
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.AddMonths(1);
}
return;
}
catch (Exception err)
{
//Errors will be logged to the console
Error("Error in Data Event:" + err.Message);
}
}
}
}