コード例 #1
0
def doTrade(tname, front, ops):

    pools = pool.VoterPool(1, prices)
    pools.estimate(tname, ops, front)
    pools.showVoters()

    t = Trader(tname)
    lastArea = -1
    countdowns = [3] * 4
    vols = [0] * 4
    for i in range(front, l):
        price = prices[i]
        volume = 0
        area = areas[i]

        notes = 'AREA:' + str(area) + ';VOL:' + str(vols[area]) + '->' + str(
            ops[i])
        volume = ops[i] - vols[area]
        vols[area] = ops[i]
        countdowns[area] = 3

        if volume == 0: notes = ''
        for j in range(4):
            if j == area: continue
            if countdowns[j] > 0: countdowns[j] -= 1
            if countdowns[j] == 0 and vols[j] != 0:
                volume -= vols[j]
                notes += ';CLOSE AREA:' + str(j) + ';VOL:' + str(
                    vols[j]) + '->0'
                vols[j] = 0

        if volume != 0:
            t.processOrder(price['dt'],
                           price['rmb'],
                           volume * 1000,
                           notes=notes)
        else:
            t.summary(price['dt'], price['rmb'])
    logs.info(t.strategyName + ',' + str(len(t.stats['buy']['date'])) + ',' +
              str(len(t.stats['sell']['date'])) + ',' +
              str(t.stats['downDays']) + ',' + str(t.equity))

    logtr.info('OP,STRATEGY,TIME,VOLUME,PRICE,POSITION,NOTES,EQUITY,BALANCE')
    for tl in t.stats['log']:
        logtr.info(tl)
    t.generateGraph(0)
コード例 #2
0
ファイル: l2voter.py プロジェクト: 547872495/pymisc
def doTrade(tname, front, ops):
	
	pools = pool.VoterPool(1, prices)
	pools.estimate(tname, ops, front)
	pools.showVoters()
	
	t = Trader(tname)
	lastArea = -1
	countdowns = [COUNT_DOWN] * 4
	vols = [0] * 4
	for i in range(front, l):
		price = prices[i]
		volume = 0
		area = areas[i]
		if pool.checkTime(price['dt']):
			if vols[area] == 0:
				notes = 'AREA:' + str(area) + ';VOL:' + str(vols[area]) + '->' + str(ops[i]) + ';'
				volume = ops[i] - vols[area]
				vols[area] = ops[i]
				countdowns[area] = COUNT_DOWN
			
			if volume == 0: notes = ''
			for j in range(4):
				#if j == area: continue
				if countdowns[j] > 0: countdowns[j] -= 1
				if countdowns[j] == 0 and vols[j] != 0:
					volume -= vols[j]
					notes += 'CLOSE AREA:' + str(j) + ';VOL:' + str(vols[j]) + '->0;'
					vols[j] = 0
		else: # not trading time
			for j in range(4):
				if countdowns[j] > 0: countdowns[j] -= 1
		
		if volume != 0:
			t.processOrder(price['dt'], price['rmb'], volume, notes=notes)
		else:
			t.summary(price['dt'], price['rmb'])
	
	logs.info(t.strategyName + ',' + str(len(t.stats['buy']['date'])) + ',' + str(len(t.stats['sell']['date'])) + ',' + str(t.stats['downDays']) + ',' + str(t.equity))
	logtr.info('OP,STRATEGY,TIME,VOLUME,PRICE,POSITION,NOTES,EQUITY,BALANCE')
	for tl in t.stats['log']:
		logtr.info(tl)
	t.generateGraph(0)
