I'm stuck on this task in the boot camp. I've attempted to copy/paste the solution, but it doesn't work. Any ideas?
Task link: https://www.quantconnect.com/learning/task/169/Taking-Positions
Copy/paste solution that fails:
using System.Reflection;
namespace QuantConnect
{
public partial class BootCampTask : QCAlgorithm
{
public override void Initialize()
{
SetStartDate(2018, 7, 1);
SetEndDate(2019, 3, 31);
SetCash(100000);
var symbols = new [] {QuantConnect.Symbol.Create("PEP", SecurityType.Equity, Market.USA), QuantConnect.Symbol.Create("KO", SecurityType.Equity, Market.USA)};
SetUniverseSelection(new ManualUniverseSelectionModel(symbols));
UniverseSettings.Resolution = Resolution.Hour;
UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw;
AddAlpha(new PairsTradingAlphaModel());
SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
SetExecution(new ImmediateExecutionModel());
}
//3. Use OnEndOfDay() to Log() your positions at the close of each trading day
public override void OnEndOfDay(Symbol symbol)
{
Log("Taking a position of " + Portfolio[symbol].Quantity.ToString() + " units of symbol " + symbol.ToString());
}
}
public partial class PairsTradingAlphaModel : AlphaModel
{
SimpleMovingAverage spreadMean;
StandardDeviation spreadStd;
public Security[] Pair;
//1. Create a private period variable that is type TimeSpan
TimeSpan period;
public PairsTradingAlphaModel()
{
spreadMean = new SimpleMovingAverage(500);
spreadStd = new StandardDeviation(500);
Pair = new Security[2];
//1. Set self.period to a 2 hour timedelta
period = TimeSpan.FromHours(2);
}
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
var spread = Pair[1].Price - Pair[0].Price;
spreadMean.Update(algorithm.Time, spread);
spreadStd.Update(algorithm.Time, spread);
// I've tried changing these to spreadMean.Current.Value and spreadStd.Current.Value,
// but it still fails.
var upperthreshold = spreadMean + spreadStd;
var lowerthreshold = spreadMean - spreadStd;
//2. Emit an Insight.Group() if the spread is greater than the upperthreshold
if (spread > upperthreshold)
{
return Insight.Group(
Insight.Price(Pair[0].Symbol, period, InsightDirection.Up),
Insight.Price(Pair[1].Symbol, period, InsightDirection.Down)
);
}
//2. Emit an Insight.Group() if the spread is less than the lowerthreshold
if (spread < lowerthreshold)
{
return Insight.Group(
Insight.Price(Pair[0].Symbol, period, InsightDirection.Down),
Insight.Price(Pair[1].Symbol, period, InsightDirection.Up)
);
}
return Enumerable.Empty<Insight>();
}
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
Pair = changes.AddedSecurities.ToArray();
}
}
}
Landon Wilkins
Error: “Looks like there weren't enough orders…”
Landon Wilkins
I contacted support. They fixed the solution \o/!
Louis Szeto
Hi Landon
We cannot reproduce this issue. If the issue persist after refreshing and retrying, please email to support@quantconnect.com
Best
Louis
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.
Landon Wilkins
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!