示例#1
0
def genMaOps(ps, ft, f, st, s):
	
	if ft == 'MA':
		fma = ma.calc_ma(ps, f)
	elif ft == 'EMA':
		fma = ma.calc_ema(ps, f)
	elif ft == 'SMA':
		fma = ma.calc_sma(ps, f)
	elif ft == 'LWMA':
		fma = ma.calc_lwma(ps, f)
		
	if st == 'MA':
		sma = ma.calc_ma(ps, s)
	elif st == 'EMA':
		sma = ma.calc_ema(ps, s)
	elif st == 'SMA':
		sma = ma.calc_sma(ps, s)
	elif st == 'LWMA':
		sma = ma.calc_lwma(ps, s)
		
	
	l = len(ps)
	ops = [0] * l
	for i in range(s, l):
		diff = fma[i] - sma[i]
		if diff > 0:
			ops[i] = 1
		elif diff < 0:
			ops[i] = -1
			
	return ops
	
示例#2
0
def genMaOps(ps, ft, f, st, s):

    if ft == 'MA':
        fma = ma.calc_ma(ps, f)
    elif ft == 'EMA':
        fma = ma.calc_ema(ps, f)
    elif ft == 'SMA':
        fma = ma.calc_sma(ps, f)
    elif ft == 'LWMA':
        fma = ma.calc_lwma(ps, f)

    if st == 'MA':
        sma = ma.calc_ma(ps, s)
    elif st == 'EMA':
        sma = ma.calc_ema(ps, s)
    elif st == 'SMA':
        sma = ma.calc_sma(ps, s)
    elif st == 'LWMA':
        sma = ma.calc_lwma(ps, s)

    l = len(ps)
    ops = [0] * l
    for i in range(s, l):
        diff = fma[i] - sma[i]
        if diff > 0:
            ops[i] = 1
        elif diff < 0:
            ops[i] = -1

    return ops
示例#3
0
文件: kdj.py 项目: 547872495/pymisc
def calc_kd(prices, kPeriod = 5, dPeriod = 3, slowing =3):
	phighs = [p['high'] for p in prices]
	plows = [p['low'] for p in prices]
	ps = [p['close'] for p in prices]
	l = len(prices)
	highest = [0] * l
	lowest = [0] * l
	kds = {}
	kds['k'] = [0] * l
	for i in range(kPeriod - 1, l):
		highest[i] = max(phighs[i-kPeriod+1 : i+1])
		lowest[i] = min(plows[i-kPeriod+1 : i+1])
		if i >= kPeriod + slowing - 1:
			highsum, lowsum = 0, 0
			for j in range(i - slowing + 1, i + 1):
				highsum += highest[j] - lowest[j]
				lowsum += ps[j] - lowest[j]
			#print i, highest[i], lowest[i], prices[j], highsum, lowsum
			#print i, highsum
			if highsum != 0:
				kds['k'][i] = round(lowsum / highsum * 100, 5)
				#print i, kds['k'][i]
	#print kds['k']
	kds['d'] = ma.calc_ma(kds['k'], dPeriod)
	
	return kds
示例#4
0
def calc_kd(prices, kPeriod=5, dPeriod=3, slowing=3):
    phighs = [p['high'] for p in prices]
    plows = [p['low'] for p in prices]
    ps = [p['close'] for p in prices]
    l = len(prices)
    highest = [0] * l
    lowest = [0] * l
    kds = {}
    kds['k'] = [0] * l
    for i in range(kPeriod - 1, l):
        highest[i] = max(phighs[i - kPeriod + 1:i + 1])
        lowest[i] = min(plows[i - kPeriod + 1:i + 1])
        if i >= kPeriod + slowing - 1:
            highsum, lowsum = 0, 0
            for j in range(i - slowing + 1, i + 1):
                highsum += highest[j] - lowest[j]
                lowsum += ps[j] - lowest[j]
            #print i, highest[i], lowest[i], prices[j], highsum, lowsum
            #print i, highsum
            if highsum != 0:
                kds['k'][i] = round(lowsum / highsum * 100, 5)
                #print i, kds['k'][i]
    #print kds['k']
    kds['d'] = ma.calc_ma(kds['k'], dPeriod)

    return kds
示例#5
0
def runStrategy(in_prices):
	global mas, emas, smas, lwmas, std, prices
	log.debug('beginning ma strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	std = [0] * 51
	l = len(prices)
	for period in range(2, 51):
		std[period] = [0] * l
		for i in range(period - 1, l):
			std[period][i] = np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0)
		
	mas = [0] * 81
	emas = [0] * 81
	smas = [0] * 81
	lwmas = [0] * 81
	for period in range(2, 81):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
		
	pool = Pool(100)
	#t = doTrade(pool, 25, 1.0, 'MA', 7, 'SMA', 12, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 13, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#pool.showStrategies()
	#return 
	
	log.debug('running ma strategy ...')
	starttime = datetime.datetime.now() 
	matypes = ['MA', 'EMA', 'SMA', 'LWMA']
	
	farr = range(5, 20)
	s1arr = range(8, 40)
	s2arr = range(0, 80)
	
	poola = Pool(10)
	poolb = Pool(10)
	for stdPeriod in [20, ]:
		for stdGuage in [1.0, ]:
			log.debug('*** ' + str(stdPeriod) + ',' + str(stdGuage) + ' ***')
			for ft, f in [(matype, period) for matype in matypes for period in farr]:
				for s1t, s1 in [(matype, period) for matype in matypes for period in s1arr]:
					if s1 != 0 and s1 <= f: continue
					elapsed = (datetime.datetime.now() - starttime).seconds
					log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
					for s2t, s2 in [(matype, period) for matype in matypes for period in s2arr]:
						if s2 != 0 and s2 <= s1: continue
						if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA' or s2t == 'LWMA'): continue
						
						doTrade(poola, stdPeriod, stdGuage, ft, f, s1t, s1, s2t, s2, '', 0, '', 0, '', 0)
						doTrade(poolb, stdPeriod, stdGuage, '', 0, '', 0, '', 0, ft, f, s1t, s1, s2t, s2)
						
	poola.showStrategies()
	poolb.showStrategies()