コード例 #3
0
ファイル: maStrategy.py プロジェクト: 547872495/pymisc
def doTrade(pool, stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt, bf, bs1mt, bs1, bs2mt, bs2):
	global std, prices
	
	sname = str(stdPeriod) + '_' + str(stdGuage)
	sname += '_' + afmt + '_' + str(af) + '_' + as1mt + '_' + str(as1)
	if as2 > 0: sname += '_' + as2mt + '_' + str(as2)
	sname +=  '_' + bfmt + '_' + str(bf) + '_' + bs1mt + '_' +str(bs1)
	if bs2 > 0: sname += '_' + bs2mt + '_' + str(bs2)
	
	afma, as1ma, as2ma = getMas(afmt, af), getMas(as1mt, as1), getMas(as2mt, as2)
	bfma, bs1ma, bs2ma = getMas(bfmt, bf), getMas(bs1mt, bs1), getMas(bs2mt, bs2)
	
	front = max(as1, as2, bs1, bs2)
	
	active = 0
	a1pos = a2pos = b1pos = b2pos = 0
	a1wait = a2wait = b1wait = b2wait = 0
	a1price = a2price = b1price = b2price = 0
	t = Trader(sname)
	for i in range(front, len(prices)):
		price = prices[i]
		
		volume = 0
		notes = ''
		oa1pos, oa2pos, ob1pos , ob2pos = a1pos, a2pos, b1pos , b2pos
		oa1wait, oa2wait, ob1wait, ob2wait = a1wait, a2wait, b1wait, b2wait
		
		if std[stdPeriod][i] >= stdGuage: active = 2
		else: active = 1
		
		#A
		if as1 > 0 and afma[i - 1] <= as1ma[i - 1] and afma[i] > as1ma[i]:
			a1wait = 1
		
		if as1 > 0 and afma[i - 1] >= as1ma[i - 1] and afma[i] < as1ma[i]:
			a1wait = -1
		
		if as2 > 0 and afma[i - 1] <= as2ma[i - 1] and afma[i] > as2ma[i]:
			a2wait = 1
		
		if as2 > 0 and afma[i - 1] >= as2ma[i - 1] and afma[i] < as2ma[i]:
			a2wait = -1
		
		if active == 1: a1pos, a2pos = a1wait, a2wait
		if active != 1 and a1pos * a1wait == -1: a1pos = 0
		if active != 1 and a2pos * a2wait == -1: a2pos = 0
		
		if oa1pos != a1pos:
			volume += a1pos - oa1pos
			notes += 'A1:'+ str(oa1pos) + '->' + str(a1pos) + ';' + str(a1price) + '->' + str(price['rmb']) + ';'
			a1price = price['rmb']
			
		if oa2pos != a2pos:
			volume += a2pos - oa2pos
			notes += 'A2:'+ str(oa1pos) + '->' + str(a1pos) + ';' + str(a2price) + '->' + str(price['rmb']) + ';'
			a2price = price['rmb']
		
		#B
		if bs1 > 0 and bfma[i - 1] <= bs1ma[i - 1] and bfma[i] > bs1ma[i]:
			b1wait = 1
		
		if bs1 > 0 and bfma[i - 1] >= bs1ma[i - 1] and bfma[i] < bs1ma[i]:
			b1wait = -1
		
		if bs2 > 0 and bfma[i - 1] <= bs2ma[i - 1] and bfma[i] > bs2ma[i]:
			b2wait = 1
		
		if bs2 > 0 and bfma[i - 1] >= bs2ma[i - 1] and bfma[i] < bs2ma[i]:
			b2wait = -1
		
		if active == 2: b1pos, b2pos = b1wait, b2wait
		if active != 2 and b1pos * b1wait == -1: b1pos = 0
		if active != 2 and b2pos * b2wait == -1: b2pos = 0
		
		if ob1pos != b1pos:
			volume += b1pos - ob1pos
			notes += 'B1:'+ str(ob1pos) + '->' + str(b1pos) + ';' + str(b1price) + '->' + str(price['rmb']) + ';'
			b1price = price['rmb']
			
		if ob2pos != b2pos:
			volume += b2pos - ob2pos
			notes += 'B2:'+ str(ob2pos) + '->' + str(b2pos) + ';' + str(b2price) + '->' + str(price['rmb']) + ';'
			b2price = price['rmb']
		
		if volume != 0:
			t.processOrder(price['dt'], price['rmb'], volume * 1000, notes=notes)
		else:
			t.summary(price['dt'], price['rmb'])
		
	pool.estimate(t)
	return t
