예제 #1
0
def importAll():
	#tables = ['XAGUSD1', 'XAGUSD5', 'XAGUSD15', 'XAGUSD30', 'XAGUSD60', 'XAGUSD240', 'XAGUSD1440', 'XAGUSD10080', 'XAGUSD43200', ]
	tables = ['XAGUSD1440',]
	for table in tables:
		log.info('importing ' + table)
		importTable(table)
		log.info('done ' + table)
예제 #2
0
def sinaAgg():
    # fetch price of London
    f = urllib2.urlopen('http://hq.sinajs.cn/?_=1386077085140/&list=hf_XAG',
                        timeout=20)
    html = f.read()
    html = html[19:len(html) - 3]
    xagArr = re.split(',', html)

    price = {}
    price['dt'] = datetime.datetime.strptime(xagArr[12] + ' ' + xagArr[6],
                                             '%Y-%m-%d %H:%M:%S')
    xag = float(xagArr[0])
    xag0 = float(xagArr[7])
    log.debug('XAG: ' + xagArr[0] + ', XAG0: ' + xagArr[2])

    # fetch USD price
    fusd = urllib2.urlopen(
        'http://hq.sinajs.cn/rn=13860770561347070422433316708&list=USDCNY',
        timeout=20)
    htmlusd = fusd.read()
    htmlusd = htmlusd[19:len(htmlusd) - 3]
    usdArr = re.split(',', htmlusd)
    usd = float(usdArr[1])
    log.debug('USD: ' + usdArr[0])

    # calculate price in RMB
    price['p'] = round(usd * xag / 31.1035, 3)
    price['p0'] = round(usd * xag0 / 31.1035, 3)
    price['per'] = float(xagArr[1])

    log.info('sina agg: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' +
             str(price['p0']) + ', ' + str(price['per']))
    return price
예제 #3
0
파일: prices.py 프로젝트: 547872495/pymisc
def sinaAgg():
	# fetch price of London
	f = urllib2.urlopen('http://hq.sinajs.cn/?_=1386077085140/&list=hf_XAG', timeout=20)
	html = f.read()
	html = html[19:len(html) - 3]
	xagArr = re.split(',', html)
	
	price = {}
	price['dt'] = datetime.datetime.strptime(xagArr[12] + ' ' + xagArr[6], '%Y-%m-%d %H:%M:%S')
	xag = float(xagArr[0])
	xag0 = float(xagArr[7])
	log.debug('XAG: ' + xagArr[0] + ', XAG0: ' + xagArr[2])
	
	# fetch USD price
	fusd = urllib2.urlopen('http://hq.sinajs.cn/rn=13860770561347070422433316708&list=USDCNY', timeout=20)
	htmlusd = fusd.read()
	htmlusd = htmlusd[19:len(htmlusd) - 3]
	usdArr = re.split(',', htmlusd)
	usd = float(usdArr[1])
	log.debug('USD: ' + usdArr[0])
	
	# calculate price in RMB
	price['p'] = round(usd * xag / 31.1035, 3)
	price['p0'] = round(usd * xag0 / 31.1035, 3)
	price['per'] = float(xagArr[1])
	
	log.info('sina agg: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' + str(price['p0']) + ', ' + str(price['per']))
	return price
예제 #4
0
def icbcAgg():
    f = urllib2.urlopen(
        'http://www.icbc.com.cn/ICBCDynamicSite/Charts/GoldTendencyPicture.aspx',
        timeout=20)
    html = f.read()
    silverRmbPattern = re.compile(
        r"""人民币账户白银\s*</td>\s*<td[\s\S]+?</td>\s*<td.*?>\s*(.*?)\s*</td>
\s*<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>""",
        re.S + re.X)
    pmsSilverRmb = silverRmbPattern.search(html)

    price = {}
    # Date Time
    price['dt'] = datetime.datetime.now()
    price['p'] = float(pmsSilverRmb.group(3))
    high = float(pmsSilverRmb.group(4))
    low = float(pmsSilverRmb.group(5))
    if high - price['p'] > price['p'] - low:
        price['p0'] = high
        price['per'] = round((price['p'] - high) * 100 / high, 3)
    else:
        price['p0'] = low
        price['per'] = round((price['p'] - low) * 100 / low, 3)

    log.info('icbc agg: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' +
             str(price['p0']) + ', ' + str(price['per']))
    return price
