tradebot


SUBMITTED BY: Guest

DATE: Jan. 13, 2015, 7:23 p.m.

FORMAT: Text only

SIZE: 2.7 kB

HITS: 1051

  1. # Donations: 16RdQZ86NUWQbE6rM86JbxnnaCDcAKqa9
  2. from urllib import urlencode
  3. import urllib2
  4. import time
  5. from datetime import datetime
  6. from hashlib import sha512
  7. from hmac import HMAC
  8. import base64
  9. import json
  10. from threading import Timer
  11. class Connector:
  12. bitstampTicker={
  13. 'url':'http://www.bitstamp.net/api/ticker/',
  14. 'interval':10,
  15. 'ticker_history':[]
  16. }
  17. def __init__(self):
  18. self.updateBitstampTicker()
  19. def updateBitstampTicker(self):
  20. ticker=self.perform_query(self.bitstampTicker['url'])
  21. temp={}
  22. temp['last']=float(ticker['last'])
  23. self.bitstampTicker['ticker_history'].append(temp)
  24. Timer(self.bitstampTicker['interval'], self.updateBitstampTicker, ()).start()
  25. def perform_query(self,url,data="",headers={}):
  26. try:
  27. #print(url)
  28. #print(str(data))
  29. #print(str(headers))
  30. req = urllib2.Request(url)
  31. res = urllib2.urlopen(req,data)
  32. #print(res)
  33. a=json.load(res)
  34. #print (a)
  35. return a
  36. except Exception,e:
  37. print "Eccezione nella richiesta:" + str(Exception) + '---' + str(e)
  38. def getPrice(self):
  39. return self.bitstampTicker['ticker_history'][-1]['last']
  40. class Trader:
  41. trend=0
  42. oldTrend=0
  43. price=0
  44. oldPrice=0
  45. conn=Connector()
  46. #hisoricalData=[]
  47. #deph={}
  48. def __init__(self):
  49. self.action()
  50. def updatePrice(self):
  51. print(self.price)
  52. self.oldPrice=self.price
  53. self.price=self.conn.getPrice()
  54. def updateTrend(self):
  55. self.trend= self.price-self.oldPrice
  56. def checkMax(self):
  57. if self.oldTrend>0 and self.trend<0:
  58. return True
  59. return False
  60. def checkMin(self):
  61. if self.oldTrend<0 and self.trend>0:
  62. return True
  63. return False
  64. def buy(self,qty):
  65. print 'buy'
  66. def sell(self,qty):
  67. print 'sell'
  68. def update(self):
  69. self.updatePrice()
  70. self.updateTrend()
  71. def action(self):
  72. print ('action')
  73. self.update()
  74. if self.trend>0 and self.checkMin():
  75. self.buy(0.01)
  76. elif self.trend<0 and self.checkMax():
  77. sell.sell(0.01)
  78. Timer(10, self.action, ()).start()
  79. if __name__ == "__main__":
  80. Trader()

comments powered by Disqus