コード例 #4
0
ファイル: s5Trader.py プロジェクト: 547872495/pymisc
def doTrade(pool, direction, lPeriod, sPeriod, profitLevel, lossLevel):
	global prices, ps, phs, pls, rsis
	
	sname = 'S5_' + str(direction) + '_' + str(lPeriod) + '_' + str(sPeriod) + '_' + str(profitLevel) + '_' + str(-lossLevel)
	
	t = Trader(sname)
	front = lPeriod + sPeriod - 1
	lastPrice = 0
	for i in range(front, len(prices)):
		price = prices[i]
		volume, notes = 0, ''
		
		slope = midy = None
		highSlope = highy = lowSlope = lowy = None
		nSlope = ny = None
		
		#cSlope = cy = None
		if t.position > 0 and price['rmb'] < prices[i-1]['rmb']:
			profit = (price['rmb'] - lastPrice) * 100 / lastPrice
			
			if profit >= profitLevel:
				notes = 'PROFIT'
				volume = 0 - t.position
			elif profit <= lossLevel:
				notes = 'LOSS'
				volume = 0 - t.position
				
		elif t.position < 0 and price['rmb'] > prices[i-1]['rmb']:
			profit = (lastPrice - price['rmb']) * 100 / lastPrice
			
			if profit >= profitLevel:
				notes = 'PROFIT'
				volume = 0 - t.position
			elif profit <= lossLevel:
				notes = 'LOSS'
				volume = 0 - t.position
				
		elif t.position == 0:
			nSlope, ny = calc_slope(ps[i - sPeriod + 1 : i + 1])
			
			if (direction == 1 and nSlope > 0) or (direction == -1 and nSlope < 0):
				ys = ps[i - lPeriod - sPeriod + 2 : i - sPeriod + 2]
				slope, midy = calc_slope(ys)
				#print i - lPeriod - sPeriod + 2,  i - sPeriod + 2
				highSlope, highy, lowSlope, lowy = calc_bands(ys)
				
				#print i - lPeriod - sPeriod + 2,  i - sPeriod + 2
				x = lPeriod + sPeriod - 2
				p = ps[i]
				high, low, mid = highy(x), lowy(x), midy(x)
				
				if direction == 1 and p > high and nSlope > 0 and high > mid and mid > low and t.position <= 0:
					notes = 'BUY'
					volume = 1
				elif direction == -1 and p < low and nSlope < 0 and high > mid and mid > low and t.position >= 0:
					notes = 'SELL'
					volume = -1
			
		if volume != 0:
			lastPrice = price['rmb']
			t.processOrder(price['dt'], price['rmb'], volume, notes=notes)
		else:
			t.summary(price['dt'], price['rmb'])
		
		#logb.info(str(i) + ',' + str(price['dt']) + ',' + str(std) + ',' + str(notes) + ',' + str(volume)
		#			+ ',' + str(p) + ',' + str(low) + ',' + str(mid) + ',' + str(high) + ',' + str(price['rmb'])
		#			+ ',' + str(slope) + ',' + str(nSlope)+ ',' + str(highSlope) + ',' + str(lowSlope))
		
		if volume != 0 and i >= 1:
			print i, rsis['rsi'][i]
		
			nxs = nys = []
			if ny:
				nxs = range(sPeriod)
				nys = map(ny, nxs)
				nxs = map(lambda x: x + lPeriod - 1, nxs)
			
			xs = upys = lowys = midys = []
			if highy:
				xs = range(lPeriod)
				upys = map(highy, xs)
				lowys = map(lowy, xs)
				midys = map(midy, xs)
			
			#cxs = cys = []
			#if cy:
			#	cxs = range(cPeriod)
			#	cys = map(cy, cxs)
			#	cxs = map(lambda x: x + lPeriod - 1, cxs)
			
			axs = range(lPeriod + sPeriod + 100)
			ays = ps[i - lPeriod - sPeriod + 2 : i + 102]
			if lPeriod + sPeriod + 100 > len(ps):
				axs = range(len(ps) - lPeriod - sPeriod + 2)
				ays = ps[i - lPeriod - sPeriod + 2 : ]
			plt.plot(axs, ays, 'b', xs, midys, 'y', xs, upys, 'r', xs, lowys, 'g', nxs, nys, 'm')
			plt.vlines(lPeriod + sPeriod - 2, min(ays), max(ays), color='y', linestyles ='dotted')
			plt.title(str(price['dt']) + ' ' + str(volume) + ' ' + str(notes))
			plt.show()
			
	
	pool.estimate(t)
	return t