示例#6
0
def runStrategy_2(in_prices):
    global mas, emas, smas, std, prices
    log.debug('beginning first strategy ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(2, 51):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = round(
                np.std(ps[i - period + 1:i + 1], dtype=np.float64, ddof=0), 3)

    mas = [0] * 61
    emas = [0] * 61
    smas = [0] * 61
    for period in range(2, 61):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)

    log.debug('running first strategy ...')
    starttime = datetime.datetime.now()

    strat_as = [
        ['MA', 7, 'SMA', 10, 'SMA', 12],
        ['MA', 7, 'SMA', 14, 'EMA', 33],
        ['MA', 7, 'SMA', 16, 'EMA', 27],
    ]
    strat_bs = [
        ['SMA', 7, 'MA', 12, 'MA', 12],
        ['SMA', 7, 'MA', 12, 'MA', 36],
        ['MA', 7, 'SMA', 14, 'EMA', 33],
    ]

    pool = StrategyPool(100)

    for stdPeriod in [25]:
        stdGuage = 1.3
        while stdGuage <= 1.3:
            elapsed = (datetime.datetime.now() - starttime).seconds
            log.debug('== ' + str(elapsed) + ',' + str(stdPeriod) + ',' +
                      str(stdGuage) + ' ==')

            for sa in strat_as:
                for sb in strat_bs:
                    doTrade(pool, stdPeriod, stdGuage, sa[0], sa[1], sa[2],
                            sa[3], sa[4], sa[5], sb[0], sb[1], sb[2], sb[3],
                            sb[4], sb[5])

            stdGuage += 0.02

    pool.showStrategies()

    return
示例#7
0
文件: macd.py 项目: 547872495/pymisc
def calc_macd(prices, fast = 12, slow = 26, sign = 9):
	ps = [p['close'] for p in prices]
	macds = {}
	macds['fast'] = ma.calc_ema(ps, fast)
	macds['slow'] = ma.calc_ema(ps, slow)
	macds['macd'] = map(lambda f,s: round(f - s, 5), macds['fast'], macds['slow'])
	macds['sign'] = ma.calc_ma(macds['macd'], sign)
	#macds['macd'] = map(lambda f,s: round(f - s, 5), macds['dif'], macds['sign'])
	
	return macds
示例#8
0
def calc_macd(prices, fast=12, slow=26, sign=9):
    ps = [p['close'] for p in prices]
    macds = {}
    macds['fast'] = ma.calc_ema(ps, fast)
    macds['slow'] = ma.calc_ema(ps, slow)
    macds['macd'] = map(lambda f, s: round(f - s, 5), macds['fast'],
                        macds['slow'])
    macds['sign'] = ma.calc_ma(macds['macd'], sign)
    #macds['macd'] = map(lambda f,s: round(f - s, 5), macds['dif'], macds['sign'])

    return macds
示例#9
0
def getMas(matype, period):
    if matype == 'MA':
        return ma.calc_ma(ps, period)
    elif matype == 'EMA':
        return ma.calc_ema(ps, period)
    elif matype == 'SMA':
        return ma.calc_sma(ps, period)
    elif matype == 'LWMA':
        return ma.calc_lwma(ps, period)
    else:
        return None
示例#10
0
def getMas(matype, period):
    if matype == "MA":
        return ma.calc_ma(ps, period)
    elif matype == "EMA":
        return ma.calc_ema(ps, period)
    elif matype == "SMA":
        return ma.calc_sma(ps, period)
    elif matype == "LWMA":
        return ma.calc_lwma(ps, period)
    else:
        return None
示例#11
0
def getMas(matype, period):
	if matype == 'MA':
		return ma.calc_ma(ps, period)
	elif matype == 'EMA':
		return ma.calc_ema(ps, period)
	elif matype == 'SMA':
		return ma.calc_sma(ps, period)
	elif matype == 'LWMA':
		return ma.calc_lwma(ps, period)
	else:
		return None
示例#12
0
def runStrategy_2(in_prices):
	global mas, emas, smas, std, prices
	log.debug('beginning first strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	std = [0] * 51
	l = len(prices)
	for period in range(2, 51):
		std[period] = [0] * l
		for i in range(period - 1, l):
			std[period][i] = round(np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0), 3)
		
	mas = [0] * 61
	emas = [0] * 61
	smas = [0] * 61
	for period in range(2, 61):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
	
	log.debug('running first strategy ...')
	starttime = datetime.datetime.now() 
	
	strat_as = [
['MA',7,'SMA',10,'SMA',12],
['MA',7,'SMA',14,'EMA',33],
['MA',7,'SMA',16,'EMA',27],
]
	strat_bs = [
['SMA',7,'MA',12,'MA',12 ],
['SMA',7,'MA',12,'MA',36 ],
['MA',7,'SMA',14,'EMA',33],
]
	
	pool = StrategyPool(100)
	
	for stdPeriod in [25]:
		stdGuage = 1.3
		while stdGuage <= 1.3:
			elapsed = (datetime.datetime.now() - starttime).seconds
			log.debug('== ' + str(elapsed) + ',' + str(stdPeriod) + ',' + str(stdGuage) + ' ==')
			
			for sa in strat_as:
				for sb in strat_bs:
					doTrade(pool, stdPeriod, stdGuage, sa[0], sa[1], sa[2], sa[3], sa[4], sa[5], sb[0], sb[1], sb[2], sb[3], sb[4], sb[5])
			
			stdGuage += 0.02
		
	pool.showStrategies()
	
	return
	
