| Overall Statistics |
|
Total Trades 1274 Average Win 0.31% Average Loss -0.26% Compounding Annual Return -100.000% Drawdown 79.100% Expectancy -0.929 Net Profit -79.055% Sharpe Ratio -13.669 Loss Rate 97% Win Rate 3% Profit-Loss Ratio 1.16 Alpha -12.861 Beta -2.964 Annual Standard Deviation 0.965 Annual Variance 0.932 Information Ratio -13.512 Tracking Error 0.985 Treynor Ratio 4.451 Total Fees $4713.80 |
using QuantConnect.Data.Consolidators;
using QuantConnect.Orders;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Algorithm;
using QuantConnect.Data.Market;
using QuantConnect.Data;
using QuantConnect.Securities;
namespace QuantConnect
{
/*
* QuantConnect University: Bollinger Bands Example:
*/
public class IndicatorSuiteAlgorithm : QCAlgorithm
{
string _symbol = "SPY";
// var futureGold = AddFuture(Futures.Metals.Gold);
private const string RootSP500 = Futures.Indices.SP500EMini;
public Symbol SP500 = QuantConnect.Symbol.Create(RootSP500, SecurityType.Future, Market.USA);
ExponentialMovingAverage _ema;
SimpleMovingAverage _sma, sma5, sma9;
int hold = 0;
int counter = 0;
int time = 0;
// RelativeStrengthIndex rsi2;
// decimal limitprice;
OrderTicket ticket;
//RSI Custom Data:
decimal _price;
decimal _price2 = 0m;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Initialize
SetStartDate(2017, 4, 18);
SetEndDate(2017, 5, 18);
SetCash(25000);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute);
//Add the Custom Data:
var futureSP500 = AddFuture(RootSP500);
futureSP500.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60));
//Set up Indicators:
_ema = EMA(_symbol, 14, Resolution.Daily);
_sma = SMA(_symbol, 1, Resolution.Minute);
sma5 = SMA(_symbol, 5, Resolution.Minute);
sma9 = SMA(_symbol, 9, Resolution.Minute);
//Custom Data Indicator:
//1. Manually create instance of indicator class
//2. Create a "consolidator". If you don't need one use "IdentityDataConsolidator" which means "pass data through".
// var bitcoinIdentityConsolidator = new IdentityDataConsolidator<Bitcoin>();
//3. Manually Register Indicator to receive updates (using data.Value to generate indicator).
// RegisterIndicator("SPY", _rsiCustom, bitcoinIdentityConsolidator, x => x.Value);
//Note: If you want you could manually update the indicator class values with _rsiCustom.Update():
}
//Custom data event handler:
public void OnData(TradeBars data)
{
if((time > 572) && (time < 959))
{
counter++;
_price = data["SPY"].Close;
Plot("sma", sma5, sma9);
Plot("amount",Portfolio["SPY"].Quantity);
}
}
public override void OnData(Slice slice)
{
if((time > 572) && (time < 959))
{
Console.WriteLine(time);
Console.WriteLine(Time);
// short stuff
//___________________________________________________________________________________________________
if ((sma5 < sma9) && (hold == 0))
{
// short order
if (!Portfolio.Invested)
{
foreach(var chain in slice.FutureChains)
{
// find the front contract expiring no earlier than in 90 days
var contract = (
from futuresContract in chain.Value.OrderBy(x => x.Expiry)
where futuresContract.Expiry > Time.Date.AddDays(5)
select futuresContract
).FirstOrDefault();
// if found, trade it
if (contract != null)
{
MarketOrder(contract.Symbol, -2);
}
}
}
hold= -1;
}
if((hold == -1) && (sma5 >= sma9))
{
// sell
Liquidate();
hold = 0;
}
// Long stuff
//___________________________________________________________________________________________________
if ((sma5 > sma9) && (hold == 0))
{
// short order
if (!Portfolio.Invested)
{
foreach(var chain in slice.FutureChains)
{
// find the front contract expiring no earlier than in 90 days
var contract = (
from futuresContract in chain.Value.OrderBy(x => x.Expiry)
where futuresContract.Expiry > Time.Date.AddDays(5)
select futuresContract
).FirstOrDefault();
// if found, trade it
if (contract != null)
{
MarketOrder(contract.Symbol, 2);
}
}
}
hold = 1;
}
if((hold == 1) && (sma5 <= sma9))
{
// sell
Liquidate();
hold = 0;
}
if(time == 960)
Liquidate();
_price2 = _price;
}
time++;
}
// Fire plotting events once per day:
public override void OnEndOfDay()
{
Console.WriteLine("End of day");
// Order("SPY", -Portfolio["SPY"].Quantity);
// sell all futures
//Custom data indicator
Liquidate();
time = 0;
//Plot("STD", _std);
// Plot("AROON", _aroon.AroonUp, _aroon.AroonDown);
// Plot("MOM", _mom);
// Plot("MACD", "Price", _price);
// Plot("MACD", _macd.Fast, _macd.Slow);
// Plot("Averages", _ema, _sma);
}
}
}