Exemplo n.º 1
0
def doTrade(pool, prices, ps, fast, slow, rsiPeriod, rsiGuage):
	
	sname = 'L2_' + str(fast) + '_' + str(slow) + '_' + str(rsiPeriod) + '_' + str(rsiGuage)
	macds = macd.calc_macd(prices, fast, slow, 7)
	rsis = rsi.calc_rsi(prices, rsiPeriod)
	
	front = max(fast, slow, rsiPeriod)
	
	t = Trader(sname)
	
	direct = odirect = 0
	for i in range(front, len(prices)):
		#print macds['macd'][i], rsis['rsi'][i], odirect , direct
		price = prices[i]
		
		if macds['macd'][i] > 0 and rsis['rsi'][i] > rsiGuage:
			direct = 1
		elif macds['macd'][i] < 0 and rsis['rsi'][i] < rsiGuage:
			direct = -1
		
		volume = 0
		if odirect == -1 and direct == 1:
			volume = 1
		elif odirect == 1 and direct == -1:
			volume = -1
			
		odirect = direct
		t.processOrder(price['dt'], price['rmb'], volume * 1000, cntNo=0, notes='')
	
	pool.estimate(t)
	return
Exemplo n.º 2
0
def doMacdTrade(pool, prices, ps, fast, slow, sign):
	global highest, openLevel, closeLevel
	
	sname = 'MACD_' + str(fast) + '_' + str(slow) + '_' + str(sign)
	macds = macd.calc_macd(prices, fast, slow, sign)
	mas = ma.calc_ema(ps, slow)
	
	t = Trader(sname)
	for i in range(slow + sign, len(prices)):
		if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and macds['macd'][i-1] < macds['sign'][i-1] and abs(macds['macd'][i]) > openLevel and mas[i] > mas[i-1]:
			notes = 'macd under 0, and abs larger than openlevel'
			t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes)
			
		if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and macds['macd'][i-1] < macds['sign'][i-1] and abs(macds['macd'][i]) > closeLevel and mas[i] > mas[i-1]:
			notes = 'macd under 0, and abs larger than closelevel'
			t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True)
		
		if macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and macds['macd'][i-1] > macds['sign'][i-1] and abs(macds['macd'][i]) > openLevel and mas[i] < mas[i-1]:
			notes = 'macd above 0, and abs larger than openlevel'
			t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes)
	
		if macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and macds['macd'][i-1] > macds['sign'][i-1] and abs(macds['macd'][i]) > closeLevel and mas[i] < mas[i-1]:
			notes = 'macd above 0, and abs larger than closeLevel'
			t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True)
			
		t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb'])
	
	pool.estimate(t)
	return
Exemplo n.º 3
0
Arquivo: main.py Projeto: ongbe/pymisc
def drawStat(prices, period):

    l = len(prices)
    ps = [0] * l
    pdts = [0] * l
    std = [0] * l
    stdper = [0] * l
    diff = [0] * l
    dmean = [0] * l

    days = 0
    for i in range(l):
        pdts[i] = prices[i]["dt"]
        ps[i] = prices[i]["close"]
        diff[i] = prices[i]["high"] - prices[i]["low"]
        if i < period - 1:
            continue
        std[i] = round(np.std(ps[i - period + 1 : i + 1], dtype=np.float64, ddof=0), 3)
        stdper[i] = round(std[i] / np.mean(ps[i - period + 1 : i + 1]), 3)
        dmean[i] = round(np.mean(diff[i - period + 1 : i + 1]), 3)

        if (std[i - 1] < 1 and std[i] >= 1) or (std[i - 1] > 1 and std[i] <= 1):
            dtstr = prices[i]["dt"].strftime("%Y-%m-%d")
            log.info(dtstr + ", std change to " + str(std[i]) + ", days: " + str(days))
            days = 0
        days += 1

    macds = macd.calc_macd(prices, 12, 26, 9)

    fig = plt.figure()
    ax1 = fig.add_subplot(311)
    ax1.set_ylabel("Price")
    ax1.grid()
    ax1.plot_date(pdts, ps, color="b", linestyle="-", marker="", label="Equity")

    ax2 = fig.add_subplot(312)
    ax2.set_ylabel("Std")
    ax2.grid()
    ax2.plot_date(pdts, std, color="b", linestyle="-", marker="", label="Equity")

    ax3 = fig.add_subplot(313)
    ax3.set_ylabel("MACD")
    ax3.grid()
    ax3.plot_date(pdts, stdper, color="b", linestyle="-", marker="", label="Equity")

    # multi = MultiCursor(fig.canvas, (ax1, ax2, ax3), color='r', lw=1, horizOn=False, vertOn=True)
    # plt.show()
    # return
    fname = str(period)

    plt.savefig(os.path.join(os.path.dirname(__file__), "result/" + fname + ".png"), dpi=150)
    plt.close(fig)
    return