示例#13
0
def runVoter(prices):
    calc_directs(prices)

    ps = [p['close'] for p in prices]

    mas7 = ma.calc_ma(ps, 7)
    smas13 = ma.calc_sma(ps, 13)

    l = len(prices)
    ops = [0] * l
    stds = [0] * l
    for i in range(7, l):
        stds[i] = 1

        if mas7[i] - smas13[i] > 0:
            ops[i] = 1
        elif mas7[i] - smas13[i] < 0:
            ops[i] = -1

    goods = [0, 0]
    bads = [0, 0]
    for i in range(7, l):
        j = 0
        if stds[i] >= 1.0: j = 1
        if ops[i] == prices[i]['direct']:
            goods[j] += 1
        else:
            bads[j] += 1

    print goods[0], bads[0]
    print goods[1], bads[1]
    return
    dts = [p['dt'] for p in prices]
    directs = [p['direct'] for p in prices]

    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    ax1.plot_date(dts, directs, 'b-')
    ax1.plot_date(dts, ops, 'r-')
    ax1.set_ylim(-2, 2)

    ax2 = fig.add_subplot(212)
    ax2.plot_date(dts, ps, 'b-')

    plt.show()


#def doScan()
示例#14
0
def runVoter(prices):
	calc_directs(prices)
	
	ps = [p['close'] for p in prices]
	
	mas7 = ma.calc_ma(ps, 7)
	smas13 = ma.calc_sma(ps, 13)
	
	l = len(prices)
	ops = [0] * l
	stds = [0] * l
	for i in range(7, l):
		stds[i] = 1
		
		if mas7[i] - smas13[i] > 0:
			ops[i] = 1
		elif mas7[i] - smas13[i] < 0:
			ops[i] = -1
			
	goods = [0, 0]
	bads = [0, 0]
	for i in range(7, l):
		j = 0
		if stds[i] >= 1.0: j = 1
		if ops[i] == prices[i]['direct']:
			goods[j] += 1
		else:
			bads[j] += 1
	
	print goods[0], bads[0]
	print goods[1], bads[1]
	return
	dts = [p['dt'] for p in prices]
	directs = [p['direct'] for p in prices]
	
	fig = plt.figure()
	ax1 = fig.add_subplot(211)
	ax1.plot_date(dts, directs, 'b-')
	ax1.plot_date(dts, ops, 'r-')
	ax1.set_ylim(-2,2)
	
	ax2 = fig.add_subplot(212)
	ax2.plot_date(dts, ps, 'b-')
	
	plt.show()
	
#def doScan()
示例#15
0
def runVoter(in_prices):
	global mas, emas, smas, lwmas, prices
	log.debug('beginning ma voter ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	malength = 101
	mas = [0] * malength
	emas = [0] * malength
	smas = [0] * malength
	lwmas = [0] * malength
	for period in range(2, malength):
		if period % 30 == 0: print period
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
	
	
	log.debug('running ma voter ...')
	starttime = time.time() 
	matypes = ['MA', 'EMA', 'SMA', 'LWMA']
	
	pool = VoterPool(1, prices)
	#calcTickets(pool, 'SMA', 95, 'LWMA', 240)
	#calcTickets(pool, 'SMA', 3, 'EMA', 4)
	#calcTickets(pool, 'MA', 7, 'SMA', 13)
	#pool.showVoters()
	#return
	
	
	farr = range(5, 30)[::3]
	sarr = range(10, 100)[::5]
	
	for ft, f in [(matype, period) for matype in matypes for period in farr]:
		elapsed = long(time.time() - starttime)
		log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ' ==')
		for st, s in [(matype, period) for matype in matypes for period in sarr]:
			if s < f: continue
			calcTickets(pool, ft, f, st, s)
	
	pool.showVoters()
示例#16
0
def runVoter(in_prices):
    global mas, emas, smas, lwmas, prices
    log.debug('beginning ma voter ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    malength = 101
    mas = [0] * malength
    emas = [0] * malength
    smas = [0] * malength
    lwmas = [0] * malength
    for period in range(2, malength):
        if period % 30 == 0: print period
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)
        lwmas[period] = ma.calc_lwma(ps, period)

    log.debug('running ma voter ...')
    starttime = time.time()
    matypes = ['MA', 'EMA', 'SMA', 'LWMA']

    pool = VoterPool(1, prices)
    #calcTickets(pool, 'SMA', 95, 'LWMA', 240)
    #calcTickets(pool, 'SMA', 3, 'EMA', 4)
    #calcTickets(pool, 'MA', 7, 'SMA', 13)
    #pool.showVoters()
    #return

    farr = range(5, 30)[::3]
    sarr = range(10, 100)[::5]

    for ft, f in [(matype, period) for matype in matypes for period in farr]:
        elapsed = long(time.time() - starttime)
        log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ' ==')
        for st, s in [(matype, period) for matype in matypes
                      for period in sarr]:
            if s < f: continue
            calcTickets(pool, ft, f, st, s)

    pool.showVoters()