コード例 #5
0
ファイル: fmaStrategy.py プロジェクト: zjw0358/pymisc
def doTrade(pool, stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt,
            bf, bs1mt, bs1, bs2mt, bs2):

    sname = str(stdPeriod) + '_' + str(stdGuage)
    sname += '_' + afmt + '_' + str(af) + '_' + as1mt + '_' + str(as1)
    if as2 > 0: sname += '_' + as2mt + '_' + str(as2)
    sname += '_' + bfmt + '_' + str(bf) + '_' + bs1mt + '_' + str(bs1)
    if bs2 > 0: sname += '_' + bs2mt + '_' + str(bs2)

    afma, as1ma, as2ma = getMas(afmt, af), getMas(as1mt,
                                                  as1), getMas(as2mt, as2)
    bfma, bs1ma, bs2ma = getMas(bfmt, bf), getMas(bs1mt,
                                                  bs1), getMas(bs2mt, bs2)

    front = max(as1, as2, bs1, bs2)

    active = 0
    a1pos = a2pos = b1pos = b2pos = 0
    a1wait = a2wait = b1wait = b2wait = 0
    a1price = a2price = b1price = b2price = 0
    t = Trader(sname)
    for i in range(front, len(prices)):
        price = prices[i]

        volume = 0
        notes = ''
        oa1pos, oa2pos, ob1pos, ob2pos = a1pos, a2pos, b1pos, b2pos
        oa1wait, oa2wait, ob1wait, ob2wait = a1wait, a2wait, b1wait, b2wait

        std = np.std(ps[i - stdPeriod + 1:i + 1], dtype=np.float64, ddof=0)
        if std >= stdGuage: active = 2
        else: active = 1

        #A
        if as1 > 0 and afma[i - 1] <= as1ma[i - 1] and afma[i] > as1ma[i]:
            a1wait = 1

        if as1 > 0 and afma[i - 1] >= as1ma[i - 1] and afma[i] < as1ma[i]:
            a1wait = -1

        if as2 > 0 and afma[i - 1] <= as2ma[i - 1] and afma[i] > as2ma[i]:
            a2wait = 1

        if as2 > 0 and afma[i - 1] >= as2ma[i - 1] and afma[i] < as2ma[i]:
            a2wait = -1

        if active == 1: a1pos, a2pos = a1wait, a2wait
        if active != 1 and a1pos * a1wait == -1: a1pos = 0
        if active != 1 and a2pos * a2wait == -1: a2pos = 0

        if oa1pos != a1pos:
            volume += a1pos - oa1pos
            notes += 'A1:' + str(oa1pos) + '->' + str(a1pos) + ';' + str(
                a1price) + '->' + str(price['rmb']) + ';'
            a1price = price['rmb']

        if oa2pos != a2pos:
            volume += a2pos - oa2pos
            notes += 'A2:' + str(oa1pos) + '->' + str(a1pos) + ';' + str(
                a2price) + '->' + str(price['rmb']) + ';'
            a2price = price['rmb']

        #B
        if bs1 > 0 and bfma[i - 1] <= bs1ma[i - 1] and bfma[i] > bs1ma[i]:
            b1wait = 1

        if bs1 > 0 and bfma[i - 1] >= bs1ma[i - 1] and bfma[i] < bs1ma[i]:
            b1wait = -1

        if bs2 > 0 and bfma[i - 1] <= bs2ma[i - 1] and bfma[i] > bs2ma[i]:
            b2wait = 1

        if bs2 > 0 and bfma[i - 1] >= bs2ma[i - 1] and bfma[i] < bs2ma[i]:
            b2wait = -1

        if active == 2: b1pos, b2pos = b1wait, b2wait
        if active != 2 and b1pos * b1wait == -1: b1pos = 0
        if active != 2 and b2pos * b2wait == -1: b2pos = 0

        if ob1pos != b1pos:
            volume += b1pos - ob1pos
            notes += 'B1:' + str(ob1pos) + '->' + str(b1pos) + ';' + str(
                b1price) + '->' + str(price['rmb']) + ';'
            b1price = price['rmb']

        if ob2pos != b2pos:
            volume += b2pos - ob2pos
            notes += 'B2:' + str(ob2pos) + '->' + str(b2pos) + ';' + str(
                b2price) + '->' + str(price['rmb']) + ';'
            b2price = price['rmb']

        if volume != 0:
            t.processOrder(price['dt'],
                           price['rmb'],
                           volume * 1000,
                           notes=notes)
        else:
            t.summary(price['dt'], price['rmb'])

    pool.estimate(t)
    return t
コード例 #6
0
ファイル: fmaStrategy.py プロジェクト: ongbe/pymisc
def doTrade(pool, stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt, bf, bs1mt, bs1, bs2mt, bs2):

    sname = str(stdPeriod) + "_" + str(stdGuage)
    sname += "_" + afmt + "_" + str(af) + "_" + as1mt + "_" + str(as1)
    if as2 > 0:
        sname += "_" + as2mt + "_" + str(as2)
    sname += "_" + bfmt + "_" + str(bf) + "_" + bs1mt + "_" + str(bs1)
    if bs2 > 0:
        sname += "_" + bs2mt + "_" + str(bs2)

    afma, as1ma, as2ma = getMas(afmt, af), getMas(as1mt, as1), getMas(as2mt, as2)
    bfma, bs1ma, bs2ma = getMas(bfmt, bf), getMas(bs1mt, bs1), getMas(bs2mt, bs2)

    front = max(as1, as2, bs1, bs2)

    active = 0
    a1pos = a2pos = b1pos = b2pos = 0
    a1wait = a2wait = b1wait = b2wait = 0
    a1price = a2price = b1price = b2price = 0
    t = Trader(sname)
    for i in range(front, len(prices)):
        price = prices[i]

        volume = 0
        notes = ""
        oa1pos, oa2pos, ob1pos, ob2pos = a1pos, a2pos, b1pos, b2pos
        oa1wait, oa2wait, ob1wait, ob2wait = a1wait, a2wait, b1wait, b2wait

        std = np.std(ps[i - stdPeriod + 1 : i + 1], dtype=np.float64, ddof=0)
        if std >= stdGuage:
            active = 2
        else:
            active = 1

        # A
        if as1 > 0 and afma[i - 1] <= as1ma[i - 1] and afma[i] > as1ma[i]:
            a1wait = 1

        if as1 > 0 and afma[i - 1] >= as1ma[i - 1] and afma[i] < as1ma[i]:
            a1wait = -1

        if as2 > 0 and afma[i - 1] <= as2ma[i - 1] and afma[i] > as2ma[i]:
            a2wait = 1

        if as2 > 0 and afma[i - 1] >= as2ma[i - 1] and afma[i] < as2ma[i]:
            a2wait = -1

        if active == 1:
            a1pos, a2pos = a1wait, a2wait
        if active != 1 and a1pos * a1wait == -1:
            a1pos = 0
        if active != 1 and a2pos * a2wait == -1:
            a2pos = 0

        if oa1pos != a1pos:
            volume += a1pos - oa1pos
            notes += "A1:" + str(oa1pos) + "->" + str(a1pos) + ";" + str(a1price) + "->" + str(price["rmb"]) + ";"
            a1price = price["rmb"]

        if oa2pos != a2pos:
            volume += a2pos - oa2pos
            notes += "A2:" + str(oa1pos) + "->" + str(a1pos) + ";" + str(a2price) + "->" + str(price["rmb"]) + ";"
            a2price = price["rmb"]

            # B
        if bs1 > 0 and bfma[i - 1] <= bs1ma[i - 1] and bfma[i] > bs1ma[i]:
            b1wait = 1

        if bs1 > 0 and bfma[i - 1] >= bs1ma[i - 1] and bfma[i] < bs1ma[i]:
            b1wait = -1

        if bs2 > 0 and bfma[i - 1] <= bs2ma[i - 1] and bfma[i] > bs2ma[i]:
            b2wait = 1

        if bs2 > 0 and bfma[i - 1] >= bs2ma[i - 1] and bfma[i] < bs2ma[i]:
            b2wait = -1

        if active == 2:
            b1pos, b2pos = b1wait, b2wait
        if active != 2 and b1pos * b1wait == -1:
            b1pos = 0
        if active != 2 and b2pos * b2wait == -1:
            b2pos = 0

        if ob1pos != b1pos:
            volume += b1pos - ob1pos
            notes += "B1:" + str(ob1pos) + "->" + str(b1pos) + ";" + str(b1price) + "->" + str(price["rmb"]) + ";"
            b1price = price["rmb"]

        if ob2pos != b2pos:
            volume += b2pos - ob2pos
            notes += "B2:" + str(ob2pos) + "->" + str(b2pos) + ";" + str(b2price) + "->" + str(price["rmb"]) + ";"
            b2price = price["rmb"]

        if volume != 0:
            t.processOrder(price["dt"], price["rmb"], volume * 10, notes=notes)
        else:
            t.summary(price["dt"], price["rmb"])

    pool.estimate(t)
    return t
コード例 #7
0
ファイル: s6Trader.py プロジェクト: zjw0358/pymisc
def doTrade(pool, direction, lPeriod, sPeriod, profitLevel, lossLevel):
	global prices, ps, phs, pls, rsis
	
	sname = 'S5_' + str(direction) + '_' + str(lPeriod) + '_' + str(sPeriod) + '_' + str(profitLevel) + '_' + str(-lossLevel)
	
	t = Trader(sname)
	front = lPeriod + sPeriod - 1
	lastPrice = 0
	for i in range(front, len(prices)):
		price = prices[i]
		volume, notes = 0, ''
		
		slope = midy = None
		highSlope = highy = lowSlope = lowy = None
		nSlope, ny = calc_slope(ps[i - sPeriod + 1 : i + 1])
		
		#if (t.position > 0 and nSlope < 0) or (t.position < 0 and nSlope > 0):
		#	notes = 'CLOSE'
		#	volume = 0 - t.position
	
		if t.position > 0 and price['rmb'] < prices[i-1]['rmb']:
			profit = (price['rmb'] - lastPrice) * 100 / lastPrice
			
			if profit >= profitLevel:
				notes = 'PROFIT'
				volume = 0 - t.position
			elif profit <= lossLevel:
				notes = 'LOSS'
				volume = 0 - t.position
				
		elif t.position < 0 and price['rmb'] > prices[i-1]['rmb']:
			profit = (lastPrice - price['rmb']) * 100 / lastPrice
			
			if profit >= profitLevel:
				notes = 'PROFIT'
				volume = 0 - t.position
			elif profit <= lossLevel:
				notes = 'LOSS'
				volume = 0 - t.position
				
		elif t.position == 0:
			if (direction == 1 and nSlope > 0) or (direction == -1 and nSlope < 0):
				ys = ps[i - lPeriod - sPeriod + 2 : i - sPeriod + 2]
				slope, midy = calc_slope(ys)
				std = np.std(ys, dtype=np.float64, ddof=0)
				x = lPeriod + sPeriod - 2
				p = ps[i]
				mid = midy(x)
				high = mid + std * 1.0
				low = mid - std * 1.0
				py = ny(sPeriod)
				
				if direction == 1 and py > high and nSlope > 0 and t.position <= 0:
					notes = 'BUY'
					volume = 1
				elif direction == -1 and py < low and nSlope < 0 and t.position >= 0:
					notes = 'SELL'
					volume = -1
				
		if volume != 0:
			lastPrice = price['rmb']
			t.processOrder(price['dt'], price['rmb'], volume, notes=notes)
		else:
			t.summary(price['dt'], price['rmb'])
		
		#logb.info(str(i) + ',' + str(price['dt']) + ',' + str(std) + ',' + str(notes) + ',' + str(volume)
		#			+ ',' + str(p) + ',' + str(low) + ',' + str(mid) + ',' + str(high) + ',' + str(price['rmb'])
		#			+ ',' + str(slope) + ',' + str(nSlope)+ ',' + str(highSlope) + ',' + str(lowSlope))
		
		if volume != 0 and i >= 10000000:
			print i, notes, price['rmb']
		
			nxs = nys = []
			if ny:
				nxs = range(sPeriod)
				nys = map(ny, nxs)
				
				nxs = map(lambda x: x + lPeriod - 1, nxs)
			
			xs = upys = lowys = midys = []
			if midy:
				xs = range(lPeriod)
				midys = map(midy, xs)
				upys = map(lambda y: y + std * 1.0, midys)
				lowys = map(lambda y: y - std * 1.0, midys)
			
			axs = range(lPeriod + sPeriod + 100)
			ays = ps[i - lPeriod - sPeriod + 2 : i + 102]
			if lPeriod + sPeriod + 100 > len(ps):
				axs = range(len(ps) - lPeriod - sPeriod + 2)
				ays = ps[i - lPeriod - sPeriod + 2 : ]
			plt.plot(axs, ays, 'b', xs, midys, 'y', xs, upys, 'r', xs, lowys, 'g', nxs, nys, 'm')
			plt.vlines(lPeriod + sPeriod - 2, min(ays), max(ays), color='y', linestyles ='dotted')
			plt.title(str(price['dt']) + ' ' + str(volume) + ' ' + str(notes))
			plt.show()
	
	pool.estimate(t)
	return t
コード例 #8
0
ファイル: s5Trader.py プロジェクト: zjw0358/pymisc
def doTrade(pool, direction, lPeriod, sPeriod, profitLevel, lossLevel):
    global prices, ps, phs, pls, rsis

    sname = 'S5_' + str(direction) + '_' + str(lPeriod) + '_' + str(
        sPeriod) + '_' + str(profitLevel) + '_' + str(-lossLevel)

    t = Trader(sname)
    front = lPeriod + sPeriod - 1
    lastPrice = 0
    for i in range(front, len(prices)):
        price = prices[i]
        volume, notes = 0, ''

        slope = midy = None
        highSlope = highy = lowSlope = lowy = None
        nSlope = ny = None

        #cSlope = cy = None
        if t.position > 0 and price['rmb'] < prices[i - 1]['rmb']:
            profit = (price['rmb'] - lastPrice) * 100 / lastPrice

            if profit >= profitLevel:
                notes = 'PROFIT'
                volume = 0 - t.position
            elif profit <= lossLevel:
                notes = 'LOSS'
                volume = 0 - t.position

        elif t.position < 0 and price['rmb'] > prices[i - 1]['rmb']:
            profit = (lastPrice - price['rmb']) * 100 / lastPrice

            if profit >= profitLevel:
                notes = 'PROFIT'
                volume = 0 - t.position
            elif profit <= lossLevel:
                notes = 'LOSS'
                volume = 0 - t.position

        elif t.position == 0:
            nSlope, ny = calc_slope(ps[i - sPeriod + 1:i + 1])

            if (direction == 1 and nSlope > 0) or (direction == -1
                                                   and nSlope < 0):
                ys = ps[i - lPeriod - sPeriod + 2:i - sPeriod + 2]
                slope, midy = calc_slope(ys)
                #print i - lPeriod - sPeriod + 2,  i - sPeriod + 2
                highSlope, highy, lowSlope, lowy = calc_bands(ys)

                #print i - lPeriod - sPeriod + 2,  i - sPeriod + 2
                x = lPeriod + sPeriod - 2
                p = ps[i]
                high, low, mid = highy(x), lowy(x), midy(x)

                if direction == 1 and p > high and nSlope > 0 and high > mid and mid > low and t.position <= 0:
                    notes = 'BUY'
                    volume = 1
                elif direction == -1 and p < low and nSlope < 0 and high > mid and mid > low and t.position >= 0:
                    notes = 'SELL'
                    volume = -1

        if volume != 0:
            lastPrice = price['rmb']
            t.processOrder(price['dt'], price['rmb'], volume, notes=notes)
        else:
            t.summary(price['dt'], price['rmb'])

        #logb.info(str(i) + ',' + str(price['dt']) + ',' + str(std) + ',' + str(notes) + ',' + str(volume)
        #			+ ',' + str(p) + ',' + str(low) + ',' + str(mid) + ',' + str(high) + ',' + str(price['rmb'])
        #			+ ',' + str(slope) + ',' + str(nSlope)+ ',' + str(highSlope) + ',' + str(lowSlope))

        if volume != 0 and i >= 1:
            print i, rsis['rsi'][i]

            nxs = nys = []
            if ny:
                nxs = range(sPeriod)
                nys = map(ny, nxs)
                nxs = map(lambda x: x + lPeriod - 1, nxs)

            xs = upys = lowys = midys = []
            if highy:
                xs = range(lPeriod)
                upys = map(highy, xs)
                lowys = map(lowy, xs)
                midys = map(midy, xs)

            #cxs = cys = []
            #if cy:
            #	cxs = range(cPeriod)
            #	cys = map(cy, cxs)
            #	cxs = map(lambda x: x + lPeriod - 1, cxs)

            axs = range(lPeriod + sPeriod + 100)
            ays = ps[i - lPeriod - sPeriod + 2:i + 102]
            if lPeriod + sPeriod + 100 > len(ps):
                axs = range(len(ps) - lPeriod - sPeriod + 2)
                ays = ps[i - lPeriod - sPeriod + 2:]
            plt.plot(axs, ays, 'b', xs, midys, 'y', xs, upys, 'r', xs, lowys,
                     'g', nxs, nys, 'm')
            plt.vlines(lPeriod + sPeriod - 2,
                       min(ays),
                       max(ays),
                       color='y',
                       linestyles='dotted')
            plt.title(str(price['dt']) + ' ' + str(volume) + ' ' + str(notes))
            plt.show()

    pool.estimate(t)
    return t
