政策资讯

Policy Information


Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统

来源: 重庆市软件正版化服务中心    |    时间: 2022-09-20    |    浏览量: 51247    |   

Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统

目录

实现结果

设计思路

实现代码


实现结果

设计思路

实现代码

  1. !/usr/bin/env python
  2. -*- coding: utf-8 -*-
  3. 利用Python调用数据库历史记录文件,实现BTC等找出最佳出仓价、收益比的加密币模拟交易系统
  4. import sqlite3
  5. from simulator import runSimulation
  6. from drama import dramaticTyping
  7. def fetchCoins(): 调用数据库的db文件实现价格显示的功能
  8. connection = sqlite3.connect('F:/File_Python/Python_example/CryptoTradingSimulator-master/CryptoSimulator/currency_monitor.db')
  9. cursor = connection.cursor()
  10. query = "SELECT first_leg, ask FROM prices WHERE timestamp='1520408341.52' AND second_leg='USD';"
  11. cursor.execute(query)
  12. coinAskPrices = cursor.fetchall()
  13. coins = {}
  14. for coinAskPrice in coinAskPrices:
  15. if coinAskPrice[0] in coins:
  16. continue
  17. coins[coinAskPrice[0]] = {"price":coinAskPrice[1], "curreny":coinAskPrice[0]}
  18. dramaticTyping("{} - ${} \n".format(coinAskPrice[0], round(coinAskPrice[1],4)))
  19. return coins
  20. def welcome():
  21. print("\n")
  22. dramaticTyping("Simple Crypto Trading Simulator \n")
  23. dramaticTyping("Hey Yo, you are back in time. It's Wednesday, March 7, 2018 7:39 AM \n")
  24. dramaticTyping("Here are the crypto currencies you can invest. \n")
  25. dramaticTyping("Fetching prices ... \n")
  26. def inputBuy():
  27. dramaticTyping("Select the crypto curreny you want to buy? \n")
  28. curreny = input("").upper()
  29. dramaticTyping("That's great. How much quantity you want to buy? \n")
  30. quantity = float(input(""))
  31. return curreny, quantity
  32. def quitMenu():
  33. dramaticTyping("Do you want to try again? Y/N ")
  34. answer = input("").upper()
  35. if answer == 'Y':
  36. main()
  37. else:
  38. exit()
  39. def main():
  40. welcome()
  41. coins = fetchCoins()
  42. currency, quantity = inputBuy()
  43. try: 处理异常
  44. price = coins[currency]['price']
  45. except Exception as e:
  46. dramaticTyping("Invalid currency entered, please try again \n")
  47. inputBuy()
  48. runSimulation(coins[currency]['price'], quantity, currency)
  49. quitMenu()
  50. main()

参考国外文章《Learn to Code a Crypto Trading Simulator in Python》

评论

产品推荐

更多 >

QQ咨询 扫一扫加入群聊,了解更多平台咨询
微信咨询 扫一扫加入群聊,了解更多平台咨询
意见反馈
立即提交
QQ咨询
微信咨询
意见反馈