Beispiel #1
0
    def showStrategies(self):
        rate = 1
        resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
        if const.currentSecId:
            resultPath = os.path.join(resultPath, const.currentSecId)
        if not os.path.isdir(resultPath): os.makedirs(resultPath)
        files = open(os.path.join(resultPath, 'strategy.csv'), 'w')
        filet = open(os.path.join(resultPath, 'trader.csv'), 'w')
        files.write(
            'STRATEGY,BUY TIMES, SELL TIMES, DOWN DAYS, FINAL EQUITY\n')
        filet.write('OP,STRATEGY,TIME,PRICE,VOLUMN,POSITION,EQUITY,NOTES\n')
        for s in self.strategies:
            if s[0] == 0: break
            ss = s[0].strategyName + ',' + str(len(
                s[0].stats['buy']['date'])) + ',' + str(
                    len(s[0].stats['sell']['date'])) + ',' + str(
                        s[0].stats['downDays']) + ',' + str(round(s[1], 2))
            log.info(ss)
            files.write(ss + '\n')
            for tl in s[0].stats['log']:
                filet.write(tl + '\n')

            s[0].generateGraph(resultPath, rate)
            rate += 1
        files.close()
        filet.close()
Beispiel #2
0
	def estimate(self, t):
		
		l = len(self.strategies)
		if t.equity <= self.strategies[-1][1]: return
		for i in range(l):
			if (t.equity > self.strategies[i][1]):
				log.info(t.strategyName + ',' + str(len(t.stats['buy']['date'])) + ',' + str(len(t.stats['sell']['date'])) + ',' + str(t.stats['downDays']) + ',' + str(round(t.equity, 2)))
				self.strategies.insert(i, [t, t.equity])
				if l + 1 > self.num:
					self.strategies.pop(-1)
				break
Beispiel #3
0
    def analysis(self, prices):
        self.buildTrend(prices)

        if len(prices) <= self.days:
            return

        current = prices[len(prices) - self.days - 1:len(prices)]
        #log.info(current)

        chour = current[len(current) - 1]['dt'].hour
        cdt = current[len(current) - 1]['dt'].strftime('%Y-%m-%d %H:%M')

        ct = ''
        for i in range(1, len(current)):
            cur = current[i]['pc']
            pre = current[i - 1]['pc']
            #log.info('Current ' + str(cur) + ' - ' + str(pre))
            if cur < pre:
                ct = ct + 'D'
            else:
                ct = ct + 'U'
            #log.info('Current ' + ct)

        for i in range(self.days, len(self.history) - self.days):
            his = self.history[i - self.days:i + 1]
            hhour = his[len(his) - 1]['dt'].hour
            if abs(chour - hhour) > 2:
                continue
            ht = self.histred[i - self.days:i]
            #if similar(ct, ht) < 3:
            if ct[0:self.days - self.expect] == ht[0:self.days - self.expect]:
                self.graphic(current, his)
                curChange = current[len(current) - self.expect -
                                    1]['pc'] - current[len(current) -
                                                       self.days - 1]['pc']
                hisChange = his[len(his) - self.expect -
                                1]['pc'] - his[len(his) - self.days - 1]['pc']
                hdt = his[len(his) - 1]['dt'].strftime('%Y-%m-%d %H:%M')
                log.info('History Date:' + hdt + ', Trend: ' + ht +
                         ',change:' + str(hisChange))
                log.info('Current Date:' + cdt + ', Trend: ' + ct +
                         ',change:' + str(curChange))
                curChange = current[len(current) -
                                    1]['pc'] - current[len(current) -
                                                       self.expect - 1]['pc']
                hisChange = his[len(his) -
                                1]['pc'] - his[len(his) - self.expect -
                                               1]['pc']
                log.info('history:' + ht[self.days - self.expect:] +
                         ',change:' + str(hisChange))
                log.info('verify:' + ct[self.days - self.expect:] +
                         ',change:' + str(curChange))
                log.info('=========================')
Beispiel #4
0
	def estimate(self, t):
		perf = calcPerformance(t.stats['equity'])
		#log.debug(c)
		l = len(self.strategies)
		if t.equity <= 0 or perf <= self.strategies[-1][1]: return
		for i in range(l):
			if (perf > self.strategies[i][1]):
				log.info(t.strategyName + ',' + str(len(t.stats['buy']['date'])) + ',' + str(len(t.stats['sell']['date'])) + ',' + str(t.stats['downDays']) + ',' + str(round(perf, 2)) + ',' + str(round(t.equity, 2)))
				#log.info('=== m:' + str(m) + ',c:' + str(c) + ', m/c:' + str(mc))
				self.strategies.insert(i, [t, perf])
				if l + 1 > self.num:
					self.strategies.pop(-1)
				break