예제 #5
0
파일: main.py 프로젝트: zjw0358/pymisc
def process(func):
    thread = threading.Thread(target=func)
    thread.start()
    thread.join(120)
    if thread.is_alive():
        log.info('* hanging thread * ')
    else:
        log.debug('* thread ended normally * ')
예제 #6
0
파일: once.py 프로젝트: 547872495/pymisc
def pm25Monitor():
	try:
		msg = ''
		msg = msg + weather.fetchPm25Forcast()
		if msg:
			log.info('* pm25Monitor MESSAGE * ' + msg)
			sendMessage('PM2.5', msg, 2)
	except:
		log.exception('pm25Monitor Exception Occured!')
예제 #7
0
def importAll():
    #tables = ['XAGUSD1', 'XAGUSD5', 'XAGUSD15', 'XAGUSD30', 'XAGUSD60', 'XAGUSD240', 'XAGUSD1440', 'XAGUSD10080', 'XAGUSD43200', ]
    tables = [
        'XAGUSD1440',
    ]
    for table in tables:
        log.info('importing ' + table)
        importTable(table)
        log.info('done ' + table)
예제 #8
0
파일: once.py 프로젝트: zjw0358/pymisc
def pm25Monitor():
    try:
        msg = ''
        msg = msg + weather.fetchPm25Forcast()
        if msg:
            log.info('* pm25Monitor MESSAGE * ' + msg)
            sendMessage('PM2.5', msg, 2)
    except:
        log.exception('pm25Monitor Exception Occured!')
예제 #9
0
	def cleanOldData(self):
		cur = self.conn.cursor()
		dtLong = long(time.time()) - 48 * 60 * 60
		log.info('cleaning database before: ' + str(dtLong))
		cur.execute('DELETE FROM FEDATA WHERE DTLONG<=?', (dtLong,))
		cur.execute('DELETE FROM NOTICE WHERE DTLONG<=?', (dtLong,))
		self.conn.commit()
		cur.close()
		return
예제 #10
0
파일: main.py 프로젝트: zjw0358/pymisc
def marketMonitor():
    # Investments
    try:
        msg = ''
        msg = msg + prices.latestPrices()
        # sending message if available
        if msg:
            log.info('* marketMonitor MESSAGE * ' + msg)
            sendMessage('Market', msg, 1)
    except:
        log.exception('marketMonitor Exception Occured!')
예제 #11
0
파일: main.py 프로젝트: zjw0358/pymisc
def minuteMonitor():
    # Investments
    try:
        msg = ''
        msg = msg + prices.monitorPrice()
        # sending message if available
        if msg:
            log.info('* minuteMonitor MESSAGE * ' + msg)
            sendMessage('Prices', msg, 1)
    except:
        log.exception('minuteMonitor Exception Occured!')
예제 #12
0
파일: pool.py 프로젝트: 547872495/pymisc
	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(t.equity))
				self.strategies.insert(i, [t, t.equity])
				if l + 1 > self.num:
					self.strategies.pop(-1)
				break
예제 #13
0
파일: mail.py 프로젝트: zjw0358/pymisc
def send(subject, content):

    msg = MIMEText(content[28:].encode('utf-8'), 'plain', 'utf-8')
    msg['Subject'] = re.sub('\n', ';', content[:28])
    msg['From'] = const.QQMAIL_ADDR
    msg['To'] = const.QQMAIL_ADDR

    qq = smtplib.SMTP('smtp.qq.com')
    qq.login(const.QQMAIL_USER, const.QQMAIL_PASSWORD)
    ret = qq.sendmail(msg['From'], msg['To'], msg.as_string())
    qq.quit()
    log.info('Sending email: ' + content + '')
