| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 |
using QuantConnect.Data.Consolidators;
namespace QuantConnect
{
/*
* QuantConnect University: Full Basic Template:
*
* The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
* We have explained some of these here, but the full algorithm can be found at:
* https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
*/
public class BasicTemplateAlgorithm : QCAlgorithm
{
//Initialize the data and resolution you require for your strategy:
private TradeBarConsolidator _tradeBarConsolidator;
private int _tradeBarResolution = 60;
private int _historyPeriods = 60;
private RollingWindow<IndicatorDataPoint> _rsiHistory;
private RelativeStrengthIndex _rsi;
private int _rsiPeriod = 14;
private RollingWindow<IndicatorDataPoint> _atrHistory;
private AverageTrueRange _atr;
private int _atrPeriod = 14;
private RollingWindow<IndicatorDataPoint> _stochHistory;
private Stochastic _stoch;
private Symbol _symbol;
private RollingWindow<TradeBar> _barHistory;
public override void Initialize()
{
//Start and End Date range for the backtest:
SetStartDate(DateTime.Now.Date.AddDays(-4));
SetEndDate(DateTime.Now.Date.AddDays(-2));
//Cash allocation
SetCash(25000);
//Add as many securities as you like. All the data will be passed into the event handler:
_symbol = QuantConnect.Symbol.Create("EURUSD", SecurityType.Forex, Market.Oanda);
AddSecurity(SecurityType.Forex, _symbol, Resolution.Minute);
_tradeBarConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(_tradeBarResolution));
_stoch = new Stochastic(11, 3, 3);
RegisterIndicator(_symbol, _stoch, _tradeBarConsolidator);
_stochHistory = HistoryTracker.TrackBar(_stoch, _historyPeriods + 1);
//_stochHistory = new RollingWindow<Stochastic>(_historyPeriods + 1);
_rsi = new RelativeStrengthIndex(_rsiPeriod);
RegisterIndicator(_symbol, _rsi, _tradeBarConsolidator);
_rsiHistory = HistoryTracker.Track(_rsi, _historyPeriods + 1);
//_rsiHistory = new RollingWindow<RelativeStrengthIndex>(_historyPeriods + 1);
_atr = new AverageTrueRange(_atrPeriod);
RegisterIndicator(_symbol, _atr, _tradeBarConsolidator);
_atrHistory = HistoryTracker.TrackBar(_atr, _historyPeriods + 1);
//_atrHistory = new RollingWindow<AverageTrueRange>(_historyPeriods + 1);
_tradeBarConsolidator.DataConsolidated += _tradeBarConsolidator_DataConsolidated;
SubscriptionManager.AddConsolidator(_symbol, _tradeBarConsolidator);
}
private void _tradeBarConsolidator_DataConsolidated(object sender, TradeBar data)
{
if (!_stoch.IsReady) return;
//_stochHistory.Add(_stoch);
//_atrHistory.Add(_atr);
//_rsiHistory.Add(_rsi);
if (_stochHistory.IsReady && _rsiHistory.IsReady && _atrHistory.IsReady)
{
for (int i = 0; i < _historyPeriods; i++)
{
if (_rsiHistory[i + 1].EndTime != _atrHistory[i + 1].EndTime && _atrHistory[i + 1].EndTime != _stochHistory[i + 1].EndTime)
{
Log("Times Do Not Align - Pre Sample Assignment");
}
double[] sample = new double[3];
//Stoch
sample[0] = Convert.ToDouble(_stochHistory[i+1]);
//RSI
sample[1] = Convert.ToDouble(_rsiHistory[i+1]);
//ATR
sample[2] = Convert.ToDouble(_atrHistory[i+1]);
DateTime stochTime = _stochHistory[i+1].EndTime;
DateTime rsiTime = _rsiHistory[i+1].EndTime;
DateTime atrTime = _atrHistory[i+1].EndTime;
if (stochTime != rsiTime && rsiTime != atrTime)
{
Log("Times Do Not Align - Post Sample Assignment");
}
}
}
}
}
}namespace QuantConnect {
/// <summary>
/// Provides helper methods to track the history of indicators and consolidators
/// </summary>
public static class HistoryTracker
{
/// <summary>
/// Track an indicator's history
/// </summary>
/// <typeparam name="T">The indicator's input type</typeparam>
/// <param name="indicator">The indicator to track</param>
/// <param name="historyLength">The length of history to keep, defaults to 8</param>
/// <returns>A rolling window of the requested length that will automatically update as the indicator does</returns>
public static RollingWindow<IndicatorDataPoint> Track<T>(IndicatorBase<T> indicator, int historyLength = 8)
where T : BaseData
{
// create a rolling window of the requested length
var window = new RollingWindow<IndicatorDataPoint>(historyLength);
// wire up an event so that each time the indicator gets updated, we add the new value to our window
indicator.Updated += (sender, args) => window.Add(indicator.Current);
// return the window now that it's wired up to the indicator
return window;
}
/// <summary>
/// Track an indicator's history
/// </summary>
/// <typeparam name="T">The indicator's input type</typeparam>
/// <param name="indicator">The indicator to track</param>
/// <param name="historyLength">The length of history to keep, defaults to 8</param>
/// <returns>A rolling window of the requested length that will automatically update as the indicator does</returns>
//TODO - This should be cleaned up to make the Track method more generic so we don't have to accept the strong type BarIndicator
public static RollingWindow<IndicatorDataPoint> TrackBar(BarIndicator indicator, int historyLength = 8)
{
// create a rolling window of the requested length
var window = new RollingWindow<IndicatorDataPoint>(historyLength);
// wire up an event so that each time the indicator gets updated, we add the new value to our window
indicator.Updated += (sender, args) => window.Add(indicator.Current);
// return the window now that it's wired up to the indicator
return window;
}
/// <summary>
/// Track an consolidator's history
/// </summary>
/// <typeparam name="T">The consolidator's output type</typeparam>
/// <param name="consolidator">The consolidator to track</param>
/// <param name="historyLength">The length of history to keep, defaults to 8</param>
/// <returns>A rolling window of the requested length that will automatically update as the consolidator does</returns>
public static RollingWindow<T> Track<T>(DataConsolidator<T> consolidator, int historyLength = 8)
where T : BaseData
{
// create a rolling window of the requested length
var window = new RollingWindow<T>(historyLength);
// wire up and event so that each time the consolidator gets updated, we add the new value to our window
consolidator.DataConsolidated += (sendar, args) => window.Add((T)args);
// return the window now that it's ired up to the indicator
return window;
}
/// <summary>
/// Track an consolidator's history
/// </summary>
/// <typeparam name="T">The consolidator's output type</typeparam>
/// <param name="consolidator">The consolidator to track</param>
/// <param name="historyLength">The length of history to keep, defaults to 8</param>
/// <returns>A rolling window of the requested length that will automatically update as the consolidator does</returns>
public static RollingWindow<T> Track<T>(IDataConsolidator consolidator, int historyLength = 8) where T : class
{
// create a rolling window of the requested length
var window = new RollingWindow<T>(historyLength);
// wire up and event so that each time the consolidator gets updated, we add the new value to our window
consolidator.DataConsolidated += (sendar, args) => window.Add(args as T);
// return the window now that it's ired up to the indicator
return window;
}
}
}