Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
namespace QuantConnect 
{   
    public class FXCMBollengerBands : QCAlgorithm
    {
        private BollingerBands bollingerBands;
        private decimal _price;
        
        //private string security = "EURUSD"; 
        private string security = "USDJPY";
        
        
        private int consolidateMinutes = 15;
        private int cash = 25000;
        
        public override void Initialize() 
        {
			
            //Start and End Date range for the backtest:

            SetStartDate(DateTime.Now.Date.AddDays(-60));
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            //Cash allocation
            SetCash(cash);
            
            
            // FXCM Brokerage
            SetBrokerageModel(BrokerageName.FxcmBrokerage);
         
         
            AddSecurity(SecurityType.Forex, security, Resolution.Minute);
            Securities[security].SetLeverage(50.0m);
            
            // Setup BB Indicator
            bollingerBands = new BollingerBands(30, 3, MovingAverageType.Simple);
            
            // 15 min Trade bars
            var fifteenConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(consolidateMinutes));
            fifteenConsolidator.DataConsolidated += OnDataFifteen;
            SubscriptionManager.AddConsolidator(security,fifteenConsolidator);
            
            // Put it all together
            RegisterIndicator(security, bollingerBands, fifteenConsolidator, x => x.Value);
            
            SetWarmup(new TimeSpan(10, 0, 0, 0, 0));  // 10 Days
          
        }
            
      
        public void OnData(TradeBars data) 
        {   
        	if (!data.ContainsKey(security)) { return; }
        	// Placeholder
        }
        
        private void OnDataFifteen(object sender,TradeBar consolidated)
        {
            _price = consolidated.Close;
            if (!bollingerBands.IsReady) return;
            Plot("BB", "Price", _price);
            Plot("BB", bollingerBands.UpperBand, bollingerBands.MiddleBand, bollingerBands.LowerBand);
            
            
            decimal buy_signal = 0;
            decimal sell_signal = 0;
            
                if (!Portfolio.HoldStock)
                {
                    var _buyPrice = bollingerBands.LowerBand;
                    var _buySignal = consolidated.Close < _buyPrice;
                    if (_buySignal) { 
                    	var orderSize = (CalculateOrderQuantity(security,1.0m) / 1000) * 1000; // FXCM Required ordering in multiples of 1000 for standard and mini accounts https://www.fxcm.com/accounts/account-types/
                    	Order(security, orderSize); 
                    	buy_signal = _price;
                    }
                }
                else
                {
                    var _sellPrice = bollingerBands.UpperBand;
                    var _sellSignal =consolidated.Close > _sellPrice;
                    if (_sellSignal) { 
                    	Liquidate(security); 
                    	sell_signal = _price;
                    }
                }
        }
    }
}