This is the code I made from Average Filter Regression in this library:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// K = 500 ({1...2000})
Series=(Open+High+Low+Close)/4
ONCEPred=Series
IFBarIndex=0THEN
AFR=Series
ELSE
Smooth=Pred+(Series-Pred)*SQRT((K/10000)*2)
Velo=Velo+((K/10000)*(Series-Pred))
Pred=Smooth+Velo
AFR=Pred
ENDIF
RETURNAFR
It looks like the first screenshot. How can I make it look like the second screenshot: If Velo > 0 then the line is green, If Velo <= 0 then the line is red?
The ‘if’ statement follows your logical criteria of ‘ Velo’ which set the value’s for ‘r’ and ‘g’. Then the addition of the ‘coloured’ modifier uses them to set the RGB colour of your ‘AFR’ line.
I recall a saying – If it’s not broken don’t try and fix it! Bare that in mind.
Since I’m on a steep learning curve trying to get in to PRT and the coding side, I decided to analyse the code you posted as an exercise. If this is beyond or distracting to what your doing, feel free to ignore. Just sharing.
The code here is just main code but the .itf file has a lot more comments in it, why I changed and/or added things.
I’ve learned a lot doing this and wouldn’t have done it if it wasn’t for your post and well laid out request/spec.
In analyzing I have, replaced/altered/created some variables with stock/new, replaced original ‘if’ block and added a value guard, error message and logic control to the ‘K’ variable if invalid.
Additionally, it made me figure out a couple of technique when checking that code changers give same outcome’s. These such as creating alternate, by a numbered index, variable’s to calculate a kind of before and after type scenario and by subtracting one from the other it should result in zero if there the same
Also by having a indicator panel with original and a new file with the original copied in it, as changes are made you can see if expected result are obtained. Also have a original in price window. A plan is better than no plan.
Not possibly rocket science or ground breaking but, kept me focused on the task for a while. As always do your own testing.
All the best.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// include dynamic variable
// 'K' decimal/integer, default 500
ONCEAFR=totalPrice
// Input Guard - Limits 'K' input range +(1 to 2000) @ 1 d.p.
validK=round(max(min(K,2000),1),1)
// Calculate CONSTANT
CONSTANT=validK/10000
// Input gaurd error code - if 'K' exceeds set limit's give warning!
ifK<1orK>2000then
drawtext("ERROR! ~ K = #K# Out of Range (1-2000)",0,8,Dialog,Bold,15)Anchor(bottom)coloured("RED")
else
// else run the number cruncher - calculate new AFR
Smooth=AFR+(totalPrice-AFR)*SQRT(CONSTANT*2)
Velo=Velo+CONSTANT*(totalPrice-AFR)
AFR=Smooth+Velo
// Set colour value's for 'AFR' line, 'green' velo > 0, 'red' velo <= 0
ifVelo>0then
g=255
r=0
else
g=0
r=255
endif
endif
RETURNAFRcoloured(r,g,0)// new AFR value set to required colour's
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok