自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用 Python 篩選收益優(yōu)秀的加密貨幣

開發(fā) 后端
我編寫了一個腳本來幫助我了解幾種加密貨幣的歷史表現(xiàn),當我決定只加入前 10 名加密貨幣并看看表現(xiàn)最好的貨幣是哪個。

 [[415776]]

Python中文社區(qū) (ID:python-china)

在市值排名前 10 的加密貨幣中,從純粹的經(jīng)濟角度來看,你認為自 2017 年以來表現(xiàn)最好的加密貨幣是哪一種?

不管你信不信,幣安自己的 BNB 實際上遠遠超過了其他所有加密貨幣。我編寫了一個腳本來幫助我了解幾種加密貨幣的歷史表現(xiàn),當我決定只加入前 10 名加密貨幣并看看表現(xiàn)最好的貨幣是哪個。

在運行腳本之前,我很確定它可能將是 DOGE。所以我坐在這里,等待歷史數(shù)據(jù)下載,以便我的腳本可以繪制一些加密圖表。

腳本運行完畢,結(jié)果出來了,感謝中本聰,這不是 DOGE。哦,等等,這更有趣——它是 BNB。

自 2017 年以來,BNB 已上漲超過 20,000%。

程序能夠為你下載歷史數(shù)據(jù),并分析任意數(shù)量的幣種。如果您想對任意數(shù)量的加密貨幣的收益百分比進行快速比較分析,這很方便。您所需要的只是一些 Python 知識。

編寫加密貨幣分析工具

該代碼也可在 GitHub 上找到。 

  1. https://github.com/CyberPunkMetalHead/crypto-performance-tracker 

首先創(chuàng)建一個文本文件并將其命名為coins.txt。在此文本文件中,放入一些您想要分析的幣種名稱。它們需要包含配對符號,并且每行必須是 1 個貨幣,不能有逗號: 

  1. BTCUSDT  
  2. ETHUSDT  
  3. BNBUSDT 

創(chuàng)建一個 binancedata.py 文件。我們將使用此文件輪詢 Binance API 以獲取我們需要的金融數(shù)據(jù)。由于我們使用的是開放端口,因此不需要 API 密鑰和密碼。

讓我們導(dǎo)入一些依賴項并定義一個空的 Binance 客戶端: 

  1. # needed for the binance API and websockets  
  2. from binance.client import Client  
  3. import csv  
  4. import os  
  5. import time  
  6. from datetime import date, datetime  
  7. client = Client() 

現(xiàn)在讓我們編寫一個函數(shù)來從我們的coins.txt文件中打開和讀取貨幣: 

  1. def get_coins():  
  2.     with open('coins.txt', 'r') as f:  
  3.         coins = f.readlines()  
  4.         coins = [coin.strip('\n') for coin in coins]  
  5.     return coins 

此文件中的最后一個函數(shù)將為我們獲取歷史數(shù)據(jù)并以 CSV 格式保存: 

  1. def get_historical_data(coin, since, kline_interval):  
  2.     """  
  3.     Args example:  
  4.     coin = 'BTCUSDT'  
  5.     since = '1 Jan 2021'  
  6.     kline_interval = Client.KLINE_INTERVAL_1MINUTE 
  7.     """  
  8.     if os.path.isfile(f'data/{coin}_{since}.csv'):  
  9.         print('Datafile already exists, loading file...')  
  10.     else:  
  11.         print(f'Fetching historical data for {coin}, this may take a few minutes...')  
  12.         start_time = time.perf_counter()  
  13.         data = client.get_historical_klines(coin, kline_interval, since)  
  14.         data = [item[0:5] for item in data]  
  15.         # field names  
  16.         fields = ['timstamp', 'high', 'low', 'open', 'close']  
  17.         # save the data  
  18.         with open(f'data/{coin}_{since}.csv', 'w', newline='') as f:  
  19.             # using csv.writer method from CSV package  
  20.             write = csv.writer(f)  
  21.             write.writerow(fields)  
  22.             write.writerows(data)  
  23.         end_time = time.perf_counter()  
  24.         # calculate how long it took to produce the file  
  25.         time_elapsed = round(end_time - start_time)  
  26.         print(f'Historical data for {coin} saved as {coin}_{since}.csv. Time elapsed: {time_elapsed} seconds')  
  27.     return f'{coin}_{since}.csv' 

此函數(shù)還將檢查文件是否已經(jīng)存在,如果存在它不會再次下載。該函數(shù)接受 3 個參數(shù):coin、since 和 kline_interval。檢查函數(shù)下方的注釋,了解我們將傳遞給這些參數(shù)的正確格式。

