Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- WINDOW_LEN = 18
- state = 0 # waiting state is start state
- aud_bal = 10000.0
- btc_bal = 0.0
- historic_prices = deque(maxlen=WINDOW_LEN)
- BUY_CONSTANT = 0.95
- SELL_CONSTANT = 1.05
- SAFETY_CONSTANT = 0.80
- def BuyBitcoin(aud_bal, buy_price):
- """
- :param aud_bal: AUD to spend
- :param buy_price: Price of 1 BTC
- :return:
- """
- print "Bought {} AUD worth of BTC @ {} ({} total BTC)".format(aud_bal, buy_price, aud_bal / buy_price)
- return aud_bal, aud_bal / buy_price
- def SellBitcoin(btc_bal, sell_price):
- print "Sold {} BTC worth of AUD @ {} ({} total AUD)".format(btc_bal, sell_price, btc_bal * sell_price)
- return btc_bal * sell_price, btc_bal
- import random
- random.seed(1)
- def GetCurrentPrice():
- return (18000.0 + random.randint(-1000, 1000))
- safety_thres = 0.0
- # buy/sell relative to program, not exchange
- while True:
- btc_aud = GetCurrentPrice()
- print "Fetched {} BTC/AUD, ({} AUD,{} BTC left)".format(btc_aud, aud_bal, btc_bal)
- historic_prices.append(btc_aud)
- # waiting
- if state == 0:
- buy_thres = historic_prices[0] * BUY_CONSTANT
- print "Buy threshold is set @ {}".format(buy_thres)
- if btc_aud < buy_thres and len(historic_prices) == WINDOW_LEN:
- buying_price = btc_aud
- state = 1
- # buying
- if state == 1:
- aud_used, btc_bought = BuyBitcoin(aud_bal, buying_price)
- btc_bal += btc_bought
- aud_bal -= aud_used
- sell_thres = buying_price * SELL_CONSTANT
- print "Sell threshold is set @ {}".format(sell_thres)
- safety_thres = buying_price * SAFETY_CONSTANT
- print "Safety threshold is set @ {}".format(safety_thres)
- state = 2
- # looking for sell
- if state == 2:
- if btc_aud > sell_thres or btc_aud < safety_thres:
- aud_bought, btc_used = SellBitcoin(btc_bal, btc_aud)
- btc_bal -= btc_used
- aud_bal += aud_bought
- if btc_aud > sell_thres:
- print "Sell threshold was @ {}".format(sell_thres)
- state = 0
- else:
- print "Safety threshold was @ {}".format(safety_thres)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement