Try   HackMD

新人資訊

技術-後台類-azure function #8-連上幣安api

對於技術和風險愛好者來說,幣圈的瘋狂事蹟很難不被吸引,大家入圈原因各有不同,個人技術水準還不到參與項目開發,但對於量化交易的開放和便利性則很對味,經過幾個月的探索,逐步摸進了幣安交易所提供的 api,功能強大效率快,一點不囉嗦歡迎上擂台比身手的感覺。基於 azure function + python 賜給我的堅實基礎,讓我可以向外伸出觸角探索世界,就來記錄一下。

在幣安的帳戶管理中點選創建新 api,即刻生成可立即使用。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

搜尋一下相關的 python + binance 竟然出現好幾種 SDK,真是生態蓬勃發展中,我選擇從官網連結的 binance-connector,在 python 環境中執行 pip install binance-connector 後就已經完全準備好了。

簡單下幾行指令,已經確認抓到帳戶資料了,帳戶裡有甚麼幣通通正確抓出。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

確認帳戶資產正確後,要開始交易必須先取得行情報價,因此下一步想要取得即時 K 線資料,經查也是一個簡單的呼叫就行了,幣安對開發者真是太友善了,反而問題出在自己對系統架構的設計,是要用同一個 function 傳回多項資訊 (帳戶餘額 and 即時 K 線),還是分成不同的 function 處理?這當然各有利弊,試了才知道,趁機學習一下傳回包含兩個陣列的複雜 json 物件是否可行。目前採用 dataframe.to_json() 雖然好用,但若要包進另一個 list 則不可行,所以腦筋動到 json.dumps() 身上,試試多層次巢狀物件次否可以運作,竟然一試就成功。如以下 crpto_info 物件 (大括號開頭) 的下一層的 balance 屬性,其值為中括號代表的陣列,如此已經預備好可以包進第二個屬性,用來儲存 K 線資訊。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

將取得 K 線的功能設計成一個子程序,傳回 list of dictionaries,因 dataframe 並不支援 json serialize 所以必須執行 to_dict(),另外 datetime 型態也不行,故暫緩在此轉換格式。成功傳回 json 字串,因為物件結構已經比較複雜了,直接看序列化資料非常痛苦,好在有好用的工具 online json viewer 可幫忙分析成完美的樹狀結構層層展開,真是太棒了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接下來一步一步趨近交易實戰,資訊透明必須先達到,面對密密麻麻的交易紀錄,要看出操作績效真是太難,通常只知淨值劇烈波動,然後無從檢討操作方法,因此我想若是能將交易紀錄標示在 K 線上,應該對檢討績效精進方法有幫助,查一下文件果然也是非常簡單,呼叫 my_trades() 即得,資料已經順利的包進函數的回傳物件裡了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這兩項資料就位後,要有效解讀就必須調用適當的圖表。傳回前端用網頁呈現一定是最方便的了,但我想先用互動式 python 畫畫看,以便快速檢視結果,若效果良好再製作網頁,達到 do anywhere 的境界。用 matplotlib 簡單的線條和標註呈現,買進紅色賣出綠色,效果非常良好,在 1000 小時中的買進賣出價位清楚標示,且可算出淨買賣金額和價格,完全可以表達這 1000 小時的操作績效。如下表示共經過 29 回合的交易,收到 123.81 USDT,結餘 0.5 單位 AR Coin,價格為 -247.62 USDT (得到 0.5 單位 AR Coin 不須花錢,市場還以 247 USDT 價格倒貼我!),以最新市價 31.22 比較,假設如果做對應數量賣出沖銷 0.5 單位,庫存數回歸 1000 小時前的狀態,則可實現損益 139.42 USDT。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

By Newman Chen 2022/3/14

後記:上圖只要十幾二十行的 python code 就可以搞定,其實這樣我自己用就很可以了,但還是手癢想挑戰一下網頁,沒想到這一試就花了超過 10 倍的時間,前端的技術實在太多樣了,但無論如何還是還是把以下的網頁搞定了,過程過於瑣碎實在很難紀錄,我也理解到前端工程師真是很難的職業。也許還會是另一種解法,就是用 python 在後端產生圖表,用靜態圖檔的方式給網頁參照,也許更能利用到 python 的圖表優勢,目前先這樣囉。此工具已經很好用了,先體驗一陣子再說。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

By Newman Chen 2022/3/16

參考資料

https://binance-docs.github.io/apidocs/spot/en/#api-library

http://jsonviewer.stack.hu/