コード例 #9
0
ファイル: s4Trader.py プロジェクト: zjw0358/pymisc
def doTrade(pool, lPeriod, sPeriod, cPeriod):
	global prices, ps, phs, pls
	
	sname = 'S4_' + str(lPeriod) + '_' + str(sPeriod) + '_' + str(cPeriod)
	
	t = Trader(sname)
	front = lPeriod + max(sPeriod, cPeriod) - 1
	lastbuy = lastsell = 0
	for i in range(front, len(prices)):
		price = prices[i]
		
		ys = ps[i - lPeriod - sPeriod + 2 : i - sPeriod + 2]
		slope, midy = calc_slope(ys)
		highSlope, highy, lowSlope, lowy = calc_bands(ys)
		nSlope, ny = calc_slope(ps[i - sPeriod + 1 : i + 1])
		
		#std = round(np.std(ys, dtype=np.float64, ddof=0), 3)
		#if i == 122: print i - lPeriod - sPeriod + 1, i - sPeriod + 1, highSlope, lowSlope
		
		x = lPeriod + sPeriod - 2
		p = ps[i]
		volume = 0
		notes = ''
		high, low, mid = highy(x), lowy(x), midy(x)
		
		if p > high and nSlope > 0 and high > mid and mid > low and t.position <= 0:
			notes = 'BUY'
			volume = 0
		elif p < low and nSlope < 0 and high > mid and mid > low and t.position >= 0:
			notes = 'SELL'
			volume = -1
			
		cSlope = cy = None
		if t.position > 0:
			cSlope, cy = calc_slope(ps[i - cPeriod + 1 : i + 1])
			if cSlope < 0:
				notes += '&CLOSE'
				volume -= t.position
		elif t.position < 0:
			cSlope, cy = calc_slope(ps[i - cPeriod + 1 : i + 1])
			if cSlope > 0:
				notes += '&CLOSE'
				volume -= t.position
		
		#logb.info(str(i) + ',' + str(price['dt']) + ',' + str(std) + ',' + str(notes) + ',' + str(volume)
		#			+ ',' + str(p) + ',' + str(low) + ',' + str(mid) + ',' + str(high) + ',' + str(price['rmb'])
		#			+ ',' + str(slope) + ',' + str(nSlope)+ ',' + str(highSlope) + ',' + str(lowSlope))
		
		if volume != 0 and i >= 100000000:
			xs = range(lPeriod)
			upys = map(highy, xs)
			lowys = map(lowy, xs)
			midys = map(midy, xs)
			nxs = range(sPeriod)
			nys = map(ny, nxs)
			nxs = map(lambda x: x + lPeriod - 1, nxs)
			
			cxs = cys = []
			if cy:
				cxs = range(cPeriod)
				cys = map(cy, cxs)
				cxs = map(lambda x: x + lPeriod - 1, cxs)
			
			axs = range(lPeriod + sPeriod + 100)
			ays = ps[i - lPeriod - sPeriod + 2 :i + 102]
			plt.plot(axs, ays, 'b', xs, midys, 'y', xs, upys, 'r', xs, lowys, 'g', nxs, nys, 'm', cxs, cys, 'k')
			plt.title(str(price['dt']) + ' ' + str(volume))
			plt.show()
			
		t.processOrder(price['dt'], price['rmb'], volume, notes=notes)
	
	pool.estimate(t)
	return t