using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
using MathNet.Numerics.Statistics;
namespace QuantConnect
{
// https://www.quantconnect.com/tutorials/dynamic-breakout-ii-strategy/
public class DynamicBreakoutAlgorithm : QCAlgorithm
{
IEnumerable<Slice> slices;
IEnumerable<decimal> close;
IEnumerable<decimal> high;
IEnumerable<decimal> low;
BollingerBands Bolband;
private const string Symbol = "BTCUSD";
//private const string Symbol = "GBPUSD";
private int numdays = 20;
private decimal buypoint, sellpoint, longLiqPoint, shortLiqPoint, yesterdayclose = 0;
public override void Initialize()
{
SetStartDate(2010,1,15);
SetEndDate(2016,2,15);
SetCash(100000);
AddCrypto(Symbol, Resolution.Hour);
SetBenchmark(Symbol);
const int ceiling = 60, floor = 20;
double[] closes, highs, lows, historyclose;
double todayvol, yesterdayvol, deltavol;
Schedule.On(DateRules.EveryDay(Symbol), TimeRules.BeforeMarketClose(Symbol, 1), () =>
{
slices = History(31, Resolution.Daily);
close = slices.Get(Symbol, Field.Close);
closes = close.ToDoubleArray();
todayvol = closes.Skip(1).StandardDeviation();
yesterdayvol = closes.Take(30).StandardDeviation();
deltavol = (todayvol - yesterdayvol) / todayvol;
numdays = (int)(Math.Round(numdays * (1 + deltavol)));
numdays = Math.Max(Math.Min(numdays, ceiling), floor);
Bolband = BB(Symbol, numdays, 2, MovingAverageType.Simple, Resolution.Daily);
slices = History(numdays, Resolution.Daily);
high = slices.Get(Symbol, Field.High);
highs = high.ToDoubleArray();
low = slices.Get(Symbol, Field.Low);
lows = low.ToDoubleArray();
buypoint = (decimal)highs.Max();
sellpoint = (decimal)lows.Min();
close = slices.Get(Symbol, Field.Close);
historyclose = close.ToDoubleArray();
longLiqPoint = (decimal)(historyclose.Mean());
shortLiqPoint = (decimal)(historyclose.Mean());
yesterdayclose = (decimal)historyclose.Skip(numdays-2).Take(1).Mean();
});
}
private decimal price, holdings;
public void OnData(TradeBars data)
{
// wait for our BollingerBand to fully initialize
if (yesterdayclose == 0)
{
return;
}
holdings = Portfolio[Symbol].Quantity;
price = Portfolio[Symbol].Price;
if (yesterdayclose > Bolband.UpperBand.Current.Value && price >= buypoint)
{
SetHoldings(Symbol, 1);
}
else if (yesterdayclose < Bolband.LowerBand.Current.Value && price <= sellpoint)
{
SetHoldings(Symbol, -1);
}
if (holdings > 0 && price <= longLiqPoint)
{
Liquidate(Symbol);
}
else if (holdings < 0 && price >= shortLiqPoint)
{
Liquidate(Symbol);
}
Log(yesterdayclose + " # of day " + numdays);
}
}
}
Douglas Stridsberg
Hi Liam,
No need to double-post - better to just post in your original thread.
BTCUSD didn't have data in 2010. In fact, the data starts somewhere in 2015. That could be your issue. Otherwise, there's plenty of places in your code where you're not checking that your variables are populated in the way you're expecting them to.
Link Liang
Hi Liam,
You could always check our data availability with our data explorer. BTCUSD is awailable from 2015-01-08 fpr GDAX and 2013-04-01 for Bitfinex, so you might want to change your start date.
By the way, you could specify which market you subscribe to when you subscribe to a new security. For example, `AddCrypto("BTCUSD", Resolution.Hour, Market.GDAX);` for GDAX and `AddCrypto("BTCUSD", Resolution.Hour, Market.Bitfinex);` for Bitfinex.
Hope it helps!
Liam Maurer
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!