| Overall Statistics |
|
Total Trades 239 Average Win 0.79% Average Loss -0.24% Compounding Annual Return -95.145% Drawdown 17.300% Expectancy -0.476 Net Profit -11.278% Sharpe Ratio -6.541 Loss Rate 88% Win Rate 12% Profit-Loss Ratio 3.30 Alpha -2.648 Beta -0.011 Annual Standard Deviation 0.405 Annual Variance 0.164 Information Ratio -7.048 Tracking Error 0.425 Treynor Ratio 242.135 Total Fees $1223.75 |
using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
namespace QuantConnect.Algorithm.Examples
{
/// <summary>
///
/// QuantConnect University: EMA + SMA Cross
///
/// In this example we look at the canonical 20/50 day moving average cross. This algorithm
/// will go long when the 20 crosses above the 50 and will liquidate when the 20 crosses
/// back below the 50.
// -------VATS CHANGES -----------
// 1) Intraday - Hourly
// 2) 1/50 period SMA cross
//
// -------VATS CHANGES -----------
/// </summary>
public class QCUMovingAverageCross : QCAlgorithm
{
private const string Symbol = "USO";
private SimpleMovingAverage fast;
private SimpleMovingAverage slow;
TradeBar _spyMinutes;
public override void Initialize()
{
SetStartDate(2015, 07, 01);
SetEndDate(2015, 07, 15);
SetCash(10000);
AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute);
// define our daily trade bar consolidator. we can access the daily bar
// from the DataConsolidated events
var minConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(5));
// attach our event handler. the event handler is a function that will be called each time we produce
// a new consolidated piece of data.
minConsolidator.DataConsolidated += OnDataMinutes;
// this call adds our daily consolidator to the manager to receive updates from the engine
SubscriptionManager.AddConsolidator(Symbol, minConsolidator);
fast = SMA(Symbol, 1, Resolution.Minute);
slow = SMA(Symbol, 50, Resolution.Minute);
//SetRunMode(RunMode.Series);
}
private void OnDataMinutes(object sender, TradeBar consolidated)
{
_spyMinutes = consolidated;
//Log(consolidated.Time.ToString("o") + " >> " + Symbol + ">> LONG >> 100 >> " + Portfolio[Symbol].Quantity);
}
private DateTime previous;
public void OnData(TradeBars data)
{
if (!slow.IsReady) return;
// only once per day
// Commented the following line to simulate intraday - Vats
//if (previous.Date == data.Time.Date) return;
// Debug (System.DateTime.Now.Hour.ToString()) ;
// if (System.DateTime.Now.Hour <= 10 && System.DateTime.Now.Hour > 12) return;
const decimal tolerance = 0.00010m;
var holdings = Portfolio[Symbol].Quantity;
{
if (fast > slow * (1 + tolerance))
{
if (holdings <= 0)
{
Log (System.DateTime.Now.Hour.ToString()) ;
Log("BUY >> " + holdings + "@ price " + Securities[Symbol].Price);
SetHoldings(Symbol, 0.95);
Log("NET POSITION BEFORE NEXT TRANSACTION >> " + holdings);
}
}
if (fast < slow)
{
if (holdings > 0)
{
Log (System.DateTime.Now.Hour.ToString()) ;
Log("SELL >> " + holdings + "@ price " + Securities[Symbol].Price);
SetHoldings(Symbol, -0.95);
}
}
}
Plot(Symbol, "Price", data[Symbol].Price);
Plot(Symbol, fast, slow);
previous = data.Time;
}
}
}