+ Reply to Thread
Results 1 to 17 of 17

Get last price from WazirX exchange and automate trades

  1. #1
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Get last price from WazirX exchange and automate trades

    Hello friends,

    I am trying to automate my crypto trades on WazirX exchange via Excel VBA. I do not have much knowledge of APIs and with my research, I came up with a code to fetch last price of symbol "wrxusdt". I have added reference to Microsoft Scripting Runtime in the VBA editor.

    I downloaded a JsonConverter.bas package to parse json from this link but somehow I am not able to fetch the last price. I am also trying to find how to post buy and sell orders automatically through excel vba based on my condition/strategy so I went through the documentation but after trying a lot I am not getting the proper syntax to work. Please help !

    Here is the Documentation
    Here is API Sample
    Please Login or Register  to view this content.
    Last edited by sabha; 06-18-2021 at 02:16 AM. Reason: to mark solved

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Hi, try this !


    Without any json parser just with some VBA basics text function :

    PHP Code: 
    Sub DemoReq1()
        
    With CreateObject("WinHttp.WinHttpRequest.5.1")
            .
    Open "GET""https://api.wazirx.com/uapi/v1/tickers/24hr"False
            
    .setRequestHeader "DNT""1"
             
    On Error Resume Next
            
    .send
             
    If .Status 200 Then V Split(.responseText"{""symbol"":""wrxusdt""")
        
    End With
             On Error 
    GoTo 0
        
    If IsArray(VThen
            
    If UBound(VThen
                V 
    Split(Split(V(1), "}")(0), """lastPrice"":""")
                If 
    UBound(VThen MsgBox Val(V(1))
            
    End If
        
    End If
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Get last price from WazirX exchange and automate trades

    Wonderful ! thank you so much !!
    I kept my symbol in range A1. Declared a string variable "symbol" to hold the value of A1 and then changed the hardcoded wrxusdt with this symbol variable but it does not fetch the value. I removed the double quotes, tried contactinating with & ampersand but still doesn't work. Can you pls tell me the correct syntax?
    Also, as per the documentation, what is the correct syntax to post my buy and sell orders if the lastPrice rise or falls above or below my price which is set in cell B1?

    I found the rest api documentaion here which has all the endpoints explained with query strings and commands but i am unable to execute it due to unawareness of syntax to be used.

    Thanks for looking into it.
    Last edited by sabha; 06-06-2021 at 05:58 AM.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get last price from WazirX exchange and automate trades


    Thanks for the rep' !

    Quote Originally Posted by sabha View Post
    I kept my symbol in range A1.
    Try If .Status = 200 Then V = Split(.responseText, "{""symbol"":""" & [A1].Text & """")

  5. #5
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Get last price from WazirX exchange and automate trades

    Great ! thanks.... appreciate if you could extend your assistance for my other question of posting buy and sell calls asked in my previous post based on the rest api documentation link I gave.

    Trade points are explained in the last section of this page under accounts endpoints
    https://wazirx.github.io/#account-endpoints

    I tried this
    https://api.wazirx.com/uapi/v1/order...amp=1622980000
    but got the below error
    {"code":2098,"message":"Request out of receiving window."}
    Last edited by sabha; 06-06-2021 at 08:30 AM.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get last price from WazirX exchange and automate trades


    As for some reason I can't use this API way so I can't help further …

  7. #7
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Get last price from WazirX exchange and automate trades

    Quote Originally Posted by Marc L View Post

    As for some reason I can't use this API way so I can't help further …
    Thank you for looking into my query. Its absolutely fine... I am trying to manage the trade endpoints somehow. Meanwhile, I have something to ask you.

    In the above code we took only 1 symbol wrxusdt and placed it on A1. In case if I have 3 symbols (wrxusdt, maticusdt & bnbusdt) and place it on A1 B1 & C1 then do we have to make three different winhttp objects or can it be done within one object putting them in some loop? I tried to do something myself but my logic doesn't seem to be correct.

    Also i am looking at putting these three symbols in an infinite FOR loop where new value is retrieved after every 5 seconds. I will write my condition in every loop to stop individual loops when the condition is met. For this example, can you please help me modifying the code as per my requirement and have a temporary condition that the first loop ends after 5 seconds, second in 10 seconds and third in 15 seconds.

    Thanks

    PS: I also wanted to understand what is "DNT" in this line .setRequestHeader "DNT"?

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this …


    My previous VBA demonstration revamped :

    PHP Code: 
    Sub DemoReq1r()
        
    With CreateObject("WinHttp.WinHttpRequest.5.1")
            .
    Open "GET""https://api.wazirx.com/uapi/v1/tickers/24hr"False
            
    .setRequestHeader "DNT""1"
             
    On Error Resume Next
            
    .send
             
    If .Status 200 Then T$ = .responseText
        End With
             On Error 
    GoTo 0
        
    If "" Then
                V 
    = [{"wrxusdt","maticusdt","bnbusdt"}]
            For 
    C% = 1 To UBound(V)
                    
    Split(T"{""symbol"":""" V(C) & """")
                If 
    UBound(WThen
                    W 
    Split(Split(W(1), "}")(0), """lastPrice"":""")
                    If 
    UBound(WThen V(C) = Val(W(1))
                
    End If
            
    Next
                
    [A1:C1].Value2 V
        
    Else
            [
    A1:C1].ClearContents:  Beep
        End 
    If
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Get last price from WazirX exchange and automate trades

    Thank you for considering my request. Your newer version looks little difficult for me to understand but I will certainly try to understand the logic behind the code. However, I cannot do it until I see the output. While running the code, it gives me error Variable not found on line T$=
    What type of vairable should i declare for this?
    I already have WinHTTP and Scripting Runtime in my references.

    EDIT - My excel version is 2007
    EDIT - I am assuming that it may even give error for C% after T$ is resolved. If you could tell me variable types to declare

    Also, in the previous demo, you suggested to add V = Split(.responseText, "{""symbol"":""" & [A1].Text & """") if I want to take values from a specific cell and I was getting the values in G1 like this If UBound(V) Then Range("G1").Value = Val(V(1)).

    In this demo, instead of hardcoding it like V = [{"wrxusdt","maticusdt","bnbusdt"}], its better to look it up from the worksheet. These three actual values are in A1, C1 and E1 and I want to get those value below them in A2 C2 & E2 instead of [A1:C1].Value2 = V (sorry about that, for some formatting requirements, I had to move A1 B1 C1 to A1 C1 E1)

    Thanks

    PS: I did not understand the usage of .setRequestHeader "DNT". Where should I lookup for more info on it?
    Last edited by sabha; 06-10-2021 at 07:29 AM.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get last price from WazirX exchange and automate trades


    If you have enabled Option Explicit instruction you can remove it or you must declare each variable like Dim T$, C% for example …

    As you can take a glance to the VBE Locals window during the execution in order to check each variable data type …

    As my demonstration uses late binding so do not activate any reference or rewrite the code in order to not use any CreateObject statement …

  11. #11
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Get last price from WazirX exchange and automate trades

    Thanks for all your help !
    It took me some time to completely understand the logic behind your code and how efficiently it was written. I also learnt about VBE Locals window which i never knew about. Thanks for that too...

    I have one last query before I marked this thread as solved !

    Referring to this line
    Please Login or Register  to view this content.
    I wish to keep it dynamic instead of hardcoded.

    In my actual file, I have the three symbols in cells D8, F8 and H8. The code should be smart enough to generate a dynamic query string that will be passed to the V variant.
    If only the D8 cell has a value then the query should be [{"wrxusdt"}] that goes into the V variable
    If two of them are filled D8 & F8 then it should be [{"wrxusdt","maticusdt"}] else [{"wrxusdt","maticusdt","bnbusdt"}] if all cells (D8 F8 & H8) has a value
    with a condition that if the first one D8 is blank and others are filled then it should remind me with a msgbox "First symbol is empty" and once OK is clicked, it should execute the rest of the code without exiting the for loop.
    Similarly, if any other cell out of D8, F8, H8 is blank then the same condition should be checked.

    I have tried different combination of double-quotes looking at the VBE Locals window but either it errors out or it includes quotes due to which the array V does not hold the proper value.

    May I request your assistance on this please?

    Thanks

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Ultimate revision …


    As the initial question is solved so this is my last code here and
    as important informations must be in the initial post in order to avoid a never ending story
    as hidding important informations may lead to a less efficient procedure, somewhat a gas factory code …

    PHP Code: 
    Sub DemoReq1r2d2()
        
    Dim VT$, C%, WX()
            [
    A1:C1].ClearContents
            V 
    Filter(Application.Index([IF(D8:H8>"",D8:H8)], 1, [{1,3,5}]), FalseFalse)
            If 
    UBound(V) = -1 Then Beep: Exit Sub
            ReDim X
    (UBound(V))
        
    With CreateObject("WinHttp.WinHttpRequest.5.1")
            .
    Open "GET""https://api.wazirx.com/uapi/v1/tickers/24hr"False
            
    .setRequestHeader "DNT""1"
             
    On Error Resume Next
            
    .send
             
    If .Status 200 Then T = .responseText
        End With
             On Error 
    GoTo 0
        
    If "" Then
            
    For 0 To UBound(V)
                    
    Split(T"{""symbol"":""" V(C) & """")
                If 
    UBound(WThen
                    W 
    Split(Split(W(1), "}")(0), """lastPrice"":""")
                    If 
    UBound(WThen X(C) = Val(W(1))
                
    End If
            
    Next
                
    [A1].Resize(, UBound(X) + 1).Value2 X
        
    Else
            
    Beep
        End 
    If
    End Sub 

  13. #13
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Hi, try this !

    Quote Originally Posted by Marc L View Post
    [INDENT]
    Without any json parser just with some VBA basics text function :
    Hello! could you please help me once again to convert this sub routine into a function? lets say A1 has the symbol name and in cell B1 if I write =DemoReq1(A1,lastprice), it should show me the last price of the symbol in A1. I tried myself but stuck at syntax on passing value to the fuction and fetching that value to add it in the array calculations. Thanks

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Get last price from WazirX exchange and automate trades


    No as within an UDF it may crash Excel …

  15. #15
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Get last price from WazirX exchange and automate trades

    Oh! I am assuming the reason would be a large response text to handle every time on every calculation activity in excel.

    Do you think it is a better idea to scrape it within an UDF and return the last price and change percentage?

    I am now trying to scrape these 2 values (change percentage & last price) from this web url (screenshot below) instead of api response text.
    I tried it with some class names and XPATH given below but not getting to scrape the values. If you think it is possible to get it via UDF and if I can get some help here on this forum then I will start a new thread. Please advice. Thanks
    class="sc-bwzfXH jsJuLQ"
    class="sc-bwzfXH jaArUU"
    class="price-box "
    /html/body/div/div/div[2]/div[2]/div[1]/div/div[2]/a[6]/div[2]/div[2]/span/text()[2]
    //*[@id="root"]/div/div[2]/div[2]/div[1]/div/div[2]/a[6]/div[2]/div[2]/span/text()[2]
    WX.png

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Get last price from WazirX exchange and automate trades


    Yes it's better to open a new thread as this one is yet solved and
    in order other helpers may show you other way like Excel web services, Power Query, …

  17. #17
    Registered User
    Join Date
    10-17-2021
    Location
    London
    MS-Off Ver
    2007
    Posts
    1

    Re: Get last price from WazirX exchange and automate trades

    [QUOTE=
    {"code":2098,"message":"Request out of receiving window."}[/QUOTE]

    Hi Sabha, could you please tell how did you solve this error?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2020, 05:29 AM
  2. How to automate weekly price updates from diff. suppliers into 1 price comparison sheet
    By blindside21 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-09-2014, 02:24 PM
  3. Replies: 3
    Last Post: 01-03-2013, 12:32 AM
  4. Replies: 0
    Last Post: 10-28-2012, 07:22 AM
  5. Replies: 4
    Last Post: 08-15-2012, 09:49 AM
  6. How many trades are open
    By censura10 in forum Excel General
    Replies: 1
    Last Post: 09-22-2009, 04:45 PM
  7. Trades Reconciliation
    By iguss in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2008, 04:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1