| Overall Statistics |
|
Total Trades 1 Average Win 0% Average Loss 0% Compounding Annual Return -0.123% Drawdown 0.200% Expectancy 0 Net Profit 0% Sharpe Ratio -1.773 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.001 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio -0.785 Tracking Error 0.095 Treynor Ratio 1.865 Total Fees $2.00 |
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<QuoteBar> _datawindow = new RollingWindow<QuoteBar>(Hist);
RollingWindow<BollingerBandState> _bbwindow = new RollingWindow<BollingerBandState>(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 QuoteBarConsolidator(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, QuoteBar consolidated)
{
_price = consolidated.Close;
if (!_bb.IsReady) return;
_datawindow.Add(consolidated);
// save off the current state of the bollinger band object
_bbwindow.Add (new BollingerBandState(_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");
}
}
// class to hold the current state of a bollinger band instance
public class BollingerBandState
{
public readonly decimal UpperBand;
public readonly decimal MiddleBand;
public readonly decimal LowerBand;
public readonly decimal StandardDeviation;
public BollingerBandState(BollingerBands bb)
{
UpperBand = bb.UpperBand;
MiddleBand = bb.MiddleBand;
LowerBand = bb.LowerBand;
StandardDeviation = bb.StandardDeviation;
}
}
}