예제 #14
0
파일: mail.py 프로젝트: 547872495/pymisc
def send(subject, content):
	
	msg = MIMEText(content[28:].encode('utf-8'), 'plain', 'utf-8')
	msg['Subject'] = re.sub('\n', ';', content[:28])
	msg['From'] = const.QQMAIL_ADDR
	msg['To'] = const.QQMAIL_ADDR

	qq = smtplib.SMTP('smtp.qq.com')
	qq.login(const.QQMAIL_USER, const.QQMAIL_PASSWORD)
	ret = qq.sendmail(msg['From'], msg['To'], msg.as_string())
	qq.quit()
	log.info('Sending email: ' + content + '')
예제 #15
0
파일: main.py 프로젝트: 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
예제 #16
0
파일: prices.py 프로젝트: 547872495/pymisc
def latestPrices():
	msg = datetime.datetime.now().strftime('%m-%d %H:%M') + ':\n'
	dtypes = ['AGG', 'AGTD', 'SHDX']
	
	db = SqliteDB()
	dtLong = long(time.time())
	for dtype in dtypes:
		price = db.getPrice(dtype, dtLong)
		msg = msg + dtype + ':' + str(price[0]) + ',' + str(price[1]) + '%,' + str(price[2]) + ';\n'
	
	log.info('latest prices: ' + msg)
	return msg
예제 #17
0
파일: macd.py 프로젝트: 547872495/pymisc
def calc_all_macd(table, fast = 12, slow = 26, sign = 9):
	log.info('MACD generating for ' + table)
	
	db = SqliteDB().createIndicator(table, 'MACD', 'A', fast, slow, sign)
	prices = SqliteDB().getAllPrices(table)
	macds = calc_macd(prices, fast, slow, sign)
	
	for i in range(len(prices)):
		db.addIndicate(prices[i]['dtlong'], macds['macd'][i], macds['sign'][i], macds['fast'][i], macds['slow'][i])
	db.commit()
		
	log.info('MACD done')
예제 #18
0
파일: main.py 프로젝트: 547872495/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
예제 #19
0
파일: kdj.py 프로젝트: 547872495/pymisc
def calc_all_kdj(table, kPeriod = 5, dPeriod = 3, slowing =3):
	log.info('KDJ generating for ' + table)
	
	db = SqliteDB().createIndicator(table, 'KDJ', 'A', kPeriod, dPeriod, slowing)
	prices = SqliteDB().getAllPrices(table)
	
	kds = calc_kd(prices, kPeriod, dPeriod, slowing)
	
	for i in range(len(prices)):
		db.addIndicate(prices[i]['dtlong'], kds['k'][i], kds['d'][i])
	db.commit()
		
	log.info('KDJ done')
예제 #20
0
파일: rsi.py 프로젝트: 547872495/pymisc
def calc_all_rsi(table, period = 14):
	log.info('RSI generating for ' + table)
	
	db = SqliteDB().createIndicator(table, 'RSI', 'A', period)
	prices = SqliteDB().getAllPrices(table)
	rsis = calc_rsi(prices, period)
	
	for i in range(len(prices)):
		db.addIndicate(prices[i]['dtlong'], rsis['rsi'][i], rsis['up'][i], rsis['down'][i])
	db.commit()
	
	log.info('RSI done')
	
예제 #21
0
def latestPrices():
    msg = datetime.datetime.now().strftime('%m-%d %H:%M') + ':\n'
    dtypes = ['AGG', 'AGTD', 'SHDX']

    db = SqliteDB()
    dtLong = long(time.time())
    for dtype in dtypes:
        price = db.getPrice(dtype, dtLong)
        msg = msg + dtype + ':' + str(price[0]) + ',' + str(
            price[1]) + '%,' + str(price[2]) + ';\n'

    log.info('latest prices: ' + msg)
    return msg
