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

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

实现结果

image.png

设计思路

image.png

实现代码


#!/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()


相关文章
|
1天前
|
数据采集 NoSQL 中间件
python-scrapy框架(四)settings.py文件的用法详解实例
python-scrapy框架(四)settings.py文件的用法详解实例
6 0
|
1天前
|
存储 数据采集 数据库
python-scrapy框架(三)Pipeline文件的用法讲解
python-scrapy框架(三)Pipeline文件的用法讲解
5 0
|
3天前
|
缓存 数据处理 Python
python读取文件到缓存
python读取文件到缓存
9 1
|
3天前
|
存储 数据挖掘 Python
Python技术分享:实现选择文件或目录路径的方法
Python技术分享:实现选择文件或目录路径的方法
15 2
|
4天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
23 0
|
10天前
|
API 数据库 Python
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
|
10天前
|
关系型数据库 MySQL API
Python web框架fastapi数据库操作ORM(一)
Python web框架fastapi数据库操作ORM(一)
|
10天前
|
Linux iOS开发 MacOS
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
|
10天前
|
SQL 关系型数据库 MySQL
Python操作mysql数据库
Python操作mysql数据库
|
10天前
|
NoSQL Python
在Python中,我们可以使用许多库来处理Excel文件
Python处理Excel常用pandas和openpyxl库。pandas的`read_excel`用于读取文件,`to_excel`写入;示例展示了数据框操作。openpyxl则用于处理复杂情况,如多工作表,`load_workbook`加载文件,`iter_rows`读取数据,`Workbook`创建新文件,写入单元格数据后保存。
20 1