Hi everyone, I have programmed an array that stores past supports and resistances and I would like to use them as conditions for triggering orders. A simple example would be that way:
in my current strategy a “buy” order is triggered if condition A is fulfilled. I would like to restrict it the the case when condition A is fulfilled AND the price crosses over (or under) any value of the resistance (or support) array.
Of course I can’t just write it
IF conditionA=true AND close crosses over $res
because “$res” is the name of the array ; I can’t write
IF conditionA=true AND close crosses over $res[a] (with a being any value)
because I have to define “a” beforehand
I could write
FOR a=ArrayMin($res) to ArrayMax($res) DO
IF conditionA=true AND close crosses over $res[a] THEN
BUY SHARE AT MARKET
BREAK
ELSE
CONTINUE
ENDIF
NEXT
But I’m unsure whether it is the most straightforward thing to do. Any ideas? Thanks!
This will do:
FOR a = 0 TO lastset($res) DO
IF conditionA AND close crosses over $res[a] THEN
BUY SHARE AT MARKET
BREAK
ENDIF
NEXT
Building upon Juld63’s idea, how would this be applied in the instance of multiple arrays containing levels of support or resistance?
For example $res1, $res2, $res3 etc?
Kind regards
Stuart
Is this what you mean?
IF Not OnMarket THEN
Flag = 0
FOR a = 0 TO lastset($res) DO
IF conditionA AND close crosses over $res[a] THEN
BUY SHARE AT MARKET
Flag = 1
BREAK
ENDIF
NEXT
IF Flag = 0 THEN
FOR a = 0 TO lastset($sup) DO
IF conditionB AND close crosses under $sup[a] THEN
SELLSHORT SHARE AT MARKET
BREAK
ENDIF
NEXT
ENDIF
ENDIF
More or less, yes. I’d rather have it scan the array from ArrayMax to ArrayMin for supports and from ArrayMin to ArrayMax for resistances so that it tests the values in the order it should be encountering them, rather than in the historical order they are stored, so as to make the code more efficient. I just wonder if there wouldn’t be a simpler way.
You can use the arraysort command to reorder your array if you wish:
ArraySort
Hello everyone,
I have coded and array indicator that gives me past supports and resistances not engulfed by subsenquent ranges. It works in Probuilder, meaning it displays as it should.
Now that this is done, I want to be able to call the values in this array to test if my remarkable candle shapes intersect with any of those values or not.
However, I have absolutely idea of how to call an array, its the various $array[n] … Of course I can just duplicate the whole indicator into the trading system’s code, but it becomes cumbersome.
This is what I have down the line, if only I can manage to call $res and $sup (“candlenb” being used for identifying how many candles make up a shape)
FOR a=ArrayMin($res) TO ArrayMAX($res) DO
IF (candlenb=1 AND open<=a AND close>=a) OR (candlenb=2 AND highest[2](high)>=a AND lowest[2](low)<=a) OR (candlenb=3 AND highest[3](high)>=a AND lowest[3](low)<=a) OR (candlenb=4 AND highest[4](high)>=a AND lowest[4](low)<=a) OR (candlenb=5 AND highest[5](high)>=a AND lowest[5](low)<=a) OR (candlenb=6 AND highest[6](high)>=a AND lowest[6](low)<=a) THEN
resres=1
BREAK
ELSIF (candlenb=1 AND close<a+1) OR (candlenb=2 AND highest[2](high)<a+1) OR (candlenb=3 AND highest[3](high)<a+1) OR (candlenb=4 AND highest[4](high)<a+1) OR (candlenb=5 AND highest[5](high)<a+1) OR (candlenb=6 AND highest[6](high)<a+1) THEN
resres=0
BREAK
ELSE
CONTINUE
ENDIF
NEXT
Any help will be much appreciated !
Also, to avoid scanning the whole array if the candle(s) is(are) between two values in the array, after it has failed to check if value “a” is inside a candle or group of candles, I want it to check whether it doesn’t reach the next value of the array in increasing order. My poor attempt is writing this value as “a+1” but of course it will return the value of “a” plus 1 point, not, if we’re starting from ArrayMin($res), the second smallest value of the array. Any idea how to get this, the nth smallest (or biggest) value of a given array beyond the absolute smallest (ArrayMin) and the absolute biggest (ArrayMax)? Thanks !
JSParticipant
Senior
Hi,
You can use “ArraySort”, for example “ArraySort($Res,Descend)…
The entire array is now sorted from highest to lowest, so your highest value is now $Res[0] and the next highest values are: $Res[1], $Res[2], $Res[3], and so on
The number between the bracket of the array is always the index of the array (not the value of the array)…
Thanks a lot! And to use it do I just need to CALL my array indicator at the beginning of the code?
I have tried to just CALL my array (res = CALL “res sup version longue”) but it tells me that it returns zero values…
Hi.. As far as I know you can not ‘call’ an array as a whole entity, with the call function from another file.
Below is 1 example of what you can do.
Example builds a replica array in the ‘Main’ file from values fetched from the ‘data’ file with the call instruction.
It does this replication on the first bar, fetching each array elements value individually.
For this to work, the index value for the required value is sent with the ‘call’ which means that variable ‘ idx’ requires setting up as a dynamic variable in the ‘data’ file.
The return value is stored in ‘Main’ in local array at same index. The local array accessed as required via a index value … arr [idx].
Also if you add additional res/sup lines to the ‘data’ arrays, since the size is calculated from how elements exists, these should ripple through to main, in indicators, not sure what effects with proOrder/bot. Not tried!
#JS brings up a good point, if you sort the array in ascending or descending order, then finding the ‘nth’ highest/ lowest, would be easy to determine from chosen array index value.
Also in practise you really probable only need to know if you have reached the next higher or lower res line from last position, you then could use a narrow loop range rather than looking through whole array.
druby
// lineMain
if barindex = 0 then //build local array on/at bar 0
// get size of array
size, ignore = CALL "lineData"[0]
// fetch 'call'ed values and store in local array
for i = 0 to size //loop!
// fetch val'ue pointed to by indexed 'i'
idx = i
ignore, val = CALL "lineData"[idx]
// store in local array at same index
$res[i] = val
next
endif
// loop to create an 'index' value to access local array elements
for i = 0 to lastset($res)
res = $res[i] // put indexed value in a variable
// display array as list
drawtext("#i# #res#",0,0 - (i*15))anchor(middle,xshift,yshift)
next
drawtext("i res",0,20)anchor(middle,xshift,yshift)
return
// lineData
// dynamic variable(s) to setup!
// 'idx' ... integer ... default = 0 ... positive
// array data
$res[0] = 10
$res[1] = 20
$res[2] = 30
$res[3] = 40
$res[4] = 50
$res[5] = 60
$res[6] = 70
$res[7] = 80
// determine size of array
size = lastset($res) // size = (arrayElements - 1), 0 to 'n'
// fetch indexed value refered by 'idx'
res = $res[idx]
return size, res
Hello,
Thanks for your help. In the meanwhile I’ve found a simpler way around: in the indicator where the array is calculated I call the other indicator and have it code whether the candle shapes are across a value of the array, and then I call the results of the array indicator not as array values but as a simple boolean operator, and then I can just call the value of this operator in my robot with a CALL instruction.
Hello, juld63,
I’m still learning to code, and so wondered if you would be kind enough to share the code of your ‘simpler way round’, as I believe it will solve a problem I have of a similar nature.
With thanks in advance…
Sure! Here’s how I finally managed to write it
ONCE myindexres=1
ONCE myindexsup=1
//10PM is when we fill and filter the arrays
if opentime=220000 Then
//add highest and lowest since yesterday 10PM
$res[myindexres] = highest[period](high)
$sup[myindexsup] = lowest[period](low)
//reset all past resistance values that are engulfed by the last 24h highest and lowest
FOR a=0 to myindexres-1 DO
IF $res[a]<$res[myindexres] AND $res[a]>$sup[myindexsup] THEN
$res[a]=0
CONTINUE
ENDIF
NEXT
//reset all past support values that are engulfed by the last 24h highest and lowest
FOR a=0 to myindexsup-1 DO
IF $sup[a]<$res[myindexres] AND $sup[a]>$sup[myindexsup] THEN
$sup[a]=0
CONTINUE
ENDIF
NEXT
//reset the index count for the next day
IF IsSet($res[myindexres])=1 THEN
myindexres=myindexres+1
ENDIF
IF IsSet($sup[myindexsup])=1 THEN
myindexsup=myindexsup+1
endif
ENDIF
resres=0
ressup=0
//sort the resistance values in ascending order
ArraySort($res,ascend)
FOR a=1 TO LastSet($res)DO
IF $res[a]=0 THEN //this avoids a bug that takes into account empty cells as 0s
continue
ELSIF YOURCONDITIONMET=1 AND low<=$res[a] AND high>=$res[a]) THEN
resres=1 // your signal candle intersects with a resistance
BREAK
ELSIF YOURCONDITIONMET=1 AND low>$res[a] AND high<$res[a+1]THEN
resres=0 // your signal candle intersects is between two resistance lines
BREAK
ELSE
CONTINUE
ENDIF
NEXT
//sort the support values in ascending order
ArraySort($sup,descend)
FOR a=1 TO LastSet($sup) DO
IF $sup[a]=0 THEN //this avoids a bug that takes into account empty cells as 0s
continue
ELSIF YOURCONDITIONMET=1 AND low<=$sup[a] AND high>=$sup[a] THEN
ressup=1 // your signal candle intersects with a support
BREAK
ELSIF YOURCONDITIONMET=1 AND high<$sup[a] AND low>$sup[a+1] THEN
ressup=0 your signal candle is between two support lines
BREAK
ELSE
CONTINUE
ENDIF
NEXT