예제 #22
0
def calc_all_macd(table, fast=12, slow=26, sign=9):
    log.info('MACD generating for ' + table)

    db = SqliteDB().createIndicator(table, 'MACD', 'A', fast, slow, sign)
    prices = SqliteDB().getAllPrices(table)
    macds = calc_macd(prices, fast, slow, sign)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], macds['macd'][i], macds['sign'][i],
                       macds['fast'][i], macds['slow'][i])
    db.commit()

    log.info('MACD done')
예제 #23
0
	def addData(self, table, data):
		cur = self.conn.cursor()
		cur.execute('SELECT DTLONG FROM ' + table + ' WHERE DTLONG=?', (long(data[0]),))
		if cur.fetchone() == None:
			#log.debug('inserting ' + table + ' : ')
			#log.info(data)
			#cur.execute('INSERT INTO PRICE(DTLONG,DDATE,DTIME,DCLOSE,DHIGH,DLOW,DOPEN,DRMB,DVOL,DINX1,DINX2,DINX3,DINX4,DINX5,DINX6,DNOTES) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', data)
			cur.execute('INSERT INTO ' + table + ' VALUES(?,?,?,?,?,?,?,?,?,?,?)', data)
		else:
			log.info('** data duplicate ** ' + table + ' : ')
			log.info(data)
		cur.close()
		return
예제 #24
0
파일: rsi.py 프로젝트: zjw0358/pymisc
def calc_all_rsi(table, period=14):
    log.info('RSI generating for ' + table)

    db = SqliteDB().createIndicator(table, 'RSI', 'A', period)
    prices = SqliteDB().getAllPrices(table)
    rsis = calc_rsi(prices, period)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], rsis['rsi'][i], rsis['up'][i],
                       rsis['down'][i])
    db.commit()

    log.info('RSI done')
예제 #25
0
def calc_all_bolling(table, period=20, deviate=2):
    log.info('Bolling generating for ' + table)

    db = SqliteDB().createIndicator(table, 'BOLLING', 'A', period, deviate)
    prices = SqliteDB().getAllPrices(table)
    bollings = calc_bolling(prices, period, deviate)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], bollings['boll'][i],
                       bollings['mean'][i], bollings['upper'][i],
                       bollings['lower'][i], bollings['std'][i])

    db.commit()
    log.info('Bolling done')
예제 #26
0
파일: pool.py 프로젝트: zjw0358/pymisc
    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(t.equity))
                self.strategies.insert(i, [t, t.equity])
                if l + 1 > self.num:
                    self.strategies.pop(-1)
                break
예제 #27
0
def calc_all_kdj(table, kPeriod=5, dPeriod=3, slowing=3):
    log.info('KDJ generating for ' + table)

    db = SqliteDB().createIndicator(table, 'KDJ', 'A', kPeriod, dPeriod,
                                    slowing)
    prices = SqliteDB().getAllPrices(table)

    kds = calc_kd(prices, kPeriod, dPeriod, slowing)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], kds['k'][i], kds['d'][i])
    db.commit()

    log.info('KDJ done')
예제 #28
0
파일: bolling.py 프로젝트: 547872495/pymisc
def calc_all_bolling(table, period = 20, deviate = 2):
	log.info('Bolling generating for ' + table)
	
	db = SqliteDB().createIndicator(table, 'BOLLING', 'A', period, deviate)
	prices = SqliteDB().getAllPrices(table)
	bollings = calc_bolling(prices, period, deviate)
	
	for i in range(len(prices)):
		db.addIndicate(prices[i]['dtlong'], bollings['boll'][i], bollings['mean'][i], bollings['upper'][i], bollings['lower'][i], bollings['std'][i])
		
	db.commit()
	log.info('Bolling done')
		
		