Exemplo n.º 4
0
def drawStat(prices, period):
	
	l = len(prices)
	ps = [0] * l
	pdts = [0] * l
	std = [0] * l
	stdper = [0] * l
	diff = [0] * l
	dmean = [0] * l
	
	days = 0
	for i in range(l):
		pdts[i] = prices[i]['dt']
		ps[i] = prices[i]['close']
		diff[i] = prices[i]['high'] - prices[i]['low']
		if i < period - 1: continue
		std[i] = round(np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0), 3)
		stdper[i] = round(std[i] / np.mean(ps[i-period+1 : i+1]), 3)
		dmean[i] = round(np.mean(diff[i-period+1 : i+1]), 3)
		
		if (std[i-1] < 1 and std[i] >= 1) or (std[i-1] > 1 and std[i] <= 1):
			dtstr = prices[i]['dt'].strftime('%Y-%m-%d')
			log.info(dtstr + ', std change to ' + str(std[i]) + ', days: ' + str(days))
			days = 0
		days += 1
		
	macds = macd.calc_macd(prices, 12, 26, 9)
	
	fig = plt.figure()
	ax1 = fig.add_subplot(311)
	ax1.set_ylabel('Price')
	ax1.grid()
	ax1.plot_date(pdts, ps, color='b', linestyle='-', marker='', label='Equity')
	
	ax2 = fig.add_subplot(312)
	ax2.set_ylabel('Std')
	ax2.grid()
	ax2.plot_date(pdts, std, color='b', linestyle='-', marker='', label='Equity')
	
	ax3 = fig.add_subplot(313)
	ax3.set_ylabel('MACD')
	ax3.grid()
	ax3.plot_date(pdts, stdper, color='b', linestyle='-', marker='', label='Equity')
	
	#multi = MultiCursor(fig.canvas, (ax1, ax2, ax3), color='r', lw=1, horizOn=False, vertOn=True)
	#plt.show()
	#return
	fname = str(period)
		
	plt.savefig(os.path.join(os.path.dirname(__file__), 'result/' + fname + '.png'), dpi=150)
	plt.close(fig)
	return
Exemplo n.º 5
0
def genMacdOps(prices, fast, slow, sign):
    openLevel, closeLevel = 0.2, 0.1
    macds = macd.calc_macd(prices, fast, slow, sign)
    front = slow + sign

    l = len(prices)
    ops = [0] * l
    for i in range(front, l):
        if macds['sign'][i] > 0.1 and macds['macd'][i] > macds['sign'][i]:
            ops[i] = 1
        elif macds['macd'][i] > 0.1 and macds['macd'][i] < macds['sign'][i]:
            ops[i] = -1
        if macds['sign'][i] < -0.1 and macds['macd'][i] < macds['sign'][i]:
            ops[i] = -1
        elif macds['macd'][i] < -0.1 and macds['macd'][i] > macds['sign'][i]:
            ops[i] = 1

    return ops
Exemplo n.º 6
0
def genMacdOps(prices, fast, slow, sign):
	openLevel, closeLevel = 0.2, 0.1
	macds = macd.calc_macd(prices, fast, slow, sign)
	front = slow + sign
	
	l = len(prices)
	ops = [0] * l
	for i in range(front, l):
		if macds['sign'][i] > 0.1 and macds['macd'][i] > macds['sign'][i]:
			ops[i] = 1
		elif macds['macd'][i] > 0.1 and macds['macd'][i] < macds['sign'][i]:
			ops[i] = -1
		if macds['sign'][i] < -0.1 and macds['macd'][i] < macds['sign'][i]:
			ops[i] = -1
		elif macds['macd'][i] < -0.1 and macds['macd'][i] > macds['sign'][i]:
			ops[i] = 1
			
	return ops
