//+--------------------------------------------------------------------+ //| Copyright: (C) 2016 Forex Software Ltd. | //| Website: http://forexsb.com/ | //| Support: http://forexsb.com/forum/ | //| License: Proprietary under the following circumstances: | //| | //| This code is a part of Forex Strategy Builder. It is free for | //| use as an integral part of Forex Strategy Builder. | //| One can modify it in order to improve the code or to fit it for | //| personal use. This code or any part of it cannot be used in | //| other applications without a permission. | //| The contact information cannot be changed. | //| | //| NO LIABILITY FOR CONSEQUENTIAL DAMAGES | //| | //| In no event shall the author be liable for any damages whatsoever | //| (including, without limitation, incidental, direct, indirect and | //| consequential damages, damages for loss of business profits, | //| business interruption, loss of business information, or other | //| pecuniary loss) arising out of the use or inability to use this | //| product, even if advised of the possibility of such damages. | //+--------------------------------------------------------------------+ #property copyright "Copyright (C) 2016 Forex Software Ltd." #property link "http://forexsb.com" #property version "2.00" #property strict #include #include //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class BetterBollingerBands : public Indicator { public: BetterBollingerBands(SlotTypes slotType) { SlotType=slotType; IndicatorName="Better Bollinger Bands"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDefaultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void BetterBollingerBands::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); MAMethod maMethod = (MAMethod) ListParam[1].Index; BasePrice price = (BasePrice) ListParam[2].Index; int BandsLength = (int) NumParam[0].Value; double dMpl = NumParam[1].Value; int previous=CheckParam[0].Checked ? 1 : 0; double adPrice[]; Price(price,adPrice); double MaBuffer[]; ArrayResize(MaBuffer,Data.Bars); ArrayInitialize(MaBuffer,0); double adUpBand[]; ArrayResize(adUpBand,Data.Bars); ArrayInitialize(adUpBand,0); double adDnBand[]; ArrayResize(adDnBand,Data.Bars); ArrayInitialize(adDnBand,0); double tBuffer[][4]; ArrayResize(tBuffer,Data.Bars); ArrayInitialize(tBuffer,0); int firstBar= 2+previous; int iMt1 = 0; int iMt2 = 1; int iUt1 = 2; int iUt2 = 3; double alpha = 2.0/(BandsLength+1.0); int k = (int)Data.Bars; for (int iBar = 3, r=iBar-1; iBar < Data.Bars; iBar++, r++) { tBuffer[r,iMt1] = tBuffer[r-1,iMt1] + alpha*(adPrice[iBar] -tBuffer[r-1,iMt1]); tBuffer[r,iUt1] = tBuffer[r-1,iUt1] + alpha*(tBuffer[r,iMt1]-tBuffer[r-1,iUt1]); double dt = ((2-alpha)*tBuffer[r,iMt1]-tBuffer[r,iUt1])/(1-alpha); tBuffer[r,iMt2] = tBuffer[r-1,iMt2] + alpha*(MathAbs(adPrice[iBar]-dt)-tBuffer[r-1,iMt2]); tBuffer[r,iUt2] = tBuffer[r-1,iUt2] + alpha*(tBuffer[r,iMt2] -tBuffer[r-1,iUt2]); double dt2 = ((2-alpha)*tBuffer[r,iMt2]-tBuffer[r,iUt2])/(1-alpha); adUpBand[iBar] = dt+dMpl*dt2; adDnBand[iBar] = dt-dMpl*dt2; MaBuffer[iBar] = dt; } ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "Upper Band"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = firstBar; ArrayCopy(Component[0].Value,adUpBand); ArrayResize(Component[1].Value,Data.Bars); Component[1].CompName = "Moving Average"; Component[1].DataType = IndComponentType_IndicatorValue; Component[1].FirstBar = firstBar; ArrayCopy(Component[1].Value,MaBuffer); ArrayResize(Component[2].Value,Data.Bars); Component[2].CompName = "Lower Band"; Component[2].DataType = IndComponentType_IndicatorValue; Component[2].FirstBar = firstBar; ArrayCopy(Component[2].Value,adDnBand); ArrayResize(Component[3].Value,Data.Bars); ArrayInitialize(Component[3].Value,0); Component[3].FirstBar=firstBar; ArrayResize(Component[4].Value,Data.Bars); ArrayInitialize(Component[4].Value,0); Component[4].FirstBar=firstBar; if(SlotType==SlotTypes_Open) { Component[3].DataType = IndComponentType_OpenLongPrice; Component[3].CompName = "Long position entry price"; Component[4].DataType = IndComponentType_OpenShortPrice; Component[4].CompName = "Short position entry price"; } else if(SlotType==SlotTypes_OpenFilter) { Component[3].DataType = IndComponentType_AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType_AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if(SlotType==SlotTypes_Close) { Component[3].DataType = IndComponentType_CloseLongPrice; Component[3].CompName = "Long position closing price"; Component[4].DataType = IndComponentType_CloseShortPrice; Component[4].CompName = "Short position closing price"; } else if(SlotType==SlotTypes_CloseFilter) { Component[3].DataType = IndComponentType_ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType_ForceCloseShort; Component[4].CompName = "Close out short position"; } if(SlotType==SlotTypes_Open || SlotType==SlotTypes_Close) { if(BandsLength>1) { for(int bar=firstBar; barData.High[bar-1] && dValueUpdOpen) || // The Data.Open price jumps below the indicator (Data.Close[bar-1]dValueUp && dValueUp>dOpen)) // The Data.Open price is in a negative gap dTempValUp=dOpen; // The entry/exit level is moved to Data.Open price // Lower band double dValueDown=adDnBand[bar-previous]; // Current value double dValueDown1=adDnBand[bar-previous-1]; // Previous value double dTempValDown=dValueDown; if((dValueDown1>Data.High[bar-1] && dValueDowndOpen) || // The Data.Open price jumps below the indicator (Data.Close[bar-1]dValueDown && dValueDown>dOpen)) // The Data.Open price is in a negative gap dTempValDown=dOpen; // The entry/exit level is moved to Data.Open price if(ListParam[0].Text=="Enter long at the Upper Band" || ListParam[0].Text=="Exit long at the Upper Band") { Component[3].Value[bar] = dTempValUp; Component[4].Value[bar] = dTempValDown; } else { Component[3].Value[bar] = dTempValDown; Component[4].Value[bar] = dTempValUp; } } } else { for(int bar=2; bar