예제 #29
0
파일: prices.py 프로젝트: 547872495/pymisc
def sinaAgTD():
	# fetch price of AG T+D
	f = urllib2.urlopen('http://hq.sinajs.cn/list=hf_AGTD', timeout=20)
	html = f.read()
	html = html[20:len(html) - 3]
	agtdArr = re.split(',', html)
	
	price = {}
	price['dt'] = datetime.datetime.strptime(agtdArr[12] + ' ' + agtdArr[6], '%Y-%m-%d %H:%M:%S')	
	price['p'] = float(agtdArr[0])
	price['p0'] = float(agtdArr[7])
	price['per'] = round((price['p'] - price['p0']) * 100 / price['p0'], 3)
	
	log.info('sina agtd: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' + str(price['p0']) + ', ' + str(price['per']))
	return price
예제 #30
0
파일: prices.py 프로젝트: 547872495/pymisc
def sinaSHDX():
	# fetch price of AG T+D
	f = urllib2.urlopen('http://hq.sinajs.cn/rn=1386417950746&list=sh000001', timeout=20)
	html = f.read()
	html = html[21:len(html) - 3]
	arr = re.split(',', html)
	
	price = {}
	price['dt'] = datetime.datetime.strptime(arr[30] + ' ' + arr[31], '%Y-%m-%d %H:%M:%S')	
	price['p'] = float(arr[3])
	price['p0'] = float(arr[2])
	price['per'] = round((price['p'] - price['p0']) * 100 / price['p0'], 3)
	
	log.info('sina sh: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' + str(price['p0']) + ', ' + str(price['per']))
	return price
예제 #31
0
	def addIndicate(self, dtlong, val1, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0, val8 = 0):
		cur = self.conn.cursor()
		cur.execute('SELECT DTLONG FROM ' + self.table + ' WHERE DTLONG=?', (dtlong,))
		if cur.fetchone() == None:
			dt = time.localtime(dtlong)
			ddate = time.strftime('%Y-%m-%d', dt)
			dtime = time.strftime('%H:%M:%S', dt)
			#log.debug('inserting ' + self.table + ' : ' + str(val1))
			#log.info(data)
			cur.execute('INSERT INTO ' + self.table + ' VALUES(?,?,?,?,?,?,?,?,?,?,?)', (dtlong,ddate,dtime,val1,val2,val3,val4,val5,val6,val7,val8))
		else:
			log.info('** data duplicate ** ' + self.table + ' : ')
			log.info(data)
		#self.conn.commit()
		cur.close()
		return
예제 #32
0
def sinaAgTD():
    # fetch price of AG T+D
    f = urllib2.urlopen('http://hq.sinajs.cn/list=hf_AGTD', timeout=20)
    html = f.read()
    html = html[20:len(html) - 3]
    agtdArr = re.split(',', html)

    price = {}
    price['dt'] = datetime.datetime.strptime(agtdArr[12] + ' ' + agtdArr[6],
                                             '%Y-%m-%d %H:%M:%S')
    price['p'] = float(agtdArr[0])
    price['p0'] = float(agtdArr[7])
    price['per'] = round((price['p'] - price['p0']) * 100 / price['p0'], 3)

    log.info('sina agtd: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' +
             str(price['p0']) + ', ' + str(price['per']))
    return price
예제 #33
0
def sinaSHDX():
    # fetch price of AG T+D
    f = urllib2.urlopen('http://hq.sinajs.cn/rn=1386417950746&list=sh000001',
                        timeout=20)
    html = f.read()
    html = html[21:len(html) - 3]
    arr = re.split(',', html)

    price = {}
    price['dt'] = datetime.datetime.strptime(arr[30] + ' ' + arr[31],
                                             '%Y-%m-%d %H:%M:%S')
    price['p'] = float(arr[3])
    price['p0'] = float(arr[2])
    price['per'] = round((price['p'] - price['p0']) * 100 / price['p0'], 3)

    log.info('sina sh: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' +
             str(price['p0']) + ', ' + str(price['per']))
    return price