示例#17
0
def runVoter(in_prices):
	global mas, emas, smas, lwmas, prices
	log.debug('beginning ma voter ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	mas = [0] * 61
	emas = [0] * 61
	smas = [0] * 61
	lwmas = [0] * 61
	for period in range(2, 61):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
	
	
	log.debug('running ma voter ...')
	starttime = time.time() 
	matypes = ['MA', 'EMA', 'SMA', 'LWMA']
	
	pool = VoterPool(2, prices)
	#calcTickets(pool, 'MA', 7, 'EMA', 18)
	#calcTickets(pool, 'SMA', 3, 'EMA', 4)
	#calcTickets(pool, 'MA', 7, 'SMA', 13)
	#pool.showVoters()
	#return
	
	
	farr = range(2, 10)
	sarr = range(4, 60)
	
	for ft, f in [(matype, period) for matype in matypes for period in farr]:
		elapsed = long(time.time() - starttime)
		#log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ' ==')
		for st, s in [(matype, period) for matype in matypes for period in sarr]:
			if s < f: continue
			calcTickets(pool, ft, f, st, s)
	
	pool.showVoters()
示例#18
0
def runVoter(in_prices):
    global mas, emas, smas, lwmas, prices
    log.debug('beginning ma voter ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    mas = [0] * 61
    emas = [0] * 61
    smas = [0] * 61
    lwmas = [0] * 61
    for period in range(2, 61):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)
        lwmas[period] = ma.calc_lwma(ps, period)

    log.debug('running ma voter ...')
    starttime = time.time()
    matypes = ['MA', 'EMA', 'SMA', 'LWMA']

    pool = VoterPool(2, prices)
    #calcTickets(pool, 'MA', 7, 'EMA', 18)
    #calcTickets(pool, 'SMA', 3, 'EMA', 4)
    #calcTickets(pool, 'MA', 7, 'SMA', 13)
    #pool.showVoters()
    #return

    farr = range(2, 10)
    sarr = range(4, 60)

    for ft, f in [(matype, period) for matype in matypes for period in farr]:
        elapsed = long(time.time() - starttime)
        #log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ' ==')
        for st, s in [(matype, period) for matype in matypes
                      for period in sarr]:
            if s < f: continue
            calcTickets(pool, ft, f, st, s)

    pool.showVoters()
示例#19
0
def runStrategy(in_prices):
	global mas, emas, smas, lwmas, prices
	log.debug('beginning ma strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	log.debug('generating mas ...')
	mas = [0] * 61
	emas = [0] * 61
	smas = [0] * 61
	lwmas = [0] * 61
	for period in range(2, 61):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
	
	pool = StrategyPool(20)
	doMaTrade(pool, 'SMA', 19, 'MA', 40, 'LWMA', 60)
	pool.showStrategies()
	return
	
	log.debug('running ma strategy ...')
	starttime = datetime.datetime.now() 
	matypes = ['MA', 'EMA', 'SMA', 'LWMA']
	pool = StrategyPool(100)
	
	for ft, f in [(matype, period) for matype in matypes for period in range(2, 10)]:
		for s1t, s1 in [(matype, period) for matype in matypes for period in range(4, 21)]:
			elapsed = (datetime.datetime.now() - starttime).seconds
			log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
			for s2t, s2 in [(matype, period) for matype in matypes for period in range(0, 41)]:
				if s2 != 0 and s2 <= s1: continue
				if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA' or s2t == 'LWMA'): continue
				doMaTrade(pool, ft, f, s1t, s1, s2t, s2)
	
	pool.showStrategies()
示例#20
0
def runStrategy_0(in_prices):
    global mas, emas, smas, std, prices
    log.debug('beginning first strategy ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(2, 51):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = round(
                np.std(ps[i - period + 1:i + 1], dtype=np.float64, ddof=0), 3)

    mas = [0] * 61
    emas = [0] * 61
    smas = [0] * 61
    for period in range(2, 61):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)

    log.debug('running first strategy ...')
    starttime = datetime.datetime.now()
    matypes = ['MA', 'EMA', 'SMA']

    farr = [
        2,
        3,
        4,
        5,
        6,
        7,
    ]
    s1arr = [
        4,
        6,
        8,
        10,
        12,
        14,
        16,
        18,
        20,
    ]
    s2arr = [
        0,
        6,
        9,
        12,
        15,
        18,
        21,
        24,
        27,
        30,
        33,
        36,
        39,
        42,
        45,
        48,
        51,
    ]

    pool = StrategyPool(100)

    for stdPeriod in [20, 30, 40]:
        stdGuage = 1.0
        while stdGuage <= 1.3:
            maxAEquity = maxBEquity = 0
            poola = StrategyPool(5)
            poolb = StrategyPool(5)

            for ft, f in [(matype, period) for matype in matypes
                          for period in farr]:
                for s1t, s1 in [(matype, period) for matype in matypes
                                for period in s1arr]:
                    elapsed = (datetime.datetime.now() - starttime).seconds
                    log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) +
                              ',' + s1t + '_' + str(s1) + ' ==')
                    for s2t, s2 in [(matype, period) for matype in matypes
                                    for period in s2arr]:
                        if s2 != 0 and s2 < s1: continue
                        if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA'): continue

                        t = doTrade(poola, stdPeriod, stdGuage, ft, f, s1t, s1,
                                    s2t, s2, '', 0, '', 0, '', 0)

                        if t.equity > maxAEquity:
                            maxAEquity = t.equity
                            maxEAs = [ft, f, s1t, s1, s2t, s2]

            elapsed = (datetime.datetime.now() - starttime).seconds
            log.info('find A time: ' + str(elapsed) + ' ')
            poola.showStrategies()

            for ft, f in [(matype, period) for matype in matypes
                          for period in farr]:
                for s1t, s1 in [(matype, period) for matype in matypes
                                for period in s1arr]:
                    elapsed = (datetime.datetime.now() - starttime).seconds
                    log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) +
                              ',' + s1t + '_' + str(s1) + ' ==')
                    for s2t, s2 in [(matype, period) for matype in matypes
                                    for period in s2arr]:
                        if s2 != 0 and s2 < s1: continue
                        if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA'): continue

                        t = doTrade(poolb, stdPeriod, stdGuage, '', 0, '', 0,
                                    '', 0, ft, f, s1t, s1, s2t, s2)

                        if t.equity > maxBEquity:
                            maxBEquity = t.equity
                            maxEBs = [ft, f, s1t, s1, s2t, s2]

            elapsed = (datetime.datetime.now() - starttime).seconds
            log.info('find B time: ' + str(elapsed) + ' ')
            poolb.showStrategies()

            logb.info(
                str(stdPeriod) + ',' + str(stdGuage) + ',' + str(maxAEquity) +
                ',' + str(maxBEquity))
            logb.info(str(maxEAs))
            logb.info(str(maxEBs))

            for i in range(5):
                sa = poola.strategies[i]
                sb = poolb.strategies[i]
                t = doTrade(pool, stdPeriod, stdGuage, sa[0].args[2],
                            sa[0].args[3], sa[0].args[4], sa[0].args[5],
                            sa[0].args[6], sa[0].args[7], sb[0].args[8],
                            sb[0].args[9], sb[0].args[10], sb[0].args[11],
                            sb[0].args[12], sb[0].args[13])
                t.generateGraph()
                pool.estimate(t)

            stdGuage += 0.1

    pool.showStrategies()
