OpenAPI for Excel

This tutorial builds on the previous one and further expands the functionality of the bulk trader by introducing timed durations ("GoodTillDate") and related orders. The goal is to cover a use case where the user is looking to place more than a simple market, limit, or stop order. Instead, we can now:

  • Precisely control for how long our orders will stay active
  • Add risk controls in the form of related stop loss orders
  • Automatically take profit on a position if the market moves in our favor

To use this sheet, follow these steps:

  1. Refresh the account overview and clear the order list by clicking "Reset & Load Accounts". The top account will be used to place these trades on.
  2. Enter order details as per instructions below. All fields are mandatory except for the price fields.
  3. Make sure to enter the data according to the specifications. Click "Send Orders" to start bulk placement.

Criteria for each order:

  • Enter the UIC, Symbol, and AssetType of the instrument you are looking to place.
  • Buy/Sell: Use only exact values "Buy" and "Sell".
  • Duration:  "GoodTillCancel",  "DayOrder",  or a date in the format "2019-08-30" (expires at end of day). Other formats will not work.
  • Take Profit:  Enter price level similar to "Price" column (duration will be GTC)
  • Stop Loss:  Enter price level similar to "Price" column (duration will be GTC)

The final result looks as below:

VBA solution

Constructing a valid order body to be posted to the trade/v1/orders endpoint is by far the most challenging part of this tutorial. Because of the number of different combinations, the structure of this body changes. This is the VBA that generates the correct orders depending on user input:

If Range("I" & curline) <> "" Or Range("J" & curline) <> "" Then ' if take profit or stop loss price entered
            
            If Range("D" & curline) = "Buy" Then
                relatedbuysell = "Sell"
            ElseIf Range("D" & curline) = "Sell" Then
                relatedbuysell = "Buy"
            Else
                Range("K" & curline) = "Buy/Sell column has incorrect data."
                Range("K" & curline).Style = "Bad"
            End If
            
            If Range("B" & curline) = "Stock" Or Range("B" & curline) = "CfdOnStock" Or Range("B" & curline) = "CfdOnFutures" Or Range("B" & curline) = "CfdOnIndex" Then
                stoptype = "StopIfTraded"
            Else
                stoptype = "Stop"
            End If
            
            ' if only take profit is added
            If Range("I" & curline) <> "" And Range("J" & curline) = "" Then
            
                takeprofit = _
                    "'AccountKey':'" & accountkey & "', " & _
                    "'Amount':" & Range("E" & curline).Value & ", " & _
                    "'AssetType':'" & Range("B" & curline).Text & "', " & _
                    "'BuySell':'" & relatedbuysell & "', " & _
                    "'Uic':" & Range("C" & curline).Value & ", " & _
                    "'OrderType':'Limit', " & _
                    "'OrderPrice':" & Range("I" & curline).Value & ", " & _
                    "'OrderDuration':{'DurationType':'GoodTillCancel'}, " & _
                    "'ManualOrder':true"
                
                orders = "'orders':[" & "{" & takeprofit & "}" & "]"
            
            ' if only stop loss is added
            ElseIf Range("I" & curline) = "" And Range("J" & curline) <> "" Then
                
                stoploss = _
                    "'AccountKey':'" & accountkey & "', " & _
                    "'Amount':" & Range("E" & curline).Value & ", " & _
                    "'AssetType':'" & Range("B" & curline).Text & "', " & _
                    "'BuySell':'" & relatedbuysell & "', " & _
                    "'Uic':" & Range("C" & curline).Value & ", " & _
                    "'OrderType':' " & stoptype & "', " & _
                    "'OrderPrice':" & Range("J" & curline).Value & ", " & _
                    "'OrderDuration':{'DurationType':'GoodTillCancel'}, " & _
                    "'ManualOrder':true"
                
                orders = "'orders':[" & "{" & stoploss & "}" & "]"
            
            ' if both take profit AND stop loss added
            ElseIf Range("I" & curline) <> "" And Range("J" & curline) <> "" Then
                
                takeprofitstoploss = "{" & _
                    "'AccountKey':'" & accountkey & "', " & _
                    "'Amount':" & Range("E" & curline).Value & ", " & _
                    "'AssetType':'" & Range("B" & curline).Text & "', " & _
                    "'BuySell':'" & relatedbuysell & "', " & _
                    "'Uic':" & Range("C" & curline).Value & ", " & _
                    "'OrderType':'Limit', " & _
                    "'OrderPrice':" & Range("I" & curline).Value & ", " & _
                    "'OrderDuration':{'DurationType':'GoodTillCancel'}, " & _
                    "'ManualOrder':true}," & _
                    "{" & _
                    "'AccountKey':'" & accountkey & "', " & _
                    "'Amount':" & Range("E" & curline).Value & ", " & _
                    "'AssetType':'" & Range("B" & curline).Text & "', " & _
                    "'BuySell':'" & relatedbuysell & "', " & _
                    "'Uic':" & Range("C" & curline).Value & ", " & _
                    "'OrderType':' " & stoptype & " ', " & _
                    "'OrderPrice':" & Range("J" & curline).Value & ", " & _
                    "'OrderDuration':{'DurationType':'GoodTillCancel'}, " & _
                    "'ManualOrder':true}"

                orders = "'orders':[" & takeprofitstoploss & "]"
                
            End If

            
            ' add order(s) to "orders" list in JSON
        End If
        
        completeorder = parentorder & orderduration & "," & orders & "}"