예제 #34
0
파일: histre.py 프로젝트: 547872495/pymisc
	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('=========================')
예제 #35
0
def tally(ptype, price):
    db = SqliteDB()
    ret = ''

    dLong = time.mktime(price['dt'].timetuple())
    dDate = price['dt'].strftime('%Y-%m-%d')
    dTime = price['dt'].strftime('%H:%M:%S')

    db.addPrice((ptype, dLong, dDate, dTime, price['p'], price['per'],
                 price['p0'], '', dLong, dDate, dTime))

    # calculate the percentage
    percent0 = price['per']

    # get the price of 3 minutes ago
    price3 = db.getPrice(ptype, dLong - 180)
    percent3 = 0
    if price3:
        log.debug(ptype + ',price3,' + str(price3[3]) + ',' + str(price3[0]))
        price3 = price3[0]
        percent3 = round((price['p'] - price3) * 100 / price3, 3)

    # get the price of 30 minutes ago
    price30 = db.getPrice(ptype, dLong - 1800)
    percent30 = 0
    if price30:
        log.debug(ptype + ',price30,' + str(price30[3]) + ',' +
                  str(price30[0]))
        price30 = price30[0]
        percent30 = round((price['p'] - price30) * 100 / price30, 3)

    # get last message information
    notper0 = db.getNotice(ptype, 0, dDate)
    log.info(ptype + ', percentage 0: ' + str(percent0) + ', last notice: ' +
             str(notper0))
    #print notper0
    if abs(percent0 - notper0) >= 1:
        ret = ptype + '0,' + str(price['p']) + ',' + str(percent0) + '%;\n'
        db.addNotice(
            (ptype, 0, dLong, dDate, dTime, price['p'], percent0, ret, ''))

    notcount30 = db.getNoticeCount(ptype, 30, dLong - 1800)
    log.info(ptype + ', percentage 30: ' + str(percent30) +
             ', notice in 30 minutes: ' + str(notcount30))
    if notcount30 == 0 and abs(percent30) >= 1:
        ret = ret + ptype + '30,' + str(
            price['p']) + ',' + str(percent30) + '%;\n'
        db.addNotice(
            (ptype, 30, dLong, dDate, dTime, price['p'], percent30, ret, ''))

    notcount3 = db.getNoticeCount(ptype, 3, dLong - 180)
    log.info(ptype + ', percentage 3: ' + str(percent3) +
             ', notice in 3 minutes: ' + str(notcount3))
    if notcount3 == 0 and abs(percent3) >= 0.5:
        ret = ret + ptype + '3,' + str(
            price['p']) + ',' + str(percent3) + '%;\n'
        db.addNotice(
            (ptype, 3, dLong, dDate, dTime, price['p'], percent3, ret, ''))

    return ret
예제 #36
0
def calc_all_ma(table, matype, period, weight=0):
    log.info('MA generating ' + matype + ' for ' + table)

    db = SqliteDB().createIndicator(table, 'MA', matype, period, weight)
    prices = SqliteDB().getAllPrices(table)
    ps = [p['close'] for p in prices]

    if matype == 'MA':
        mas = calc_ma(ps, period)
    elif matype == 'EMA':
        mas = calc_ema(ps, period)
    elif matype == 'SMA':
        mas = calc_sma(ps, period)
    elif matype == 'LWMA':
        mas = calc_lwma(ps, period)

    for i in range(len(prices)):
        db.addIndicate(prices[i]['dtlong'], mas[i])
    db.commit()

    log.info('MA done')
    return mas
