ProRealCode - Trading & Coding with ProRealTime™
$MyTab[Lastset($MyTab)+1] = value //should be avoid
////////////////////////////////
once MyTabIndex = -1
...
MyTabIndex = MyTabIndex +1
$MyTab[MyTabIndex] = value //More safe to avoid strange bugs
// MyTabIndex is last index of the array $MyTab defined
2 – Sometimes you will get an error message from ProrealTime saying that the code is trying to reach an index of an array that is not strictly positive integer…
And that is weird as i use one variable pointing to lastset element for each array i use… (like mentioned above)
First, i mention that i always have this condition in my code :
If MyArrayIndex >= 0 then
...
Drawtext($Myarray[MyArrayIndex], barindex, high+10) //like this it may lead to an error message from ProrealTime
...
endif
I always add this condition before trying to reach any value of the array, because of course you don’t know if it had been already filled or not…
But even with this condition before trying to get a value of the array, you may have the error message from Prorealcode, which is really strange!
The only way i found to solve this problem is to use an other variable just before accessing to the array, like this below :
If MyArrayIndex >= 0 then
...
Mytemporaryvariable = MyArrayIndex
Drawtext($Myarray[Mytemporaryvariable], barindex, high+10) //like this i have no error message
...
endif
Have you also experiences such errors/bugs with arrays ?
2 – Sometimes you will get an error message from ProrealTime saying that the code is trying to reach an index of an array that is not strictly positive integer…That is the very general error you’ll receive when you try to access a non-existing array variable (or other “collection” means). Meanwhile the message tells you that you can not use negative indices to an array (collection). Please notice that I am not talking from PRT experience as such, but from the general coding environment, and general means handing out error messages (think in terms of the Operating System doing this). PRT just passes those messages on to the user (you). I hope I made some sense, and feel free to ask to clarify more/better ! Peter
It has always been the case that you need to convert an array variable into a non-array variable to use it in ‘DrawText’… (no bug)I do not convert the value that is inside the array at index : myarrayindex. I don’t do this : temp = $Myarray[myarrayindex] Drawtext(temp, barindex, high+10) I do this : temp = myarrayindex Drawtext($Myarray[temp], barindex, high+10) And it works fine… So it is not true that you need to convert an array variable into a non array variable to use it with drawtext or any over instruction. In the picture below, i would have to change “ObS1” by “temp” (for exemple) and to add before the drawtext instruction : “temp = ObS1” And that is why it is really STRANGE! Because it does not change anything from a logical point of vbiew… But in the first case it leads to an error and in the second case no error.
// https://www.prorealcode.com/topic/array-variables-availability-in-prorealtime/
// (please do not remove the link above for future reference)
// Example #5: tag exact time and price of an event (2 MA cross over) in real time, during the same bar
//declare the moving averages
f=average[7]
s=average[10]
//declare the events (trading signals)
bullish = (f > s and f[1] < s[1])
bearish = (f < s and f[1] > s[1])
if islastbarupdate then
//check and store the signal
if (bullish or bearish) and lastset($crosstime)<barindex then //if no signal already occurred on that bar then ..
$crosstime[barindex]=time //store the time of event
$crossprice[barindex]=close //store the price of event
if bullish then
$signal[barindex]=1 //store the signal direction
elsif bearish then
$signal[barindex]=-1
endif
endif
//draw the graphic component
if isset($signal[barindex]) then //if an event occurred on that bar then do something
if $signal[barindex]=1 then //bullish case
drawarrowup(barindex,$crossprice[barindex]) coloured(0,255,255)
else //bearish case
drawarrowdown(barindex,$crossprice[barindex]) coloured(0,255,255)
endif
itime=$crosstime[barindex] //create time readable variable for drawtext
iprice=$crossprice[barindex] //create price readable variable for drawtext
drawtext("time=#itime#",barindex,0,serif,bold,20) //plot the time of the event
drawtext("price=#iprice#",barindex,0.4,serif,bold,20) //plot the price of the event
endif
endif
return
// https://www.prorealcode.com/topic/array-variables-availability-in-prorealtime/
// (please do not remove the link above for future reference)
// Example #5: tag exact time and price of an event (2 MA cross over) in real time, during the same bar
//declare the moving averages
f=average[7]
s=average[10]
//declare the events (trading signals)
bullish = (f > s and f[1] < s[1])
bearish = (f < s and f[1] > s[1])
if islastbarupdate then
//check and store the signal
if (bullish or bearish) and lastset($crosstime)<barindex then //if no signal already occurred on that bar then ..
$crosstime[barindex]=time //store the time of event
$crossprice[barindex]=close //store the price of event
if bullish then
$signal[barindex]=1 //store the signal direction
elsif bearish then
$signal[barindex]=-1
endif
endif
//draw the graphic component
if isset($signal[barindex]) then //if an event occurred on that bar then do something
if $signal[barindex]=1 then //bullish case
drawarrowup(barindex,$crossprice[barindex]) coloured(0,255,255)
else //bearish case
drawarrowdown(barindex,$crossprice[barindex]) coloured(0,255,255)
endif
//itime=$crosstime[barindex] //create time readable variable for drawtext
//iprice=$crossprice[barindex] //create price readable variable for drawtext
drawtext("time=",barindex-6,0,serif,bold,20)
drawtext($crosstime[barindex],barindex,0,serif,bold,20) //plot the time of the event
drawtext("price=",barindex-6,0.4,serif,bold,20)
drawtext($crossprice[barindex],barindex,0.4,serif,bold,20) //plot the price of the event
endif
endif
return
You’re absolutely right and Nicolas examples are wrong…
You’re absolutely right and Nicolas examples are wrong…Have Nicolas said “Never use arrays value directly in any graphical instructions ? I don’t think so Did i said Nicolas is wrong anywhere ? No Is it written in the Bible that you should never use arrays directly inside drawtext, otheirwise you will burn in Hell ? I asked ChatGPT and it says “No” Just try and see… What else can i say? Anyway, the bug i’m talking about is not because of that.
LucasBest wrote: So it is not true that you need to convert an array variable into a non array variable to use it with drawtext or any over instruction.
Don’t know why this bug happen even if i check my “index” variable before using it to access the array with : “If index = Max(Round(index),0) then” to be sure that is is strickly positive integer, i need to use that temporary variable…but use it slightly differently from how I would do it. What I mean is : When you know how to look for it, you can see that variables – by you set to e.g. 12 – end up as 12.000000000001 (don’t pin me down on the number of decimals, but something like that). That would *not* suffice as an index reference for an array, obviously. This addition of a very small decimal in itself is a bug of course. Because this is a buggy situation to begin with, equaling that variable to another variable (MyTemp) will remove the extra decimals. Why ? because with PRT you can’t work with more than 5 decimals to begin with (some times you can, but for my case in this post you will trust me). Thus, the MyTemp = MyArrayIndex would be sufficient to solve this buggy / erroneous situation (!). There is another situation which incurs for error : use MyArrayIndex as an Optimization parameter. It is far-sought because you probably don’t do this, but *if* you do, I think it could go wrong in the very first usage of that variable as an index. Here too, if you know where and how to look for it, you will see that ALL your defined integer variables end up as decimal variables. Thus, a set MyArrayIndeex as 12, will end up as 12.0. Without guarantee, I bet you that this goes wrong when used as index variable. In order for further (re)search, be warned that rounding a variable (like Round,0) internally already will lead to an output with decimals, or better put : an output which is not an integer variable. If you know a normal programming language like C++ or C# (or VB to a lesser extent), you will know what I mean (int vs double vs decimal and such). … I implied it before … you really should not approach this as a bug because then you will not find the cause. Thus, my 12.00000001 example would be the real bug which you won’t look for. And I definitely bet you it is something like that (not exactly this, but something which leads to the error you receive and the error WILL be justified). Can’t you hand a working code which produces the bug ? I ask this, because then we can work together on finding the culprit. This is – as you say it yourself – very important because others will be “bugged” by it as well, and it is very hard to track down. Thus, would we be able to find a real underlaying cause, then PRT will solve that (I will take care of that myself). Deal ? If you can hand a working code, please mention the instrument and timeframe of the chart.
Once RSILen = 14
Once prd = 1
Once pBcTi = -1
Once pBmTi = -1
src = RSI[RSILen](close)
If src[prd] = lowest[2*prd+1](src) then
pBcTi=pBcTi+1
$pBcT[pBcTi]= Barindex-1
if pBcTi > 2 then
if src[barindex-$pBcT[pBcTi-1]] <= src[barindex-$pBcT[pBcTi-2]] and src[barindex-$pBcT[pBcTi-1]] <= src[barindex-$pBcT[pBcTi]] then
pBmTi=pBmTi+1
$pBmT[pBmTi]= $pBcT[pBcTi-1]
drawpoint($pBcT[pBcTi-1],src[barindex-$pBcT[pBcTi-1]],3) coloured("red",100)
endif
endif
endif
Return src
Note that variables i use as index for the arrays can be only integers as i increment them only with 1 (which is interger too) : MyArrayIndex = MyArrayIndex + 1 (thus MyArrayIndex remain integer from the begining to the end.
In order to solve the bug, i do like this (in this exemple) :
Once RSILen = 14
Once prd = 1
Once pBcTi = -1
Once pBmTi = -1
src = RSI[RSILen](close)
If src[prd] = lowest[2*prd+1](src) then
pBcTi=pBcTi+1
$pBcT[pBcTi]= Barindex-1
if pBcTi > 2 then
pb1 = pBcTi-2
pb2 = pBcTi-1
pb3 = pBcTi
if src[barindex-$pBcT[pb2]] <= src[barindex-$pBcT[pb1]] and src[barindex-$pBcT[pb2]] <= src[barindex-$pBcT[pb3]] then
pBmTi=pBmTi+1
$pBmT[pBmTi]= $pBcT[pb2]
drawpoint($pBcT[pb2],src[barindex-$pBcT[pb2]],3) coloured("red",100)
endif
endif
endif
Return src
Maybe the fact to use normal variable in between make prorealtime wait until the close of the bars and not use not permanent updates of the arrays…
Maybe the fact to use normal variable in between make prorealtime wait until the close of the bars and not use not permanent updates of the arrays…Great thinking ! Yea, this is all very mystique. So, would you use that variable further down the line in the same call of the code (at this particular tick) then you can just do that. But would you, for example, add a tick count to that/a variable, then it will add per the next bar only. So for example, 10 ticks may pass during a 1 second bar, but your incrementing variable will have added 1 only. I now suddenly wonder … If we’d increment $MyArray[17] each tick (thus call of the code) would *that* then be remembered in element 17 ?? … I think so ? If that is so, this really would (be news ? and) bring new opportunities.
$MyArray[17] = $MayArray[17] + 1
Thus, use an Array element as vehicle to perform math per tick after all and save that to the calling program.
Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.
Arrays related bugs
This topic contains 18 replies,
has 3 voices, and was last updated by LucasBest
1 year, 8 months ago.
| Forum: | ProBuilder: Indicators & Custom Tools |
| Language: | English |
| Started: | 12/28/2024 |
| Status: | Active |
| Attachments: | 1 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.