Gold Customer
Joined: 3/15/2005 Posts: 11
|
Hi, I want to know or understand how to use variables within a realcode formula for creating custom indicators which uses conditional statements combined to result in an indicator that can be ploted. Metastock has some formulas that take chuncks of data such as sum( c,4) which take 4 days of close and add them together to get a sum of the c(-3)+C(-2)+C(-1)+C but it can also be used with indicators like sum(ATR,5)/4 total of last 5 days ATR /4 My logic for the indicator was this RM = if(H-L),=,0,.001,(H-L) 'Checks to see if range of high-low =0, if so make it .001 else make it the value of high - low. no 0 ranges for math functions to deal with next up range= ((C-L)/RM) dn range= ((H-C)/RM) uptick1= if (c(-1),>,C,(IF(H,>=,C(-1),((H-C(-1)+(C-L))/.5,0)),0) uptick2= if (c(-1),<,C,(IF(L,>,C(-1),((H-C(-1))/.5,0)),0) dntick1= if (c(-1),<,C,(IF(L,<=,C(-1),((C(-1)-L)+(H-C))/.5,0)),0) dntick2= if (c(-1),>,C,(IF(H,<,C(-1),((C(-1)-L))/.5,0)),0) VC1=uptick1 + uptick2 VC2=dntick1 + dntick2 VC3= IF(VC1/(VC1+VC2)),<,.2,.2,VC1/(VC1+VC2) 'if uptick <.2 make it .2 or return the value Plot VS VS=sum(VC2,4)/sum(VC3,4) 'VS=4 day sum of VC2/4 day sum of V3 last but not least how do you program OBV from scratch. Metastock has a formula called cum which keeps a running total of in this case volume. can custom indicators be turned into blocks and called like other blocks? Thanks in advance
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
I don't speak "Metastock", but I think something similar to the RealCode Indicator would replicate the supplied code.
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:VS
'|******************************************************************
Static VC2 As New List(Of Single)
Static VC2sum As Single
Static VC3 As New List(Of Single)
Static VC3sum As Single
If isFirstBar Then
VC2.Clear
VC2sum = 0
VC3.Clear
VC3sum = 0
Plot = Single.NaN
Else
Dim RM As Single = Price.High - Price.Low
If RM = 0 Then RM = .001 'Checks to see if range of high-low =0, if so make it .001 else make it the value of high - low. no 0 ranges for math functions to deal with next up
Dim range As Single = (Price.Last - Price.Low) / RM
Dim dnRange As Single = (Price.High - Price.Last) / RM
Dim uptick1 As Single = 0
Dim uptick2 As Single = 0
Dim dntick1 As Single = 0
Dim dntick2 As Single = 0
If Price.Last(1) > Price.Last Then
If Price.High >= Price.Last(1) Then
uptick1 = (Price.High - Price.Last(1) + Price.Last - Price.Low) / .5
Else
dntick2 = (Price.Last(1) - Price.Low) / .5
End If
Else If Price.Last(1) < Price.Last Then
If Price.Low > Price.Last(1) Then
uptick2 = (Price.High - Price.Last(1)) / .5
Else
dntick1 = (Price.Last(1) - Price.Low + Price.High - Price.Last) / .5
End If
End If
Dim VC1 As Single = uptick1 + uptick2
VC2.Insert(0, dntick1 + dntick2)
VC2sum += VC2(0)
If VC1 + VC2(0) <> 0 Then
VC3.Insert(0, Math.Max(.2, VC1 / (VC1 + VC2(0)))) 'if uptick <.2 make it .2 or return the value
Else
VC3.Insert(0, .2)
End If
VC3sum += VC3(0)
Dim VS As Single = Single.NaN
If VC2.Count = 4 Then
VS = VC2sum / VC3sum 'VS=4 day sum of VC2/4 day sum of V3
VC2sum -= VC2(3)
VC2.RemoveAt(3)
VC3sum -= VC3(3)
VC3.RemoveAt(3)
End If
Plot = VS
If isLastBar Then
VC2.Clear
VC3.Clear
End If
End If
OBV in RealCode from scratch would be a lot simpler.
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:On Balance Volume
'|******************************************************************
'# Cumulative
Static OBV As Single
If isFirstBar Then
OBV = 0
Else
OBV += Math.Sign(Price.Last - Price.Last(1)) * Volume.Value
End If
Plot = OBV
RealCode Indicators cannot be turned into Blocks to use in Block Diagrams. It is possible to create Custom Code Blocks, but while the language is similar in that it is based on Visual Basic, it is not the same as RealCode. I don't have any documentation or videos on this feature I can give you to reference either.
You can right-click on a Block in a Block Diagram and select Edit Code to view the code for the Code Block. This may be enough to allow you to replicate the techniques used. Note that there will be a warning when you do so to let you know that RealCode has replaced Custom Code Blocks and that Custom Code Blocks are only available in the program for backwards compatibility.
Note that actually create a Custom Code Block involves right-clicking on the background of a Block Diagram and selecting Create Code Block and choosing the desired inputs and outputs of that Custom Code Block from the list on the left.
The alligator topic has some example code in it for use as a Custom Code Block.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
Gold Customer
Joined: 3/15/2005 Posts: 11
|
Thanks a lot. It's a lot more difficult than I imagine but I think I get the gist.
with v3sum += vc3(0) vs vc2sum -= vc2(3) the sign in front of the equal sign and does the (#) represent array locations? vc3.insert (0,.2) places .2 into the 0 location?
you used a Math.Max (.2,.....
If I wanted to say if the value is in a range,
V3=IF(VC/(VC1+VC2),<,.2,.2,IF (VC/(C1+VC2),>.8,.8,VC1/(VC1+VC2)))
is there another Math Function like Math.Max? what about nested if statement like above. What would it look like?
Sometimes used Dim, is there rule or rational when to use it. You Dim VS and VC1 but not VC2.
lastly Plot= Single.NaN at the begining vs the solved VS which you plot at the end. What does Plot=single.NaN do.
Trying to learn
Thanks again
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
QUOTE (ronate12) with v3sum += vc3(0) vs vc2sum -= vc2(3) the sign in front of the equal sign and does the (#) represent array locations?
Yes, these are locations in the list.
QUOTE (ronate12) vc3.insert (0,.2) places .2 into the 0 location?
Yes, this is correct as well.
QUOTE (ronate12) If I wanted to say if the value is in a range,
V3=IF(VC/(VC1+VC2),<,.2,.2,IF (VC/(C1+VC2),>.8,.8,VC1/(VC1+VC2)))
is there another Math Function like Math.Max?
I am not aware of a built in math function for between, but I am not familiar with them all either. I am not a programmer.
QUOTE (ronate12) what about nested if statement like above. What would it look like?
If we just nested Math.Min and Math.Max:
V3 = Math.Min(.8, Math.Max(.2, VC / (VC1 + VC2)))
QUOTE (ronate12) Sometimes used Dim, is there rule or rational when to use it. You Dim VS and VC1 but not VC2.
The Static statements at the top are a type of Dim where the value persists between bars. The items for which Dim was used do not have the value saved between bars.
QUOTE (ronate12) lastly Plot= Single.NaN at the begining vs the solved VS which you plot at the end. What does Plot=single.NaN do.
Single.NaN means Not A Number in the single precision floating point data type. In this case, I use it to tell StockFinder that there isn't actully a valid value for that bar.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|