Beispiel #5
0
	def estimate(self, t):
		m, c = slope(t.stats['equity'])
		mc = m * t.equity / c
		#log.debug(c)
		l = len(self.strategies)
		if mc <= self.strategies[-1][1]: return
		for i in range(l):
			#if (t.equity > self.strategies[i][1]):
			if (mc > self.strategies[i][1]):
				log.info(t.strategyName + ',' + str(len(t.stats['buy']['date'])) + ',' + str(len(t.stats['sell']['date'])) + ',' + str(t.stats['downDays']) + ',' + str(round(t.equity, 2)))
				log.info('=== m:' + str(m) + ',c:' + str(c) + ', m/c:' + str(mc))
				self.strategies.insert(i, [t, mc])
				if l + 1 > self.num:
					self.strategies.pop(-1)
				break
Beispiel #6
0
	def estimate(self, t):
		m, c, r, s = slope(t.stats['equity'])
		mc = m * 10000 / c
		mc = t.equity
		mc = (t.equity - const.INITIAL_BAL) * (t.equity - const.INITIAL_BAL) * (t.equity - const.INITIAL_BAL) / r
		#log.debug(c)
		l = len(self.strategies)
		if t.equity <= 0 or mc <= self.strategies[-1][1]: return
		for i in range(l):
			if (mc > self.strategies[i][1]):
				log.info(t.strategyName + ',' + str(len(t.stats['buy']['date'])) + ',' + str(len(t.stats['sell']['date'])) + ',' + str(t.stats['downDays']) + ',' + str(round(t.equity, 2)))
				#log.info('=== m:' + str(m) + ',c:' + str(c) + ', m/c:' + str(mc))
				self.strategies.insert(i, [t, mc, m, c, r, s])
				if l + 1 > self.num:
					self.strategies.pop(-1)
				break
Beispiel #7
0
	def showStrategies(self):
		rate = 1
		files = open(os.path.join(os.path.dirname(__file__), const.RESULT_PATH + '/strategy.csv'), 'w')
		filet = open(os.path.join(os.path.dirname(__file__), const.RESULT_PATH + '/trader.csv'), 'w')
		files.write('STRATEGY,BUY TIMES, SELL TIMES, DOWN DAYS, FINAL EQUITY\n')
		filet.write('OP,STRATEGY,TIME,PRICE,VOLUMN,POSITION,EQUITY,NOTES\n')
		for s in self.strategies:
			if s[0] == 0: break
			ss = s[0].strategyName + ',' + str(len(s[0].stats['buy']['date'])) + ',' + str(len(s[0].stats['sell']['date'])) + ',' + str(s[0].stats['downDays']) + ',' + str(round(s[1], 2))
			log.info(ss)
			files.write(ss + '\n')
			for tl in s[0].stats['log']:
				filet.write(tl + '\n')
				
			s[0].generateGraph(rate)
			rate += 1
		files.close()
		filet.close()
Beispiel #8
0
	def analysis(self, prices):
		self.buildTrend(prices)
		
		if len(prices) <= self.days:
			return
			
		current = prices[len(prices) - self.days - 1 : len(prices)]
		#log.info(current)
		
		chour = current[len(current) - 1]['dt'].hour
		cdt = current[len(current) - 1]['dt'].strftime('%Y-%m-%d %H:%M')
		
		ct = ''
		for i in range(1, len(current)):
			cur = current[i]['pc']
			pre = current[i - 1]['pc']
			#log.info('Current ' + str(cur) + ' - ' + str(pre))
			if cur < pre:
				ct = ct + 'D'
			else:
				ct = ct + 'U'
			#log.info('Current ' + ct)
		
		for i in range(self.days, len(self.history) - self.days):
			his = self.history[i - self.days : i + 1]
			hhour = his[len(his) - 1]['dt'].hour
			if abs(chour - hhour) > 2:
				continue
			ht = self.histred[i - self.days : i]
			#if similar(ct, ht) < 3:
			if ct[0 : self.days - self.expect] == ht[0 : self.days - self.expect]:
				self.graphic(current, his)
				curChange = current[len(current) - self.expect - 1]['pc'] - current[len(current) - self.days - 1]['pc']
				hisChange = his[len(his) - self.expect - 1]['pc'] - his[len(his) - self.days - 1]['pc']
				hdt = his[len(his) - 1]['dt'].strftime('%Y-%m-%d %H:%M')
				log.info('History Date:' + hdt + ', Trend: ' + ht + ',change:' + str(hisChange))
				log.info('Current Date:' + cdt + ', Trend: ' + ct + ',change:' + str(curChange))
				curChange = current[len(current) - 1]['pc'] - current[len(current) - self.expect - 1]['pc']
				hisChange = his[len(his) - 1]['pc'] - his[len(his) - self.expect - 1]['pc']
				log.info('history:' + ht[self.days - self.expect:] + ',change:' + str(hisChange))
				log.info('verify:' +  ct[self.days - self.expect:] + ',change:' + str(curChange))
				log.info('=========================')
