Help Creating Bull Market Indicator with a Custom Indicator
Forums › ProRealTime English forum › ProBuilder support › Help Creating Bull Market Indicator with a Custom Indicator
- This topic has 12 replies, 4 voices, and was last updated 5 years ago by Bard.
-
-
03/27/2019 at 8:48 PM #94831
Hi
I was studying Bulkowki’s Candlestick Patterns: https://www.prorealcode.com/topic/8-high-probability-bulkowski-candlestick-patterns/
And wanted to make an indicator that would really define a Bull (and Bear) Market so that his percentage probabilities (eg there’s an 84% chance of a Bullish Reversal with a Bearish 3 Line Strike Up in a Bull Market) could be traded upon.
Bulkowski defined a Bull and Bear as:
I define a bear market as when an index or average (I prefer the S & P 500 Index) declines by more than 20%. Similarly, a bull market occurs when the index or average rises by at least 20%. Based on this definition, you will not know you are in a bull or bear market until well after they have begun. The definition does not matter because the words bull and bear are just labels. Following the current market or stock trend is more important than a label.
I added the moving averages condition (prices above the 50, 100 and 200 day moving averages) and this worked with a simple moving average but when I added the ALMA moving average I got a warning about the number of parameters called, pls see screenshot:
Not sure how to solve that – tried adding variables for 50, 100 and 200 etc but that didn’t fix it.
20% Bull Market123456789101112// 20% Bull Market increase in PricesShortMA = CALL "ALMA Moving Average"[50]MediumMA = CALL "ALMA Moving Average"[100]LongMA = CALL "ALMA Moving Average"[200]BullC1 = Close[0] > ShortMABullC2 = Close[0] > MediumMABullC3 = Close[0] > LongMABullC4 = Close[0] >= (Close[200] * 1.2)Return BullC1 and BullC2 and BullC3 and BullC4 as "BullMarket"Also happy to hear of any other ideas how to make a better Bull / Bear indicator?
Using simple moving averages it looked like this: Screens with 20% rule = images 1 & 2 and without (and just using moving averages), screen #3:
03/27/2019 at 10:11 PM #94844“Interestingly” the indicator acts at times as a good contrarian signal of the end of Bull and Bear markets too:
Bull/Bear 20%1234567891011121314151617181920// 20% Bull Market increase in PricesShortMA = Average[50]MediumMA = Average[100]LongMA = Average[200]BullC1 = Close[0] > ShortMABullC2 = Close[0] > MediumMABullC3 = Close[0] > LongMABullC4 = Close[0] >= (Close[200] * 1.2)// 20% Bear Market decrease in PricesBearC5 = Close[0] < ShortMABearC6 = Close[0] < MediumMABearC7 = Close[0] < LongMABearC8 = Close[0] <= (Close[200] * 1.2)Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", BearC5 and BearC6 and BearC7 and BearC8 as "Bear Market"Does anyone know how to make it have a “zero line axis” so it’s green on the top half and red on the bottom half of the indicator panel?
Cheers
03/28/2019 at 12:46 AM #94851Add “,0” to line 20:
1Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", BearC5 and BearC6 and BearC7 and BearC8 as "Bear Market",0Though this is not enough, by itself, to plot above/below the zero line. Values above/below 0 must be returned, I think the same line 20 should be replaced by (not tested) so that it returns either 1 or -1:
1Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", -(BearC5 and BearC6 and BearC7 and BearC8) as "Bear Market",01 user thanked author for this post.
03/28/2019 at 9:26 AM #9485903/28/2019 at 1:50 PM #94888Because your “ALMA Moving Average” indicator doesn’t have any external setting, such as the period. I’m sure the settings are embedded directly in the code and not as external variables. Thought you were now perfectly trained for that newbie error Bard! (just joking) 😆
03/28/2019 at 4:56 PM #94913Thanks for sorting that out, thing is I’m not sure how to program it to buy when the Bear conditions end?
For example after those (short period) red spikes have ended and the indicator has returned to neutral (zero), I would like the system to take a long trade at that point? (Pls see last screenshot). Because there are no “crossovers” I can’t make it do that. I’d also probably program the Buy rules to define a Bear spike as any Bear condition that lasts for less than 5 or maybe 10 days, (depending on backtests), it (the pull backs) appears – at least on observation – to be a great place to enter Long. Similar in concept to what Nicolas coded for me here: https://www.prorealcode.com/topic/how-would-you-create-a-mov-ave-distribution-histogram/#post-92863
Cheers
03/28/2019 at 5:59 PM #94914Tbh, I didn’t spend too much time looking “under the hood” of the ALMA! And I certainly didn’t see “Period” because: “Fenetre…” sigh… 😄
If I add “Fenetre” as a variable and strike it out in the original ALMA indicator code and my indicator “works,” well, it loaded okay even without adding //Fenetre in my code below (although I have just added it)… but is my code now referring to the new 50, 100, & 200 periods of this indicator code? I think it is.
Bull/Bear 20% w. ALMA123456789101112131415161718192021222324252627// 20% Bull Market increase in PricesDEFPARAM CalculateOnLastBars = 2000//FenetreShortPeriod = 50MediumPeriod = 100LongPeriod = 200ShortMA = CALL "ALMA Moving Average"[ShortPeriod]MediumMA = CALL "ALMA Moving Average"[MediumPeriod]LongMA = CALL "ALMA Moving Average"[LongPeriod]BullC1 = Close[0] > ShortMABullC2 = Close[0] > MediumMABullC3 = Close[0] > LongMABullC4 = Close[0] >= (Close[200] * 1.2)// 20% Bear Market decrease in PricesBearC5 = Close[0] < ShortMABearC6 = Close[0] < MediumMABearC7 = Close[0] < LongMABearC8 = Close[0] <= (Close[200] * 1.2)Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", -(BearC5 and BearC6 and BearC7 and BearC8) as "Bear Market",0Also it doesn’t like “CALL” as it keeps re-calculating on my 15000 unit chart, even though I added DEFPARAM CalculateOnLastBars = 2000?
Cheers
03/28/2019 at 10:54 PM #94933I always embed indicators in my code, to avoid CALLing them, whenever possible. You may try, despite you have to embed it three times or use a FOR…NEXT loop to calulate all of them (just in this case, since each MA is twice the previous one):
12345678910111213141516171819202122232425262728Period = 50 //start with 50 periodsSeries = customcloseFOR j = 1 TO 3Sigma = 6Offset = 0.85m = ROUND(Offset * (Period - 1))s = Period / SigmaWtdSum = 0CumWt = 0FOR k = 0 TO Period - 1 DOWtd = EXP(-((k - m) * (k - m)) / (2 * s * s))WtdSum = WtdSum + Wtd * Series[Period - 1 - k]CumWt = CumWt + WtdNEXTIF CumWt <= 0 THENAFR = SeriesELSEAFR = WtdSum / CumWtENDIFIF j = 1 THENShortMA = AFRELSIF j = 2 THENMediumMA = AFRELSELongMA = AFRENDIFPeriod = Period * 2 //double periodsNEXT1 user thanked author for this post.
03/28/2019 at 11:01 PM #94934Your full code would be (not tested):
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849//Bear 20% w. ALMA// 20% Bull Market increase in PricesDEFPARAM CalculateOnLastBars = 2000//FenetrePeriod = 50 //start with 50 periodsSeries = customcloseFOR j = 1 TO 3Sigma = 6Offset = 0.85m = ROUND(Offset * (Period - 1))s = Period / SigmaWtdSum = 0CumWt = 0FOR k = 0 TO Period - 1 DOWtd = EXP(-((k - m) * (k - m)) / (2 * s * s))WtdSum = WtdSum + Wtd * Series[Period - 1 - k]CumWt = CumWt + WtdNEXTIF CumWt <= 0 THENAFR = SeriesELSEAFR = WtdSum / CumWtENDIFIF j = 1 THENShortMA = AFRELSIF j = 2 THENMediumMA = AFRELSELongMA = AFRENDIFPeriod = Period * 2 //double periodsNEXTBullC1 = Close[0] > ShortMABullC2 = Close[0] > MediumMABullC3 = Close[0] > LongMABullC4 = Close[0] >= (Close[200] * 1.2)// 20% Bear Market decrease in PricesBearC5 = Close[0] < ShortMABearC6 = Close[0] < MediumMABearC7 = Close[0] < LongMABearC8 = Close[0] <= (Close[200] * 1.2)Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", -(BearC5 and BearC6 and BearC7 and BearC8) as "Bear Market",003/28/2019 at 11:22 PM #94935Ps// To stop the indicator from constantly reloading I also tried to speed up the code with:
Speed CALL fx up123456789MyALMA = CALL "ALMA Moving Average"ShortPeriod = 50MediumPeriod = 100LongPeriod = 200ShortMA = CALL "MyALMA"[ShortPeriod]MediumMA = CALL "MyALMA"[MediumPeriod]LongMA = CALL "MyALMA"[LongPeriod]but it says the variable myalma is not used in the code? I borrowed an example from the PRT manual: myindic = CALL “My Function” from p24 of : https://www.prorealtime.com/nl/pdf/probacktest_c1504281788c.pdf
So I added the ALMA to the Bull/Bear indicator:
Bull/Bear w. ALMA Embedded12345678910111213141516171819202122232425262728293031323334353637383940414243444546// 20% Bull/Bear Market increase/decrease in Prices//DEFPARAM CalculateOnLastBars = 3000// Parameters// Fenetre = 9Sigma = 6Offset = 0.85Price = Closem = (Offset * (Fenetre - 1))s = Fenetre/SigmaWtdSum = 0CumWt = 0for k = 0 to Fenetre - 1 doWtd = Exp(-((k-m)*(k-m))/(2*s*s))WtdSum = WtdSum + Wtd * Price[Fenetre - 1 - k]CumWt = CumWt + WtdnextALAverage = WtdSum / CumWtShortPeriod = 50MediumPeriod = 100LongPeriod = 200ShortMA = ALAverage[ShortPeriod]MediumMA = ALAverage[MediumPeriod]LongMA = ALAverage[LongPeriod]BullC1 = Close[0] > ShortMABullC2 = Close[0] > MediumMABullC3 = Close[0] > LongMABullC4 = Close[0] >= (Close[200] * 1.2)// 20% Bear Market decrease in PricesBearC5 = Close[0] < ShortMABearC6 = Close[0] < MediumMABearC7 = Close[0] < LongMABearC8 = Close[0] <= (Close[200] * 1.2)Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", -(BearC5 and BearC6 and BearC7 and BearC8) as "Bear Market",0I’m not sure why there are more Bull and Bear Spikes when the ALMA was being CALLED compared to embedded? Adding the ALMA into the Bull/Bear indicator produces a different set of Bear Bull Spikes compared to the original code where ALMA is CALLED in the code (the one I posted directly above in post #94914)?
Pls see two screenshots below — one with the ALMA average not embedded and being CALLED and the other with the ALMA code imbedded directly IN the indicator to avoid the CALL issue. The issue for me is how to get it to work using 3 different moving averages and whether I’ve achieved that or not.
Cheers
Veteran “Newbie” 😀03/29/2019 at 12:38 AM #94939In line 1 you assign a value to MyAlma, you cannot later CALL it, since it is not an indicator and ProBuilder reports (correctly) that you are not using that variable. You should remove line 1 and replace “MyAlma” with “ALMA Moving Average” throughout the code when CALLing it.
The way you embedded and use the code for the ALMA Average is wrong.
You need to embed it three times (once for each average) using different names for variables, or use a FOR..NEXT loop. There’s no workaround.
Lines 30-33 are absolutely wrong. Line 30 will assign ShortMA the value ALAaverage retained 50 periods ago! That has nothing to do to with computing the average of the last 50 periods!
Moreover, you are ALWAYS using FENETRE as the number of periods, instead of the three different periods you would like to use.
My code (https://www.prorealcode.com/topic/help-creating-bull-market-indicator-with-a-custom-indicator/#post-94934) will work the same as your code at https://www.prorealcode.com/topic/help-creating-bull-market-indicator-with-a-custom-indicator/#post-94914.
1 user thanked author for this post.
03/29/2019 at 5:56 PM #95047Thanks very much @RobertoGozzi and @Nicolas, works very well. Better than Standard Deviation variances and Moving Average Distributions entries.
It acts as a good contrarian indicator if you use those short term (couple of days) Bear Spikes as long entries: Dow Jones
Pls see images (this indicator is the bottom of the two Bull/Bear indicators being used, the top one is just a regular 50, 100, 200 SMA).Dow Jones Daily/3.8 Spread/Random Dates: 26th Aug 2010 – 12 Oct 2017.
Dev Stop 6.0 worked “best.” Everyones risk/drowdown profile is different.Bull/Bear ALMA + Dev Stop12345678910111213141516171819202122232425// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedcapital = 100000 + strategyprofit //Current profit made by the closed trades of the running strategy.n = capital / close// Conditions to enter long positionsignored, indicator1, ignored = CALL "Bull/Bear 20% ALMA"(close)c1 = (indicator1 = -1)ignored, indicator2, ignored = CALL "Bull/Bear 20% ALMA"(close)c2 = (indicator2[1] = -1)ignored, indicator3, ignored = CALL "Bull/Bear 20% ALMA"(close)c3 = (indicator3[2] = -1)IF c1 AND c2 AND c3 THENBUY n PERPOINT AT MARKETENDIF// Conditions to exit long positionsignored, ignored, ignored, ignored, ignored, DEV = CALL "Kase Dev Stop Lisse+SAR+4.5/6"c4 = (close CROSSES UNDER DEV)IF c4 THENSELL AT MARKETENDIFKase Dev Stop Lisse+SAR+4.5/6123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113//Voici le code en version SAR : plus WITH DEV STOP 4.5 + 6.0//Settingsn=30p1=1.0p2=2.2p3=3.6p4=4.5p5=6.0difference=0Hg=highest[2](high)Lw=lowest[2](low)DTR=max(max(Hg-Lw,abs(Hg-close[2])),abs(Lw-close[2]))aDTR=average[n](DTR)for i=0 to n-1 dodifference=difference+square(DTR[i]-aDTR)nextdifference=difference/nsdev=sqrt(difference)dev0=close-aDTRdev1=close-aDTR-p1*sdevdev2=close-aDTR-p2*sdevdev3=close-aDTR-p3*sdevdev4=close-aDTR-p4*sdevdev5=close-aDTR-p5*sdevif dev0<dev0[1] and close>dev5[1] thendev0=dev0[1]endifif dev1<dev1[1] and close>dev5[1] thendev1=dev1[1]endifif dev2<dev2[1] and close>dev5[1] thendev2=dev2[1]endifif dev3<dev3[1] and close>dev5[1] thendev3=dev3[1]endifif dev4<dev4[1] and close>dev5[1] thendev4=dev4[1]endifif dev5<dev5[1] and close>dev5[1] thendev5=dev5[1]endifdev6=close+aDTRdev7=close+aDTR+p1*sdevdev8=close+aDTR+p2*sdevdev9=close+aDTR+p3*sdevdev10=close+aDTR+p4*sdevdev11=close+aDTR+p5*sdevif dev6>dev6[1] and close<dev11[1] thendev6=dev6[1]endifif dev7>dev7[1] and close<dev11[1] thendev7=dev7[1]endifif dev8>dev8[1] and close<dev11[1] thendev8=dev8[1]endifif dev9>dev9[1] and close<dev11[1] thendev9=dev9[1]endifif dev10>dev10[1] and close<dev11[1] thendev10=dev10[1]endifif dev11>dev11[1] and close<dev11[1] thendev11=dev11[1]endifif close>dev11[1] thenflag=-1elseif close<dev5[1] thenflag=1endifendifif flag=-1 thenind0=dev0ind1=dev1ind2=dev2ind3=dev3ind4=dev4ind5=dev5//k=1 Bluer=0g=191b=255elseind0=dev6ind1=dev7ind2=dev8ind3=dev9ind4=dev10ind5=dev11//k=-1 Oranger=255g=128b=0endif//ORIG return ind0 COLOURED BY k,ind1 coloured by k,ind2 coloured by k,ind3 coloured by k////ORANGE AND LIGHT BLUEreturn ind0 coloured(r,g,b) style(dottedline,2) as "Warning Line", ind1 coloured(r,g,b) style(dottedline,2) as "Dev Stop 1.0", ind2 coloured(r,g,b) style(dottedline,2) as "Dev Stop 2.2", ind3 coloured(r,g,b) style(line,2) as "Dev Stop 3.6", ind4 coloured(r,g,b) style(dottedline,2) as "Dev Stop 4.5", ind5 coloured(r,g,b) style(line,2) as "Dev Stop 6.0"//NO CHANGE OF COLOUR FOR TREND CHANGE return ind0 coloured(2, 118, 253) style(dottedline,2) as "Warning Line" ,ind1 coloured(2, 118, 253) style(dottedline,2) as "Dev Stop 1.0", ind2 coloured(2, 118, 253) style(dottedline,2) as "Dev Stop 2.2", ind3 coloured(2, 118, 253) style(line,2) as "Dev Stop 3.6", ind4 coloured(2, 118, 253) style(dottedline,2) as "Dev Stop 4.5", ind5 coloured(2, 118, 253) style(line,2) as "Dev Stop 6.0"Bull/Bear 20% ALMA1234567891011121314151617181920212223242526272829303132333435363738394041424344454647//Bear 20% w. ALMA// 20% Bull Market increase in Prices//DEFPARAM CalculateOnLastBars = 5000Period = 50 //start with 50 periodsSeries = customcloseFOR j = 1 TO 3Sigma = 6Offset = 0.85m = ROUND(Offset * (Period - 1))s = Period / SigmaWtdSum = 0CumWt = 0FOR k = 0 TO Period - 1 DOWtd = EXP(-((k - m) * (k - m)) / (2 * s * s))WtdSum = WtdSum + Wtd * Series[Period - 1 - k]CumWt = CumWt + WtdNEXTIF CumWt <= 0 THENAFR = SeriesELSEAFR = WtdSum / CumWtENDIFIF j = 1 THENShortMA = AFRELSIF j = 2 THENMediumMA = AFRELSELongMA = AFRENDIFPeriod = Period * 2 //double periodsNEXTBullC1 = Close[0] > ShortMABullC2 = Close[0] > MediumMABullC3 = Close[0] > LongMABullC4 = Close[0] >= (Close[200] * 1.2)// 20% Bear Market decrease in PricesBearC5 = Close[0] < ShortMABearC6 = Close[0] < MediumMABearC7 = Close[0] < LongMABearC8 = Close[0] <= (Close[200] * 1.2)Return BullC1 and BullC2 and BullC3 and BullC4 as "Bull Market", -(BearC5 and BearC6 and BearC7 and BearC8) as "Bear Market",0I was actually trying to enter Long AFTER a 3 day Bear Spike Pullback ENDS. The code above is a random accident in an attempt to code it as just described.
Instinctively that’s where I feel the drawdowns can be reduced more through better entry timing. Hooking onto the price after it has plummeted and is already safe and returning back north not heading for possibly more falls as per the system as it currently stands.
So now look at the Drawdown with a wild Dev Stop 6.0 and how it maintains the profits when using this entry:
Improved Timing of Long Entry as Bear Ends1234567// Conditions to enter long positionsignored, indicator1, ignored = CALL "Bull/Bear 20% ALMA"(close)c1 = (indicator1 = 0) // Bear = -1ignored, indicator2, ignored = CALL "Bull/Bear 20% ALMA"(close)c2 = (indicator2[1] = -1)ignored, indicator3, ignored = CALL "Bull/Bear 20% ALMA"(close)c3 = (indicator3[2] = -1)Cheers
Bard03/29/2019 at 6:00 PM #95053Logged out and back in and there’s definitely a few images didn’t post, 2nd attempt..
Caveat: Wide Dev Stops like 6.0 are always late in catching quick adverse market turns. There is no one size fits all algo system. Different dates work better than others (even with the “genius” of John Ehler’s indcators), different setting work better than others. This example above is just illustrative of the search for better entries.. and it’s original intent: To define a Bull or Bear market for the purposes of Bulkowski’s Candlestick patterns.
-
AuthorPosts
Find exclusive trading pro-tools on