Hello everybody!
I'm trying to do my first easy authomatic code,as my little first prove with Excel VBA, and I have a really big issue trying to configure my excel to modificate an open position.
For example, if the epic goes up, the Stop Loss or the Limit Price, will go up too (automatically).
I've "some" knowledge with VBA, and i thought that this example could be easy, anyone can help please? or say me if it's really difficult to do that?
Thanks.
Hi DAVID
The following links may help:
See the section for PUT of /workingorders/otc/{dealId}
See the section for PUT of /positions/otc/{dealId}
You'lll have to write code to call these endpoints but you can use the existing code as some sort of guide.
Hope that helps.
Chris
Thanks Chris!
I've proved this code, but there's an issue, i've been trying all different possibilities, used the existing code in the excel as a guide, but there're no good results with this.
I hope somebody can find the errors:
Private Sub editPositionButton_Click()
If Not m_loggedIn Then
MsgBox "Please login First!"
Exit Sub
End If
Dim dealId As String
Dim stopLevel As String
Dim limitLevel As String
Dim trailingStop As String
Dim dealReference As String
Dim count As Integer
dealId = "DIAAAAAQBA5F9A7"
stopLevel = "16850"
limitLevel = "18000"
trailingStop = "False"
dealReference = restClient.editPosition(dealId, stopLevel, limitLevel, trailingStop) (**In this line marks the error**)
If dealReference <> "" Then
appendActivity "Deal submitted: " + dealReference
dealId = ""
count = 0
Do While dealId = "" And count < 3
Application.Wait (Now + TimeValue("0:00:01"))
dealId = restClient.dealConfirmation(dealReference)
count = count + 1
Loop
If dealId <> "" Then
dealId = restClient.dealConfirmation(dealReference)
End If
If dealId = "" Then
appendActivity "Failed to retrieve deal confirmation"
Else
appendActivity "Deal confirmation: " + dealId
End If
End If
End Sub
Public Function editPosition(dealId, stopLevel As String, limitLevel As String, trailingStop As String) As String
'Modify position API
Call oXMLHTTP.Open("PUT", IG_API_HOST + "/positions/otc/{dealId}", False)
Dim requestBodyDictionary As Dictionary
Set requestBodyDictionary = New Dictionary
Call requestBodyDictionary.Add("dealId", dealId)
Call requestBodyDictionary.Add("stopLevel", stopLevel)
Call requestBodyDictionary.Add("limitLevel", limitLevel)
Call requestBodyDictionary.Add("trailingStop", trailingStop)
Dim requestBodyString As Variant
requestBodyString = JSON.toString(requestBodyDictionary)
Call oXMLHTTP.SetRequestHeader("X-SECURITY-TOKEN", m_accountToken)
Call oXMLHTTP.SetRequestHeader("CST", m_clientToken)
Call oXMLHTTP.SetRequestHeader("Content-Type", "applicaction/json; charset=utf-8")
Call oXMLHTTP.SetRequestHeeader("Accept", "application/json; charset=utf-8")
Call oXMLHTTP.sent(requestBodyString)
Dim responseData As Dictionary
Set responseData = JSON.parse(oXMLHTTP.responseText)
If oXMLHTTP.Status = 200 Then
editPosition = responseData.Item("dealReference")
Else
[APIMsg] = oXMLHTTP.responseText
editPosition = ""
End If
End Function
After correct some mistakes in the Public Function, i'm still having the same issue in the Private Function.
Anybody knows where is the problem?? And how to solve it??
I copy the Public Function:
Call oXMLHTTP.Open("PUT", IG_API_HOST + "/positions/otc/" + dealId, False)
Call oXMLHTTP.SetRequestHeader("_method", "PUT")
Dim requestBodyDictionary As Dictionary
Set requestBodyDictionary = New Dictionary
Call requestBodyDictionary.Add("dealId", dealId)
Call requestBodyDictionary.Add("stopLevel", stopLevel)
Call requestBodyDictionary.Add("limitLevel", limitLevel)
Dim requestBodyString As Variant
requestBodyString = JSON.toString(requestBodyDictionary)
Call oXMLHTTP.SetRequestHeader("X-SECURITY-TOKEN", m_accountToken)
Call oXMLHTTP.SetRequestHeader("CST", m_clientToken)
Call oXMLHTTP.SetRequestHeader("X-IG-API-KEY", m_apiKey)
Call oXMLHTTP.SetRequestHeader("Content-Type", "application/json; charset=utf-8")
Call oXMLHTTP.SetRequestHeader("Accept", "application/json; charset=utf-8")
Call oXMLHTTP.sent(requestBodyString)
Dim responseData As Dictionary
Set responseData = JSON.parse(oXMLHTTP.responseText)
If oXMLHTTP.Status = 200 Then
editPosition = responseData.Item("deMalReference")
Else
[APIMsg] = oXMLHTTP.responseText
editPosition = ""
End If
End Function
Hi DAVID,
We can see that responseData.Item("deMalReference") has a typo (should be "dealReference").
Would it be possible to set a breakpoint, debug the code and let us know where and how it is failing please?
Chris
Hi Chris, Thanks for the help!
Yes i saw this typo, but this is not the problem.
The problem comes in this line:
dealReference = restClient.editPosition(dealId, limitLevel, stopLevel)
And the error message is:
"Error 438 en tiempo de ejecución: El objeto no admite esta propiedad o método"
"Runtime Error 438 : Object does not support this property or method"
Hi David,
Have you defined editPosition() as a method of IGApiRestClient? Or is it defined as a global method in the sheet?
Hi Belfort, Thanks for the help.
I defined editPosition() in the IGApiRestClient.
Did you try specifying the type of the first parameter?
Public Function editPosition(dealId As String, stopLevel As String, limitLevel As String, trailingStop As String) As String
Yes, in the code i wrote as you say.
'Edit position API
Public Function editPosition(dealId As String, stopLevel As String, limitLevel As String, trailingStop As String) As String
Call oXMLHTTP.Open("PUT", IG_API_HOST + "/positions/otc/" + dealId, False)
Dim requestBodyDictionary As Dictionary
Set requestBodyDictionary = New Dictionary
Call requestBodyDictionary.Add("dealId", dealId)
Call requestBodyDictionary.Add("stopLevel", stopLevel)
Call requestBodyDictionary.Add("limitLevel", limitLevel)
Call requestBodyDictionary.Add("trailingStop", trailingStop)
Dim requestBodyString As Variant
requestBodyString = JSON.toString(requestBodyDictionary)
Call oXMLHTTP.SetRequestHeader("X-IG-API-KEY", m_apiKey)
Call oXMLHTTP.SetRequestHeader("X-SECURITY-TOKEN", m_accountToken)
Call oXMLHTTP.SetRequestHeader("CST", m_clientToken)
Call oXMLHTTP.SetRequestHeader("Content-Type", "application/json; charset=utf-8")
Call oXMLHTTP.SetRequestHeader("Accept", "application/json; charset=utf-8")
Call oXMLHTTP.sent(requestBodyString)
Dim responseData As Dictionary
Set responseData = JSON.parse(oXMLHTTP.responseText)
If oXMLHTTP.Status = 200 Then
editPosition = responseData.Item("dealReference")
Else
[APIMsg] = oXMLHTTP.responseText
editPosition = ""
End If
End Function
The problem can be something related with the dealId??
Any progress on this? I can edit stop and limit values via the API companion, but can't get them to work via the Excel interface.
Here's the code:
Public Function editPosition(dealId As String, stopLevel As String, limitLevel As String, trailingstop As String) As String
Dim trailingStopDistance As String
Dim trailingStopIncrement As String
Dim look As String
Dim count As Integer
Call oXMLHTTP.Open("PUT", IG_API_HOST + "/positions/otc/" + dealId, False)
Call oXMLHTTP.SetRequestHeader("_method", "PUT")
Dim requestBodyDictionary As Dictionary
Set requestBodyDictionary = New Dictionary
Call requestBodyDictionary.Add("limitLevel", limitLevel)
Call requestBodyDictionary.Add("stopLevel", stopLevel)
Call requestBodyDictionary.Add("trailingStop", "false")
Call requestBodyDictionary.Add("trailingStopDistance", Null)
Call requestBodyDictionary.Add("trailingStopIncrement", Null)
Dim requestBodyString As Variant
requestBodyString = JSON.toString(requestBodyDictionary)
Call oXMLHTTP.SetRequestHeader("X-SECURITY-TOKEN", m_accountToken)
Call oXMLHTTP.SetRequestHeader("CST", m_clientToken)
Call oXMLHTTP.SetRequestHeader("X-IG-API-KEY", m_apiKey)
Call oXMLHTTP.SetRequestHeader("Content-Type", "application/json; charset=utf-8")
Call oXMLHTTP.SetRequestHeader("Accept", "application/json; charset=utf-8")
Call oXMLHTTP.send(requestBodyString)
Dim responseData As Dictionary
Set responseData = JSON.parse(oXMLHTTP.responseText)
If oXMLHTTP.Status = 200 Then
editPosition = responseData.Item("dealReference")
Else
MsgBox oXMLHTTP.responseText
editPosition = ""
End If
End Function
Anyone got any ideas?
The (not very helpful) error message is {"error code":"invalid input"}
I'm testing it on a current demo sell trade. The body is {"limitLevel":"5011","stopLevel":"6011","trailingStop":"false","trailingStopDistance":null,"trailingStopIncrement":null}