Indicator projecting past candles into the future
Forums › ProRealTime English forum › ProBuilder support › Indicator projecting past candles into the future
- This topic has 9 replies, 3 voices, and was last updated 2 months ago by
Florian878.
-
-
06/04/2024 at 8:38 AM #233475
Hi there @ProRealCode!
(I’m new to this forum and this is my first post, so hopefully I’ve choosen the correct place to post it. If not, please feel free to move it.)
I’m about to create an indicator that will project past candle patterns (based on a defined search pattern – which I’ll come back to later) into the future – after the current candle so to speak.
As ProBuilder is giving me a hard time doing so, I’d like to place some questions first.
So my initial two questions would be:
1. Is it possible to draw a variable amount of candles (i.e. 7-20) ‘after’ the current candle?2. If yes, how could I do it?
My challenges:
Saving the open, high, low, close within defined arrays. Honestly I wasn ‘t able to debug my arrays and see if this works in the first place.
The result is always the same: I receive a single candle. It’s always the candle with the highest index of that array. But I’m missing the previous saved candles. So if my number of candles would be 7, I always receive candle no. 7 but not 1-6.
Furthermore the candle is always printed at the same spot of the current candle and not the spot/s following after.My code so far:
project candles into 'future'123456789101112131415161718192021222324252627282930defparam drawonlastbaronly = truedefparam calculateonlastbars = 200// define number of candles to save to the arrays; can be adjusted at the input tab of the indicator but will be set to 7 for nowperiod = 7// Reset Arraysif not i <= period thenlastBarIndex = 0unset($Open)unset($High)unset($Low)unset($Close)endif// Save candles to arraysfor i = 0 to period - 1$Open[i] = open[i + 1]$High[i] = high[i + 1]$Low[i] = low[i + 1]$Close[i] = close[i + 1]next// Draw x candles after current barfor i = 0 to period - 1drawcandle($Open[i], $High[i], $Low[i], $Close[i]) coloured(0,0,230)nextreturnAttached you’ll also find two screenshots, one to show how it looks like and one how it is supposed to look like if working correct.
Any help, solution,… is highly appreciated.
Thank you in advance.
Best regards,
Florian06/04/2024 at 9:22 AM #233479Hi,
the issue is because the drawcandle command is only on same barindex the code is read.
Instead, you could go for now with the following workaround: using the drawsegment command, once with a bigger thickness for candle body, and twice with smaller thickness for both candle wicks (or once from low to high if same color to avoid potential overlap effects), and placing those onan x coordinate being bardindex+N, N being the number of candles in the future of the current one.
https://www.prorealcode.com/documentation/drawsegment/
adding the option at the end style(line,thickness), thickness being an integer between 1 and 5 (option not in the doc page above as it appeared some time later), line could also be dottedline and its variants, but I assume you’ll want a line.
DRAWSEGMENT(x1,y1,x2,y2) COLOURED(R,V,B,a) style(line,thickness)
For later, a request I placed some time ago could become handy for a future slightly more inetresting workaround… It is in “the right hands”, and it “might” be granted (I prefer to wait until informed of next updates before considering it’s coming for sure), and it would result in being able to access x coordinate “between” 2 barindex for a graphic object moving if scrolling or zooming. If that happens, then the better workaround would become using a drawrectangle for body and a drawsegment for wicks.
There could also be more directly: placing a request to be able to drawcandle anywhere, asking for a 5th optional parameter for position.
But for now, I’d pragmatically stick with 1st workaround above, to be able now to draw your patterns in the future, not just for an end result, but first to really visualise what your code is doing to debug it more easily .
3 users thanked author for this post.
06/04/2024 at 10:11 AM #233484Hi JC,
thank you for your fast response.
To make sure I understand it correct, my assumptions would be:
currently it is not possible due to the fact that the function ‘drawcandle” will not move through the different indices with no option of shifting it incrementally by 1. Correct?
I furthermore assume, my arrays are each correctly ‘filled’ with the last 7 open’s, high’s, low’s and close’s, correct?
So before taking action on the workaround you supposed I would have one more essential question (of course to all readers around):
is it possible to search the historic data for a similar pattern as stored within the arrays?
Thanks again,
Florian06/04/2024 at 7:41 PM #2334951234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162defparam drawonlastbaronly = truecandleNumber = 20 // Number of forward candles 2+// make sure candleNumber not greater than barindexonce candleNumber = max(barindex+1,candleNumber)once period = candleNumber -1 // array index range, 0 to period// load arraysif candleNumber > 1 thenfor i = 0 to period // loop to load array's$open[i] = open[i]$high[i] = high[i]$low[i] = low[i]$close[i] = close[i]next// draw candlesif islastbarupdate thenfor i = 0 to period // loop to draw future candlesos = i+1 // offset for, start from current bar + 1x1 = barindex + osx2 = x1idx = period-i // reverse i to idx, 0-6 to 6-0y1 = $open[idx]y2 = $close[idx]bear = y1= max(y1,y2) // define a bear candle type// candle bodyif bear then // beardrawrectangle(x1,y1 ,x2,y2 )style(line,5)coloured("red")else // bulldrawrectangle(x1,y1 ,x2,y2 )style(line,5)coloured("lime")endify1 = $high[idx]y2 = $low[idx]// candle wicksif bear then // beardrawsegment(x1,y1,x2,y2)style(line,1)coloured("red")else // bulldrawsegment(x1,y1,x2,y2)style(line,1)coloured("lime")endifnextendifendifreturnHi,
Based on you code and JC’s comments try this.
2 users thanked author for this post.
06/05/2024 at 10:26 PM #233570Hi druby,
this is awesome!
If I would like to search for that pattern in historic data (i. e. the past 1,000 candles), the following questions arise:
1. Is there maybe any kind of ‘pattern’-detector already included in ProBuilder?
2. Does ProBuilder support any kind of function to accomplish my task or would I have to hard-code it into the indicator?
Again, any helpful information and support is highly appreciated 🙂
Thanks all.
regards,
Florian06/08/2024 at 3:40 PM #233642Hi…
Compiled two long reply and for some reason lost both.
Code below is a starting point for pattern recognition I’ve been working on. 6 bar pattern bull, bull, bear, bear, bull, bull
You need to break down specifically what variables of the pattern are required to define it.
Along with the reasoning of the process of what your trying to do.
Logic takes no prisoners! , but can take you on a long journey and dump you somewhere!
druby
pattern main V1.05123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566defparam drawonlastbaronly = trueif barindex = 0 then // set constantsBULL = 1BEAR = -1endif// candle typeif open = max(open,close) thentype = BEARelsetype = BULLendif// load pattern (4,3,2,1)$type[1] = BULL // first LHS$type[2] = BULL$type[3] = BEAR$type[4] = BEAR //$type[5] = BULL //$type[6] = BULL // last RHS// variablesLB = lastset($type) // which bar looking atpat = 0 // pattern countstart = 0 // start barif islastbarupdate thenbar = startwhile bar < barindex//-LBx1 = barindex-bar // x1 offsetpat = pat + 1 // pattern countif type[x1] = $type[pat] then // if pattern matchif type[x1] = BEAR thendrawtext(pat,barindex[x1],30*pat)anchor(bottom,index,yshift) coloured("red")elsif type[x1] = BULL thendrawtext(pat,barindex[x1],30*pat)anchor(bottom,index,yshift) coloured("lime")endifif pat = LB then // set if end of pattern reachedreset = 1endifelse // set if no match of patternreset = 1endifif reset thenbar = bar - (pat-1) // jump back 'n' bars to bar after last pattern startpat = 0reset = 0 // reset from complete or no complete patternendifbar=bar+1 // increment while loop bar countwendendifreturn1 user thanked author for this post.
06/17/2024 at 9:22 AM #233990Hey druby,
first of all: thank you again and you’re so right. I got completely lost with your last suggestion. Not understanding, what exactly the outcome will be.
Maybe I did not explain what I really want to do as accurate as it might be needed.
So here we go:
1. I want to define a range between 3-7 (max. 10) candles. I will draw a rectangle around those candles (only for visualization purposes).
2. I’ll save the data (3-7 candles in total) from step 1 into an array like: write a ‘1’ if the candle is green (open < close); write a ‘0’ (=zero) if the candle is red (open > close); write a ‘2’ if the candle is a ‘doji’ (open = close).
3. Save the price data of historic candles with the same codification (as above), i.e. the last 5.000 (will later be adjustable by user) candles into a second array.
4. Compare the sample (step 1) with the historic data (step 3); if the pattern is found, then
5. draw a second rectangle around the pattern within the historic data. And save the next 100 (also adjustable) candles following the found pattern into a third array.
6. Project those candles into the future (after the current candle) so to speak.5a. if the pattern is not found, simply write a notification saying that into the chart. <- already working.
I do have accomplished the steps 1 through 3.
step 4 is not working yet. As I get confused with the way, PRC counts the candles.
step 5 shouldn’t be a huge task to solve – similar to step 1.
step 6 is already solved with your first response.Questions I still have:
1. How does ProRealCode (ProBuilder) count the candles? I’m pretty sure, the current candle is counted as ‘0’ (=zero). But what about the prior candles? Will they be assigned any numbers from 1 to xyz counting upwards with each new candle forming?
2. Why do I have to flip (reverse) the variable of the variable i (code line #32: idx = period–i // reverse i to idx, 0-6 to 6-0)? Would I have to flip the indeces of the ‘historic array’ as well before any comparison with another array?I can provide my code if this would be helpful – as it has some lines (above 200) of now I would suggest to export it into a separate file instead of copy-pasting it. Any suggestions from the experts? 🙂
Thanks again,
Florian06/17/2024 at 12:57 PM #234005Hi Florian, re: your questions…
- For a new bar to be valid, it has to have, at least one tick in the bars time period. A current count of the bars is stored in the variable ‘BARINDEX’. The first bar of a chart is barindex = 0, and is incremented by 1 every valid bar, and where the current candle is the highest value.
Just to explain why I use the word ‘Valid’ for a bar, if their is a bar time period, where there was no tick, then the barindex’s, current number, is not incremented. This could lead to the count of barindex, being different to the count of the time periods.
You are right by saying the current candles index is [0] and, the previous one to that would be [1], and then [2], [3] etc. Now this is where it gets tricky. The current candle is always [0], so you can not directly look back to a specific candle as an index since it points to a different bar every new bar.
If you compare the, barindex and the candle index, the barindex is increasing every bar and the candle index is also increasing but from the opposite end. This, because the candle index shifts to the left by 1 bar on each new valid bar
So the index is actually an offset from the current bar to a previous bar, where the current barindex is the reference point.
Looking back to close[10] give the value of close 10 bar prior to the current barindex. After a new bar , then it still points to 10 bars ago but the reference point as change because barindex has been incremented by 1.
Therefore to access the same candle, you need the barindex number, or the offset in bars since, for that candle, let say it was at barindex 300, subtract that from the current barindex, lets say 550, 550-300 = 150, then the candle offset from current bar = 150, close[150] or close[barindex-300].
Now I think the draw command use the reference point from the other end, so you need the offset from barindex = 0, to the bar you want, which I end up with barindex[barindex- 300].
2. lines
#4 candleNumber sets the number of candle required
#8 period is set to 1 less than candleNumber, because array starts at index[0] , this aligns to give correct number required.
#15-20 loads array ‘backward’ from current bar.
# 26 creates a loop for the drawing of the candles in the future with the store array data
# 28,29,30 ensures as ‘i’ increases the offset increase points to the next future bar, the +1 just set the start point to the first future bar when i=0.
# 32 remember the data was stored backwards from the current bar in the array’s, but the future bars are being index forward, so the array needs accessing from last to first, which period – i does.
if i=0, then 0+1 points to first future bar, barindex+1, if period = 9 {0 to 9} then 9-0 = 9 for array index. Next time, when i=1, barindex + 2, 9-1 = 8, etc
Regarding the rest i’ll look into it, posting code would be helpful, at least we would be on same page talking about same thing.
I’m sure I have several questions, but here’s the first,
the first code I posted, made a patten using a number of bars, this pattern is going to change every new bar, is this what initiates the pattern or is it some other criteria.
1 user thanked author for this post.
06/17/2024 at 6:15 PM #234012Q. what timeframe(s) are you planning on using?
Looking through your specification:-
- Range, between 3-7 max 10, that’s ok, but where does it start! Is it the last ‘closed’ candle, or the current. If current, then that candle will be always changing till it closes, changing the pattern till then, when it becomes the previous candle
- If bull/bear/doji value is assigned to variable “candle” on each bar, then these values are available using candle[n], just need to know the barindex of the start point, when searching. eg. barindex – 5000
- As with 2. , no need to store historical candle’s price data since it’s already stored, just need to know the barindex or offset to the candle(s) required, then use close[n] etc.
- The code, should show a rectangle around the pattern, kind of, and at the bottom, the bull/bear/doji values of the pattern range and for all bars in the chart. Just need to compare using pattern code above.
Q. Is the pattern only matching the bull/bear/doji values 1,0,2 and the position sequence.
Q. Its possible that, there might be more than 1 match of the pattern, which one is going to be the relevant one for projecting forward.
5. A rectangle around found pattern, no problem, Again, no need to store 100 candle data, just need to know what barindex it starts, eg. current barindex minus offset to start of pattern + pattern range + 1, retreve data using ohlc with [n].
5a. No pattern label, no problem
6. Project 100 candles forward like in code above.
once it’s up and running, the variable you want to adjust can be setup using the dynamic variable settings, may need to add limits to enter values and make sure they don’t break code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748defparam drawonlastbaronly = true// general variablesonce bear = 0once bull = 1once doji = 2// range in bars of chosen patterniRange = 3 //requirement integer, 3 - 7 max 10, default = 3// determine candle typeif close < open thencandle = bear // bearelsif close > open thencandle = bull // bullelsecandle = doji // doji , open=closeendif// highest and lowest of iRangehi = highest[iRange](high)lo = lowest[iRange](low)// drawingif islastbarupdate then// draw rectangle around iRange candlesx1 = iRangey1 = hix2 = 0y2 = lodrawrectangle(barindex[x1],y1,barindex[x2],y2)coloured("red",100)bordercolor(0,0,0,0)// display all types for historical data in chartfor i = 0 to barindexx1 = barindex - ivar = candle[x1]drawtext(var,barindex[x1],20)anchor(bottom,index,yshift)nextfor i = 0 to iRange - 1x1 = ivar = candle[i]drawtext(var,barindex[x1],40)anchor(bottom,index,yshift)nextendifreturn close1 user thanked author for this post.
06/23/2024 at 2:40 PM #234240Hi Druby,
first of all my apollogies for being offline the past week
Secondly I want to thank you for all your support given so far. It was of great help and I hope that I can give it back one day. Currently I’m still learning a lot of how the ProBuilder works and how it doesn’t.
However: with your great support and help I pretty much solved my initial challenges. But more are already on the horizon… So I’d kindly ask the admins of this forum to leave this post open for now.What you (and of course all other users of this forum) will find is the code of my indicator (defining it to be in some sort of alpha-status). It’s still under development and not 100% finished. Attached you’ll also find the export-file to use with your own ProRealTime Trading Plattform.
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Important Note:
The use of this indicator is completely at YOUR OWN risk and does NOT recommend any trading ideas or decisions based on real money. I shall not be hold liable to any losses made due to using this indicator (Forecast_PRC_p-type).
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~Attached you’ll also find a screenshot of the current state of my indicator. It shows the search pattern (rectangle with blue dashed line), the found pattern (rectangle thick green dashed line) and the result ‘vector’ (rectangle slim blue line). It also shows a potential output for the future price based on historic price information.
So then – the code:
I tried to structure it the best way possible and add comments to show what it does.forecast indicator123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212// Draws a rectangle across the past x candles - defined by input of user// Parameters, to draw the rectangle only once around the defined search patterndefparam drawonlastbaronly = true//defparam calculateonlastbars = 200 // temporarily void to gain more candles for pattern searchingdefparam calculateonlastbars = 5000// define the number of candles (period) for search pattern// period = 7 // void, due to available user input// define the number of forecast candles to be printed (historic data)// forecast = 100 // void, due to available user inputif islastbarupdate then // to reduce the loading times of an indicator; especially if you would need to pre-load data// initialize variablesif not hh or not ll or not lastBarIndex thenhh = 0ll = 0lastBarIndex = 0endif// Calculation of highest high and lowest low for search pattern (without current candle) - to visualize the search pattern with a rectangleif barindex <> lastBarIndex thenhh = high[1]ll = low[1]for i = 1 to period dohh = max(hh, high[i])ll = min(ll, low[i])nextlastBarIndex = barindexendif// Define first and last candle for 'pattern'-rectanglestartbar = barindex[period+1]endbar = barindex[1]// draw rectangledrawrectangle(startbar, ll, endbar, hh) style(dottedline3, 1) coloured(0,0,255)// RESET arrays// definition of array to temporarily save all pricevalues of defined search pattern (period)unset($Open)unset($High)unset($Low)unset($Close)// definition of array to temporarily save all pricevalues of historic values (period) - for comparisonunset($Colorcode)unset($histOpen)unset($histHigh)unset($histLow)unset($histClose)unset($histCC)unset($forecastOpen)unset($forecastHigh)unset($forecastLow)unset($forecastClose)// Save price values for search pattern as new Arrays "Open, High, Low, Close"for i = 0 to period do$Open[i] = open[i]$High[i] = high[i]$Low[i] = low[i]$Close[i] = close[i]next// save price values of historic pices as new Arrays "histOpen, histHigh, histLow, histClose" (for comparison)for j = 0 to BarIndex do$histOpen[j] = open[j]$histHigh[j] = high[j]$histLow[j] = low[j]$histClose[j] = close[j]next// DEFINE color coding for search pattern and historic data and save as new arrays each.// define sequence of color coding for search pattern (period) and save as new Array "Colorcde"for i = 0 to period doif open[i] < close[i] then$Colorcode[i] = 1endifif open[i] > close[i] then$Colorcode[i] = 0endifif open[i] = close[i] then$Colorcode[i] = 2endifnext// define sequence of color coding for historic values (forecast) and save as new Array "histCC"for j = 0 to BarIndex doif open[j] < close[j] then$histCC[j] = 1endifif open[j] > close[j] then$histCC[j] = 0endifif open[j] = close[j] then$histCC[j] = 2endifnext// SEARCH for pattern// Definition of additional variableshistoric = BarIndex// starting loop at a predefined value of jstartIndex = period + forecastfor j = startIndex to historic - period domatch = 1 // set match to 1 at start of each j-loop// Check the search patternfor i = 0 to period - 1 doif $Colorcode[i] <> $histCC[j + i] thenmatch = 0breakendifnextif match = 1 then// Define hhPattern and llPattern based on candles found in hisoric datahhPattern = $histHigh[j]llPattern = $histLow[j]// Find highest high and lowest low within patternfor i = 0 to period - 1 dohhPattern = max(hhPattern, $histHigh[j + i])llPattern = min(llPattern, $histLow[j + i])next// Define start- and endbars of patternpatternStartBar = BarIndex[j]-1patternEndBar = BarIndex[j + period]-1// draw a rectangle around found pattern (green dashed line)drawrectangle(patternStartBar, llPattern, patternEndBar, hhPattern) style(dottedline3, 3) coloured("green")// Save the historic candles follwoing after the patternfor k = 0 to forecast - 1 do$forecastOpen[k] = open[j - k]$forecastHigh[k] = high[j - k]$forecastLow[k] = low[j - k]$forecastClose[k] = close[j - k]next// Draw a rectangle around forecast candles regarding highest high and lowest low (slim blue line)hhForecast = $forecastHigh[0]llForecast = $forecastLow[0]for k = 0 to forecast - 1 dohhForecast = max(hhForecast, $forecastHigh[k])llForecast = min(llForecast, $forecastLow[k])nextforecastStartBar = BarIndex[j]-1forecastEndBar = BarIndex[j - forecast + 1]-1drawrectangle(forecastStartBar, llForecast, forecastEndBar, hhForecast) style(line, 1) coloured(0,0,255)breakendifnext// Calculate the delta between the last close (or open) and the corresponding close (or open) of historic data//priceShift = close[0] - $forecastClose[0]priceShift = open[0] - $forecastOpen[0]for k = 0 to forecast - 1 // Loop to draw the last 10 candles// Offset for, start from current bar + 1os = k + 1// Adjusted x1 and x2 to use the correct offsetx1 = barindex + osx2 = x1// Reverse j to idx, 0-9 to 9-0; 9 = last indexidx = k// Retrieve open and close values for the current indexy1 = $forecastOpen[idx] + priceShifty2 = $forecastClose[idx] + priceShift//New by June 13th 2024 (~*~)// Define if it's a doji candledoji = y1 = y2 // Compare open and close values// Define if it's a bear or bull candlebear = y1 > y2 // Compare open and close values// Candle bodyif doji then //(~*~)drawrectangle(x1,y1, x2,y2) style(line, 5) coloured("grey")elseif bear then // Beardrawrectangle(x1,y1, x2,y2) style(line,5) coloured("orange")else // Bulldrawrectangle(x1,y1, x2,y2) style(line,5) coloured("blue")endifendif// Retrieve high and low values for the current indexy1 = $forecastHigh[idx] + priceShifty2 = $forecastLow[idx] + priceShift// Candle wicksif not doji then // (~*~)if bear then // Beardrawsegment(x1,y1,x2,y2) style(line,1) coloured("orange")else // Bulldrawsegment(x1,y1,x2,y2) style(line,1) coloured("blue")endifelsedrawsegment(x1,y1,x2,y2) style(line, 1) coloured("grey")endifnextendifreturnThank you and good trades.
regards,
Florian -
AuthorPosts
Find exclusive trading pro-tools on