예제 #37
0
파일: ma.py 프로젝트: ongbe/pymisc
def calc_all_ma(table, matype, period, weight=0):
    log.info("MA generating " + matype + " for " + table)

    db = SqliteDB().createIndicator(table, "MA", matype, period, weight)
    prices = SqliteDB().getAllPrices(table)
    ps = [p["close"] for p in prices]

    if matype == "MA":
        mas = calc_ma(ps, period)
    elif matype == "EMA":
        mas = calc_ema(ps, period)
    elif matype == "SMA":
        mas = calc_sma(ps, period)
    elif matype == "LWMA":
        mas = calc_lwma(ps, period)

    for i in range(len(prices)):
        db.addIndicate(prices[i]["dtlong"], mas[i])
    db.commit()

    log.info("MA done")
    return mas
예제 #38
0
파일: prices.py 프로젝트: 547872495/pymisc
def icbcAgg():
	f = urllib2.urlopen('http://www.icbc.com.cn/ICBCDynamicSite/Charts/GoldTendencyPicture.aspx', timeout=20)
	html = f.read()
	silverRmbPattern = re.compile(r"""人民币账户白银\s*</td>\s*<td[\s\S]+?</td>\s*<td.*?>\s*(.*?)\s*</td>
\s*<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>\s*<td.*?>\s*(.*?)\s*</td>""", re.S + re.X)
	pmsSilverRmb = silverRmbPattern.search(html)
	
	price = {}
	# Date Time
	price['dt'] = datetime.datetime.now()
	price['p'] = float(pmsSilverRmb.group(3))
	high = float(pmsSilverRmb.group(4))
	low = float(pmsSilverRmb.group(5))
	if high - price['p'] > price['p'] - low:
		price['p0'] = high
		price['per'] = round((price['p'] - high) * 100 / high, 3)
	else:
		price['p0'] = low
		price['per'] = round((price['p'] - low) * 100 / low, 3)
		
	log.info('icbc agg: ' + str(price['dt']) + ', ' + str(price['p']) + ', ' + str(price['p0']) + ', ' + str(price['per']))
	return price
예제 #39
0
	def createIndicator(self, dtype, itype, subtype, arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0, arg5 = 0):
		self.table = dtype + '_' + itype + '_' + subtype + '_' + str(arg1) + '_' + str(arg2) + '_' + str(arg3) + '_' + str(arg4) + '_' + str(arg5)
		log.info('create table ' + self.table)
		cur = self.conn.cursor()
		cur.execute('''
CREATE TABLE IF NOT EXISTS ''' + self.table + '''
(
DTLONG INTEGER PRIMARY KEY,
DDATE  TEXT,
DTIME  TEXT,
VAL1   REAL,
VAL2   REAL,
VAL3   REAL,
VAL4   REAL,
VAL5   REAL,
VAL6   REAL,
VAL7   REAL,
VAL8   REAL
)
''')
		cur.execute('DELETE FROM ' + self.table)
		self.conn.commit()
		cur.close()
		return self