Exemplo n.º 7
0
def genMacdOps2(prices, fast, slow, sign):
	openLevel, closeLevel = 0.2, 0.1
	macds = macd.calc_macd(prices, fast, slow, sign)
	front = slow + sign
	
	l = len(prices)
	ops = [0] * l
	for i in range(front, l):
		if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and abs(macds['macd'][i]) > openLevel:
			ops[i] = 1
		elif ops[i-1] == -1 and macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and abs(macds['macd'][i]) > closeLevel:
			ops[i] = 0
		elif macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and abs(macds['macd'][i]) > openLevel:
			ops[i] = -1
		elif ops[i-1] == 1 and macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and abs(macds['macd'][i]) > closeLevel:
			ops[i] = 0
		else:
			ops[i] = ops[i-1]
	return ops
Exemplo n.º 8
0
def genMacdOps2(prices, fast, slow, sign):
	openLevel, closeLevel = 0.2, 0.1
	macds = macd.calc_macd(prices, fast, slow, sign)
	front = slow + sign
	
	l = len(prices)
	ops = [0] * l
	for i in range(front, l):
		if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and abs(macds['macd'][i]) > openLevel:
			ops[i] = 1
		elif ops[i-1] == -1 and macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and abs(macds['macd'][i]) > closeLevel:
			ops[i] = 0
		elif macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and abs(macds['macd'][i]) > openLevel:
			ops[i] = -1
		elif ops[i-1] == 1 and macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and abs(macds['macd'][i]) > closeLevel:
			ops[i] = 0
		else:
			ops[i] = ops[i-1]
	return ops
Exemplo n.º 9
0
def calcTickets(pool, prices, fast, slow, sign):
	vname = 'MACD_' + str(fast) + '_' + str(slow) + '_' + str(sign)
	macds = macd.calc_macd(prices, fast, slow, sign)
	front = slow + sign
	
	l = len(prices)
	ops = [0] * l
	for i in range(front, l):
		if macds['sign'][i] > 0.1 and macds['macd'][i] > macds['sign'][i]:
			ops[i] = 1
		elif macds['macd'][i] > 0.1 and macds['macd'][i] < macds['sign'][i]:
			ops[i] = -1
		if macds['sign'][i] < -0.1 and macds['macd'][i] < macds['sign'][i]:
			ops[i] = -1
		elif macds['macd'][i] < -0.1 and macds['macd'][i] > macds['sign'][i]:
			ops[i] = 1
	
	pool.estimate(vname, ops, front)

		
Exemplo n.º 10
0
def calcTickets2(pool, prices, fast, slow, sign):
	vname = 'MACD_' + str(fast) + '_' + str(slow) + '_' + str(sign)
	macds = macd.calc_macd(prices, fast, slow, sign)
	front = slow + sign
	
	l = len(prices)
	ops = [0] * l
	for i in range(front, l):
		if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and abs(macds['macd'][i]) > openLevel:
			ops[i] = 1
		elif ops[i-1] == -1 and macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and abs(macds['macd'][i]) > closeLevel:
			ops[i] = 0
		elif macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and abs(macds['macd'][i]) > openLevel:
			ops[i] = -1
		elif ops[i-1] == 1 and macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and abs(macds['macd'][i]) > closeLevel:
			ops[i] = 0
		else:
			ops[i] = ops[i-1]
	
	pool.estimate(vname, ops, front)
Exemplo n.º 11
0
def doTrade(pool, prices, ps, fast, slow, rsiPeriod, rsiGuage):

    sname = 'L2_' + str(fast) + '_' + str(slow) + '_' + str(
        rsiPeriod) + '_' + str(rsiGuage)
    macds = macd.calc_macd(prices, fast, slow, 7)
    rsis = rsi.calc_rsi(prices, rsiPeriod)

    front = max(fast, slow, rsiPeriod)

    t = Trader(sname)

    direct = odirect = 0
    for i in range(front, len(prices)):
        #print macds['macd'][i], rsis['rsi'][i], odirect , direct
        price = prices[i]

        if macds['macd'][i] > 0 and rsis['rsi'][i] > rsiGuage:
            direct = 1
        elif macds['macd'][i] < 0 and rsis['rsi'][i] < rsiGuage:
            direct = -1

        volume = 0
        if odirect == -1 and direct == 1:
            volume = 1
        elif odirect == 1 and direct == -1:
            volume = -1

        odirect = direct
        t.processOrder(price['dt'],
                       price['rmb'],
                       volume * 1000,
                       cntNo=0,
                       notes='')

    pool.estimate(t)
    return
