Quantcast
Channel: Market Calls
Viewing all articles
Browse latest Browse all 2070

Algomojo – Sending Simultaneous Orders to Multi Clients – ExcelSheet Module and VBA Code

$
0
0

Got frequent requirements from a couple of traders to automate Excel based button trading where with a click of a button one could punch orders in multiple accounts (friends & family).

Designed a simple interface in excel on sending simultaneous orders from the excel application to multiple trading accounts. Code uses algomojo API to transmit multiple orders using the PlaceMultiOrder function.

Supported Broker : Angel Broking
Requirements : Algomojo Login

Download the Excel File

Steps to follow after downloading the Excel File
1)Download the File and Unzip it to the local folder
2)Press Enable Content button as the Security Warning display that Macros have been disabled
3)Sheet Place Order is used to Send MultiOrders
4)Sheet Clients is used to manage multiple clients, API Keys , API Secret Keys

Here are the supported order parameters in the excel sheets

ParamValueDescription
varietyNORMAL
STOPLOSS
AMO
ROBO
Normal Order (Regular)
Stop-loss order,
After Market Order,
ROBO (Bracket Order)
transactiontypeBUY
SELL
Buy
Sell
ordertypeMARKET
LIMIT
STOPLOSS_LIMIT
STOPLOSS_MARKET
Market Order(MKT)
Limit Order(L)
Stop Loss Limit Order(SL)
Stop Loss Market Order(SL-M)
producttypeDELIVERY
CARRYFORWARD
MARGIN
INTRADAY
BO
Cash & Carry for equity (CNC)
Normal for futures and options (NRML)
Margin Delivery
Margin Intraday Square off (MIS)
Bracket Order (Only for ROBO)
DurationDAY
IOC
Regular Order
Immediate or Cancel
exchangeBSE
NSE
NFO
MCX
BSE Equity
NSE Equity
NSE Future and Options
MCX Commodity

Order Parameters

These parameters are common across different order varieties.

ParamDescription
tradingsymbolTrading Symbol of the instrument
symboltokenSymbol Token is unique identifier ( can be obtained from Algomojo Watchlist section->Security Info)
ExchangeName of the exchange (BSE,NSE,NFO,MCX)
transactiontypeBUY or SELL
ordertypeOrder type (MARKET, LIMIT etc.)
quantityQuantity to transact
producttypeProduct type (CNC,MIS)
priceThe min or max price to execute the order at (for LIMIT orders)
triggerpriceThe price at which an order should be triggered (SL, SL-M)
squareoffOnly For ROBO (Bracket Order)
stoplossOnly For ROBO (Bracket Order)
trailingStopLossOnly For ROBO (Bracket Order)
disclosedquantityQuantity to disclose publicly (for equity trades)
durationOrder duration (DAY,IOC)

VBA Source Code

Sub FetchLogin()

Dim Broker As String
Dim Version As String
Dim apikey As String
Dim apisecret As String
Dim apidata As String
Dim numofRows As Integer
Dim i As Integer

Broker = "an"
Version = "1.0"

numofRows = Range("A5", Range("A5").End(xlDown)).Rows.Count
apidata = "{}"

For i = 0 To numofRows - 1
    apikey = Sheets("Clients").Cells(5 + i, "B").Value
    apisecret = Sheets("Clients").Cells(5 + i, "C").Value
    Limits = AMConnect(apikey, apisecret, Broker, Version, "Limits", apidata)
    Profile = AMConnect(apikey, apisecret, Broker, Version, "Profile", apidata)
    Set ProfileResponse = JsonConverter.ParseJson(Profile)
    Set LimitsResponse = JsonConverter.ParseJson(Limits)
    
    If StrComp(ProfileResponse("message"), "SUCCESS") = 0 Then
        Sheets("Clients").Cells(5 + i, "D") = ProfileResponse("data")("name")
        Sheets("Clients").Cells(5 + i, "E") = LimitsResponse("data")("net")
        Sheets("Clients").Cells(5 + i, "F") = LimitsResponse("data")("availablecash")
    Else
        Sheets("Clients").Cells(5 + i, "D") = "Login Error"
        Sheets("Clients").Cells(5 + i, "E") = "Login Error"
        Sheets("Clients").Cells(5 + i, "F") = "Login Error"
    End If
    
Next
End Sub




Sub AlgomojoMulitOrder()


Dim Broker As String
Dim Version As String
Dim ClientID As String
Dim user_apikey As String
Dim api_secret As String
Dim masterapikey As String
Dim masterapisecret As String
Dim variety As String
Dim tradingsymbol As String
Dim symboltoken As String
Dim transactiontype As String
Dim exchange As String
Dim ordertype As String
Dim producttype As String
Dim duration As String
Dim price As String
Dim squareoff As String
Dim stoploss As String
Dim quantity As String
Dim triggerprice As String
Dim trailingStopLoss As String
Dim disclosedquantity As String

Dim apidata As String
Dim numofRows As Long
Dim i As Integer

