Can you use for statements for automated trading, where each indicator refers to the last bars value?
Just a random example:
once x=0
ax = x/3*abs(x-5)
x=x+1
if barindex>100 then
a=0
b=0
c=0
for i=1 to 100
a=a+ax[barindex-i+1]
b=b+ax[barindex-i+1]*i
c=c+ax[barindex-i+1]*i*i
next
endif
indictor = a*b/c
if close crosses under indicator
buy n contracts at market
endif
Doesn’t seem to work for me.. Is it allowed?
WingParticipant
Veteran
Could it be that you spell ‘indicator’ differently on row 18 and 20?
Good eye!
But sadly, no.. It’s just an example I made up now.
WingParticipant
Veteran
What is the error message you receive, if any? Is there a value returned for a, b and c? Does it work with say, 30 iterations instead of 100? Could it be the close cross under indicator check that does not work?
Literally nothing happens, I get the summary (but no trades). And I tried all kinds of conditions apart from ‘cross under’.
WingParticipant
Veteran
Use the ‘graph’ function and check how each of your variables behave.
They are displayed correctly; that’s the weird part.
WingParticipant
Veteran
Do you define all your variables from the start, and have you tried for example defparam preloadbars=1000? Also, isn’t the value of ‘indicator’ much greater than the close price? I don’t think there is anything wrong with your method or the for statements here, but rather the example you have. Here is for example an indicator I have made that uses for statements to gather data from previous bars:
https://www.prorealcode.com/topic/pattern-matching-price-movements-in-prt/