Exemplo n.º 12
0
def drawStat(prices, period):
    l = len(prices)
    print l
    ps = [p['close'] for p in prices]
    print slope(ps)
    ps = np.array(ps)
    #ts = [p['dtlong'] - prices[0]['dtlong'] for p in prices]
    #ts = np.array(ts)
    ts = np.arange(l)
    z = np.polyfit(ts[0:300], ps[0:300], 1)
    zp = np.poly1d(z)
    print z
    z30 = np.polyfit(ts[0:290], ps[0:290], 10)
    zp30 = np.poly1d(z30)
    print z30
    plt.plot(ts, ps, '-', ts, zp(ts), '-', ts, zp30(ts), '--')
    #plt.plot(ts, ps, 'o', label='Original data', linestyle='-', markersize=2)
    #plt.plot(ts, m * ts + c, 'r', label='Fitted line')
    #plt.legend()
    plt.show()
    return

    l = len(prices)
    ps = [0] * l
    pdts = [0] * l
    std = [0] * l
    stdper = [0] * l
    diff = [0] * l
    dmean = [0] * l

    days = 0
    for i in range(l):
        pdts[i] = prices[i]['dt']
        ps[i] = prices[i]['close']
        diff[i] = prices[i]['high'] - prices[i]['low']
        if i < period - 1: continue
        std[i] = round(
            np.std(ps[i - period + 1:i + 1], dtype=np.float64, ddof=0), 3)
        stdper[i] = round(std[i] / np.mean(ps[i - period + 1:i + 1]), 3)
        dmean[i] = round(np.mean(diff[i - period + 1:i + 1]), 3)

        if (std[i - 1] < 1 and std[i] >= 1) or (std[i - 1] > 1
                                                and std[i] <= 1):
            dtstr = prices[i]['dt'].strftime('%Y-%m-%d')
            log.info(dtstr + ', std change to ' + str(std[i]) + ', days: ' +
                     str(days))
            days = 0
        days += 1

    macds = macd.calc_macd(prices, 12, 26, 9)

    fig = plt.figure()
    ax1 = fig.add_subplot(311)
    ax1.set_ylabel('Price')
    ax1.grid()
    ax1.plot_date(pdts,
                  ps,
                  color='b',
                  linestyle='-',
                  marker='',
                  label='Equity')

    ax2 = fig.add_subplot(312)
    ax2.set_ylabel('Std')
    ax2.grid()
    ax2.plot_date(pdts,
                  std,
                  color='b',
                  linestyle='-',
                  marker='',
                  label='Equity')

    ax3 = fig.add_subplot(313)
    ax3.set_ylabel('MACD')
    ax3.grid()
    ax3.plot_date(pdts,
                  stdper,
                  color='b',
                  linestyle='-',
                  marker='',
                  label='Equity')

    multi = MultiCursor(fig.canvas, (ax1, ax2, ax3),
                        color='r',
                        lw=1,
                        horizOn=False,
                        vertOn=True)
    plt.show()
    return
    fname = str(period)

    plt.savefig(os.path.join(os.path.dirname(__file__),
                             'result/' + fname + '.png'),
                dpi=150)
    plt.close(fig)
    return
