using System;
using System.Globalization;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
namespace QuantConnect.EMA.GDAX
{
public class MovingAverageCross : QCAlgorithm
{
private const string Symbol = "MSFT";
private DoubleExponentialMovingAverage fast;
private DoubleExponentialMovingAverage slow;
public override void Initialize()
{
SetStartDate(2014, 01, 01);
SetEndDate(2018, 02, 03);
AddEquity(Symbol, Resolution.Daily);
fast = DEMA(Symbol, 30, Resolution.Daily);
slow = DEMA(Symbol, 150, Resolution.Daily);
}
private DateTime previous;
public void OnData(TradeBars data)
{
if (!slow.IsReady) return;
if (previous.Minute == Time.Minute) return;
const decimal tolerance = 0.00015m;
var holdings = Portfolio[Symbol].Quantity;
if (holdings <= 0)
{
// if the fast is greater than the slow, go long
if (fast > slow * (1 + tolerance))
{
Log("BUY >> " + Securities[Symbol].Price);
SetHoldings(Symbol, 1);
}
}
// Liquidate if currently holding a position
// if the fast is less than the slow then sell
if (holdings > 0 && fast < slow)
{
Log("SELL >> " + Securities[Symbol].Price);
Liquidate(Symbol);
}
Plot(Symbol, "Price", data[Symbol].Price);
Plot(Symbol, fast, slow);
previous = Time;
}
}
}
Ted Smith
I'm having an issue with the indicators using daily resolution. I've added a snippet of the code and you will see it's not executing any trades, yet if I substitute minute resolution into the same code, it works fine. I have left previous time to be evaluated on a minute basis because daily resolution for previous day gives an error. Any help would be greatly appreciated.
Also, could you please provide some insights to the non-trading hours and the indicators? How can I ignore the indicators during the non-trading hours, as well as, how to can I leave these data points out of the DEMA calcuations? Or is it ignored automatically?
Quant Trader
This one is with Daily resolution:
using System; using System.Globalization; using System.Linq; using QuantConnect.Indicators; using QuantConnect.Models; namespace QuantConnect.EMA.GDAX { public class MovingAverageCross : QCAlgorithm { private const string Symbol = "MSFT"; private DoubleExponentialMovingAverage fast; private DoubleExponentialMovingAverage slow; public override void Initialize() { SetStartDate(2009, 01, 01); SetEndDate(2018, 02, 03); AddEquity(Symbol, Resolution.Daily); fast = DEMA(Symbol, 30, Resolution.Daily); slow = DEMA(Symbol, 150, Resolution.Daily); SetWarmup(150); } //private DateTime previous; public void OnData(TradeBars data) { if (!slow.IsReady) return; //if (previous.Minute == Time.Minute) return; const decimal tolerance = 0.00015m; var holdings = Portfolio[Symbol].Quantity; if (holdings <= 0) { // if the fast is greater than the slow, go long if (fast > slow * (1 + tolerance)) { Log("BUY >> " + Securities[Symbol].Price); SetHoldings(Symbol, 1); } } // Liquidate if currently holding a position // if the fast is less than the slow then sell if (holdings > 0 && fast < slow) { Log("SELL >> " + Securities[Symbol].Price); Liquidate(Symbol); } Plot(Symbol, "Price", data[Symbol].Price); Plot(Symbol, fast, slow); //previous = Time; } } }
This one is with minute data:
using System; using System.Globalization; using System.Linq; using QuantConnect.Indicators; using QuantConnect.Models; namespace QuantConnect.EMA.GDAX { public class MovingAverageCross : QCAlgorithm { private const string Symbol = "MSFT"; private DoubleExponentialMovingAverage fast; private DoubleExponentialMovingAverage slow; public override void Initialize() { SetStartDate(2009, 01, 01); SetEndDate(2018, 02, 03); AddEquity(Symbol, Resolution.Minute); fast = DEMA(Symbol, 30, Resolution.Minute); slow = DEMA(Symbol, 150, Resolution.Minute); SetWarmup(150); } //private DateTime previous; public void OnData(TradeBars data) { if (!slow.IsReady) return; //if (previous.Minute == Time.Minute) return; const decimal tolerance = 0.00015m; var holdings = Portfolio[Symbol].Quantity; if (holdings <= 0) { // if the fast is greater than the slow, go long if (fast > slow * (1 + tolerance)) { Log("BUY >> " + Securities[Symbol].Price); SetHoldings(Symbol, 1); } } // Liquidate if currently holding a position // if the fast is less than the slow then sell if (holdings > 0 && fast < slow) { Log("SELL >> " + Securities[Symbol].Price); Liquidate(Symbol); } Plot(Symbol, "Price", data[Symbol].Price); Plot(Symbol, fast, slow); //previous = Time; } } }
No idea of those questions:
Also, could you please provide some insights to the non-trading hours and the indicators? How can I ignore the indicators during the non-trading hours, as well as, how to can I leave these data points out of the DEMA calcuations? Or is it ignored automatically?
I guess you only receive data once there is data to consume. Otherwise indicators would show empty for zero or with last price. You can use PlotIndicator(Symbol, fast); to see that.
Ted Smith
Thanks Quant Trader!!! That makes sense! As for the indicator questions, I've ran the code you provided and it's not plotting the indictors the same way my original code was plotting. Just as an FYI, my code was plotting the indicators during non-trading hours (at night) and I suspected it was taking these values into account whem making the DEMA calc. Therefore, throwing off the DEMAs.
Thank you for the helpful insights. I really appreciate it.
Ted Smith
Hi Quant Trader, I implemented your revisions into my code and it works very well for backtesting. The graphs are coming out great and it refines them significantly more than my original code. Yet, I'm noticing a significant lag in backtesting now because well, its running through all the code without waiting the minute I had previously told it to wait:
if (previous.Minute == Time.Minute) return;
Any recommendations on how to speed this up without passing in the "if (previous.Minute == Time.Minute) return;" statement?
Quant Trader
Can you share your algo so that it is easier to see how the code flows. If you do not trade per minute or make decision upon such data you can use daily resolution. You can use a scheduler which event is triggered for instance once per day to check if there is something to trade. Or use consolidators to summarize minute data to for instance hour data.
Quant Trader
Hi,
I have looked into your algorithm and I do not understand why you would not run per minute? You fast/ slow are per minute and you take a period of 30 minutes into account. So if your logic would not run per minute, you will miss the righ trade moments. Hope this helps. Processing data takes time.
J.
fast = DEMA(Symbol, 30, Resolution.Minute); slow = DEMA(Symbol, 150, Resolution.Minute);
Ted Smith
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!