| Overall Statistics |
|
Total Trades 1 Average Win 0% Average Loss -3.54% Compounding Annual Return -72.188% Drawdown 17.900% Expectancy -1 Net Profit -3.537% Sharpe Ratio -0.365 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -1.477 Beta 3.179 Annual Standard Deviation 1.041 Annual Variance 1.084 Information Ratio -0.702 Tracking Error 1.032 Treynor Ratio -0.119 Total Fees $104.02 |
using System;
using System.Linq;
using QuantConnect.Data.Consolidators;
using QuantConnect.Indicators;
using QuantConnect.Data.Market;
using QuantConnect.Algorithm;
using QuantConnect.Data.Custom;
namespace QuantConnect
{
public class KYJIndicators : QCAlgorithm
{
//string crude = "CME/CLN2015";
string symbol = "UWTI";
MovingAverageConvergenceDivergence macd;
MovingAverageConvergenceDivergence fifteenMinute_MACD;
RelativeStrengthIndex rsi;
RelativeStrengthIndex fifteenMinute_RSI;
MoneyFlowIndex mfi;
MoneyFlowIndex fifteenMinute_MFI;
decimal price;
TradeBar symbolDaily;
//Initialize the data and resolution you require for your strategy:
public override void Initialize ()
{
SetStartDate (2015, 5, 11);
SetEndDate (2015, 5, 22);
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, fillDataForward: false);
//AddData<QuandlFuture> (crude, Resolution.Daily);
//Set up Indicators
macd = MACD (symbol, 12, 26, 9, MovingAverageType.Simple, Resolution.Minute);
rsi = RSI (symbol, 14, MovingAverageType.Wilders, Resolution.Minute);
mfi = MFI (symbol, 14, Resolution.Minute);
//Setup Consolidators
// define our daily trade bar consolidator. we can access the daily bar
// from the DataConsolidated events
var dailyConsolidator = new TradeBarConsolidator (TimeSpan.FromDays (1));
var fifteenMinuteConsolidator = new TradeBarConsolidator (TimeSpan.FromMinutes (15));
fifteenMinute_MACD = new MovingAverageConvergenceDivergence (12, 26, 9, MovingAverageType.Simple);
fifteenMinute_RSI = new RelativeStrengthIndex (14, MovingAverageType.Wilders);
fifteenMinute_MFI = new MoneyFlowIndex (14);
RegisterIndicator (symbol, fifteenMinute_MACD, fifteenMinuteConsolidator, x => x.Value); // x => ((TradeBar) x).Close
RegisterIndicator (symbol, fifteenMinute_RSI, fifteenMinuteConsolidator, x => x.Value);
//RegisterIndicator (symbol, fifteenMinute_MFI, fifteenMinuteConsolidator, x => x.Value);
// attach our event handler. the event handler is a function that will be called
// each time we produce a new consolidated piece of data
dailyConsolidator.DataConsolidated += OnDataDaily;
// this call adds our daily consolidator to the manager to receive updates
// from the engine
SubscriptionManager.AddConsolidator (symbol, dailyConsolidator);
// setup Charts
var chart = new Chart ("rsi");
var buy = new Series ("buy", SeriesType.Scatter);
var sell = new Series ("sell", SeriesType.Scatter);
var rsis = new Series ("rsi");
chart.AddSeries (buy);
chart.AddSeries (sell);
chart.AddSeries (rsis);
AddChart (chart);
}
public void OnDataDaily (object sender, TradeBar consolidated)
{
symbolDaily = consolidated;
Log (Time.ToString ("u") + " Close price: " + consolidated.Close);
}
//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
//public void OnData (Quandl data)
public void OnData (TradeBars data)
{
// removed MFI since broken...
if (!macd.IsReady || !rsi.IsReady || !fifteenMinute_MACD.IsReady ||
!fifteenMinute_RSI.IsReady) {
return;
}
decimal macdLine = macd.Fast - macd.Slow;
decimal macdHistogram = Math.Round (macdLine - macd.Signal, 3);
price = Math.Round (data [symbol].Close, 3);
// Plot RSI
Plot ("rsi", "rsi", fifteenMinute_RSI);
// MFI not working (always 100) & can't RegisterIndicator
if (!Portfolio.Invested && fifteenMinute_RSI < 30) {
Plot ("rsi", "buy", fifteenMinute_RSI);
SetHoldings (symbol, 0.75);
Log (Time.ToString ("u") + " Purchased: " + symbol + " price = " + price + " MACDHis = " + macdHistogram + " 15m RSI = " + fifteenMinute_RSI + " MFI = " + mfi);
}
else if (Portfolio.Invested && fifteenMinute_RSI > 70) {
Plot ("rsi", "sell", fifteenMinute_RSI);
Liquidate (symbol);
Log (Time.ToString ("u") + " Sold: " + symbol + " price = " + price + " MACDHis = " + macdHistogram + " 15m RSI = " + fifteenMinute_RSI + " MFI = " + mfi);
}
}
// Fire plotting events once per day:
public override void OnEndOfDay (string symbol)
{
/*
if (!macd.IsReady || !rsi.IsReady || !mfi.IsReady) {
return;
}
*/
//Plot("MACD", "Price", price);
//Plot("MACD", macd.Fast, macd.Slow);
//Plot ("RSI", "rsi", rsi);
//Plot ("RSI", "price", price);
//Plot ("MFI", mfi);
}
}
// Custom quandl data type for setting customized value column name.
// Value column is used for the primary trading calculations and charting.
public class QuandlFuture : Quandl
{
public QuandlFuture () : base (valueColumnName: "Settle")
{
}
}
}