Thanks so much!!
using System;
using System.Collections.Generic;
using QuantConnect.Data.Consolidators;
using QuantConnect.Indicators;
using QuantConnect.Data.Market;
namespace QuantConnect.Algorithm
{
public class ForexBollinger : QCAlgorithm
{
BollingerBands _bb;
//Use our new consolidator class - 15 minutes / 15 bars joined.
decimal _price;
decimal stopPrice;
string symbol = "EURUSD";
static int Hist = 180;
static int Inputs = 4;
RollingWindow _datawindow = new RollingWindow(Hist);
RollingWindow _bbwindow = new RollingWindow(Hist);
public override void Initialize()
{
SetStartDate(2014, 5, 1);
SetEndDate(2015,6,4);
SetCash(200000);
AddSecurity(SecurityType.Forex, symbol, Resolution.Minute);
_bb = new BollingerBands(20, 2, MovingAverageType.Simple);
var fifteenConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(15));
fifteenConsolidator.DataConsolidated += OnDataFifteen;
SubscriptionManager.AddConsolidator(symbol,fifteenConsolidator);
RegisterIndicator(symbol, _bb, fifteenConsolidator, x => x.Value);
}
public void OnData(TradeBars data)
{
if (!_bb.IsReady) return;
if (!Portfolio.HoldStock)
{
Order("EURUSD", 1000);
Debug("Purchased EURUSD on " + Time.ToShortDateString());
}
foreach(string symbol in Securities.Keys){
if (Securities [symbol].Holdings.IsLong) {
if (data[symbol].Close <= stopPrice) {
Liquidate(symbol);
Debug ("Hit StopLoss: " + data[symbol].Close);
}
}
if (Securities [symbol].Holdings.IsShort) {
if (data[symbol].Close >= stopPrice) {
Liquidate(symbol);
Debug ("Hit StopLoss: " + data[symbol].Close);
}
}
}
}
private void OnDataFifteen(object sender,TradeBar consolidated)
{
_price = consolidated.Close;
if (!_bb.IsReady) return;
_datawindow.Add(consolidated);
// **** _bbwindow does not really keep a history of the Bollingerbands!!
_bbwindow.Add (_bb);
if (!_datawindow.IsReady) return;
if (!_bbwindow.IsReady) return;
Plot("BB", "Price", _price);
Plot("BB", _bb.UpperBand, _bb.MiddleBand, _bb.LowerBand);
}
// Fire plotting events once per day:
public override void OnEndOfDay()
{
//Log("EndOfDay");
}
}
}