Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. from collections import deque
  2.  
  3. WINDOW_LEN = 18
  4.  
  5. state = 0 # waiting state is start state
  6.  
  7. aud_bal = 10000.0
  8. btc_bal = 0.0
  9.  
  10. historic_prices = deque(maxlen=WINDOW_LEN)
  11.  
  12. BUY_CONSTANT = 0.95
  13. SELL_CONSTANT = 1.05
  14. SAFETY_CONSTANT = 0.80
  15.  
  16. def BuyBitcoin(aud_bal, buy_price):
  17. """
  18.  
  19. :param aud_bal: AUD to spend
  20. :param buy_price: Price of 1 BTC
  21. :return:
  22. """
  23. print "Bought {} AUD worth of BTC @ {} ({} total BTC)".format(aud_bal, buy_price, aud_bal / buy_price)
  24. return aud_bal, aud_bal / buy_price
  25.  
  26. def SellBitcoin(btc_bal, sell_price):
  27. print "Sold {} BTC worth of AUD @ {} ({} total AUD)".format(btc_bal, sell_price, btc_bal * sell_price)
  28. return btc_bal * sell_price, btc_bal
  29.  
  30. import random
  31. random.seed(1)
  32.  
  33. def GetCurrentPrice():
  34. return (18000.0 + random.randint(-1000, 1000))
  35.  
  36. safety_thres = 0.0
  37.  
  38. # buy/sell relative to program, not exchange
  39. while True:
  40. btc_aud = GetCurrentPrice()
  41. print "Fetched {} BTC/AUD, ({} AUD,{} BTC left)".format(btc_aud, aud_bal, btc_bal)
  42. historic_prices.append(btc_aud)
  43.  
  44. # waiting
  45. if state == 0:
  46. buy_thres = historic_prices[0] * BUY_CONSTANT
  47. print "Buy threshold is set @ {}".format(buy_thres)
  48. if btc_aud < buy_thres and len(historic_prices) == WINDOW_LEN:
  49. buying_price = btc_aud
  50. state = 1
  51.  
  52. # buying
  53. if state == 1:
  54. aud_used, btc_bought = BuyBitcoin(aud_bal, buying_price)
  55. btc_bal += btc_bought
  56. aud_bal -= aud_used
  57. sell_thres = buying_price * SELL_CONSTANT
  58. print "Sell threshold is set @ {}".format(sell_thres)
  59. safety_thres = buying_price * SAFETY_CONSTANT
  60. print "Safety threshold is set @ {}".format(safety_thres)
  61. state = 2
  62.  
  63. # looking for sell
  64. if state == 2:
  65. if btc_aud > sell_thres or btc_aud < safety_thres:
  66. aud_bought, btc_used = SellBitcoin(btc_bal, btc_aud)
  67. btc_bal -= btc_used
  68. aud_bal += aud_bought
  69. if btc_aud > sell_thres:
  70. print "Sell threshold was @ {}".format(sell_thres)
  71. state = 0
  72. else:
  73. print "Safety threshold was @ {}".format(safety_thres)
  74. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement