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


导师贪玩
导师贪玩 2022-09-20 10:44: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》

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=4093
赞同 0
评论 0 条
导师贪玩L0
粉丝 0 发表 5 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2941
【软件正版化】软件正版化工作要点  2860
统信UOS试玩黑神话:悟空  2819
信刻光盘安全隔离与信息交换系统  2712
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1246
grub引导程序无法找到指定设备和分区  1213
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  163
点击报名 | 京东2025校招进校行程预告  162
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  160
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  157
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!