Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib
- import matplotlib.pyplot as plt
- import random
- #textfile = 'test.txt'
- #textfile = 'ETH.txt'
- textfile = 'SPREADS.txt'
- howFarBack = 14000
- nLen = 1000
- EMAlen = 900
- EMA2len = 50
- EMA3len = 50
- rsiMin = 30
- rsiMax = 70
- def ExpMovingAverage(values, window):
- weights = np.exp(np.linspace(-1., 0., window))
- weights /= weights.sum()
- a = np.convolve(values, weights, mode='full')[:len(values)]
- a[:window] = a[window]
- return a
- def rsiFunc(prices, n=300):
- deltas = np.diff(prices)
- seed = deltas[:n+1]
- up = seed[seed>=0].sum()/n
- down = -seed[seed<0].sum()/n
- rs = up/down
- rsi = np.zeros_like(prices)
- rsi[:n] = 100. - 100./(1. +rs)
- for i in range(n, len(prices)):
- delta = deltas[i-1]
- if delta>0:
- upval = delta
- downval = 0.
- else:
- upval = 0.
- downval = -delta
- up = (up*(n-1)+ upval)/n
- down = (down*(n-1) + downval)/n
- rs = up/down
- rsi[i] = 100. - 100./(1.+rs)
- return rsi
- def testingData():
- btcDatear = []
- BFXbidar = []
- BFXaskar = []
- GDAXbidar = []
- GDAXaskar = []
- GbBa = []
- GaBb = []
- BbGa = []
- BaGb = []
- try:
- sourceCode = open(textfile, 'r').read()
- splitSource = sourceCode.split('\n')
- for eachLine in splitSource[-howFarBack:]:
- splitLine = eachLine.split(', ')
- btcDate = splitLine[0]
- BFXbid = splitLine[1]
- BFXask = splitLine[2]
- GDAXbid = splitLine[3]
- GDAXask = splitLine[4]
- btcDatear.append(float(btcDate))
- BFXbidar.append(float(BFXbid))
- BFXaskar.append(float(BFXask))
- GDAXbidar.append(float(GDAXbid))
- GDAXaskar.append(float(GDAXask))
- GbBa.append(float (float(GDAXbid) - float(BFXask)))
- GaBb.append(float(float(GDAXask) - float(BFXbid)))
- BbGa.append(float(float(BFXbid) - float(GDAXask)))
- BaGb.append(float(float(BFXask) - float(GDAXbid)))
- except Exception, e:
- print "failed raw data", str(e)
- EMA = ExpMovingAverage(BbGa, EMAlen)
- EMA2 = ExpMovingAverage(BbGa, EMA2len)
- #EMA3 = ExpMovingAverage(btcPricear, EMA3len)
- rsiLine = rsiFunc(BbGa, n=nLen)
- rsiLine2 = rsiFunc(BbGa, n=nLen)
- #rsiLine3 = rsiFunc(EMA2, n=nLen)
- #rsiLine4 = rsiFunc(EMA3, n=nLen)
- ax1 = plt.subplot2grid((6, 4), (4,0), rowspan=2, colspan=4)
- ax1.plot(btcDatear, BFXbidar, color = 'b')
- ax1.plot(btcDatear, BFXaskar, color = 'b')
- ax1.plot(btcDatear, GDAXbidar, color = 'g')
- ax1.plot(btcDatear, GDAXaskar, color = 'g')
- ax1.grid(True)
- ax2 = plt.subplot2grid((6, 4), (1,0),sharex= ax1, rowspan=3, colspan=4)
- ax2.grid(True)
- ax2.plot(btcDatear, GbBa, color = 'g')
- #ax2.plot(btcDatear, GaBb, color = "b")
- ax2.plot(btcDatear, BbGa, color = 'g')
- #ax2.plot(btcDatear, (rsiLine2/100), color='b')
- #ax2.plot(btcDatear, BaGb, color = 'b')
- ax2.plot(btcDatear, EMA, color = 'y')
- #ax2.plot(btcDatear, EMA2, color = 'y')
- ax2.set_yticks([-3,-2,-1.66, -1.33, -1, 0, 1, 1.33, 1.66, 2, 3])
- ax3 = plt.subplot2grid((6, 4), (0,0),sharex= ax1, rowspan=1, colspan=4)
- ax3.grid(True)
- ax3.plot(btcDatear, (rsiLine), color="r")
- ax3.set_yticks([47,53])
- plt.show()
- #############################################################
- def backTesting():
- btcDatear = []
- BFXbidar = []
- BFXaskar = []
- GDAXbidar = []
- GDAXaskar = []
- GbBa = []
- GaBb = []
- BbGa = []
- BaGb = []
- initSpread = []
- spreads = 0
- try:
- sourceCode = open(textfile, 'r').read()
- splitSource = sourceCode.split('\n')
- for eachLine in splitSource[-howFarBack:]:
- splitLine = eachLine.split(', ')
- btcDate = splitLine[0]
- BFXbid = splitLine[1]
- BFXask = splitLine[2]
- GDAXbid = splitLine[3]
- GDAXask = splitLine[4]
- btcDatear.append(float(btcDate))
- BFXbidar.append(float(BFXbid))
- BFXaskar.append(float(BFXask))
- GDAXbidar.append(float(GDAXbid))
- GDAXaskar.append(float(GDAXask))
- GbBa.append(float (float(GDAXbid) - float(BFXask)))
- GaBb.append(float(float(GDAXask) - float(BFXbid)))
- BbGa.append(float(float(BFXbid) - float(GDAXask)))
- BaGb.append(float(float(BFXask) - float(GDAXbid)))
- except Exception, e:
- print "failed raw data", str(e)
- GDAXusd = 10
- GDAXltc = 13.1
- BFXusd = 363
- BFXltc = 0.88
- trades= 0
- x= 0
- stance = 'none'
- lastBoughtFor = 0
- totalProfit = 0
- while x < len(GaBb):
- ############################################
- ###########################################
- rand = "%.2f"%(random.uniform(-0.01, 0.04))
- ############################################
- #print GbBa[x], GaBb[x], BbGa[x], BaGb[x]
- #if GbBa[x] > 1.5:
- #print GbBa[x]
- #if BbGa[x] > -1.3:
- #print BbGa[x]
- if BFXusd > BFXaskar[x] and GDAXltc > 1 and float(GbBa[x]) >= float(1.28):
- openFee = ((BFXaskar[x] + GDAXbidar[x]) * 0.002)
- initSpread.append((float(GbBa[x])- openFee))
- spreads = spreads + 1
- GDAXltc = GDAXltc - 1
- GDAXusd = GDAXusd + float(GDAXbidar[x])
- BFXusd = BFXusd - float(BFXaskar[x])
- BFXltc = BFXltc + 1
- #print spreads, GbBa[x]
- if spreads > 0 and float(BbGa[x]) >= float(-1.13) and float(GDAXusd) > float(GDAXaskar[x]) and float(BFXltc) > float(1):
- closeFee = ((BFXbidar[x] + GDAXaskar[x]) * 0.002)
- totalProfit = totalProfit + ((float(initSpread[0]) + float(BbGa[x]))- closeFee)
- print ((float(initSpread[0]) + float(BbGa[x]))-closeFee), float(initSpread[0]),float(BbGa[x])
- del initSpread[0]
- spreads = spreads - 1
- GDAXltc = GDAXltc + 1
- GDAXusd = GDAXusd - float(GDAXaskar[x])
- BFXusd = BFXusd + float(BFXbidar[x])
- BFXltc = BFXltc - 1
- trades= trades +1
- #print
- x += 1
- print "total profit:", totalProfit
- print trades
- backTesting()
- testingData()
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement