PDA

View Full Version : Useing MACD indicator in Server Script



nhasan
08-13-2013, 07:19 AM
Dear Team

I am trying to use MACD indicator in a server script.
In general we use two variables from MACD indicator - 1. Main Value 2. Signal Value

I need main MACD and Signal value in bot current and previous candle to identify change in direction of the signal.

I am testing sample code given in knowledge base that shows 100 values in message window but I can't identify required Main and Signal value of current and previous candle from them.

Can anyone please help me in getting these indicator values?

Thanks
Naeem Hasan

b_morar
08-13-2013, 09:25 AM
Dear Sir,


The bar details stored in the database after it is closed.
GetChartHistory retrieve prices for specific bars from database.

That means you can not apply any TA functions indicator over current bar because its price is not available.

Anyway this is a sample describe how to get the value of MACD indicator for last two saved candles in GOLD-Day chart, I changed "100" with Bars (http://www.hybrid-solutions.com/support/index.php?/Knowledgebase/Article/View/3634)function, which returns the number of bars that is stored in the database for specific symbol and period :



Public Sub main()

'Variables

Dim _symbolInfo As VTLGeneral.CSymbol=ClientCode.GetSymbolByName("GOLD")

Dim DB As New VTLGeneral.Database()

Dim RecordCount As Integer

Dim m_Recordset As VTLGeneral.RecordSet

Dim _historyData As object()

Dim output As String

Dim Record As Integer

Dim m_Date As VTLGeneral.Field

Dim m_Open As VTLGeneral.Field

Dim m_High As VTLGeneral.Field

Dim m_Low As VTLGeneral.Field

Dim m_Close As VTLGeneral.Field

Dim j As Integer = 0

Dim i As Integer = 0

Dim _recordCount As Integer = clientcode. Bars( _symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day)



m_Recordset = DB.CreateRecord

m_Open = New VTLGeneral.Field

m_High = New VTLGeneral.Field

m_Low = New VTLGeneral.Field

m_Close = New VTLGeneral.Field


DB.RecordCount = _recordCount

RecordCount = _recordCount


'Initialize Recordsets

m_Open.initialize(_recordCount, "Open")

m_High.initialize(_recordCount, "High")

m_Low.initialize(_recordCount, "Low")

m_Close.initialize(_recordCount, "Close")

'load high, low ,open and data

_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_HIGH, _recordCount)


For i = 1 To _recordCount-1

m_High.setValue(i,_historyData(i))

Next


_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_LOW, _recordCount)


For i = 1 To _recordCount-1

m_Low.setValue(i,_historyData(i))

Next


_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_OPEN, _recordCount)


For i = 1 To _recordCount-1

m_Open.setValue(i,_historyData(i))

Next


_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_CLOSE, _recordCount)


For i = 1 To _recordCount-1

m_Close.setValue(i,_historyData(i))

Next


m_Recordset.addField(m_Open)

m_Recordset.addField(m_High)

m_Recordset.addField(m_Low)

m_Recordset.addField(m_Close)


'MACD indicator

Dim _indRecord As New VTLGeneral.RecordSet()

Dim osc As New VTLGeneral.Oscillator()

_indRecord = osc.MACD(DB,m_Recordset,5)


For i = DB.getRecordCount-2 To DB.getRecordCount-1

output = output & CSTR(_indRecord.getValue(_indRecord.getName(1), i) ) & vbcrlf

Next

GUI.MsgDialog(output)
End Sub

nhasan
08-13-2013, 10:57 AM
Thanks for your reply

So far as I understand _indRecord.getName(1) will return Main value of MACD and _indRecord.getName(2) will return Signal value

Is it?

b_morar
08-13-2013, 12:06 PM
Dear Sir,

Yes exactly, You can check that using MsgDialog :



GUI.MsgDialog(m_Recordset. getName(2))


Let me know if you need any further assistance;):cool:.

nhasan
08-14-2013, 04:21 AM
Thanks for your quick support.

We have three parameters in MACD calculation - 1. Fast EMA (Default Value 12), 2. Slow EMA (Default value 26) and 3. Signal Level (Default value 9)
Can I change them from server script code?

