我正在使用QuantConnect平台实现一些交易策略,目前我正在尝试在自定义时间框架(4h、8h、12h等)上运行一些测试。使用合并程序,但对于使用合并程序和不使用合并程序的同一时间解决方案,指示符值不同。我已经将问题隔离到这个简单的例子中,在这个例子中,我只在1小时的时间范围内创建了2个EMA指标,一个使用consolidator,另一个没有consolidator,并记录它们的值,这两个值是不同的。
感谢您的反馈。
namespace QuantConnect
{
public class ConsolidatorAlgorithm : QCAlgorithm
{
private readonly Resolution _resolution = Resolution.Hour;
private readonly string _ticker = "ETHUSD";
private readonly int _startingCash = 2000;
private readonly int _fastPeriod = 12;
private ExponentialMovingAverage _fastEmaCustomTimeFrame;
private ExponentialMovingAverage _fastEmaStandardResolution;
private string _baseSymbol;
public override void Initialize()
{
SetStartDate(2017, 1,1); //Set Start Date
SetEndDate(2017, 1, 2); //Set End Date
SetCash(_startingCash); //Set Strategy Cash
QuantConnect.Securities.Crypto.Crypto crypto = AddCrypto(_ticker, _resolution);
_baseSymbol = crypto.BaseCurrencySymbol;
SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);
TradeBarConsolidator consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));
SubscriptionManager.AddConsolidator(_ticker, consolidator);
consolidator.DataConsolidated += OnCustomHandler;
_fastEmaCustomTimeFrame = EMA(_ticker, _fastPeriod);
_fastEmaStandardResolution = EMA(_ticker, _fastPeriod, _resolution);
RegisterIndicator(_ticker, _fastEmaCustomTimeFrame, consolidator);
var history = History<TradeBar>(_ticker, 12);
foreach (var bar in history) {
_fastEmaCustomTimeFrame.Update(bar.EndTime, bar.Close);
_fastEmaStandardResolution.Update(bar.EndTime, bar.Close);
}
}
public void OnCustomHandler(object sender, TradeBar data)
{
if (!_fastEmaCustomTimeFrame.IsReady && !_fastEmaStandardResolution.IsReady) {
return;
}
Log($"ema custom time frame: {_fastEmaCustomTimeFrame}");
}
public void OnData(TradeBars data)
{
if (!_fastEmaCustomTimeFrame.IsReady && !_fastEmaStandardResolution.IsReady) {
return;
}
Log($"ema standard time resolution: {_fastEmaStandardResolution}");
}
}
}
Log output:
2017-01-01 00:00:00 : Launching analysis for a7d9bc8bc4829b1ba77ee9753d0f3cdc with LEAN Engine v2.4.0.0.6246
2017-01-01 00:00:00 : ema custom time frame: 8.32998
2017-01-01 00:00:00 : ema standard time resolution: 8.32998
2017-01-01 01:00:00 : ema custom time frame: 8.36372
2017-01-01 01:00:00 : ema standard time resolution: 8.33768
2017-01-01 02:00:00 : ema custom time frame: 8.38526
2017-01-01 02:00:00 : ema standard time resolution: 8.36111
2017-01-01 03:00:00 : ema custom time frame: 8.4027
2017-01-01 03:00:00 : ema standard time resolution: 8.36863
2017-01-01 04:00:00 : ema custom time frame: 8.40478
2017-01-01 04:00:00 : ema standard time resolution: 8.375
2017-01-01 05:00:00 : ema custom time frame: 8.40164
2017-01-01 05:00:00 : ema standard time resolution: 8.37577
2017-01-01 06:00:00 : ema custom time frame: 8.3678
2017-01-01 06:00:00 : ema standard time resolution: 8.34873
2017-01-01 07:00:00 : ema custom time frame: 8.32476
2017-01-01 07:00:00 : ema standard time resolution: 8.33046
2017-01-01 08:00:00 : ema custom time frame: 8.29785
2017-01-01 08:00:00 : ema standard time resolution: 8.31501
2017-01-01 09:00:00 : ema custom time frame: 8.30319
2017-01-01 09:00:00 : ema standard time resolution: 8.32654
2017-01-01 10:00:00 : ema custom time frame: 8.32169
2017-01-01 10:00:00 : ema standard time resolution: 8.33015
2017-01-01 11:00:00 : ema custom time frame: 8.3405
2017-01-01 11:00:00 : ema standard time resolution: 8.34397...发布于 2019-09-11 03:43:42
如果有人对解决方案感兴趣-我需要使用ExponentialMovingAverage的构造函数而不是工厂方法QCAlgorithm#EMA,之后指示符值是相同的。
namespace QuantConnect
{
public class ConsolidatorAlgorithm : QCAlgorithm
{
private readonly Resolution _resolution = Resolution.Hour;
private readonly string _ticker = "ETHUSD";
private readonly int _startingCash = 2000;
private readonly int _fastPeriod = 12;
private ExponentialMovingAverage _fastEmaCustomTimeFrame;
private ExponentialMovingAverage _fastEmaStandardResolution;
private string _baseSymbol;
public override void Initialize()
{
SetStartDate(2017, 1,1); //Set Start Date
SetEndDate(2017, 1, 2); //Set End Date
SetCash(_startingCash); //Set Strategy Cash
QuantConnect.Securities.Crypto.Crypto crypto = AddCrypto(_ticker, _resolution);
_baseSymbol = crypto.BaseCurrencySymbol;
SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);
TradeBarConsolidator consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));
SubscriptionManager.AddConsolidator(_ticker, consolidator);
consolidator.DataConsolidated += OnCustomHandler;
// use constructor for ExponentialMovingAverage instead of QCAlgorithm#EMA
_fastEmaCustomTimeFrame = new ExponentialMovingAverage(_fastPeriod);
_fastEmaStandardResolution = EMA(_ticker, _fastPeriod, _resolution);
RegisterIndicator(_ticker, _fastEmaCustomTimeFrame, consolidator);
var history = History<TradeBar>(_ticker, 12);
foreach (var bar in history) {
_fastEmaCustomTimeFrame.Update(bar.EndTime, bar.Close);
_fastEmaStandardResolution.Update(bar.EndTime, bar.Close);
}
}
public void OnCustomHandler(object sender, TradeBar data)
{
if (!_fastEmaCustomTimeFrame.IsReady && !_fastEmaStandardResolution.IsReady) {
return;
}
Log($"ema custom time frame: {_fastEmaCustomTimeFrame}");
}
public void OnData(TradeBars data)
{
if (!_fastEmaCustomTimeFrame.IsReady && !_fastEmaStandardResolution.IsReady) {
return;
}
Log($"ema standard time resolution: {_fastEmaStandardResolution}");
}
}
}发布于 2019-09-11 08:04:19
是的,两种不同方法的指标值是相同的。
您说得对,在处理合并数据时,我们需要定义一个自定义指标,而不是使用内置的指标。我在Python语言中附加了一个backtest,以便其他感兴趣的用户可以查看它。此外,我们的Github代码库中的this demonstration algorithm有助于展示如何使用自定义指示器。
最好的
Xin
https://stackoverflow.com/questions/57664246
复制相似问题