Beispiel #9
0
 def estimate(self, t):
     m, c = slope(t.stats['equity'])
     mc = m * t.equity / c
     #log.debug(c)
     l = len(self.strategies)
     if mc <= self.strategies[-1][1]: return
     for i in range(l):
         #if (t.equity > self.strategies[i][1]):
         if (mc > self.strategies[i][1]):
             log.info(t.strategyName + ',' +
                      str(len(t.stats['buy']['date'])) + ',' +
                      str(len(t.stats['sell']['date'])) + ',' +
                      str(t.stats['downDays']) + ',' +
                      str(round(t.equity, 2)))
             log.info('=== m:' + str(m) + ',c:' + str(c) + ', m/c:' +
                      str(mc))
             self.strategies.insert(i, [t, mc])
             if l + 1 > self.num:
                 self.strategies.pop(-1)
             break
Beispiel #10
0
	def showStrategies(self):
		rate = 1
		resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
		if const.currentSecId:
			resultPath = os.path.join(resultPath, const.currentSecId)
		if not os.path.isdir(resultPath): os.makedirs(resultPath) 
		files = open(os.path.join(resultPath, 'strategy.csv'), 'w')
		filet = open(os.path.join(resultPath, 'trader.csv'), 'w')
		files.write('STRATEGY,BUY TIMES, SELL TIMES, DOWN DAYS, FINAL EQUITY, POSITION, PERFORMANCE\n')
		filet.write('OP,STRATEGY,TIME,PRICE,VOLUMN,POSITION,EQUITY,NOTES\n')
		for s in self.strategies:
			if s[0] == 0: break
			ss = s[0].strategyName + ',' + str(len(s[0].stats['buy']['date'])) + ',' + str(len(s[0].stats['sell']['date'])) + ',' + str(s[0].stats['downDays']) + ',' + str(round(s[0].equity, 2)) + ',' + str(s[0].position) + ',' + str(round(s[1], 5))
			log.info(ss)
			files.write(ss + '\n')
			for tl in s[0].stats['log']:
				filet.write(tl + '\n')
				
			s[0].generateGraph(resultPath, rate)
			rate += 1
		files.close()
		filet.close()
Beispiel #11
0
    resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
    plt.savefig(os.path.join(resultPath, stock['id'] + '.png'), dpi=150)
    plt.close(fig)

    return stock['symbol'] + ', ' + dt + ', ' + str(
        ps[-1]) + ', ' + active + ', ' + adesc + ', ' + bdesc


if __name__ == "__main__":
    rwlogging.clearLog()
    plt.rcParams.update({'axes.labelsize': 10})
    plt.rcParams.update({'xtick.labelsize': 8})
    plt.rcParams.update({'ytick.labelsize': 8})

    result = 'STOCK, DATE, PRICE, ACTIVE, POS A, POS B\n'
    stocks = dataloader.importVSignalList(const.VSIGNAL_FILE)
    for stock in stocks:
        if const.REFRESH_DATA: dataloader.downloadStockData(stock)
        prices = dataloader.importData(4, stock['file'], const.DATA_NUM * 2)
        if const.UPDATE_SINA:
            prices = dataloader.makeupStockFromSina(stock, prices)
        desc = runSignal(stock, prices)
        result += desc + '\n'

    log.info(result)
    resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
    files = open(os.path.join(resultPath, 'signal.csv'), 'w')
    files.write(result)
    files.close()
Beispiel #12
0
	if opos == pos:
		desc = str(pos)
	else:
		desc = str(opos) + ' -> ' + str(pos)
	
	dt = datetime.datetime.now().strftime('%Y-%m-%d')
		
	return stock['symbol'] + ',' + dt + ',' + str(ps[-1]) + ',' + desc
	

if __name__ == "__main__":
	rwlogging.clearLog()
	plt.rcParams.update({'axes.labelsize':10})
	plt.rcParams.update({'xtick.labelsize':8})
	plt.rcParams.update({'ytick.labelsize':8})
	
	result = 'STOCK, DATE, PRICE, POSITION\n'
	stocks = dataloader.importSignalList(const.SIGNAL_FILE)
	for stock in stocks:
		if const.REFRESH_DATA: dataloader.downloadStockData(stock)
		prices = dataloader.importData(4, stock['file'], const.DATA_NUM * 2)
		if const.UPDATE_SINA: prices = dataloader.makeupStockFromSina(stock, prices)
		desc = runSignal(stock, prices)
		result += desc + '\n'
		
	log.info(result)
	resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
	files = open(os.path.join(resultPath, 'signal.csv'), 'w')
	files.write(result)
	files.close()