Overall Statistics |
Total Trades 1852 Average Win 2.62% Average Loss -2.47% Compounding Annual Return 157.213% Drawdown 6.800% Expectancy 0.132 Net Profit 13174.994% Sharpe Ratio 9.193 Loss Rate 45% Win Rate 55% Profit-Loss Ratio 1.06 Alpha 0.944 Beta 0.062 Annual Standard Deviation 0.104 Annual Variance 0.011 Information Ratio 4.42 Tracking Error 0.181 Treynor Ratio 15.426 |
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 ShortSPXUAlgorithm : QCAlgorithm { //---------------------------------------------------- // Parameters //---------------------------------------------------- string mSymbol1 = "TMV"; string mSymbol2 = "TMF"; decimal mLeverage = 2.5m; Consolidator TimeFrameBar = new Consolidator(TimeSpan.FromMinutes(390)); // Custom time frame bar bool mGetOut = false; decimal mTakeProfit = 99999999999999999.0m; decimal mTakeProfitRatio = 1.004m; //1.004; //Initialize the data and resolution you require for your strategy: public override void Initialize() { SetStartDate(2010, 1, 1); //SetEndDate(2011, 2, 1); SetEndDate(DateTime.Now.Date.AddDays(-1)); SetCash(30000); AddSecurity(SecurityType.Equity, mSymbol1, Resolution.Minute); AddSecurity(SecurityType.Equity, mSymbol2, 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) { if(data.ContainsKey(mSymbol1) && data.ContainsKey(mSymbol2)) { if (TimeFrameBar.Update(data[mSymbol1])) { if (mGetOut) { mGetOut = false; mTakeProfit = Portfolio.TotalPortfolioValue * mTakeProfitRatio; } } if(!mGetOut && data[mSymbol1].Time.Hour >= 15 && data[mSymbol1].Time.Minute >= 50 ) { decimal wCash = Portfolio.Cash; if (!mGetOut && Securities[mSymbol1].Holdings.Quantity == 0) { if (!mGetOut && Securities[mSymbol1].Holdings.Quantity == 0) Order(mSymbol1, (int)Math.Floor(mLeverage*0.5m*wCash / (data[mSymbol1].Close))); if (!mGetOut && Securities[mSymbol2].Holdings.Quantity == 0) Order(mSymbol2, (int)Math.Floor(mLeverage*0.5m*wCash / (data[mSymbol2].Close))); } mTakeProfit = Portfolio.TotalPortfolioValue * mTakeProfitRatio; } if (Portfolio.TotalPortfolioValue >= mTakeProfit) { if(data[mSymbol1].Time.Hour >= 15 && data[mSymbol1].Time.Minute >= 50 ) mGetOut = true; Order(mSymbol1, -Securities[mSymbol1].Holdings.Quantity); Order(mSymbol2, -Securities[mSymbol2].Holdings.Quantity); } } } } }
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { /* * TimeSpanConsolidator Helper Routine: Assemble generic timespan bar lengths: e.g. 10 minutes: * * 1. Setup the new Consolidator class with the timespan period: * var _consolidator = new Consolidator(TimeSpan.FromMinutes(10)); * * 2. Add in the data with the update routine. It will return true when bar ready * if (_consolidator.Update(data["MSFT"])) { UseBar } */ public class Consolidator { private TradeBar _resultBar; private TradeBar _workingBar; private DateTime _start; private TimeSpan _period; //Result: public TradeBar Bar { get { return _resultBar; } } //Constructor: Set the period we'd like to scan public Consolidator(TimeSpan span) { this._period = span; this._resultBar = new TradeBar(); this._workingBar = new TradeBar(new DateTime(), "", Decimal.Zero, Decimal.MinValue, Decimal.MaxValue, 0, 0); } //Submit this bar, return true if we've started a new one. public bool Update(TradeBar newBar) { //Intialize: if (_start == new DateTime()) { _start = newBar.Time; } //While we're less than end date, keep adding to this bar: if (newBar.Time < (_start + _period)) { //Building bar: AddToBar(newBar); return false; } else { //Completed bar: start new one: _resultBar = _workingBar; //Create a new bar: _workingBar = new TradeBar(newBar.Time, newBar.Symbol, Decimal.Zero, Decimal.MinValue, Decimal.MaxValue, 0, 0); //Start of this bar: _start = newBar.Time; AddToBar(newBar); return true; } } //Add to a tradebar private void AddToBar(TradeBar newBar) { //Add this data to working bar: if (_workingBar.Time == new DateTime()) _workingBar.Time = newBar.Time; if (_workingBar.Symbol == "") _workingBar.Symbol = newBar.Symbol; if (_workingBar.Open == Decimal.Zero) _workingBar.Open = newBar.Open; if (newBar.High > _workingBar.High) _workingBar.High = newBar.High; if (newBar.Low < _workingBar.Low) _workingBar.Low = newBar.Low; _workingBar.Close = newBar.Close; _workingBar.Volume = newBar.Volume; } } }