Bollinger band Squeeze strategies
Forums › ProRealTime English forum › ProOrder support › Bollinger band Squeeze strategies
- This topic has 14 replies, 3 voices, and was last updated 3 years ago by robertogozzi.
Tagged: atr, average true range, averagetruerange, bands, BB, Bollinger, bollinger bands, capital, dd, drawdown, equity, loss, lot size, lotsize, management, margin, Money, positionperf, POSITIONPRICE, Profit, range, risk, Risk Management, ru, runup, set, SL, target, tp, true
-
-
08/19/2021 at 6:52 AM #175668
Hello,
Please i need help in programming a strategy with bollinger band and the bandwidth.
As shown in the attached screenshot;
Go long [L] when Bollinger Band Width starts to rise after contracting to a historic low.
How do i program this?
NB: Bollinger requires contractions below 2.0%, but wider contractions provide perfectly adequate signals.
08/19/2021 at 10:52 AM #175687There you go:
123456789101112131415161718BBperiods = 20 //periods to calculate Bollinger BandsLookBack = 50 //min BB gap in these last barsMinWidth = 2 //max percentage of distance between the two bandsBBwidth = BollingerBandWidth[BBperiods](close)L1 = BBwidth = lowest[LookBack](BBwidth)L2 = BBwidth > BBwidth[1] //Contraction endedBollUP = BollingerUP[BBperiods](close)BollDOWN = BollingerDOWN[BBperiods](close)BBgap = BollUP - BollDOWNMinBBgap = lowest[LookBack](BBgap)L3 = MinBBgap = lowest[LookBack](MinBBgap)L4 = MinBBgap <= (BollUP * MinWidth / 100)CondL = L1[1] AND L2 AND L3[1] AND L4[1] AND Not OnMarketIF CondL THENBUY 1 Contract at MarketSET STOP pLOSS 200SET TARGET pPROFIT 600ENDIF08/20/2021 at 10:25 AM #175768Thanks very much, i appreciate .
- i run the codes in the probacktest but receiving no data notifications as shown in the attached screenshot.
- I have also modified the codes to run it as indicator with Buy and Sell signals so i can have a visual display of the strategy effects before moving to backtest, but i have the attached errors when running;
MyLongConditions = close CROSSES OVER BollingerDOWN[20]
MyShortConditions = close CROSSES UNDER BollingerUP[20]
BBperiods = 20 //periods to calculate Bollinger Bands
LookBack = 50 //min BB gap in these last bars
MinWidth = 2 //max percentage of distance between the two bands
BBwidth = BollingerBandWidth[BBperiods](close)
L1 = BBwidth = lowest[LookBack](BBwidth)
L2 = BBwidth > BBwidth[1] //Contraction ended
BollUP = BollingerUP[BBperiods](close)
BollDOWN = BollingerDOWN[BBperiods](close)
BBgap = BollUP – BollDOWN
MinBBgap = lowest[LookBack](BBgap)
L3 = MinBBgap = lowest[LookBack](MinBBgap)
L4 = MinBBgap <= (BollUP * MinWidth / 100)
CondL = L1[1] AND L2 AND L3[1] AND L4[1] AND Not OnMarketIF CondL THEN
//IF MyLongConditions THEN
DrawText(“Buy”,barIndex, low – range*2,serif,bold,15) coloured(0,128,0,135)
//
//BUY 1 Contract at Market
//SET STOP pLOSS 200
//SET TARGET pPROFIT 600
ELSE
DrawText(“Sell”,barIndex, low – range*2,serif,bold,15) coloured(0,128,0,135)
ENDIFRETURN
3. the codes does not take into account or does not consider the bollinger upper and lower crossovers.
“Go long [L] when Bollinger Band Width starts to rise after contracting to a historic low.” ; how do we incorporate upper and lower crossovers into this strategy?
08/20/2021 at 11:05 AM #175774Ok, I’m sorry, I coded a strategy instead of an indicator.
There you go:
12345678910111213141516171819BBperiods = 20 //periods to calculate Bollinger BandsLookBack = 50 //min BB gap in these last barsMinWidth = 2 //max percentage of distance between the two bandsBBwidth = BollingerBandWidth[BBperiods](close)L1 = BBwidth = lowest[LookBack](BBwidth)L2 = BBwidth > BBwidth[1] //Contraction endedBollUP = BollingerUP[BBperiods](close)BollDOWN = BollingerDOWN[BBperiods](close)BBgap = BollUP - BollDOWNMinBBgap = lowest[LookBack](BBgap)L3 = MinBBgap = lowest[LookBack](MinBBgap)L4 = MinBBgap <= (BollUP * MinWidth / 100)CondL = L1[1] AND L2 AND L3[1] AND L4[1]IF CondL THENDrawText(“Buy”,barIndex, low – range*2,serif,bold,15) coloured(0,128,0,135)//ELSE// DrawText(“Sell”,barIndex, high + range*2,serif,bold,15) coloured(0,128,0,135)ENDIFRETURNIf you want CROSSOVERS to be taken into account you need to tell me what to do in that case.
Furthermore, this code only considers LONG signals as per your request. For gthe SHORT side, if needed, tell me what to do.08/20/2021 at 12:30 PM #175780Thanks very much,
- i have run the code and please see attached results (BollingerBandSqueeze_Bandwidth), you will notice that Buy is taking place right within the consolidation/squeeze period which does not provide opportunities.
2. Also the first and original bollinger band strategy results is also attached (BollingerBandSqueeze_Bandwidth2) based on the codes below; here too trading taking place within the squeeze.
MyLongConditions = close CROSSES OVER BollingerDOWN[20]
MyShortConditions = close CROSSES UNDER BollingerUP[20]
MyBandWidth = BollingerBandWidth[20](close)//For MyBandWidth <= 0.00295
IF MyShortConditions THEN
DrawText(“Sell”,barIndex, high + range*2,serif,bold,15) coloured(255,0,0,255)
ELSIF MyLongConditions THEN
DrawText(“Buy”,barIndex, low – range*2,serif,bold,15) coloured(0,128,0,135)
ENDIFRETURN
3. “Go long [L] when Bollinger Band Width starts to rise after contracting to a historic low.” this should be modified to wait for crossovers first before Buy and Sell after contraction.
In other words there should be no Buy and Sell during consolidation or contraction
08/20/2021 at 5:39 PM #175819Hello, i just want to clarify further:
- test for the squeeze/contraction.
- wait till the end of the contraction and the Bollinger Band Width starts to rise after contracting to a historic low.
- when Bollinger Band Width starts to rise after contracting to a historic low, don not just enter the market for every rise of the width, but enter only at rise that produce CROSSOVERS
- Buy—> MyLongConditions = close CROSSES OVER BollingerDOWN[20]
Sell—>MyShortConditions = close CROSSES UNDER BollingerUP[20]
Hope this has fully clarified the strategy.
08/22/2021 at 11:19 AM #175907There you go:
123456789101112131415161718BBperiods = 20 //periods to calculate Bollinger BandsLookBack = 50 //min BB gap in these last barsMinWidth = 2 //max percentage of distance between the two bandsBBwidth = BollingerBandWidth[BBperiods](close)C1 = BBwidth = lowest[LookBack](BBwidth)C2 = BBwidth > BBwidth[1] //Contraction endedBollUP = BollingerUP[BBperiods](close)BollDOWN = BollingerDOWN[BBperiods](close)L1 = close CROSSES OVER BollDOWNS1 = close CROSSES UNDER BollUPCondL = C1[1] AND C2 AND L1CondS = C1[1] AND C2 AND S1IF CondL THENDrawText("Buy",barIndex, low - range*2,serif,bold,15) coloured(0,128,0,255)//135ELSIF CondS THENDrawText("Sell",barIndex, high + range*2,serif,bold,15) coloured(255,0,0,255)ENDIFRETURN1 user thanked author for this post.
08/23/2021 at 1:56 PM #175992Hello , thanks very much.
I have made many modifications and got the attached backtest results : 100% results for few data , 56% for bigger data.
Now i want to apply risk management to improve the results, but am not sure whether to add stop loss & take profit at the end of the complete codes or i add it after both Buy statement and Sell statements.
I want to apply risk in percentage , so please i will be glad you provide me with the codes for that
08/24/2021 at 12:29 PM #176071Placing SL & TP at the end of your code or just after the entry doesn’t make any difference, unless you use different values for the two sides.
For position size management you can add this at the beginning of your code (just after DEFPARAMs):
12345678910111213141516171819202122232425262728////////////////////////////////////////////////////////////////////////// DrawDown calculation + LARRY WILLIAMS' formula for size management//ONCE Capital = 10000 //set your initial investmentONCE LotSize = 1 //start with 1//------------------------------------------// DrawDown calculationONCE MinPoint = CapitalONCE MaxPoint = 0ONCE MaxRU = 0ONCE MaxDD = 0IF StrategyProfit <> 0 THENEquity = Capital + StrategyProfitTempProfit = PositionPerf * PositionPrice / PipSizeTempEquity = Equity + TempProfitMaxPoint = max(MaxPoint,TempEquity)DD = MaxPoint - TempEquityMaxDD = max(MaxDD,DD)//------------------------------------------// LARRY WILLIAMS' formula (+margin)MinSize = 0.5 //Minimum lot size allowedMargin = high / 100 * 0.5 //Margin required 0.5%Risk = 5 //Risk per trade 2%TempLotSize = max(MinSize,(Equity * Risk / 100) / (MaxDD + Margin))LotSize = round((TempLotSize * 10) - 0.5) / 10 //only 1 decimal digit allowedENDIF//////////////////////////////////////////////////////////////////////////then use LotSize to trade the calculated lots.
Make sure in your code you are not using any of the above variable names.1 user thanked author for this post.
08/25/2021 at 2:32 PM #17621208/25/2021 at 3:18 PM #176217YOU asked about the SL & TO and I replied
YOU asked me to provide the code for risk management and I posted the code.
Was tour account hacked and you never posted it?
08/27/2021 at 4:39 PM #176386Thank you for the codes, Roberto. It is useful for the rest of the PRT community. Have a great weekend ahead.
1 user thanked author for this post.
08/30/2021 at 11:28 PM #176506Hello, you will notice from my correspondence that am not an expert in the probuilder language, so am relying on the the lessons under this Prorealtime:
Money/Risk management
The terms “Money management” and “Risk management” usually refer to rules for:
- Managing capital
- Managing positions
- Managing risk
A well planned money management strategy should allow you to maximize your gains while limiting your risk. Depending on your money management technique, a given strategy may be winning or losing for a given set of historical data.
The questions below may give you some ideas about the type of money management you may want to use:
- Leverage: What is the maximum leverage you do not want to go over?
Leverage is calculated as follows: 1 / [available cash / position value]
To limit leverage, you can either limit position size or increase portfolio value.
- Short selling: Do you only want to take buying positions or do you also want to take short selling positions?
- Keep positions overnight or close them: do you want to never be in position (“flat”) before the close of the market each day to avoid the risk of an gap between the previous close and the next day’s open (also called an opening gap)?
- Investment timeframe: do you want to use an investment timeframe of minutes, hours, days, or more long term?
- Time restrictions: Do you want to put time restrictions on your system, for example to not open new positions after a certain time of day close to the market close?
- Targets: Do you want to use a target with a fixed value that will close your positions once a certain amount of gain has been reached or would you rather close your positions based on technical analysis conditions?
- Protect your positions: do you want to add protection stops based on a maximum loss amount per position or based on a price level on the chart?
- Types of stops: if you use stops, do you want to use fixed stops or trailing stops with levels that are constantly re-evaluated? Learn more about the different types of stops
- Max position size: what is the maximum position size which you do not want to go over?
Note that the maximum position size will be asked each time you start a trading system and will override the conditions in the code. - Maximum number of orders: what is the maximum amount of orders you want your system to execute per day?
(Ex: one round-turn per day or be more active).Note that the maximum number of orders can be set via the “Trading Options” menu.
can you please add stop loss and profit target calculations or setup in your codes to make it more complete.
Also the Probacktest engine already comes with Drawdown calculations displayed in the backtest results so why are you recalulating them in your codes making it look confusing?
Sorry if i may have shown some ignorance, but i really need education.
08/31/2021 at 12:14 AM #176508Topic moved from ProBuilder to ProOrder support.
09/01/2021 at 11:06 AM #176585DrawDown calculated by ProBackTest can’t be known from within a trading system, it’s reported on your screen so that you can take note of it only, so it needs to be calculated by the TS itself if you plan to manage position sizing and risk management on it.
As to SL and TP, well… that dipends on your likings, it can be fixed (number of pips) or dynamic (according to bars recently formed), or based on ATR (Average True Range) or other indicators.
TP, Target (or Take) Profit, is often calculated multiplying SL by a factor (1, 1.5, 2, etc…).Fixed SL (valid for both Long and Short trades):
12SET STOP PLOSS 50 //SL = fixed number of PipsSET TARGET PPROFIT 150 //TP = 3 times SLSL calculated on the lowest Low (for Long trades) and highest High (for Short trades) in the last N bars:
123456N = 5 //5-bar LookBack periodFactor = 2.5 //TP factor (multiplier)LongSL = lowest[N](low)ShortSL = highest[N](high)LongTP = LongSL * FactorShortTP = ShortSL * FactorSL calculated on the ATR (valid for both Long and Short trades):
123Atr = AverageTrueRange[14](Close)SET STOP LOSS AtrSET TARGET PROFIT Atr * 1.8As you can see, PROFIT and LOSS both require a difference (or range) expressed in price when SET. They can also be preceded by P if you prefer to set them using Pips, % if you prefer to use a percentage of the price or $ if you prefer to use a fixed amount of currency.
-
AuthorPosts
Find exclusive trading pro-tools on