Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib
  3. import matplotlib.pyplot as plt
  4. import random
  5.  
  6. #textfile = 'test.txt'
  7. #textfile = 'ETH.txt'
  8. textfile = 'SPREADS.txt'
  9. howFarBack = 14000
  10. nLen = 1000
  11. EMAlen = 900
  12. EMA2len = 50
  13. EMA3len = 50
  14.  
  15. rsiMin = 30
  16. rsiMax = 70
  17.  
  18. def ExpMovingAverage(values, window):
  19. weights = np.exp(np.linspace(-1., 0., window))
  20. weights /= weights.sum()
  21. a = np.convolve(values, weights, mode='full')[:len(values)]
  22. a[:window] = a[window]
  23. return a
  24.  
  25. def rsiFunc(prices, n=300):
  26. deltas = np.diff(prices)
  27. seed = deltas[:n+1]
  28. up = seed[seed>=0].sum()/n
  29. down = -seed[seed<0].sum()/n
  30. rs = up/down
  31. rsi = np.zeros_like(prices)
  32. rsi[:n] = 100. - 100./(1. +rs)
  33.  
  34. for i in range(n, len(prices)):
  35. delta = deltas[i-1]
  36.  
  37. if delta>0:
  38. upval = delta
  39. downval = 0.
  40. else:
  41. upval = 0.
  42. downval = -delta
  43.  
  44. up = (up*(n-1)+ upval)/n
  45. down = (down*(n-1) + downval)/n
  46.  
  47. rs = up/down
  48. rsi[i] = 100. - 100./(1.+rs)
  49. return rsi
  50.  
  51.  
  52. def testingData():
  53. btcDatear = []
  54. BFXbidar = []
  55. BFXaskar = []
  56. GDAXbidar = []
  57. GDAXaskar = []
  58. GbBa = []
  59. GaBb = []
  60. BbGa = []
  61. BaGb = []
  62.  
  63.  
  64. try:
  65. sourceCode = open(textfile, 'r').read()
  66. splitSource = sourceCode.split('\n')
  67.  
  68. for eachLine in splitSource[-howFarBack:]:
  69. splitLine = eachLine.split(', ')
  70. btcDate = splitLine[0]
  71. BFXbid = splitLine[1]
  72. BFXask = splitLine[2]
  73. GDAXbid = splitLine[3]
  74. GDAXask = splitLine[4]
  75.  
  76. btcDatear.append(float(btcDate))
  77. BFXbidar.append(float(BFXbid))
  78. BFXaskar.append(float(BFXask))
  79. GDAXbidar.append(float(GDAXbid))
  80. GDAXaskar.append(float(GDAXask))
  81. GbBa.append(float (float(GDAXbid) - float(BFXask)))
  82. GaBb.append(float(float(GDAXask) - float(BFXbid)))
  83. BbGa.append(float(float(BFXbid) - float(GDAXask)))
  84. BaGb.append(float(float(BFXask) - float(GDAXbid)))
  85.  
  86.  
  87. except Exception, e:
  88. print "failed raw data", str(e)
  89.  
  90. EMA = ExpMovingAverage(BbGa, EMAlen)
  91. EMA2 = ExpMovingAverage(BbGa, EMA2len)
  92. #EMA3 = ExpMovingAverage(btcPricear, EMA3len)
  93. rsiLine = rsiFunc(BbGa, n=nLen)
  94. rsiLine2 = rsiFunc(BbGa, n=nLen)
  95. #rsiLine3 = rsiFunc(EMA2, n=nLen)
  96. #rsiLine4 = rsiFunc(EMA3, n=nLen)
  97.  
  98. ax1 = plt.subplot2grid((6, 4), (4,0), rowspan=2, colspan=4)
  99. ax1.plot(btcDatear, BFXbidar, color = 'b')
  100. ax1.plot(btcDatear, BFXaskar, color = 'b')
  101. ax1.plot(btcDatear, GDAXbidar, color = 'g')
  102. ax1.plot(btcDatear, GDAXaskar, color = 'g')
  103. ax1.grid(True)
  104.  
  105. ax2 = plt.subplot2grid((6, 4), (1,0),sharex= ax1, rowspan=3, colspan=4)
  106. ax2.grid(True)
  107.  
  108.  
  109.  
  110. ax2.plot(btcDatear, GbBa, color = 'g')
  111. #ax2.plot(btcDatear, GaBb, color = "b")
  112. ax2.plot(btcDatear, BbGa, color = 'g')
  113. #ax2.plot(btcDatear, (rsiLine2/100), color='b')
  114. #ax2.plot(btcDatear, BaGb, color = 'b')
  115. ax2.plot(btcDatear, EMA, color = 'y')
  116. #ax2.plot(btcDatear, EMA2, color = 'y')
  117. ax2.set_yticks([-3,-2,-1.66, -1.33, -1, 0, 1, 1.33, 1.66, 2, 3])
  118.  
  119. ax3 = plt.subplot2grid((6, 4), (0,0),sharex= ax1, rowspan=1, colspan=4)
  120. ax3.grid(True)
  121. ax3.plot(btcDatear, (rsiLine), color="r")
  122. ax3.set_yticks([47,53])
  123.  
  124. plt.show()
  125. #############################################################
  126. def backTesting():
  127. btcDatear = []
  128. BFXbidar = []
  129. BFXaskar = []
  130. GDAXbidar = []
  131. GDAXaskar = []
  132. GbBa = []
  133. GaBb = []
  134. BbGa = []
  135. BaGb = []
  136. initSpread = []
  137. spreads = 0
  138.  
  139.  
  140. try:
  141. sourceCode = open(textfile, 'r').read()
  142. splitSource = sourceCode.split('\n')
  143.  
  144. for eachLine in splitSource[-howFarBack:]:
  145. splitLine = eachLine.split(', ')
  146. btcDate = splitLine[0]
  147. BFXbid = splitLine[1]
  148. BFXask = splitLine[2]
  149. GDAXbid = splitLine[3]
  150. GDAXask = splitLine[4]
  151.  
  152. btcDatear.append(float(btcDate))
  153. BFXbidar.append(float(BFXbid))
  154. BFXaskar.append(float(BFXask))
  155. GDAXbidar.append(float(GDAXbid))
  156. GDAXaskar.append(float(GDAXask))
  157. GbBa.append(float (float(GDAXbid) - float(BFXask)))
  158. GaBb.append(float(float(GDAXask) - float(BFXbid)))
  159. BbGa.append(float(float(BFXbid) - float(GDAXask)))
  160. BaGb.append(float(float(BFXask) - float(GDAXbid)))
  161. except Exception, e:
  162. print "failed raw data", str(e)
  163.  
  164. GDAXusd = 10
  165. GDAXltc = 13.1
  166. BFXusd = 363
  167. BFXltc = 0.88
  168. trades= 0
  169.  
  170. x= 0
  171. stance = 'none'
  172. lastBoughtFor = 0
  173. totalProfit = 0
  174. while x < len(GaBb):
  175. ############################################
  176. ###########################################
  177. rand = "%.2f"%(random.uniform(-0.01, 0.04))
  178. ############################################
  179. #print GbBa[x], GaBb[x], BbGa[x], BaGb[x]
  180. #if GbBa[x] > 1.5:
  181. #print GbBa[x]
  182. #if BbGa[x] > -1.3:
  183. #print BbGa[x]
  184. if BFXusd > BFXaskar[x] and GDAXltc > 1 and float(GbBa[x]) >= float(1.28):
  185. openFee = ((BFXaskar[x] + GDAXbidar[x]) * 0.002)
  186. initSpread.append((float(GbBa[x])- openFee))
  187. spreads = spreads + 1
  188. GDAXltc = GDAXltc - 1
  189. GDAXusd = GDAXusd + float(GDAXbidar[x])
  190. BFXusd = BFXusd - float(BFXaskar[x])
  191. BFXltc = BFXltc + 1
  192. #print spreads, GbBa[x]
  193.  
  194.  
  195. if spreads > 0 and float(BbGa[x]) >= float(-1.13) and float(GDAXusd) > float(GDAXaskar[x]) and float(BFXltc) > float(1):
  196. closeFee = ((BFXbidar[x] + GDAXaskar[x]) * 0.002)
  197. totalProfit = totalProfit + ((float(initSpread[0]) + float(BbGa[x]))- closeFee)
  198. print ((float(initSpread[0]) + float(BbGa[x]))-closeFee), float(initSpread[0]),float(BbGa[x])
  199. del initSpread[0]
  200. spreads = spreads - 1
  201. GDAXltc = GDAXltc + 1
  202. GDAXusd = GDAXusd - float(GDAXaskar[x])
  203. BFXusd = BFXusd + float(BFXbidar[x])
  204. BFXltc = BFXltc - 1
  205. trades= trades +1
  206. #print
  207.  
  208. x += 1
  209. print "total profit:", totalProfit
  210. print trades
  211. backTesting()
  212. testingData()
  213. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement