jimmy999 Posted March 24 Share Posted March 24 (edited) expandcollapse popup#Include <Array.au3> #include <String.au3> #include <Json.au3> #include <Binance UDF.au3> #include <MsgBoxConstants.au3> ;----- API Keys ----- Global $sAPI_Key_Access = "xxx" Global $sAPI_Key_Secret = "yyy" ;----- Prepare DLL ----- Global $hDll_WinHTTP = DllOpen("winhttp.dll") _BINANCE_API_Call("api/v3/time") ConsoleWrite($sData & @CRLF) _BINANCE_API_Call("api/v3/ticker/24hr") If @error Then MsgBox(16, "Error", "Failed to fetch data from Binance API.") Exit EndIf Local $aCryptos = json_decode($sData, 1) If @error Then MsgBox(16, 'Error', 'Error decoding JSON response') Exit EndIf Local $aGainers[1][2] = [['Symbol', 'Change %']] ;this part is created with chat gpt For $i = 0 To UBound($aCryptos) - 1 If Number($aCryptos[$i]['priceChangePercent']) > 0 Then _ArrayAdd($aGainers, [$aCryptos[$i]['symbol'], $aCryptos[$i]['priceChangePercent']]) EndIf Next _ArraySort($aGainers, 0, 1, 0, 2, 0) For $i = 1 To 10 ConsoleWrite($aGainers[$i][0] & ' - ' & $aGainers[$i][1] & '%' & @CRLF) Next hello, I want to make a script that will extract the top 10 cryptocurrencies that have grown the most in the last 24 hours using the binance api. I have problems sorting them in an array... please help me the error > "C:\Users\ladro\Desktop\TEST\Binance.au3" (42) : ==> Error in expression.: _ArrayAdd($aGainers, [$aCryptos[$i]['symbol'], $aCryptos[$i]['priceChangePercent']]) _ArrayAdd($aGainers, ^ ERROR >Exit code: 1 the script is bigger, the bought and sold part works. the first time I made it take data from livecoinwatch com with imacros-firefox. but it's faster through api binance data coming from binance - [{"symbol":"ETHBTC","priceChange":"-0.00031000","priceChangePercent":"-0.595","weightedAvgPrice":"0.05197174","prevClosePrice":"0.05213000","lastPrice":"0.05182000","lastQty":"20.06760000","bidPrice":"0.05182000","bidQty":"23.61320000","askPrice":"0.05183000","askQty":"30.12050000","openPrice":"0.05213000","highPrice":"0.05243000","lowPrice":"0.05156000","volume":"24595.97310000","quoteVolume":"1278.29551268","openTime":1711203672778,"closeTime":1711290072778,"firstId":438016051,"lastId":438113829,"count":97779},{"symbol":"LTCBTC","priceChange":"0.00001600","priceChangePercent":"1.192","weightedAvgPrice":"0.00134319","prevClosePrice":"0.00134200","lastPrice":"0.00135800","lastQty":"1.64000000","bidPrice":"0.00135800","bidQty":"2.93700000","askPrice":"0.00135900","askQty":"135.64200000","openPrice":"0.00134200","highPrice":"0.00137700","lowPrice":"0.00131400","volume":"99931.64700000","quoteVolume":"134.22719281","openTime":1711203670214,"closeTime":1711290070214,"firstId":96069410,"lastId":96092177,"count":22768},{"symbol":"BNBBTC","priceChange":"-0.00016800","priceChangePercent":"-1.927","weightedAvgPrice":"0.00860063","prevClosePrice":"0.00871800","lastPrice":"0.00854800","lastQty":"0.75000000","bidPrice":"0.00854600","bidQty":"1.01500000","askPrice":"0.00854700","askQty":"0.51100000","openPrice":"0.00871600","highPrice":"0.00871700","lowPrice":"0.00849800","volume":"26867.77800000","quoteVolume":"231.07994652","openTime":1711203672378,"closeTime":1711290072378,"firstId":237459363,"lastId":237516989,"count":57627},{"symbol":"NEOBTC","priceChange":"0.00000050","priceChangePercent":"0.220","weightedAvgPrice":"0.00023046","prevClosePrice":"0.00022710","lastPrice":"0.00022740","lastQty":"2.80000000","bidPrice":"0.00022720","bidQty":"40.03000000","askPrice":"0.00022750","askQty":"31.85000000","openPrice":"0.00022690","highPrice":"0.00023590","lowPrice":"0.00022540","volume":"19836.53000000","quoteVolume":"4.57156205","openTime":1711203663295,"closeTime":1711290063295,"firstId":46240496,"lastId":46242649,"count":2154},{"symbol":"QTUMETH","priceChange":"0.00001700","priceChangePercent":"1.340","weightedAvgPrice":"0.00129800","prevClosePrice":"0.00127600","lastPrice":"0.00128600","lastQty":"0.90000000","bidPrice":"0.00128700","bidQty":"45.50000000","askPrice":"0.00129000","askQty":"354.40000000","openPrice":"0.00126900","highPrice":"0.00131600","lowPrice":"0.00126700","volume":"5090.00000000","quoteVolume":"6.60683920","openTime":1711203643004,"closeTime":1711290043004,"firstId":5437599,"lastId":5437748,"count":150}, etc... I have also attached the udf files that I use BinaryCall.au3 Json.au3 Binance UDF.au3 Edited March 24 by jimmy999 Link to comment Share on other sites More sharing options...
Developers Jos Posted March 25 Developers Share Posted March 25 10 hours ago, jimmy999 said: [$aCryptos[$i]['symbol'], $aCryptos[$i]['priceChangePercent']] Should be easy as this is invalid syntax as stated by the error! Opening with a square bracket is wrong. jimmy999 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Andreik Posted March 25 Share Posted March 25 Beside invalid syntax it looks like the json is decoded to objects without keys. I don't investigate this further but using this json UDF it work pretty nice: #include <Array.au3> #include <Json.au3> Local $aGainers[1][2] = [['Symbol', 'Change %']] Local $sJSON = '[{"symbol":"ETHBTC","priceChange":"-0.00031000","priceChangePercent":"-0.595","weightedAvgPrice":"0.05197174","prevClosePrice":"0.05213000","lastPrice":"0.05182000","lastQty":"20.06760000","bidPrice":"0.05182000","bidQty":"23.61320000","askPrice":"0.05183000","askQty":"30.12050000","openPrice":"0.05213000","highPrice":"0.05243000","lowPrice":"0.05156000","volume":"24595.97310000","quoteVolume":"1278.29551268","openTime":1711203672778,"closeTime":1711290072778,"firstId":438016051,"lastId":438113829,"count":97779},{"symbol":"LTCBTC","priceChange":"0.00001600","priceChangePercent":"1.192","weightedAvgPrice":"0.00134319","prevClosePrice":"0.00134200","lastPrice":"0.00135800","lastQty":"1.64000000","bidPrice":"0.00135800","bidQty":"2.93700000","askPrice":"0.00135900","askQty":"135.64200000","openPrice":"0.00134200","highPrice":"0.00137700","lowPrice":"0.00131400","volume":"99931.64700000","quoteVolume":"134.22719281","openTime":1711203670214,"closeTime":1711290070214,"firstId":96069410,"lastId":96092177,"count":22768},{"symbol":"BNBBTC","priceChange":"-0.00016800","priceChangePercent":"-1.927","weightedAvgPrice":"0.00860063","prevClosePrice":"0.00871800","lastPrice":"0.00854800","lastQty":"0.75000000","bidPrice":"0.00854600","bidQty":"1.01500000","askPrice":"0.00854700","askQty":"0.51100000","openPrice":"0.00871600","highPrice":"0.00871700","lowPrice":"0.00849800","volume":"26867.77800000","quoteVolume":"231.07994652","openTime":1711203672378,"closeTime":1711290072378,"firstId":237459363,"lastId":237516989,"count":57627},{"symbol":"NEOBTC","priceChange":"0.00000050","priceChangePercent":"0.220","weightedAvgPrice":"0.00023046","prevClosePrice":"0.00022710","lastPrice":"0.00022740","lastQty":"2.80000000","bidPrice":"0.00022720","bidQty":"40.03000000","askPrice":"0.00022750","askQty":"31.85000000","openPrice":"0.00022690","highPrice":"0.00023590","lowPrice":"0.00022540","volume":"19836.53000000","quoteVolume":"4.57156205","openTime":1711203663295,"closeTime":1711290063295,"firstId":46240496,"lastId":46242649,"count":2154},{"symbol":"QTUMETH","priceChange":"0.00001700","priceChangePercent":"1.340","weightedAvgPrice":"0.00129800","prevClosePrice":"0.00127600","lastPrice":"0.00128600","lastQty":"0.90000000","bidPrice":"0.00128700","bidQty":"45.50000000","askPrice":"0.00129000","askQty":"354.40000000","openPrice":"0.00126900","highPrice":"0.00131600","lowPrice":"0.00126700","volume":"5090.00000000","quoteVolume":"6.60683920","openTime":1711203643004,"closeTime":1711290043004,"firstId":5437599,"lastId":5437748,"count":150}]' Local $aCryptos = _JSON_Parse($sJSON) Local $mCurrency For $i = 0 To UBound($aCryptos) - 1 $mCurrency = $aCryptos[$i] If Number($mCurrency['priceChangePercent']) > 0 Then _ArrayAdd($aGainers, $mCurrency['symbol'] & '|' & $mCurrency['priceChangePercent']) EndIf Next _ArrayDisplay($aGainers) jimmy999 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
AspirinJunkie Posted March 25 Share Posted March 25 I would also do some advertising, as the data fits very well for this. With the TableData UDF you can simplify things quite a bit: #include "Json.au3" #include "TableData.au3" Local $sJSON = '[{"symbol":"ETHBTC","priceChange":"-0.00031000","priceChangePercent":"-0.595","weightedAvgPrice":"0.05197174","prevClosePrice":"0.05213000","lastPrice":"0.05182000","lastQty":"20.06760000","bidPrice":"0.05182000","bidQty":"23.61320000","askPrice":"0.05183000","askQty":"30.12050000","openPrice":"0.05213000","highPrice":"0.05243000","lowPrice":"0.05156000","volume":"24595.97310000","quoteVolume":"1278.29551268","openTime":1711203672778,"closeTime":1711290072778,"firstId":438016051,"lastId":438113829,"count":97779},{"symbol":"LTCBTC","priceChange":"0.00001600","priceChangePercent":"1.192","weightedAvgPrice":"0.00134319","prevClosePrice":"0.00134200","lastPrice":"0.00135800","lastQty":"1.64000000","bidPrice":"0.00135800","bidQty":"2.93700000","askPrice":"0.00135900","askQty":"135.64200000","openPrice":"0.00134200","highPrice":"0.00137700","lowPrice":"0.00131400","volume":"99931.64700000","quoteVolume":"134.22719281","openTime":1711203670214,"closeTime":1711290070214,"firstId":96069410,"lastId":96092177,"count":22768},{"symbol":"BNBBTC","priceChange":"-0.00016800","priceChangePercent":"-1.927","weightedAvgPrice":"0.00860063","prevClosePrice":"0.00871800","lastPrice":"0.00854800","lastQty":"0.75000000","bidPrice":"0.00854600","bidQty":"1.01500000","askPrice":"0.00854700","askQty":"0.51100000","openPrice":"0.00871600","highPrice":"0.00871700","lowPrice":"0.00849800","volume":"26867.77800000","quoteVolume":"231.07994652","openTime":1711203672378,"closeTime":1711290072378,"firstId":237459363,"lastId":237516989,"count":57627},{"symbol":"NEOBTC","priceChange":"0.00000050","priceChangePercent":"0.220","weightedAvgPrice":"0.00023046","prevClosePrice":"0.00022710","lastPrice":"0.00022740","lastQty":"2.80000000","bidPrice":"0.00022720","bidQty":"40.03000000","askPrice":"0.00022750","askQty":"31.85000000","openPrice":"0.00022690","highPrice":"0.00023590","lowPrice":"0.00022540","volume":"19836.53000000","quoteVolume":"4.57156205","openTime":1711203663295,"closeTime":1711290063295,"firstId":46240496,"lastId":46242649,"count":2154},{"symbol":"QTUMETH","priceChange":"0.00001700","priceChangePercent":"1.340","weightedAvgPrice":"0.00129800","prevClosePrice":"0.00127600","lastPrice":"0.00128600","lastQty":"0.90000000","bidPrice":"0.00128700","bidQty":"45.50000000","askPrice":"0.00129000","askQty":"354.40000000","openPrice":"0.00126900","highPrice":"0.00131600","lowPrice":"0.00126700","volume":"5090.00000000","quoteVolume":"6.60683920","openTime":1711203643004,"closeTime":1711290043004,"firstId":5437599,"lastId":5437748,"count":150}]' ; parse JSON and convert the structure into a table object $mData = _td_MapsToTable(_JSON_Parse($sJSON)) ; sort data - to do this convert attribute into number type and use this as comparison value _td_Sort($mData, "Number($x.priceChangePercent)", True) ; show data _td_display($mData) jimmy999 and Andreik 1 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now