示例#21
0
def runStrategy(in_prices):
	global mas, emas, smas, lwmas, std, prices
	log.debug('beginning one strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	std = [0] * 51
	l = len(prices)
	for period in range(20, 21):
		std[period] = [0] * l
		for i in range(period - 1, l):
			std[period][i] = round(np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0), 3)
	
	mas = [0] * 181
	emas = [0] * 181
	smas = [0] * 181
	lwmas = [0] * 181
	for period in range(2, 181):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
		
	
	#pool = StrategyPool(100)
	#t = doTrade(pool, 20, 0.1, 0.2, 'SMA', 20, 'SMA', 34, 'LWMA', 40, 'SMA', 20, 'SMA', 34, 'LWMA', 120)
	#pool.showStrategies()
	#return 
	
	log.debug('running first strategy ...')
	starttime = time.time() 
	matypes = ['MA', 'EMA', 'SMA', 'LWMA']
	
	#farr = [2, 3, 4, 5, 6, 7, ]
	#s1arr = [4, 6, 8, 10, 12, 14, 16, 18, 20, ]
	#s2arr = [0, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, ]
	#farr = [20,]
	#s1arr = [40, ]
	#s2arr = [0, ]
	farr = range(40, 41)[::3]
	s1arr = range(4, 121)[::6]
	s2arr = range(0, 181)[::15]
	
	stdGuage1, stdGuage2 = 0.1, 0.2
	pool = StrategyPool(50)
	poola = StrategyPool(10)
	poolb = StrategyPool(10)
	
	for stdPeriod in [20, ]:
		for no in ['A', 'B', ]:
			for ft, f in [(matype, period) for matype in matypes for period in farr]:
				for s1t, s1 in [(matype, period) for matype in matypes for period in s1arr]:
					if s1 < f: continue
					elapsed = long(time.time() - starttime)
					log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
					for s2t, s2 in [(matype, period) for matype in matypes for period in s2arr]:
						if s2 != 0 and s2 <= s1: continue
						if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA' or s2t == 'LWMA'): continue
						if no == 'A':
							doTrade(poola, stdPeriod, stdGuage1, stdGuage2, ft, f, s1t, s1, s2t, s2, '', 0, '', 0, '', 0)
						elif no == 'B':
							doTrade(poolb, stdPeriod, stdGuage1, stdGuage2, '', 0, '', 0, '', 0, ft, f, s1t, s1, s2t, s2)
			elapsed = long(time.time() - starttime)
			log.info('find ' + no + ' time: ' + str(elapsed) + ' ')
		
		for i in range(10):
			sa = poola.strategies[i]
			sb = poolb.strategies[i]
			t = doTrade(pool, stdPeriod, stdGuage1, stdGuage2, sa[0].args[0], sa[0].args[1], sa[0].args[2], sa[0].args[3], sa[0].args[4], sa[0].args[5], sb[0].args[6], sb[0].args[7], sb[0].args[8], sb[0].args[9], sb[0].args[10], sb[0].args[11])
			#t.generateGraph()
	
	pool.showStrategies()
示例#22
0
def runStrategy(in_prices):
    global mas, emas, smas, lwmas, std, prices
    log.debug('beginning ma strategy ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(2, 51):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = np.std(ps[i - period + 1:i + 1],
                                    dtype=np.float64,
                                    ddof=0)

    malength = 181
    mas = [0] * malength
    emas = [0] * malength
    smas = [0] * malength
    lwmas = [0] * malength
    for period in range(2, malength):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)
        lwmas[period] = ma.calc_lwma(ps, period)

    pool = Pool(10)
    t = doTrade(pool, 12, 0.15, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'SMA', 30,
                'EMA', 58, 'SMA', 160)
    t = doTrade(pool, 12, 0.15, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 12, 0.15, 'SMA', 25, 'EMA', 38, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 12, 0.1, 'SMA', 25, 'EMA', 38, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 12, 0.12, 'SMA', 25, 'EMA', 38, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 20, 0.15, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 20, 0.1, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 20, 0.12, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 40, 0.15, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 40, 0.1, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    t = doTrade(pool, 40, 0.12, 'SMA', 25, 'LWMA', 48, 'SMA', 160, 'MA', 30,
                'SMA', 68, 'SMA', 100)
    #t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 13, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
    pool.showStrategies()
    return

    log.debug('running ma strategy ...')
    starttime = datetime.datetime.now()
    matypes = ['MA', 'EMA', 'SMA', 'LWMA']

    farr = range(5, 40)
    s1arr = range(8, 80)
    s2arr = range(0, 180)

    poola = Pool(10)
    poolb = Pool(10)
    for stdPeriod in [
            12,
    ]:
        for stdGuage in [
                0.15,
        ]:
            log.debug('*** ' + str(stdPeriod) + ',' + str(stdGuage) + ' ***')
            for ft, f in [(matype, period) for matype in matypes
                          for period in farr]:
                for s1t, s1 in [(matype, period) for matype in matypes
                                for period in s1arr]:
                    if s1 != 0 and s1 <= f: continue
                    elapsed = (datetime.datetime.now() - starttime).seconds
                    log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) +
                              ',' + s1t + '_' + str(s1) + ' ==')
                    for s2t, s2 in [(matype, period) for matype in matypes
                                    for period in s2arr]:
                        if s2 != 0 and s2 <= s1: continue
                        if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA'
                                        or s2t == 'LWMA'):
                            continue

                        doTrade(poola, stdPeriod, stdGuage, ft, f, s1t, s1,
                                s2t, s2, '', 0, '', 0, '', 0)
                        doTrade(poolb, stdPeriod, stdGuage, '', 0, '', 0, '',
                                0, ft, f, s1t, s1, s2t, s2)

    poola.showStrategies()
    poolb.showStrategies()