예제 #40
0
파일: prices.py 프로젝트: 547872495/pymisc
def tally(ptype, price):
	db = SqliteDB()
	ret = ''
	
	dLong = time.mktime(price['dt'].timetuple())
	dDate = price['dt'].strftime('%Y-%m-%d')
	dTime = price['dt'].strftime('%H:%M:%S')
	
	db.addPrice((ptype, dLong, dDate, dTime, price['p'], price['per'], price['p0'], '', dLong, dDate, dTime))
	
	# calculate the percentage
	percent0 = price['per']
	
	# get the price of 3 minutes ago
	price3 = db.getPrice(ptype, dLong - 180)
	percent3 = 0
	if price3:
		log.debug(ptype + ',price3,' + str(price3[3]) + ',' + str(price3[0]))
		price3 = price3[0]
		percent3 = round((price['p'] - price3) * 100 / price3, 3)
	
	# get the price of 30 minutes ago
	price30 = db.getPrice(ptype, dLong - 1800)
	percent30 = 0
	if price30:
		log.debug(ptype + ',price30,' + str(price30[3]) + ',' + str(price30[0]))
		price30 = price30[0]
		percent30 = round((price['p'] - price30) * 100 / price30, 3)
	
	# get last message information
	notper0 = db.getNotice(ptype, 0, dDate)
	log.info(ptype + ', percentage 0: ' + str(percent0) + ', last notice: ' + str(notper0))
	#print notper0
	if abs(percent0 - notper0) >= 1:
		ret = ptype + '0,' + str(price['p']) + ',' + str(percent0) + '%;\n'
		db.addNotice((ptype, 0, dLong, dDate, dTime, price['p'], percent0, ret, ''))
	
	notcount30 = db.getNoticeCount(ptype, 30, dLong - 1800)
	log.info(ptype + ', percentage 30: ' + str(percent30) + ', notice in 30 minutes: ' + str(notcount30))
	if notcount30 == 0 and abs(percent30) >= 1:
		ret = ret + ptype + '30,' + str(price['p']) + ',' + str(percent30) + '%;\n'
		db.addNotice((ptype, 30, dLong, dDate, dTime, price['p'], percent30, ret, ''))
	
	notcount3 = db.getNoticeCount(ptype, 3, dLong - 180)
	log.info(ptype + ', percentage 3: ' + str(percent3) + ', notice in 3 minutes: ' + str(notcount3))
	if notcount3 == 0 and abs(percent3) >= 0.5:
		ret = ret + ptype + '3,' + str(price['p']) + ',' + str(percent3) + '%;\n'
		db.addNotice((ptype, 3, dLong, dDate, dTime, price['p'], percent3, ret, ''))
	
	return ret
예제 #41
0
파일: fetion.py 프로젝트: zjw0358/pymisc
def sendMultiSms(multimsg):
	try:
		phone = PyFetion.PyFetion(const.FETION_USER, const.FETION_PASSWORD, 'TCP', debug=True)
		if phone.login(PyFetion.FetionOnline):
			log.info('Fetion login success!')
			for msgs in multimsg:
				msg = msgs[0]
				for receiver in msgs[1]:
					phone.send_sms(msg.encode('utf-8'), receiver, True)
					log.info('SMS sent! receiver ' + receiver + 'Sending ' + msg + '')
			phone.logout()
			return True
		else:
			log.info('Fetion login failed, message not send! receivers: ' + receivers + ', msg: ' + msg)
			return False
			
	except:
		log.exception('SMS sent failed!')
		return False
예제 #42
0
파일: main.py 프로젝트: zjw0358/pymisc
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
예제 #43
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()
예제 #44
0
파일: main.py 프로젝트: zjw0358/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
예제 #45
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()
예제 #46
0
파일: main.py 프로젝트: zjw0358/pymisc
        db.cleanOldData()
    except:
        log.exception('dayMonitor Exception Occured!')


def process(func):
    thread = threading.Thread(target=func)
    thread.start()
    thread.join(120)
    if thread.is_alive():
        log.info('* hanging thread * ')
    else:
        log.debug('* thread ended normally * ')


if __name__ == "__main__":
    sched = Scheduler(standalone=True)
    #sched.add_cron_job(process, args=[minuteMonitor], day_of_week='mon', hour='7-23', minute='*')
    #sched.add_cron_job(process, args=[minuteMonitor], day_of_week='tue-fri', second='0,30')
    #sched.add_cron_job(process, args=[minuteMonitor], day_of_week='sat', hour='0-3', minute='*')
    sched.add_cron_job(process, args=[weatherMonitor], hour='7', minute='30')
    sched.add_cron_job(process,
                       args=[weatherMonitor],
                       hour='17,21',
                       minute='0')
    sched.add_cron_job(process, args=[pm25Monitor], hour='19', minute='0')
    #sched.add_cron_job(process, args=[marketMonitor], day_of_week='mon-fri', hour='11,15,21', minute='15')
    #sched.add_cron_job(process, args=[batchMonitor], hour='1', minute='0')
    log.info('Monitor starting...')
    sched.start()
예제 #47
0
파일: main.py 프로젝트: xiaobozi/pymisc
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
예제 #48
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()
예제 #49
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()
예제 #50
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()