Exemplo n.º 13
0
def drawStat(prices, period):

    l = len(prices)
    ps = [0] * l
    pdts = [0] * l
    std = [0] * l
    stdper = [0] * l
    diff = [0] * l
    dmean = [0] * l

    days = 0
    for i in range(l):
        pdts[i] = prices[i]['dt']
        ps[i] = prices[i]['close']
        diff[i] = prices[i]['high'] - prices[i]['low']
        if i < period - 1: continue
        std[i] = round(
            np.std(ps[i - period + 1:i + 1], dtype=np.float64, ddof=0), 3)
        stdper[i] = round(std[i] / np.mean(ps[i - period + 1:i + 1]), 3)
        dmean[i] = round(np.mean(diff[i - period + 1:i + 1]), 3)

        if (std[i - 1] < 1 and std[i] >= 1) or (std[i - 1] > 1
                                                and std[i] <= 1):
            dtstr = prices[i]['dt'].strftime('%Y-%m-%d')
            log.info(dtstr + ', std change to ' + str(std[i]) + ', days: ' +
                     str(days))
            days = 0
        days += 1

    macds = macd.calc_macd(prices, 12, 26, 9)

    fig = plt.figure()
    ax1 = fig.add_subplot(311)
    ax1.set_ylabel('Price')
    ax1.grid()
    ax1.plot_date(pdts,
                  ps,
                  color='b',
                  linestyle='-',
                  marker='',
                  label='Equity')

    ax2 = fig.add_subplot(312)
    ax2.set_ylabel('Std')
    ax2.grid()
    ax2.plot_date(pdts,
                  std,
                  color='b',
                  linestyle='-',
                  marker='',
                  label='Equity')

    ax3 = fig.add_subplot(313)
    ax3.set_ylabel('MACD')
    ax3.grid()
    ax3.plot_date(pdts,
                  stdper,
                  color='b',
                  linestyle='-',
                  marker='',
                  label='Equity')

    #multi = MultiCursor(fig.canvas, (ax1, ax2, ax3), color='r', lw=1, horizOn=False, vertOn=True)
    #plt.show()
    #return
    fname = str(period)

    plt.savefig(os.path.join(os.path.dirname(__file__),
                             'result/' + fname + '.png'),
                dpi=150)
    plt.close(fig)
    return
Exemplo n.º 14
0
def drawStat(prices, period):
	l = len(prices)
	print l
	ps = [p['close'] for p in prices]
	print slope(ps)
	ps = np.array(ps)
	#ts = [p['dtlong'] - prices[0]['dtlong'] for p in prices]
	#ts = np.array(ts)
	ts = np.arange(l)
	z = np.polyfit(ts[0:300], ps[0:300], 1)
	zp = np.poly1d(z)
	print z
	z30 = np.polyfit(ts[0:290], ps[0:290], 10)
	zp30 = np.poly1d(z30)
	print z30
	plt.plot(ts, ps, '-', ts, zp(ts), '-', ts, zp30(ts), '--')
	#plt.plot(ts, ps, 'o', label='Original data', linestyle='-', markersize=2)
	#plt.plot(ts, m * ts + c, 'r', label='Fitted line')
	#plt.legend()
	plt.show()
	return
	
	l = len(prices)
	ps = [0] * l
	pdts = [0] * l
	std = [0] * l
	stdper = [0] * l
	diff = [0] * l
	dmean = [0] * l
	
	days = 0
	for i in range(l):
		pdts[i] = prices[i]['dt']
		ps[i] = prices[i]['close']
		diff[i] = prices[i]['high'] - prices[i]['low']
		if i < period - 1: continue
		std[i] = round(np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0), 3)
		stdper[i] = round(std[i] / np.mean(ps[i-period+1 : i+1]), 3)
		dmean[i] = round(np.mean(diff[i-period+1 : i+1]), 3)
		
		if (std[i-1] < 1 and std[i] >= 1) or (std[i-1] > 1 and std[i] <= 1):
			dtstr = prices[i]['dt'].strftime('%Y-%m-%d')
			log.info(dtstr + ', std change to ' + str(std[i]) + ', days: ' + str(days))
			days = 0
		days += 1
		
	macds = macd.calc_macd(prices, 12, 26, 9)
	
	fig = plt.figure()
	ax1 = fig.add_subplot(311)
	ax1.set_ylabel('Price')
	ax1.grid()
	ax1.plot_date(pdts, ps, color='b', linestyle='-', marker='', label='Equity')
	
	ax2 = fig.add_subplot(312)
	ax2.set_ylabel('Std')
	ax2.grid()
	ax2.plot_date(pdts, std, color='b', linestyle='-', marker='', label='Equity')
	
	ax3 = fig.add_subplot(313)
	ax3.set_ylabel('MACD')
	ax3.grid()
	ax3.plot_date(pdts, stdper, color='b', linestyle='-', marker='', label='Equity')
	
	multi = MultiCursor(fig.canvas, (ax1, ax2, ax3), color='r', lw=1, horizOn=False, vertOn=True)
	plt.show()
	return
	fname = str(period)
		
	plt.savefig(os.path.join(os.path.dirname(__file__), 'result/' + fname + '.png'), dpi=150)
	plt.close(fig)
	return