def run(self, generator_function): self.generator = generator_function self.win = Tk() self.win.grid_columnconfigure(1, weight=1) self.win.wm_title("Cardcinogen") self.template = StringVar() self.deck = StringVar() self.prefix = StringVar() Label(self.win, text="Card template:").grid(row=0, column=0, sticky=E) self.txtTemplate = Entry(self.win, textvariable=self.template).grid(row=0, column=1, sticky=E+W) self.btnTemplate = Button(self.win, text="...", command=self.setTemplate).grid(row=0, column=2) Label(self.win, text="Deck data:").grid(row=1, column=0, sticky=E) self.txtDeck = Entry(self.win, textvariable=self.deck).grid(row=1, column=1, sticky=E+W) self.btnDeck = Button(self.win, text="...", command=self.setDeck).grid(row=1, column=2) Label(self.win, text="Output file prefix:").grid(row=2, column=0, sticky=E) self.txtDeck = Entry(self.win, textvariable=self.prefix).grid(row=2, column=1, sticky=E+W) self.btnGenerate = Button(self.win, text="Generate!", command=self.invoke).grid(row=3, column=0, columnspan=3, sticky=E+W) self.log = Text(self.win) self.log.grid(row=4, column=0, columnspan=3, sticky=N+S+E+W) self.log.config(state=DISABLED) log.setlog(self) return self.win.mainloop()
#!/usr/bin/env python # -*- coding: utf-8 -*- """ calculate indicators """ import numpy as np import pandas as pd import log logger = log.setlog("indicator.py") def typ(df_prices_high, df_prices_low, df_prices_close): """ Typical Price = average( high + low + close ) """ df_typ = (df_prices_high + df_prices_low + df_prices_close) / 3 df_typ.name = 'typ' return df_typ def ema(df_prices, i_period, i_start=1): """ Exponential Moving Average S(t) = w*P(t) + (1-w) * S(t-1) w = 2/(N+1) """ i_len = len(df_prices) assert i_len >= i_period
""" run several arguments and sort results """ import local import strategy as st import backtester as bt import sys,os,itertools import pandas as pd import multiprocessing as mp import log logger=log.setlog("optimizer.py") BASE_DIR = os.path.abspath(os.path.dirname(__file__)) def parameters_generator(): arg1 = range(48,51) arg2 = range(28,31) return itertools.product(arg1,arg2) def tryArgList(): """ run several arguments according to arglist file """ logger.info("backtest start.prod=%s",s_prod) ### write backtester file header ### filepath_backtester =BASE_DIR+ '/backtester/' + s_prod + '.bt'
""" define operation functions """ import pandas as pd import log logger = log.setlog("operation.py") global time def countif(df_data, i_period): """ count the number of True data in the last i_period time Example: countif([True,False,True,True,False],3)=2 :param df_data: a Series of boolean value, result from conditions such as 'ema27 > ema50 and cci10>100 or typ>ema27' :param i_period: time length :return: a number """ assert i_period <= len(df_data) index = df_data.index.get_loc(time) i_count = len(filter(bool, df_data[index - i_period + 1 : index + 1])) return i_count def mro(df_data, i_period, i_index): """ Most Recent occur Example: mro([True,False,True,True,False],3,1)=1
import datetime as dt import dateutil import matplotlib.pyplot as plt import pandas as pd import numpy as np import os, glob import multiprocessing as mp import log logger=log.setlog("pdd.py") #rights = pd.DataFrame() def process_pd(): ## three types of profit distribution: cash, stock, increase ##Prod ProductCode PlanDate DecisionDate PDD Cash Stock Increase StockSum ##SH SH600000 20000328 20000509 20000706 0.15 0 0 0 ##SH SH600000 20020321 20020629 20020822 0.2 0 0.5 0.5 try: confpath = './configuration/PDData.txt' global rights rights = pd.read_csv(confpath, sep ='\t') # process security simultanously using multiprocess filepath = './datas/Stock' seclist = glob.glob(filepath+'/*_nopd') logger.info("nopdlist:%s",seclist) po = mp.Pool (mp.cpu_count()) teams = po.map(work, seclist) po.close() po.join()
import datetime as dt import dateutil import matplotlib.pyplot as plt import pandas as pd import numpy as np import os, glob import multiprocessing as mp import log logger = log.setlog("ts.py") freqlist = ['5min','15min','1H','1D'] rights = pd.DataFrame() def convert_ts(): try: # process security simultanously using multiprocess filepath = './datas/Stock' seclist = glob.glob(filepath+'/*_pd') logger.info("pd file lists:%s",seclist) arglist=[] for f in seclist: for freq in freqlist: arglist.append((f,freq)) po = mp.Pool (mp.cpu_count()) teams = po.map(work, arglist) po.close() po.join()
import pandas as pd import multiprocessing as mp import os import glob import dateutil import fcntl import log logger = log.setlog("ts_futures.py") """ Input: individual file containing daily tick data =================== 20140303_IF1403_CFFEX_L1.txt ======================= close high low tlots tvols openint zero1 zero2 time zero3 bid bidvol ask askvol 2165.6 2165.6 2165.6 587 381362160 94019 0 0 09:14:00 0 2165.4 7 2165.6 8 2165.6 2166 2164.4 809 525567540 94045 0 0 09:15:00 0 2165.6 142 2166 7 2165 2166.6 2164.4 1166 757512840 94027 0 0 09:15:00 0 2165 12 2165.4 9 ... ======================================================================== OutPut: combine all daily files into one big tick file """ ############## global configuration ############ prod = 'IF' s_starttime = '09:15:00' s_breakmor = '11:30:00' s_breakrep = '11:29:59' s_endtime = '15:15:00'
import datetime as dt import matplotlib.pyplot as plt import pandas as pd import numpy as np import os, glob import multiprocessing as mp import log logger=log.setlog("processdata.py") def list_securities (): try: ## get all the securities to be processed ## loop through to process filepath_rawdata='../share/stocks_data/' #filepath_rawdata='./datas_raw/Stock/' seclist = glob.glob(filepath_rawdata+'*') logger.info("rawdata list:%s",seclist) po = mp.Pool (mp.cpu_count()) po.map(work, seclist) po.close() po.join() except Exception,ex: logger.error( "{0}: {1}".format(Exception,ex) ) def work (s): logger.info("work(%s) start",s) secname, result =process_one_stock(s) ## formated print '''
# datestimes, typ, ema50, ema27, rsi14, cci10, tr, kpi # 2014-11-12 09:15:00, 2557.9333, 2557.4000, 2557.4000, 0.5000, 0.0000, 2.8000, 2557.4000 # # ######################################################################### # Output filepath_blotter ='./blotter/Future/IF/IF_50_30.bl' # datestimes, direction, lot, product, price, refprice, PNL, maxPNL, minPNL # 2014-11-17 09:38:15, B, 1, IF, 2587.0000, 2586.1100 # 2014-11-17 09:40:15, S, 1, IF, 2581.8000, 2582.5770, -5.2000, 1.4000, -5.2000 import os BASE_DIR = os.path.abspath(os.path.dirname(__file__)) import operation as op import pandas as pd import sys import log logger=log.setlog("strategy.py") i_lot=1 class Strategy(object): def __init__(self, s_prod, s_prodtype): if not (s_prodtype in ["Future","Stock"]): logger.error(r'prodtype not in ["Future","Stock"]. incorrect product type. ') self.prod = s_prod self.prodtype = s_prodtype self.freq = None self.freq2 =None self.freq3 =None def preparedata(self):
import pandas as pd import glob,os,sys import multiprocessing as mp import indicator as ic import log logger = log.setlog("gencator.py") stock_freq = ['5min','15min','1H','1D'] future_freq = ['4s','15s','1min'] def generate_all(): try: if s_prodtype not in ["Future","Stock"]: logger.error("incorrect product type") return ## grab all prods under ts directory prodlist = glob.glob('./ts/'+s_prodtype+'/*') prodnamelist = [] for prod in prodlist: prodname = prod.split('/')[-1] prodnamelist.append(prodname) if len(prodnamelist)==0: logger.warning("no prod to generate") return else: logger.info("{} prods to generate indicator (.ics)".format(len(prodnamelist)))
#! /usr/bin/env python # -*- coding: utf-8 -*- import urllib import urllib2 import cookielib import base64 import re import json import rsa import binascii import redis #logger import log logger=log.setlog("backtester.py") #global variable userMap={} #redis-server r = redis.Redis(host='localhost',port=6379,db=0) class weiboLogin: cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) postdata = { 'entry': 'weibo', 'gateway': '1',