b_morar
08-14-2013, 09:45 AM
Hello,
For the Signal Level, actually it is the EMA applied on MACD, so it is something can be done from your side by adding ExponentialMovingAverage applied on the MACD array (send it on the source).

For the Fast/Slow EMA parameters, we are working on it now to be added as parameters on the MACD function.

We will keep you updated.

nhasan
08-14-2013, 12:39 PM
so it is something can be done from your side by adding ExponentialMovingAverage applied on the MACD array (send it on the source). --

Is there any sample code for this?

b_morar
08-14-2013, 02:25 PM
Dear Sir,


The following sample describes how to apply EMA indicator based on the result of MACD indicator.


Public Sub main()

'Variables

Dim _symbolInfo As VTLGeneral.CSymbol=ClientCode.GetSymbolByName("GOLD")

Dim DB As New VTLGeneral.Database()

Dim RecordCount As Integer

Dim m_Recordset As VTLGeneral.RecordSet

Dim _historyData As object()

Dim output As String

Dim Record As Integer

Dim m_Date As VTLGeneral.Field

Dim m_Open As VTLGeneral.Field

Dim m_High As VTLGeneral.Field

Dim m_Low As VTLGeneral.Field

Dim m_Close As VTLGeneral.Field

Dim j As Integer = 0

Dim i As Integer = 0

Dim _recordCount As Integer =clientcode.bars(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day)


m_Recordset = DB.CreateRecord

m_Open = New VTLGeneral.Field

m_High = New VTLGeneral.Field

m_Low = New VTLGeneral.Field

m_Close = New VTLGeneral.Field


DB.RecordCount = _recordCount

RecordCount = _recordCount


'Initialize Recordsets

m_Open.initialize(_recordCount, "Open")

m_High.initialize(_recordCount, "High")

m_Low.initialize(_recordCount, "Low")

m_Close.initialize(_recordCount, "Close")


'load high, low ,open and data

_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_HIGH, _recordCount)


For i = 1 To _recordCount-1

m_High.setValue(i,_historyData(i))

Next


_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_LOW, _recordCount)


For i = 1 To _recordCount-1

m_Low.setValue(i,_historyData(i))

Next


_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_OPEN, _recordCount)


For i = 1 To _recordCount-1

m_Open.setValue(i,_historyData(i))

Next


_historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY _TYPE.HIS_CLOSE, _recordCount)


For i = 1 To _recordCount-1

m_Close.setValue(i,_historyData(i))

Next


m_Recordset.addField(m_Open)

m_Recordset.addField(m_High)

m_Recordset.addField(m_Low)

m_Recordset.addField(m_Close)


'MACD indicator

Dim _indRecord As New VTLGeneral.RecordSet()
Dim osc As New VTLGeneral.Oscillator()
_indRecord = osc.MACD(DB,m_Recordset,5)



Dim _indRecord2 As New VTLGeneral.RecordSet()
Dim mv As New VTLGeneral.MovingAverage()
Dim MACD As VTLGeneral.Field
MACD =_indRecord.getField(_indRecord.getName(1))
_indRecord2 = mv.ExponentialMovingAverage(DB,MACD,14,"ExponentialMovingAverage")
For i = DB.getRecordCount-3 To DB.getRecordCount-1
output = output & CSTR(_indRecord2.getValue(_indRecord2.getName(1), i) ) & vbcrlf
Next
GUI.MsgDialog(output)
End Sub

b_morar
08-18-2013, 02:08 PM
Dear Hasan,

It's our pleasure to tell you that now you can apply MACD (http://www.hybrid-solutions.com/support/index.php?/Knowledgebase/Article/View/3067) indicator with custom fast slow EMA.

For more information please visit this link (http://www.hybrid-solutions.com/support/index.php?/Knowledgebase/Article/View/3067), you can find a helpful sample there ;).

b_morar
09-12-2013, 08:00 AM
Dear Sir,


Please update us if every thing is OK now or not, and let me know if you need any further assistance.