保存文件,現(xiàn)在是創(chuàng)建我們的主要執(zhí)行文件的時候了,我們將把這個文件的內(nèi)容導(dǎo)入到其中。

繼續(xù)創(chuàng)建一個 main.py 文件并安裝以下依賴項: 

  1. from binancedata import *  
  2. import threading  
  3. import matplotlib.pyplot as plt  
  4. import matplotlib.cbook as cbook  
  5. import numpy as np  
  6. import pandas as pd  
  7. # needed for the binance API and websockets  
  8. from binance.client import Client  
  9. import csv  
  10. import os  
  11. import time  
  12. from datetime import datetime, date 

讓我們開始一些線程。該腳本是為了一次下載多個數(shù)據(jù)文件,所以為了避免等待一次下載每個歷史數(shù)據(jù)文件,我們將使用線程并下載這些文件,如下所示: 

  1. threads = []  
  2. coins = get_coins()  
  3. for coin in coins:  
  4.     t = threading.Thread(target=get_historical_dataargs=(coin, '1 Jan 2017', Client.KLINE_INTERVAL_1DAY) ) #'get_historical_data('ETHUSDT', '1 Jan 2021', Client.KLINE_INTERVAL_1MINUTE) 
  5.     t.start()  
  6.     threads.append(t)  
  7. [thread.join() for thread in threads] 

現(xiàn)在我們需要一個函數(shù)來返回我們下載的所有數(shù)據(jù)文件的文件名: 

  1. def get_all_filenames():  
  2.     return [get_historical_data(coin, '1 Jan 2017', Client.KLINE_INTERVAL_1DAY) for coin in coins] 

最后,我們將定義主要函數(shù),我們將在其中繪制這些數(shù)據(jù)并運行腳本: 

  1. def main():  
  2.     historical_data = get_all_filenames()  
  3.     for file in historical_data:  
  4.         data = pd.read_csv(f'data/{file}')  
  5.         rolling_percentage = data['close']  
  6.         rolling_percentage = [(item - rolling_percentage[0]) / rolling_percentage[0]*100 for item in rolling_percentage ]  
  7.         timestamp = data['timstamp']  
  8.         timestamp = [datetime.fromtimestamp(item/1000) for item in timestamp]  
  9.         plt.legend()  
  10.         plt.plot(timestamp, rolling_percentage, label=file 
  11.         plt.xlabel("Date")  
  12.         plt.ylabel("% gain")  
  13.     plt.show()  
  14. if __name__ == "__main__":  
  15.     main() 

現(xiàn)在剩下要做的就是在腳本目錄中創(chuàng)建一個空文件夾并將其命名為 data。大功告成,您現(xiàn)在可以分析您想要的所有代幣的歷史收益。 

 

責任編輯:龐桂玉 來源: Python中文社區(qū)
相關(guān)推薦

2021-06-04 10:31:41

PythonUniswap加密貨幣

2021-01-25 22:11:38

加密貨幣區(qū)塊鏈貨幣

2021-01-11 11:20:36

加密貨幣稅收股票

2021-05-14 14:33:07

Python加密貨幣

2021-04-12 10:29:56

加密貨幣貨幣比特幣

2021-01-14 11:08:05

加密貨幣貨幣技術(shù)

2022-07-12 14:42:24

加密貨幣區(qū)塊鏈數(shù)字貨幣

2021-06-09 14:26:50

加密貨幣比特幣貨幣

2022-03-18 13:50:06

區(qū)塊鏈加密貨幣去中心化

2021-01-22 16:02:29

加密貨幣數(shù)字貨幣瑞銀

2021-03-09 16:41:42

加密貨幣區(qū)塊鏈技術(shù)

2022-05-31 08:00:00

加密貨幣數(shù)字化比特幣

2022-09-06 14:52:56

NFT加密貨幣代幣

2021-09-29 09:35:29

Python典型化事實代碼

2020-12-31 14:37:40

比特幣加密貨幣區(qū)塊鏈

2022-09-13 11:15:33

加密貨幣區(qū)塊鏈

2022-09-16 16:40:47

加密貨幣比特幣貨幣

2018-12-27 15:13:47

加密貨幣攻擊惡意軟件

2022-06-09 13:55:15

加密貨幣區(qū)塊鏈

2021-03-22 22:48:45

加密貨幣比特幣以太坊
點贊
收藏

51CTO技術(shù)棧公眾號