| Overall Statistics |
|
Total Trades 646 Average Win 1.65% Average Loss -0.61% Compounding Annual Return 2452.904% Drawdown 6.600% Expectancy 0.570 Net Profit 190.556% Sharpe Ratio 6.272 Loss Rate 58% Win Rate 42% Profit-Loss Ratio 2.73 Alpha 2.788 Beta -0.369 Annual Standard Deviation 0.441 Annual Variance 0.195 Information Ratio 5.605 Tracking Error 0.484 Treynor Ratio -7.491 Total Fees $2791.48 |
//Copyright Warren Harding 2016.
//Granted to the public domain.
//Use entirely at your own risk.
//Custom algorithm development: warrencharding@yahoo.com.
//Do not remove this copyright notice.
using System;
using System.Collections.Generic;
using QuantConnect.Data.Market;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.Slippage;
using QuantConnect.Securities;
namespace QuantConnect
{
public class Algo3 : QCAlgorithm
{
int MompPeriod = 30;
decimal momentumCutoff = -10;
decimal ratioOfLastMinuteForMaxTrade =0.05m;
TradeBars lastData = null;
int quantity = 0;
TradeBar lastBar = null;
decimal minimumPurchase = 500m;
Dictionary<string,MomentumPercent> moms=new Dictionary<string,MomentumPercent>();
public override void Initialize()
{
//Start and End Date range for the backtest:
//SetStartDate(2015, 1, 1);
//SetEndDate(DateTime.Now.Date.AddDays(-1));
SetStartDate(2016, 1, 1);
SetEndDate(2016, 5, 1);
SetCash(25000);
//volatile etf's
string tickersString="UVXY,XIV,NUGT,DUST,JNUG,JDUST,LABU,LABD,GUSH,DRIP,TVIX,GASL,GASX,DWTI,UWTI,DGAZ,UGAZ,UBIO,ZBIO,BRZU,RUSS,SCO,UCO,RUSL,ERY,ERX,BIOL,SVXY,VXX,SILJ,BIB,BIS,VIXY,SOXL,VIIX,SOXS,BZQ,USLV,SLVP,DSLV,GDXJ,GLDX";
//gold
//string tickersString="NUGT,DUST,JNUG,JDUST";
//68 biggest companies ranked by market cap.
//string tickersString="AAPL,GOOGL,GOOG,MSFT,XOM,BRK.A,BRK.B,FB,AMZN,JNJ,GE,WFC,T,NSRGY,CHL,JPM,RHHBY,PG,RDS.B,RDS.A,WMT,VZ,PFE,BUD,KO,BABA,CVX,TCEHY,SPY,NVS,V,DIS,HD,ORCL,TM,SSNLF,PM,MRK,BAC,PEP,CMCSA,NVO,INTC,IBM,CSCO,C,PTR,HSBC,UNH,MO,TSM,BMY,GILD,AMGN,TOT,SLB,RLNIY,MCD,MDT,CVS,MA,SNY,GSK,BTI,BP,LRLCY,MMM,IDEXY";
string[] tickers = tickersString.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (string ticker in tickers)
{
AddSecurity(SecurityType.Equity,ticker,Resolution.Minute);
}
foreach (Security s in Securities.Values)
{
s.FeeModel=new CustomFeeModel();
moms.Add(s.Symbol,MOMP(s.Symbol,MompPeriod));
}
}
public void OnData(TradeBars data)
{
decimal maxTrade;
if (lastData != null)
{
foreach (TradeBar bar in data.Values)
{
if (Portfolio.Cash < minimumPurchase)
{
break;
}
if (!Portfolio[bar.Symbol].HoldStock)
{
if (lastData.ContainsKey(bar.Symbol))
{
Debug("Before variables - Section 1");
Debug("Symbol" + bar.Symbol);
Debug("Bar Close" + bar.Close.ToString());
Debug("Bar Volume" + bar.Volume.ToString());
Debug("Ratio" + ratioOfLastMinuteForMaxTrade.ToString());
Debug("Portfolio Cash" + Portfolio.Cash.ToString());
maxTrade = bar.Close * bar.Volume / ratioOfLastMinuteForMaxTrade;
Debug("MaxTrade" + maxTrade.ToString());
quantity =(int)Math.Floor(Math.Min(Portfolio.Cash, maxTrade) / bar.Close);
Debug("Quantity" + (Portfolio.Cash/bar.Close).ToString());
//quantity = (int)(Portfolio.Cash/bar.Close);
lastBar = lastData[bar.Symbol];
Debug("Lastbar" + lastBar.ToString());
Debug("minimumPurchase" + minimumPurchase.ToString());
Debug("moms[bar.Symbol]" + moms[bar.Symbol].ToString());
Debug("momentumCutoff" + momentumCutoff.ToString());
Debug("lastBar.Close" + lastBar.Close.ToString());
//Why the code keep failing...
if (quantity * bar.Close > minimumPurchase & quantity > 0)
{
Debug("Prior buying ");
if (moms[bar.Symbol] < momentumCutoff & bar.Close>lastBar.Close)
{
Debug("Order Prior Execution - Symbol: " + bar.Symbol);
Debug("Order Prior Execution - Qtt: " + quantity.ToString());
Order(bar.Symbol, quantity);
}
}
Debug("After Buying");
}
}
}
TradeBar bar2;
foreach (SecurityHolding stock in Portfolio.Values)
{
if (Portfolio[stock.Symbol].Quantity > 0 & lastData.ContainsKey(stock.Symbol) & Portfolio.ContainsKey(stock.Symbol) & data.ContainsKey(stock.Symbol))
{
Debug("Before variables - Section 2");
lastBar = lastData[stock.Symbol];
bar2 = data[stock.Symbol];
Debug("Bar2 Close" + bar2.Close.ToString());
Debug("LastBar Close" + lastBar.Close.ToString());
if (bar2.Close < lastBar.Close)
{
Order(stock.Symbol, -Portfolio[stock.Symbol].Quantity);
}
}
}
}
lastData = data;
}
}
public class CustomFeeModel : IFeeModel
{
public decimal GetOrderFee(Security security, Order order)
{
var fee = order.AbsoluteQuantity*0.01m;
if (fee<5)
{
fee=5;
}
if (fee>10)
{
fee=10;
}
return fee/2m;
}
}
}