| Overall Statistics |
|
Total Trades 132 Average Win 0% Average Loss 0% Compounding Annual Return 4.210% Drawdown 38.100% Expectancy 0 Net Profit 59.105% Sharpe Ratio 0.286 Probabilistic Sharpe Ratio 0.194% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.017 Beta 0.47 Annual Standard Deviation 0.133 Annual Variance 0.018 Information Ratio -0.582 Tracking Error 0.138 Treynor Ratio 0.081 Total Fees $0.00 Estimated Strategy Capacity $600000.00 Lowest Capacity Asset CVS R735QTJ8XC9X |
///<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 = "CVS";
private DateTime startDate = new DateTime(2010, 07, 01);
private DateTime endDate = new DateTime(2021, 10, 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);
Securities[symbol].FeeModel = new ConstantFeeModel(0);
}
//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;
// decimal quantity = (decimal)(monthlyDollarValue / price);
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);
}
}
}
}