示例#23
0
文件: l1Trader.py 项目: ongbe/pymisc
def runStrategy_0(in_prices):
    global mas, emas, smas, std, prices
    log.debug("beginning first strategy ...")

    prices = in_prices
    ps = [p["close"] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(2, 51):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = round(np.std(ps[i - period + 1 : i + 1], dtype=np.float64, ddof=0), 3)

    mas = [0] * 61
    emas = [0] * 61
    smas = [0] * 61
    for period in range(2, 61):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)

    log.debug("running first strategy ...")
    starttime = datetime.datetime.now()
    matypes = ["MA", "EMA", "SMA"]

    farr = [2, 3, 4, 5, 6, 7]
    s1arr = [4, 6, 8, 10, 12, 14, 16, 18, 20]
    s2arr = [0, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51]

    pool = StrategyPool(100)

    for stdPeriod in [20, 30, 40]:
        stdGuage = 1.0
        while stdGuage <= 1.3:
            maxAEquity = maxBEquity = 0
            poola = StrategyPool(5)
            poolb = StrategyPool(5)

            for ft, f in [(matype, period) for matype in matypes for period in farr]:
                for s1t, s1 in [(matype, period) for matype in matypes for period in s1arr]:
                    elapsed = (datetime.datetime.now() - starttime).seconds
                    log.debug("== " + str(elapsed) + "," + ft + "_" + str(f) + "," + s1t + "_" + str(s1) + " ==")
                    for s2t, s2 in [(matype, period) for matype in matypes for period in s2arr]:
                        if s2 != 0 and s2 < s1:
                            continue
                        if s2 == 0 and (s2t == "EMA" or s2t == "SMA"):
                            continue

                        t = doTrade(poola, stdPeriod, stdGuage, ft, f, s1t, s1, s2t, s2, "", 0, "", 0, "", 0)

                        if t.equity > maxAEquity:
                            maxAEquity = t.equity
                            maxEAs = [ft, f, s1t, s1, s2t, s2]

            elapsed = (datetime.datetime.now() - starttime).seconds
            log.info("find A time: " + str(elapsed) + " ")
            poola.showStrategies()

            for ft, f in [(matype, period) for matype in matypes for period in farr]:
                for s1t, s1 in [(matype, period) for matype in matypes for period in s1arr]:
                    elapsed = (datetime.datetime.now() - starttime).seconds
                    log.debug("== " + str(elapsed) + "," + ft + "_" + str(f) + "," + s1t + "_" + str(s1) + " ==")
                    for s2t, s2 in [(matype, period) for matype in matypes for period in s2arr]:
                        if s2 != 0 and s2 < s1:
                            continue
                        if s2 == 0 and (s2t == "EMA" or s2t == "SMA"):
                            continue

                        t = doTrade(poolb, stdPeriod, stdGuage, "", 0, "", 0, "", 0, ft, f, s1t, s1, s2t, s2)

                        if t.equity > maxBEquity:
                            maxBEquity = t.equity
                            maxEBs = [ft, f, s1t, s1, s2t, s2]

            elapsed = (datetime.datetime.now() - starttime).seconds
            log.info("find B time: " + str(elapsed) + " ")
            poolb.showStrategies()

            logb.info(str(stdPeriod) + "," + str(stdGuage) + "," + str(maxAEquity) + "," + str(maxBEquity))
            logb.info(str(maxEAs))
            logb.info(str(maxEBs))

            for i in range(5):
                sa = poola.strategies[i]
                sb = poolb.strategies[i]
                t = doTrade(
                    pool,
                    stdPeriod,
                    stdGuage,
                    sa[0].args[2],
                    sa[0].args[3],
                    sa[0].args[4],
                    sa[0].args[5],
                    sa[0].args[6],
                    sa[0].args[7],
                    sb[0].args[8],
                    sb[0].args[9],
                    sb[0].args[10],
                    sb[0].args[11],
                    sb[0].args[12],
                    sb[0].args[13],
                )
                t.generateGraph()
                pool.estimate(t)

            stdGuage += 0.1

    pool.showStrategies()