Broker = Sheets("PlaceOrder").Cells(5, "A").Value
Version = Sheets("PlaceOrder").Cells(5, "B").Value
masterapikey = Sheets("PlaceOrder").Cells(5, "D").Value
masterapisecret = Sheets("PlaceOrder").Cells(5, "E").Value
tradingsymbol = Sheets("PlaceOrder").Cells(5, "F").Value
symboltoken = Sheets("PlaceOrder").Cells(5, "G").Value
quantity = Sheets("PlaceOrder").Cells(5, "H").Value
transactiontype = Sheets("PlaceOrder").Cells(5, "I").Value
exchange = Sheets("PlaceOrder").Cells(5, "J").Value
variety = Sheets("PlaceOrder").Cells(5, "K").Value
ordertype = Sheets("PlaceOrder").Cells(5, "L").Value
producttype = Sheets("PlaceOrder").Cells(5, "M").Value
duration = Sheets("PlaceOrder").Cells(5, "N").Value
price = Sheets("PlaceOrder").Cells(5, "O").Value
triggerprice = Sheets("PlaceOrder").Cells(5, "P").Value
squareoff = Sheets("PlaceOrder").Cells(5, "Q").Value
stoploss = Sheets("PlaceOrder").Cells(5, "R").Value
trailingStopLoss = Sheets("PlaceOrder").Cells(5, "S").Value
disclosedquantity = Sheets("PlaceOrder").Cells(5, "T").Value

'clear the table contents of the display output
Worksheets("PlaceOrder").Range("C16:F200").Clear

Sheets("Clients").Activate
numofRows = Sheets("Clients").Range("A5", Range("A5").End(xlDown)).Rows.Count
apidata = "{""" & "orders" & """:" & "[ "

stgy_name = "Excel"


    
For i = 0 To numofRows - 1
    Sheets("Clients").Activate
    ClientID = Sheets("Clients").Cells(5 + i, "A").Value
    user_apikey = Sheets("Clients").Cells(5 + i, "B").Value
    api_secret = Sheets("Clients").Cells(5 + i, "C").Value
    Sheets("PlaceOrder").Activate
    Sheets("PlaceOrder").Cells(16 + i, "C") = ClientID
    apidata = apidata & "{""" _
                & "order_refno" & """:""" & CStr(i + 1) & """,""" _
                & "user_apikey" & """:""" & user_apikey & """,""" _
                & "api_secret" & """:""" & api_secret & """,""" _
                & "stgy_name" & """:""" & stgy_name & """,""" _
                & "variety" & """:""" & variety & """,""" _
                & "tradingsymbol" & """:""" & tradingsymbol & """,""" _
                & "symboltoken" & """:""" & symboltoken & """,""" _
                & "transactiontype" & """:""" & transactiontype & """,""" _
                & "exchange" & """:""" & exchange & """,""" _
                & "ordertype" & """:""" & ordertype & """,""" _
                & "producttype" & """:""" & producttype & """,""" _
                & "duration" & """:""" & duration & """,""" _
                & "price" & """:""" & price & """,""" _
                & "squareoff" & """:""" & squareoff & """,""" _
                & "stoploss" & """:""" & stoploss & """,""" _
                & "quantity" & """:""" & quantity & """,""" _
                & "triggerprice" & """:""" & triggerprice & """,""" _
                & "trailingStopLoss" & """:""" & trailingStopLoss & """,""" _
                & "disclosedquantity" & """:""" & disclosedquantity
                
    If i = numofRows - 1 Then
        apidata = apidata & """}"
    Else
        apidata = apidata & """},"
    End If
       
                
Next
    
apidata = apidata & "]}"



PlaceOrder = AMConnect(masterapikey, masterapisecret, Broker, Version, "PlaceMultiOrder", apidata)
Set PlaceOrderResponse = JsonConverter.ParseJson(PlaceOrder)



For i = 0 To numofRows - 1
     If StrComp(PlaceOrderResponse(i + 1)("message"), "SUCCESS") = 0 Then
        Sheets("PlaceOrder").Cells(16 + i, "D").Value = PlaceOrderResponse(i + 1)("message")
        Sheets("PlaceOrder").Cells(16 + i, "E").Value = PlaceOrderResponse(i + 1)("data")("script")
        Sheets("PlaceOrder").Cells(16 + i, "F").Value = PlaceOrderResponse(i + 1)("data")("orderid")
        Sheets("PlaceOrder").Cells(16 + i, "F").NumberFormat = "0"
    Else
        Sheets("PlaceOrder").Cells(16 + i, "D") = "Login Error"
        Sheets("PlaceOrder").Cells(16 + i, "E") = "Login Error"
        Sheets("PlaceOrder").Cells(16 + i, "F") = "Login Error"
    End If
    
Next

'check multiorder response
'Sheets("PlaceOrder").Cells(12, "D") = PlaceOrder

End Sub




Private Function AMConnect(api_key As String, api_secret As String, Broker As String, Version As String, api_name As String, InTD As String) As String
    Dim objHTTP As Object
    Dim result As String
    Dim postdate As String
    Dim BrkPrefix As String
    Dim BaseURL As String
    BaseURL = "https://" & LCase(Broker) & "api.algomojo.com/" & Version & "/"
    postdata = "{""" & "api_key" & """:""" & api_key & """,""" _
                    & "api_secret" & """:""" & api_secret & """,""" _
                    & "data" & """:" & InTD & "}"
    'MsgBox (postdata)
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    Url = BaseURL & api_name
    objHTTP.Open "POST", Url, False
    objHTTP.setRequestHeader "Content-type", "application/json"
    objHTTP.send (postdata)
    result = objHTTP.responseText
    Set objHTTP = Nothing
    AMConnect = result
    
End Function


Credits

VBA Code uses VBA-JSON for Json Processing

The post Algomojo – Sending Simultaneous Orders to Multi Clients – ExcelSheet Module and VBA Code appeared first on Marketcalls.


Viewing all articles
Browse latest Browse all 2070

Trending Articles