PDA

View Full Version : How to get values from BollingerBands study into an Array?



malcolm
06-26-2015, 10:53 AM
Dear Fellow Programmers

I am trying to use Bollinger Bands in an indicator (VTL Client Side Script) and I have 2 issues:

(1) When I try and use the BollingerBands(0,Price_close,20,2,1) study, this puts the study below the chart panel. I would like this study/indicator to be superimposed on the price chart as it is normally used. I tried using Chart ID 0, but no luck. Any suggestions?

(2) Someone suggested extracting the values from BollingerBands(0,Price_close,20,2,1) into an array and then passing this array to the AddCustomIndicator function and then use the SubWindow/Panel ID to put the study on top of the price bars. However, when you try and use BollingerBands(0,Price_close,20,2,1) in your code, it just returns the handle. How do you get the actual Bollinger Bands values to put into an array?

For example Upper Bollinger Band value at Bar1 = 1.0224, Upper Bollinger Band Value at Bar2 = 1.0226, etc. Lower Bollinger Band Value at Bar1 = 1.0202, etc.

Any help would be much appreciated as this is very urgent.

Thank you in advance.

Malcolm

Hassan-HS
06-28-2015, 08:14 AM
Hello,


You can use iBandsOnArray (http://www.hybrid-solutions.com/support/index.php?/Knowledgebase/Article/View/3519)method to get the array of the Bollinger bands then you can add it easily to the chart.


Please check this code


Dim indKey1
Dim indKey2
Dim indKey3
Dim ary ()
Dim Top()
Dim Bottom()
Dim Period
Dim result()
Dim MA_Type
Dim StandardDeviation
Public Sub main()
Period = 13
MA_Type = 1
StandardDeviation = 2
CopyClose 0, 1, Bars (0), Ary
iBandsOnArray Ary, CInt(Period), CInt(StandardDeviation), CInt (MA_Type), Result,Top,Bottom
For i= 1 to Period
Result(i)=-987654321 ' DTNull Value, because the 0 is not a valid value
Top(i)=-987654321
Bottom(i)=-987654321
Next
indKey1=AddCustomIndicator(0, Result,1,False)
indKey2=AddCustomIndicator(0, Top,1,False)
indKey3=AddCustomIndicator(0, Bottom,1,False)
End Sub


''' <summary>
''' To Update the bollinger band every new candle.....
''' </summary>


Public Sub OnCalculate(symbol, symbolPeriod, openVal, highVal, lowVal, closeVal)
CopyClose 0, 1, Bars (0), Ary
iBandsOnArray Ary, CInt(Period), CInt(StandardDeviation), CInt (MA_Type), Result,Top,Bottom
SetIndicatorData 0,CSTR(indKey1),CDbl(Result(Ubound(Result)))
SetIndicatorData 0,CSTR(indKey2),CDbl(Top(Ubound(Top)))
SetIndicatorData 0,CSTR(indKey3),CDbl(Bottom(Ubound(Bottom)))
End Sub


''' <summary>
''' The function is generated when deinitialization script
''' </summary>


Public Sub OnDeInit()
'''TODO: deinitialization method
ObjectDelete 0,CSTR(indKey1)
ObjectDelete 0,CSTR(indKey2)
ObjectDelete 0,CSTR(indKey3)
End Sub

malcolm
06-29-2015, 10:29 AM
Thank you very much. We will take a look now.

Hassan-HS
07-12-2015, 11:39 AM
Great, welcome bro :)