示例#24
0
文件: l1Trader.py 项目: ongbe/pymisc
def runStrategy_1(in_prices):
    global mas, emas, smas, std, prices
    log.debug("beginning first strategy ...")

    prices = in_prices
    ps = [p["close"] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(2, 51):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = round(np.std(ps[i - period + 1 : i + 1], dtype=np.float64, ddof=0), 3)

    mas = [0] * 61
    emas = [0] * 61
    smas = [0] * 61
    for period in range(2, 61):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)

    log.debug("running first strategy ...")
    starttime = datetime.datetime.now()

    strat_as = [
        ["MA", 6, "SMA", 14, "EMA", 39],
        ["MA", 7, "SMA", 10, "SMA", 12],
        ["MA", 7, "SMA", 12, "EMA", 18],
        ["MA", 7, "SMA", 12, "MA", 27],
        ["MA", 7, "SMA", 12, "SMA", 12],
        ["MA", 7, "SMA", 14, "EMA", 27],
        ["MA", 7, "SMA", 14, "EMA", 30],
        ["MA", 7, "SMA", 14, "EMA", 33],
        ["MA", 7, "SMA", 14, "EMA", 45],
        ["MA", 7, "SMA", 14, "MA", 27],
        ["MA", 7, "SMA", 14, "SMA", 15],
        ["MA", 7, "SMA", 14, "SMA", 30],
        ["MA", 7, "SMA", 16, "EMA", 24],
        ["MA", 7, "SMA", 16, "EMA", 27],
        ["MA", 7, "SMA", 16, "EMA", 30],
        ["MA", 7, "SMA", 16, "MA", 30],
    ]
    strat_bs = [
        ["EMA", 3, "EMA", 16, "MA", 42],
        ["EMA", 3, "EMA", 16, "MA", 45],
        ["EMA", 6, "SMA", 6, "MA", 30],
        ["EMA", 7, "MA", 4, "EMA", 51],
        ["MA", 6, "SMA", 16, "EMA", 45],
        ["MA", 6, "SMA", 18, "MA", 36],
        ["MA", 6, "SMA", 20, "MA", 36],
        ["MA", 6, "SMA", 20, "MA", 39],
        ["MA", 7, "EMA", 18, "EMA", 45],
        ["MA", 7, "SMA", 12, "EMA", 42],
        ["MA", 7, "SMA", 12, "SMA", 21],
        ["MA", 7, "SMA", 14, "EMA", 42],
        ["MA", 7, "SMA", 14, "MA", 45],
        ["MA", 7, "SMA", 14, "SMA", 21],
        ["SMA", 2, "EMA", 16, "MA", 42],
        ["SMA", 4, "MA", 4, "EMA", 51],
        ["SMA", 5, "MA", 4, "MA", 15],
        ["SMA", 5, "MA", 6, "MA", 42],
        ["SMA", 6, "EMA", 10, "MA", 36],
        ["SMA", 6, "MA", 12, "MA", 12],
        ["SMA", 7, "MA", 12, "EMA", 18],
        ["SMA", 7, "MA", 12, "EMA", 27],
        ["SMA", 7, "MA", 12, "EMA", 36],
        ["SMA", 7, "MA", 12, "EMA", 45],
        ["SMA", 7, "MA", 12, "EMA", 48],
        ["SMA", 7, "MA", 12, "MA", 12],
        ["SMA", 7, "MA", 12, "MA", 18],
        ["SMA", 7, "MA", 12, "MA", 33],
        ["SMA", 7, "MA", 12, "MA", 36],
        ["SMA", 7, "MA", 12, "MA", 51],
        ["SMA", 7, "MA", 12, "SMA", 15],
    ]

    pool = StrategyPool(100)
    for stdPeriod in [5, 8, 10, 12, 15, 18, 19, 20, 21, 22, 25, 30, 32, 34, 38, 40]:
        stdGuage = 0.6
        while stdGuage <= 2:
            elapsed = (datetime.datetime.now() - starttime).seconds
            log.debug("== " + str(elapsed) + "," + str(stdPeriod) + "," + str(stdGuage) + " ==")

            for sa in strat_as:
                for sb in strat_bs:
                    doTrade(
                        pool,
                        stdPeriod,
                        stdGuage,
                        sa[0],
                        sa[1],
                        sa[2],
                        sa[3],
                        sa[4],
                        sa[5],
                        sb[0],
                        sb[1],
                        sb[2],
                        sb[3],
                        sb[4],
                        sb[5],
                    )

            stdGuage += 0.1

    pool.showStrategies()

    return
示例#25
0
def runStrategy(in_prices):
	global mas, emas, smas, std, prices
	log.debug('beginning first strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	std = [0] * 51
	l = len(prices)
	for period in range(2, 51):
		std[period] = [0] * l
		for i in range(period - 1, l):
			std[period][i] = round(np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0), 3)
		
	mas = [0] * 61
	emas = [0] * 61
	smas = [0] * 61
	for period in range(2, 61):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		
	pool = StrategyPool(100)
	#t = doTrade(pool, 25, 1.0, 'MA', 7, 'SMA', 12, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 13, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.0, 'MA', 7, 'SMA', 13, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.0, 'MA', 7, 'SMA', 12, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#
	#t = doTrade(pool, 25, 1.1, 'MA', 7, 'SMA', 12, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.1, 'MA', 7, 'SMA', 13, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.1, 'MA', 7, 'SMA', 13, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.1, 'MA', 7, 'SMA', 12, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#
	#t = doTrade(pool, 25, 1.2, 'MA', 7, 'SMA', 12, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.2, 'MA', 7, 'SMA', 13, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.2, 'MA', 7, 'SMA', 13, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.2, 'MA', 7, 'SMA', 12, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#
	#t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 12, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 13, 'EMA', 31, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 13, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.3, 'MA', 7, 'SMA', 12, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 13)
	#t = doTrade(pool, 25, 1.0, 'MA', 7, 'SMA', 13, 'EMA', 26, 'SMA', 7, 'MA', 12, 'MA', 12)
	pool.showStrategies()
		
	return 
	
	log.debug('running first strategy ...')
	starttime = datetime.datetime.now() 
	matypes = ['MA', 'EMA', 'SMA']
	
	#farr = [2, 3, 4, 5, 6, 7, ]
	#s1arr = [4, 6, 8, 10, 12, 14, 16, 18, 20, ]
	#s2arr = [0, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, ]
	
	farr = [2,]
	s1arr = [4, ]
	s2arr = [0, ]
	
	pool = StrategyPool(100)
	for stdPeriod in [20, ]:
		stdGuage = 0.5
		for stdGuage in [1.3, ]:
			maxAEquity = maxBEquity = 0
			poola = StrategyPool(5)
			poolb = StrategyPool(5)
			
			for ft, f in [(matype, period) for matype in matypes for period in farr]:
				for s1t, s1 in [(matype, period) for matype in matypes for period in s1arr]:
					elapsed = (datetime.datetime.now() - starttime).seconds
					log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
					for s2t, s2 in [(matype, period) for matype in matypes for period in s2arr]:
						if s2 != 0 and s2 < s1: continue
						if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA'): continue
						
						t = doTrade(poola, stdPeriod, stdGuage, ft, f, s1t, s1, s2t, s2, '', 0, '', 0, '', 0)
						
						if t.equity > maxAEquity:
							maxAEquity = t.equity
							maxEAs = [ft, f, s1t, s1, s2t, s2]
						
			elapsed = (datetime.datetime.now() - starttime).seconds
			log.info('find A time: ' + str(elapsed) + ' ')
			#poola.showStrategies()
			
			for ft, f in [(matype, period) for matype in matypes for period in farr]:
				for s1t, s1 in [(matype, period) for matype in matypes for period in s1arr]:
					elapsed = (datetime.datetime.now() - starttime).seconds
					log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
					for s2t, s2 in [(matype, period) for matype in matypes for period in s2arr]:
						if s2 != 0 and s2 < s1: continue
						if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA'): continue
						
						t = doTrade(poolb, stdPeriod, stdGuage, '', 0, '', 0, '', 0, ft, f, s1t, s1, s2t, s2)
						
						if t.equity > maxBEquity:
							maxBEquity = t.equity
							maxEBs = [ft, f, s1t, s1, s2t, s2]
						
			elapsed = (datetime.datetime.now() - starttime).seconds
			log.info('find B time: ' + str(elapsed) + ' ')
			#poolb.showStrategies()
			
			logb.info(str(stdPeriod) + ',' + str(stdGuage) + ',' + str(maxAEquity) + ',' + str(maxBEquity))
			logb.info(str(maxEAs))
			logb.info(str(maxEBs))
			
			for i in range(5):
				sa = poola.strategies[i]
				sb = poolb.strategies[i]
				t = doTrade(pool, stdPeriod, stdGuage, sa[0].args[2], sa[0].args[3], sa[0].args[4], sa[0].args[5], sa[0].args[6], sa[0].args[7], sb[0].args[8], sb[0].args[9], sb[0].args[10], sb[0].args[11], sb[0].args[12], sb[0].args[13])
				t.generateGraph()
				#pool.estimate(t)
				
			#stdGuage += 0.1
	
	pool.showStrategies()
