我是靠谱客的博主 年轻绿草,最近开发中收集的这篇文章主要介绍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》

最后

以上就是年轻绿草为你收集整理的Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统的全部内容,希望文章能够帮你解决Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部