天天看點

Py經典案例:利用Python調用資料庫曆史記錄檔案,實作BTC、LTC等Encrypted currency找出最佳出倉價、收益比的加密币模拟交易系統

實作結果

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()