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 !
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(V) Then If UBound(V) Then V = Split(Split(V(1), "}")(0), """lastPrice"":""") If UBound(V) Then 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 » ! ◄ ◄
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.
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.
Re: Get last price from WazirX exchange and automate trades
Originally Posted by Marc L
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"?
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 T > "" Then V = [{"wrxusdt","maticusdt","bnbusdt"}] For C% = 1 To UBound(V) W = Split(T, "{""symbol"":""" & V(C) & """") If UBound(W) Then W = Split(Split(W(1), "}")(0), """lastPrice"":""") If UBound(W) Then 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 » ! ◄ ◄
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?
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 !
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.
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 V, T$, C%, W, X() [A1:C1].ClearContents V = Filter(Application.Index([IF(D8:H8>"",D8:H8)], 1, [{1,3,5}]), False, False) 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 T > "" Then For C = 0 To UBound(V) W = Split(T, "{""symbol"":""" & V(C) & """") If UBound(W) Then W = Split(Split(W(1), "}")(0), """lastPrice"":""") If UBound(W) Then X(C) = Val(W(1)) End If Next [A1].Resize(, UBound(X) + 1).Value2 = X Else Beep End If End Sub
[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
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
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, …
Bookmarks