Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统
Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统
实现结果
设计思路
实现代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#利用Python调用数据库历史记录文件,实现BTC等找出最佳出仓价、收益比的加密币模拟交易系统
import sqlite3
from simulator import runSimulation
from drama import dramaticTyping
def fetchCoins(): #调用数据库的db文件实现价格显示的功能
connection = sqlite3.connect('F:/File_Python/Python_example/CryptoTradingSimulator-master/CryptoSimulator/currency_monitor.db')
cursor = connection.cursor()
query = "SELECT first_leg, ask FROM prices WHERE timestamp='1520408341.52' AND second_leg='USD';"
cursor.execute(query)
coinAskPrices = cursor.fetchall()
coins = {}
for coinAskPrice in coinAskPrices:
if coinAskPrice[0] in coins:
continue
coins[coinAskPrice[0]] = {"price":coinAskPrice[1], "curreny":coinAskPrice[0]}
dramaticTyping("{} - ${} \n".format(coinAskPrice[0], round(coinAskPrice[1],4)))
return coins
def welcome():
print("\n")
dramaticTyping("Simple Crypto Trading Simulator \n")
dramaticTyping("Hey Yo, you are back in time. It's Wednesday, March 7, 2018 7:39 AM \n")
dramaticTyping("Here are the crypto currencies you can invest. \n")
dramaticTyping("Fetching prices ... \n")
def inputBuy():
dramaticTyping("Select the crypto curreny you want to buy? \n")
curreny = input("").upper()
dramaticTyping("That's great. How much quantity you want to buy? \n")
quantity = float(input(""))
return curreny, quantity
def quitMenu():
dramaticTyping("Do you want to try again? Y/N ")
answer = input("").upper()
if answer == 'Y':
main()
else:
exit()
def main():
welcome()
coins = fetchCoins()
currency, quantity = inputBuy()
try: #处理异常
price = coins[currency]['price']
except Exception as e:
dramaticTyping("Invalid currency entered, please try again \n")
inputBuy()
runSimulation(coins[currency]['price'], quantity, currency)
quitMenu()
main()
参考国外文章《Learn to Code a Crypto Trading Simulator in Python》
赞 (0)