示例#26
0
def runStrategy(in_prices):
    global mas, emas, smas, lwmas, std, prices
    log.debug('beginning one strategy ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(5, 41):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = round(
                np.std(ps[i - period + 1:i + 1], dtype=np.float64, ddof=0), 3)

    mas = [0] * 181
    emas = [0] * 181
    smas = [0] * 181
    lwmas = [0] * 181
    for period in range(2, 181):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)
        lwmas[period] = ma.calc_lwma(ps, period)

    #pool = StrategyPool(100)
    #t = doTrade(pool, 20, 0.1, 0.2, 'SMA', 20, 'SMA', 34, 'LWMA', 40, 'SMA', 20, 'SMA', 34, 'LWMA', 120, 'MA', 20, 'SMA', 34, 'LWMA', 120)
    #pool.showStrategies()
    #return

    log.debug('running first strategy ...')
    starttime = time.time()
    matypes = ['MA', 'EMA', 'SMA', 'LWMA']

    #farr = [2, 3, 4, 5, 6, 7, ]
    #s1arr = [4, 6, 8, 10, 12, 14, 16, 18, 20, ]
    #s2arr = [0, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, ]
    #farr = [20,]
    #s1arr = [40, ]
    #s2arr = [0, ]
    farr = range(40, 41)[::3]
    s1arr = range(4, 121)[::6]
    s2arr = range(0, 181)[::15]

    stdGuage1, stdGuage2 = 0.1, 0.2
    pool = StrategyPool(50)
    poola = StrategyPool(10)
    poolb = StrategyPool(10)
    poolc = StrategyPool(10)

    for stdPeriod in [
            20,
    ]:
        for no in ['A', 'B', 'C']:
            for ft, f in [(matype, period) for matype in matypes
                          for period in farr]:
                for s1t, s1 in [(matype, period) for matype in matypes
                                for period in s1arr]:
                    if s1 < f: continue
                    elapsed = long(time.time() - starttime)
                    log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) +
                              ',' + s1t + '_' + str(s1) + ' ==')

                    for s2t, s2 in [(matype, period) for matype in matypes
                                    for period in s2arr]:
                        if s2 != 0 and s2 <= s1: continue
                        if s2 == 0 and (s2t == 'EMA' or s2t == 'SMA'
                                        or s2t == 'LWMA'):
                            continue
                        if no == 'A':
                            doTrade(poola, stdPeriod, stdGuage1, stdGuage2, ft,
                                    f, s1t, s1, s2t, s2, '', 0, '', 0, '', 0,
                                    '', 0, '', 0, '', 0)
                        elif no == 'B':
                            doTrade(poolb, stdPeriod, stdGuage1, stdGuage2, '',
                                    0, '', 0, '', 0, ft, f, s1t, s1, s2t, s2,
                                    '', 0, '', 0, '', 0)
                        elif no == 'C':
                            doTrade(poolc, stdPeriod, stdGuage1, stdGuage2, '',
                                    0, '', 0, '', 0, '', 0, '', 0, '', 0, ft,
                                    f, s1t, s1, s2t, s2)
            elapsed = long(time.time() - starttime)
            log.info('find ' + no + ' time: ' + str(elapsed) + ' ')

        for i in range(10):
            sa = poola.strategies[i]
            sb = poolb.strategies[i]
            sc = poolc.strategies[i]
            t = doTrade(pool, stdPeriod, stdGuage1, stdGuage2, sa[0].args[0],
                        sa[0].args[1], sa[0].args[2], sa[0].args[3],
                        sa[0].args[4], sa[0].args[5], sb[0].args[6],
                        sb[0].args[7], sb[0].args[8], sb[0].args[9],
                        sb[0].args[10], sb[0].args[11], sc[0].args[12],
                        sc[0].args[13], sc[0].args[14], sc[0].args[15],
                        sc[0].args[16], sc[0].args[17])
            #t.generateGraph()

    pool.showStrategies()