Exemple #1
0
def main():
    cmdKeys = "w:"
    fw = public.getCmdParam(cmdKeys, ("-w", 2))

    FT = fytuObject()
    # 每10分钟检查一次窝轮行情
    if fw == 1:
        valid = FT.isTradeDate('HK')
        if valid:
            obj = warModel()
            obj.online()

    # 每15分钟检查一次基金申购:
    if fw == 2:
        if FT.isTradeDate('SH'):
            obj = etf4_compare()
            d, t = public.getDate(), public.getDatetime()
            # 大于2点45分,则每15秒运行一次直到结束
            if t > d + " 14:45:00" and t < d + " 15:00:00":
                while True:
                    t = public.getDatetime()
                    if not (t > d + " 14:45:00" and t < d + " 15:00:00"): break
                    obj.create(1)
                    time.sleep(15)
            else:
                # 小于2点45分,则每15分钟运行一次
                obj.create(0)
Exemple #2
0
    def order(self, docs, type='stock_order', callback=None):
        if not docs or len(docs) == 0: return None

        maps = self.matic_csv_map[type]

        # 文件夹对应文件
        filename = maps["filename"] % (
            self.stage, str(public.getDatetime(style=self.timeStyle))[:-3])
        CSV = csvFile(folder=self.Folder,
                      filename=filename,
                      headers=[n[0] for n in maps["columns"]])

        orders = []
        for doc in docs:
            order = copy.deepcopy(doc)
            c = doc['code']
            order.update({
                "code":
                c[:c.find('.')],
                "market":
                'SZ' if c.find('XSHE') > -1 else
                'SH' if c.find('XSHG') > -1 else '',
                'mode':
                doc['isBuy'],
                'type':
                "stock",
                'volume':
                doc['vol'],
                'createtime':
                public.getDatetime()
            })

            # 检查资金和持仓
            if not self.check(order): continue

            rec = self.parseDoc(order, "stock_order")
            CSV.writeLine(rec)

            doc.update({
                "orderID": rec["local_report_no"],
                "returnid": rec["local_report_no"],
            })
            orders.append(doc)

        CSV.close()
        # ftp 上传
        if self.ftpUsed:
            self.ftpUpload(filename)

        # 保存序号, 待bug修复后 改为uuid
        self.setIndex(self.recordIndex)

        # 检查操作结果,回写数据库

        return self.result(orders)
Exemple #3
0
    def get_price(self, type=None):
        self.keylist = sh50_price().keylist
        ary_code = []

        # 缓存
        if len(self.codelist) == 0:
            res = self.Base.getCodes()
            for doc in res:
                ary_code.append(doc["code"])
                ary_code.append(doc["code_down"])
                # expireDay
                self.expireMap[doc["code"]] = self.expireMap[
                    doc["code_down"]] = doc["expireDay"]

            self.codelist = ary_code
        else:
            ary_code = self.codelist

        param = ",".join([self.paramStyle % c.strip() for c in ary_code])
        href = self.url + param
        # 查询代码
        res = self.getI(href)
        dfs = []

        i = 0
        # 写入到pandas
        for item in res.split(";")[:-1]:
            tmp = [ary_code[i], public.getDatetime()] + self.extract(item)
            dfs.append(self.set(tmp))
            # dfs.append([0 if n=="" else n for n in tmp])
            i += 1
        # df = pd.DataFrame(dfs, columns =self.keylist)
        return dfs
Exemple #4
0
    def record(self):
        # 510050 行情
        stock = self.Int.get510050()
        stock["datetime"] = public.getDatetime()
        self.Owner.insert(stock)

        # 期权行情
        etfs = self.Int.get_price()
        spys = []

        self.price_owner = float(stock["price"])

        for i in range(0, len(etfs), 2):
            # 计算剩余天数
            r = public.dayDiff(self.Int.expireMap[etfs[i]["code"]],
                               public.getDate())

            rec0 = self.calc(etfs[i], etfs[i + 1], r, mode=0)
            spys.append(rec0)

            rec1 = self.calc(etfs[i + 1], etfs[i], r, mode=1)
            spys.append(rec1)

        # inform 通知
        try:
            self.inform(spys)
        except:
            logger.error("ws disconnect")

        # 写入数据库
        self.sPrice.insertAll(etfs)

        self.Spy.insertAll(spys)
Exemple #5
0
    def start(self, codes, kline, tms):
        print((public.getDatetime(), ':  start ', codes, kline, tms))
        self.Rice = interface_Rice()
        self.Record = sh50_orderForm()
        self.Record.tablename = 'sh50_orderForm_train'

        self.codes = codes
        self.Price = sh50_price_s()
        self.klineType = kline
        self.timePeriods = tms

        res = self.Rice.kline(codes, period=kline, start=self.startDate, end=self.endDate)
        df = res[codes[0]]
        # 计算统一特征
        for conds in self.iterCond():

            self.uid = "%s_%s_%s_%s" % (codes[0].replace('.', '-'), tms, kline[:-1], conds)

            df = self.add_stock_index(df, index_list=self.index_list)
            df['createTime'] = df.index

            saveList = ['30_5_0.25_1.75', '15_15_0.25_1.75', '15_30_0.25_1.75', '30_60_0.25_1.75']
            if self.uid[12:] in saveList:
                cs = []
                bans = 'ma,bullwidth,open,sard,rsi,high,low,std,top,lower,p_h,p_l,,close,volume,wmean,width,kdj_d2,createTime'.split(
                    ',')
                for c in df.columns:
                    if c not in bans:
                        cs.append(c)

                file = self.Rice.basePath + '%s.csv' % self.uid
                df.to_csv(file, index=1, columns=cs)

            # print(('start ', self.uid))
            self.saveStage(df)
Exemple #6
0
    def closeFuture(self, id='278231', price=5059, isstop=2, date=None):
        self.Record = future_orderForm()
        self.BI = BaseInfo([])

        doc = self.Record.getById(id)
        docnew = copy.deepcopy(doc)
        base = self.BI.doc(doc['name'])
        amount = price * doc['hands'] * base['contract_multiplier']
        r = base['ratio']
        fee = doc['hands'] * r if r >= 0.5 else amount * r
        docnew.update({
            "isopen":
            0,
            "isstop":
            isstop,
            "price":
            price,
            "mode":
            -doc["mode"],
            "vol":
            doc['hands'] * base['contract_multiplier'],
            "status":
            6,
            "fee":
            fee,
            "createdate":
            public.getDatetime() if date is None else date,
            "income":
            np.sign(doc["mode"]) * (price - doc['price']) * doc['hands'] *
            base['contract_multiplier'] - fee,
            "memo":
            ''
        })
        print(docnew)
        self.Record.insert(docnew)
Exemple #7
0
    def getDf(self, codes, period='1d'):
        Rice = interface_Rice()
        Rice.setIndexList(self.indexCodeList)
        Base = future_baseInfo()
        BI = BaseInfo([])
        if len(codes) == 0:
            codes = Base.getUsedMap(hasIndex=True)

        mCodes = Rice.getMain(codes)
        dfs = Rice.kline(mCodes,
                         period=period,
                         start=public.getDate(diff=-10),
                         pre=200)
        i = 0

        for mcode in mCodes:
            c = codes[i]
            last = BI.att(c, 'nightEnd')[0:6].replace(':', '')
            file = Rice.basePath + 'future_%s_%s_%s.csv' % (
                mcode, last, public.getDatetime(style='%Y%m%d_%H%M%S'))
            print(mcode, last)
            df = dfs[mcode]
            df['datetime'] = df.index

            df0 = df[df['datetime'] > '2019-01-17 13:47:40.000']
            print(df0.index, len(df0))

            #df.to_csv(file, index=0)
            i += 1
            break
Exemple #8
0
    def calc(self, code, df, type=0):

        if type == 0:
            avg = df["close"][:-1].mean()
            c = df['close'][self.k_amount - 1]
            self.lastVolume_Map[code] = df['volume'][self.k_amount - 1]
        else:
            avg = df["close"][1:].mean()
            c = df['close'][self.k_amount]
            self.lastVolume_Map[code] = df['volume'][self.k_amount]

        res = (c - avg) / avg * 100

        # 购买方式
        if res > 1:
            type = 1
        elif res < -1:
            type = -1
        else:
            type = 0

        # 生成记录
        doc = {
            "code": code,
            'returnid': 0,
            'type': type,
            'price': round(c, 3),
            'volume': int(round(500000 / c / 100, 0) * 100),
            'amount': 0,
            'value': res,
            'createtime': public.getDatetime()
        }
        return doc
Exemple #9
0
    def total(self, dfs, period=14):
        # 计算参数
        df0 = dfs[self.mCodes[0]]

        close = df0["close"]
        df0["datetime"] = df0.index
        df0['mcode'] = rq.get_dominant_future(self.code,
                                              start_date=self.startDate,
                                              end_date=self.endDate)

        df0['atr'] = ta.ATR(df0['high'], df0['low'], close, timeperiod=period)
        df0['atrc'] = ta.ATR(df0['high'], df0['low'], close,
                             timeperiod=1) / df0['atr']

        # kdj顶点
        kdjK, kdjD = ta.STOCH(df0["high"],
                              df0["low"],
                              close,
                              fastk_period=9,
                              slowk_period=3,
                              slowk_matype=1,
                              slowd_period=3,
                              slowd_matype=1)

        df0["kdj_d2"] = kdj_d2 = kdjK - kdjD
        df0["kdjm"] = kdj_d2 * kdj_d2.shift(1)
        df0["kdjm"] = df0.apply(
            lambda row: self.turn(row['kdjm'], row['kdj_d2'], 1), axis=1)

        # 循环 scale
        docs = []
        for conds in self.iterCond():
            uid = self.uidKey % ('_'.join(self.codes), str(period),
                                 self.klineType, str(self.bullwidth), conds)

            df1 = df0.apply(lambda row: self.jump0(row), axis=1)
            for key in self.pointColumns:
                df0[key] = df1[key]

            if self.code in self.csvList:
                file = self.Rice.basePath + '%s_pre.csv' % (uid)
                print(uid, '---------------------------- to_cvs', file,
                      df0.columns)
                df0.to_csv(file, index=0)

            #tot = None
            tot = self.detect(df0, period=period, uid=uid)
            if tot is not None and tot['amount'] != 0:
                tot.update({
                    "method": self.method,
                    "code": self.code,
                    "period": period,
                    "uid": uid,
                    "createdate": public.getDatetime()
                })
                docs.append(tot)
        return docs
Exemple #10
0
    def Twap(self, docs):
        types = [1, -1]  # 分为买卖2类进行下单操作
        results = ""
        for t in types:
            results += self.parseTwap(
                [doc for doc in docs if doc["type"] == t], t) + "<br><br><br>"

        return results + " ---------------- " + public.getDatetime(
        ) + "----------------"
Exemple #11
0
    def total(self, dfs, period=14):
        # 计算参数
        df0 = dfs[self.mCodes[0]]

        close = df0["close"]
        df0["datetime"] = df0.index
        df0['mcode'] = rq.get_dominant_future(self.code,
                                              start_date=self.startDate,
                                              end_date=self.endDate)

        df0['high_5'] = ta.MAX(df0['high'].shift(1), timeperiod=self.preDays)
        df0['low_5'] = ta.MIN(df0['low'].shift(1), timeperiod=self.preDays)

        df0['high_p'] = df0['high'].shift(1)
        df0['low_p'] = df0['low'].shift(1)

        df0['atr'] = ta.ATR(df0['high'], df0['low'], close, timeperiod=period)

        df0['mcode_1'] = df0['mcode'].shift(1)
        df0['isAlter'] = df0.apply(lambda r: 1
                                   if r['mcode'] != r['mcode_1'] else 0,
                                   axis=1)

        df1 = df0.apply(lambda row: self.jump0(row), axis=1)
        for key in self.pointColumns:
            df0[key] = df1[key]

        df0['jump_min'], df0['jump_max'] = ta.MINMAXINDEX(
            df0['jump0'], timeperiod=self.keepDays + 1)
        df2 = df0.apply(lambda row: self.jumps(row, df0), axis=1)
        for key in self.pointColumns2:
            df0[key] = df2[key]

        # 循环 scale
        docs = []
        for conds in self.iterCond():
            uid = self.uidKey % ('_'.join(self.codes), str(period),
                                 self.klineType, str(self.bullwidth), conds)

            if self.code in self.csvList:
                file = self.Rice.basePath + '%s_pre.csv' % (uid)
                print(uid, '---------------------------- to_cvs', file,
                      df0.columns)
                df0.to_csv(file, index=0)

            #tot = None
            tot = self.detect(df0, period=period, uid=uid)
            if tot is not None and tot['amount'] != 0:
                tot.update({
                    "method": self.method,
                    "code": self.code,
                    "period": period,
                    "uid": uid,
                    "createdate": public.getDatetime()
                })
                docs.append(tot)
        return docs
Exemple #12
0
    def order(self, n0, isBuy, mode):
        pN = self.procMap.preNode
        if pN is None:
            batchid = uuid.uuid1()
        else:
            batchid = pN['batchid']

        now = public.getDatetime()
        vol, fee, amount, income, p0 = 0, 0, 0, 0, 0
        price = n0["p_h"] if isBuy == 1 else n0["p_l"]

        if isBuy > 0:
            self.batchid = uuid.uuid1()
            p0 = price
            vol = int(self.iniAmount / p0 / 100) * 100
            amount = vol * p0
            fee = vol * p0 * self.ratio
            income = -fee

        elif isBuy < 0:
            p0 = price
            vol = pN['vol']
            amount = vol * p0
            fee = vol * p0 * self.ratio
            income = vol * (p0 - pN['price']) - fee

        doc = {
            "code": n0['code'],
            "name": n0['code'],
            "createdate": now,
            "price": p0,
            "vol": vol,
            "ini_price": p0,
            "ini_vol": vol,
            "mode": mode,
            "isBuy": isBuy,
            "fee": fee,
            "amount": amount,
            "income": income,
            "method": self.methodName,
            "batchid": batchid,
            "status": 1,
            "uid": self.procMap.currentUid
        }

        self.procMap.isBuy = isBuy
        # 设置上一个记录
        if isBuy > 0:
            self.procMap.preNode = doc
            self.procMap.batchid = uuid.uuid1()

        else:
            self.preNode = None

        #self.orders.append(doc)
        return doc
Exemple #13
0
    def _getFilename(self, action="records", record_type="stkhold", diff=0):
        cur_date = str(public.getDatetime(diff=diff, style=self.dateStyle))

        filename = ''
        if action == "records":
            filename = "%s.%s.csv" % (record_type, cur_date)
        elif action == "results":
            filename = "result_instr.%s.csv" % cur_date

        return filename
Exemple #14
0
    def paramCalc(self, df0, t0):
        # 替换即时k线
        df = copy.deepcopy(df0[:-1])
        # 计算相关参数
        df.loc[public.getDatetime()] = t0

        df = self.add_stock_index(df)
        df['createTime'] = df.index
        param = df.iloc[-1].to_dict()
        return param
Exemple #15
0
 def paramCalc(self, df0, t0):
     # 替换即时k线
     df = copy.deepcopy(df0[:-1])
     # 计算相关参数
     df.loc[public.getDatetime()] = t0
     df = self.add_stock_index(df)
     df['createTime'] = df.index
     df['mode'] = df.apply(lambda row: self.point(row), axis=1)
     param = df.iloc[-1].to_dict()
     return param
Exemple #16
0
    def getETF(self, sign=1, price=2.30, currentTime=None, code=None):
        if currentTime is None:
            currentTime = public.getDatetime()

        if code is None:
            sql = self.sql_etf % (self.tablename, str(currentTime), str(price), str(sign))
        else:
            sql = self.sql_code % (self.tablename, str(currentTime), code)

        # print(sql)
        return self.execOne(sql)
Exemple #17
0
    def total(self, dfs, dfs2=None, period=60):
        # 计算参数
        df0 = dfs[self.mCodes[0]]
        df0["rel_price"] = close = df0["close"]
        df0["datetime"] = df0.index

        s0 = self.shift[0]
        p_l = df0["p_l"] = (df0["close"] + s0)
        p_h = df0["p_h"] = (df0["close"] - s0)

        close2 = dfs2[self.mCodes[0]]["close"]

        # 双线模拟计算k线数据的tick行为
        df0['ma'] = ma = df0.apply(lambda row: self.k_ma(
            row['datetime'], row['rel_price'], close2, period, 0),
                                   axis=1)
        df0['std'] = std = df0.apply(lambda row: self.k_ma(
            row['datetime'], row['rel_price'], close2, period, 1),
                                     axis=1)

        # bullWidth
        df0["bullwidth"] = width = (4 * std / ma * 100).fillna(0)

        # 近三分钟width变动
        df0["widthDelta"] = ta.MA(width - width.shift(1),
                                  timeperiod=3).fillna(0)
        df0["delta"] = (p_l - p_h) / std

        # 循环 scale
        docs = []
        for scale in self.scaleList:
            uid = self.uidKey % ("_".join(self.codes), str(period), str(scale),
                                 self.klineType[:-1], str(self.shiftScale))

            df0["top"], df0["lower"] = ma + scale * std, ma - scale * std

            df0.fillna(0, inplace=True)
            tot = self.detect(df0, period=period, uid=uid)

            if tot is not None and tot['amount'] != 0:
                tot.update({
                    "scale": scale,
                    "code": self.codes[0],
                    "period": period,
                    "uid": uid,
                    "shift": (p_l - p_h).mean(),
                    "method": self.method,
                    "stage": self.stage,
                    "createdate": public.getDatetime(),
                })
                docs.append(tot)

        return docs
Exemple #18
0
    def alterPosi(self, openDocs):
        """自动处理调仓, 调仓结束后,子进程启动时自动检查交易记录,生成新的买单"""
        self.baseInfo = BaseInfo([])
        map = self.posMap

        if map == {}: return None
        orders = []

        # 检查主力合约是否跨期,并自动调仓
        monthWeek, weekDay = public.getMonthDay()

        for code in openDocs:
            # 手动调仓清单
            if code in self.banAlterList: continue

            doc = openDocs[code][0]
            mCode = self.baseInfo.mCode(code)
            key = '%s_%s' % (code, '0' if doc['mode'] > 0 else '1')
            print('alterPosi check:', key, mCode, doc['code'])

            opt0 = mCode != doc['code']
            opt1 = (doc['name'] in self.indexList and monthWeek == 3
                    and weekDay == 3)  # 股指期货调仓

            if (opt0 or opt1) and key in map and map[key][0] >= doc['hands']:
                logger.info(('alterPosi start: c1,c0, v, v0', key, mCode,
                             doc['code'], map[key][0], doc['hands']))
                # 卖出单
                del doc['id']
                d0 = copy.deepcopy(doc)
                d0['mode'] = -d0['mode']
                d0['isopen'] = 0
                d0['isstop'] = 4
                d0['createdate'] = public.getDatetime()
                orders.append(d0)

                # 按原状态买入
                d1 = copy.deepcopy(doc)
                d1['code'] = mCode
                d1['symbol'] = self.baseInfo.parseCtpCode(mCode)
                d1['batchid'] = uuid.uuid1()
                d1['status'] = d0['status'] = 4
                d1['createdate'] = doc['createdate']
                d1['isstop'] = 0
                orders.append(d1)

        # 下单数
        if len(orders) > 0:
            self.sendOrder(orders)
            # return 0, orders

        return self.checkResult()
Exemple #19
0
    def paramCalc(self, dfs, cur):
        if len(dfs) == 0: return None
        period = self.klinePeriod
        c0 = cur[self.mCodes[0]]
        c0['close'] = c0['last']

        # 去掉当前的即时k线
        df0 = copy.deepcopy(dfs[self.mCodes[0]].iloc[-40:])
        #print(len(df0))
        # 计算相关参数
        columns = df0.columns
        # 添加即时K线
        df0.loc[public.getDatetime()] = pd.Series([c0[key] for key in columns],
                                                  index=columns)
        close = df0["close"]
        df0["datetime"] = df0.index

        df0["ma"] = ma = ta.MA(close, timeperiod=period)
        df0["std"] = std = ta.STDDEV(close, timeperiod=period, nbdev=1)

        df0["stdc"] = std / ta.MA(std, timeperiod=period)

        df0['atr'] = ta.ATR(df0['high'], df0['low'], close, timeperiod=period)
        df0['atrr'] = df0['atr'] / ma * 10000
        df0['atrc'] = ta.ATR(df0['high'], df0['low'], close,
                             timeperiod=1) / df0['atr']

        # kdj顶点
        kdjK, kdjD = ta.STOCH(df0["high"],
                              df0["low"],
                              close,
                              fastk_period=5,
                              slowk_period=3,
                              slowk_matype=1,
                              slowd_period=3,
                              slowd_matype=1)

        df0["kdj_d2"] = kdj_d2 = kdjK - kdjD
        df0["kdjm"] = kdj_d2 * kdj_d2.shift(1)
        df0["kdjm"] = df0.apply(
            lambda row: self.turn(row['kdjm'], row['kdj_d2'], 1), axis=1)

        df1 = df0.apply(lambda row: self.point(row), axis=1)
        for key in self.pointColumns:
            df0[key] = df1[key]

        param = copy.deepcopy(df0.iloc[-1]).to_dict()
        self.df0 = df0
        param.update({"p_l": c0["asks"][0], "p_h": c0["bids"][0]})
        return param
Exemple #20
0
    def order(self, n0, isBuy, mode):
        pN = self.preNode
        now = public.getDatetime()
        vol, fee, amount,income, p0 = 0,0,0,0, 0
        price = n0["close"]
        if isBuy > 0 :
            self.batchid =  uuid.uuid1()
            p0 = price * (1+ self.shift)
            vol = int(self.iniAmount/p0)
            amount = vol * p0
            fee = vol * p0 * self.ratio
            income = -fee

        elif isBuy < 0:
            p0 = price * (1 - self.shift)
            vol = pN['vol']
            amount = vol * p0
            fee = vol * p0 * self.ratio
            income = amount - pN['amount']-fee

        doc = {
            "code": n0['code'],
            "name": n0['code'],
            "createdate": n0['createTime'],
            "price": p0,
            "vol": vol,
            "mode": int(mode),
            "isBuy": int(isBuy),
            "fee": fee,
            "amount": amount,
            "income": income,
            "method": self.methodName,
            "batchid": self.batchid,
            "uid": self.uid
        }

        self.records.append(doc)
        if self.saveMongo:
            self.mon_saveTick(n0, doc)

        # 设置上一个记录
        if isBuy > 0:
            self.preNode = doc
        else:
            self.preNode = None

        return True
Exemple #21
0
    def total(self, dfs, period=14):
        # 计算参数
        df0 = dfs[self.mCodes[0]]
        # print(df0.iloc[-1])
        code = self.codes[0]
        #b0 = self.BS[code]
        #rate, mar, mul = (b0[c] for c in ['ratio', 'margin_rate', 'contract_multiplier'])

        df0["datetime"] = df0.index

        print(df0.columns)

        df0["curMa"] = df0.apply(lambda row: self.curMA(row, df0), axis=1)
        df0["ap"], df0["bp"] = df0['a1'].shift(1), df0['b1'].shift(1)
        df0["vol"] = df0['volume'].diff()
        df0["delta"] = df0['open_interest'].diff()
        df0["raise"] = df0['last'].diff()

        # 循环 scale
        docs = []
        for conds in self.iterCond():
            uid = self.uidKey % ('_'.join(self.codes), self.klineType, conds)

            df1 = df0.apply(lambda row: self.calcMode(row), axis=1)
            for key in self.pointColumns:
                df0[key] = df1[key]

            if code in self.csvList:
                file = self.Rice.basePath + '%s_pre.csv' % (uid)
                print(uid, '---------------------------- to_cvs', file,
                      df0.columns)
                df0.to_csv(file, index=0)

            return
            tot = self.detect(df0, period=period, uid=uid)
            print(tot)
            if tot is not None and tot['amount'] != 0:
                tot.update({
                    "method": self.method,
                    "code": self.code,
                    "period": period,
                    "uid": uid,
                    "createdate": public.getDatetime()
                })
                docs.append(tot)
        return docs
Exemple #22
0
    def total(self, dfs, period=14):
        # 计算参数
        self.dfs = dfs
        df0, df1 = (dfs[self.mCodes[i]] for i in [0, 1])

        df = pd.DataFrame(index=df0.index)

        df['close'] = close = df0['close'] / df1['close']
        df['volume'] = (df0['volume'] + df1['volume']) / 2

        df["ma"] = ma = ta.MA(close, timeperiod=period)
        df["std"] = ta.STDDEV(close, timeperiod=period, nbdev=1)
        df["bias"] = (close - ma) / ma

        print(close.mean(), close.std())

        # 循环 scale
        docs = []
        for conds in self.iterCond():
            uid = self.uidKey % ('_'.join(self.codes), self.klineType, conds)

            df1 = df0.apply(lambda row: self.point(row), axis=1)
            for key in self.pointColumns:
                df0[key] = df1[key]

            isIn = False
            for c in self.codes:
                if c in self.cvsList:
                    isIn = True
                    break

            tot = self.detect(df, period=period, uid=uid)
            print(tot)
            if tot is not None and tot['amount'] != 0:
                tot.update({
                    "method": self.method,
                    "code": self.codes[0],
                    "code1": self.codes[1],
                    "period": period,
                    "uid": uid,
                    "createdate": public.getDatetime()
                })
                docs.append(tot)

        return docs
Exemple #23
0
    def paramCalc(self, tick):
        code = self.procMap.codes[0]
        c0 = tick[code]  # 快照
        c0['close'] = c0['last']
        df0 = copy.deepcopy(self.shareDict[code])
        # 部分修正即时K线
        df0.loc[public.getDatetime()] = pd.Series(
            [c0[key] for key in self.klinecolumns], index=self.klinecolumns)

        close = df0["close"]

        df0['max90'] = mx = ta.MAX(close, timeperiod=60)
        df0['drop'] = (mx - close) / mx

        sar = ta.SAR(df0['high'],
                     df0['low'],
                     acceleration=self.sarStart,
                     maximum=0.2)
        df0['sard'] = sard = close - sar
        df0['sarm'] = sard * sard.shift(1)
        df0['sarm'] = df0.apply(
            lambda row: self.turn(row['sarm'], row['sard'], 1), axis=1)

        sar5 = ta.SAR(df0['high'],
                      df0['low'],
                      acceleration=self.sarEnd,
                      maximum=0.2)
        df0['sard5'] = sard5 = close - sar5
        df0['sarm5'] = sard5 * sard5.shift(1)
        df0['sarm5'] = df0.apply(
            lambda row: self.turn(row['sarm5'], row['sard5'], 1), axis=1)

        df0['createTime'] = df0.index
        df0['mode'] = df0.apply(lambda row: self.point(row), axis=1)

        param = copy.deepcopy(df0.iloc[-1]).to_dict()
        param.update(c0)
        #if param['sarm']==1:  print(df0)
        #print(param)
        param.update({
            "code": code,
            "p_l": c0["asks"][0],
            "p_h": c0["bids"][0]
        })
        return param
Exemple #24
0
    def start(self, codes, share):
        print("子进程:", codes)
        self.time0 = time0 = time.time()
        self.Base = future_baseInfo()
        self.Rice = tick_csv_Rice()
        self.Rice.shareDict = share

        self.Rice.basePath = self.basePath
        self.Rice.cvs_start = self.cvs_start
        self.Rice.cvs_end = self.cvs_end
        self.Rice.dd = self.dd

        self.new_docs = []
        self.codes = codes
        self.baseInfo = {}
        self.filePath = self.basePath + "%s.csv" % "_".join(codes)

        # 查询获得配置 - 费率和每手单量
        for doc in self.Base.getInfo(codes):
            self.baseInfo[doc["code"]] = doc

        cs0, cs1 = self.baseInfo[codes[0]], self.baseInfo[codes[1]]
        if cs0["nightEnd"] != cs1["nightEnd"]:  return None

        # 设置结束时间,用于数据清洗
        try:
            res = self.Rice.curKlineByCsv(codes, period=1, callback=self.new_tick)

        except Exception as e:
            print('error', codes, len(self.new_docs), e )
            res = True

        if res and len(self.new_docs) > 0 :
            # 保存文件
            df = pd.DataFrame(self.new_docs)
            # 数据整理
            df = self.detail(df)

            print(public.getDatetime(), " 已保存", codes, "耗时:", time.time()-time0)

            df.to_csv(self.filePath, columns=self.columns1, index=0)
            self.new_docs= []
            return True
Exemple #25
0
    def subFilter(self, codes, k):
        print("stock filter subprocess start:", k / self.pageCount)
        Rice = interface_Rice()
        Base = stock_baseInfo()

        startDate = public.getDatetime(diff=-200)
        res = Rice.kline(codes, period=self.period, start=startDate, pre=90)

        period = 180
        line1 = 0.5
        codeList = []
        for code in codes:
            df = res[code]

            # 计算跌幅和回跌幅度
            close = df['close']
            mx0 = ta.MAX(close, timeperiod=60).values[-1]
            last = close.values[-1]

            opt0 = (mx0 - last) / mx0 > 0.2

            mx = close[-period:].max()
            mi = close[-period:].min()

            miw = ta.MININDEX(close, timeperiod=period).values[-1]
            mid = close[miw:].max()
            # 超过M5
            ma5 = ta.MA(close, timeperiod=5)

            opt1 = (mx - mi) / mx > line1 and (mid - mi) / (
                mx - mi) < 0.30 and (mx0 - last) / mx0 > 0.12
            opt2 = (last > ma5.values[-1] or last > ma5.values[-2])

            if (opt0) or (opt1 and opt2):
                codeList.append(code)

        print(k, len(codes), len(codeList))
        Base.updateBound(codeList)
        Base.closecur()
Exemple #26
0
    def timeDiff(self, preDate, curDate=None, quick=0):
        #
        if curDate is None:
            curDate = public.getDatetime(style='%Y-%m-%d %H:%M:%S')

        preDate = str(preDate)
        if preDate.find('.') > -1:
            preDate = preDate[:preDate.find(".")]
        elif len(preDate) == 8:
            preDate = public.parseTime(preDate,
                                       format='%Y%m%d',
                                       style='%Y-%m-%d %H:%M:%S')

        diff = public.timeDiff(str(curDate), preDate) / 60
        # 快速比较
        if quick > 0 and diff < quick: return 1

        # 间隔小于120,为非跨区
        if diff < 120: return int(diff)
        # 间隔周末
        if diff > (24 * 60 * 2): diff -= 24 * 60 * 2

        # 日内跨区
        m0, m1 = (public.parseTime(d, style='%H:%M:%S')
                  for d in [preDate, curDate])
        for ta in self.TimeDiffArea[:-1]:
            if m0 < ta[0] < m1:
                diff -= ta[1]

        if diff < 120: return int(diff)

        # 夜盘隔日
        date = public.parseTime(curDate, style='%Y-%m-%d')
        ta = self.TimeDiffArea[-1]
        taTime = str(date) + ' ' + ta[0]
        if preDate < taTime < curDate:
            diff -= ta[1]
        return int(diff)
Exemple #27
0
 def getMaps2(self, mapList):
     codes = [c[0] for c in mapList]
     orders = []
     mCodes = self.Rice.getMain(codes)
     snaps = self.Rice.snap(mCodes)
     i = 0
     for m in mCodes:
         s = self.BI.ctpCode(m)
         order = {
             "symbol": s,
             "code": m,
             "name": mapList[i][0],
             "mode": mapList[i][1],
             "isopen": mapList[i][2],
             "hands": mapList[i][3],
             "price":
             snaps[m]['last'] if len(mapList[i]) < 6 else mapList[i][5],
             "method": mapList[i][4],
             "createdate": public.getDatetime(diff=0),
         }
         orders.append(order)
         i += 1
     return orders
Exemple #28
0
    def calc(self, doc1, doc2, remainDays, mode=0):
        rec = {
            'code': doc1["code"],
            'code_1': doc2["code"],
            'price_bid': doc1["bid"],
            'price_ask': doc2["ask"],
            'price_owner': self.price_owner,
            'remainDay': int(remainDays),
            'power': doc1["power"],
            'mode': mode,
            'currentTime': public.getDatetime()
        }

        if mode == 0:
            if doc2["ask"] == 0 or doc1["bid"] == 0:
                res = 1.0
            else:
                res = (float(self.price_owner) + float(doc2["ask"]) -
                       float(doc1["bid"])) / float(doc1["power"]) / math.exp(
                           -1 * self.interest * remainDays)

                res1 = 0
        else:
            if doc2["ask"] == 0 or doc1["bid"] == 0:
                res = 1.0
            else:
                res = (float(self.price_owner) - float(doc2["ask"]) +
                       float(doc1["bid"])) / float(doc1["power"]) / math.exp(
                           -1 * self.interest * remainDays)

                res1 = (float(self.price_owner) - float(doc2["ask"]) +
                        float(doc1["bid"])) / float(doc1["power"]) / math.exp(
                            self.interest_rong * remainDays)

        rec["result"], rec["result1"] = res, res1
        return rec
Exemple #29
0
    def total(self, dfs, period= 21):

        # 计算参数
        df01 = dfs[self.mCodes[0]]
        close = df01["close"]
        df01["datetime"] = df01.index
        df01["time"] = df01["datetime"].apply(lambda x: str(x).split(' ')[1])

        # 日线数据
        self.df_day = df1 = self.Rice.kline(self.mCodes, period='1d', start=self.startDate, end=self.endDate, pre=21)[self.mCodes[0]]
        df1['jump'] = df1['open'] - df1['close'].shift(1)
        df1['atr'] = ta.ATR(df1['high'], df1['low'], df1['close'], timeperiod=14)
        df1['atrc'] = ta.ATR(df1['high'], df1['low'], df1['close'], timeperiod=1) / df1['atr']

        df1['mVol'] = self.iniAmount * self.stopLine / self.BS[self.codes[0]]["contract_multiplier"] / (2 * df1['atr'])

        # 循环 scale
        docs = []
        for conds in self.iterCond():
            uid = self.uidKey % ('_'.join(self.mCodes), str(period),  self.klineType, str(self.bullwidth), conds)

            df1["ma"] = ta.MA(df1["close"], timeperiod=period)
            df1["ma5"] = ta.MA(df1["close"], timeperiod=5)
            df1["ma10"] = ta.MA(df1["close"], timeperiod=10)
            df1["ma20"] = ta.MA(df1["close"], timeperiod=20)

            df0 = copy.deepcopy(df01[(df01['time'] >'09:10:00') & (df01['time'] <'10:15:00')][self.startDate:])

            df2 = df0.apply(lambda row: self.jump(row, df01, df1), axis=1)
            for key in self.pointColumns:  df0[key] = df2[key]

            if self.code in self.csvList:
                file = self.Rice.basePath + '%s_sg.csv' % (uid)
                file1 = self.Rice.basePath + '%s_sg1.csv' % (uid)
                print(uid, '---------------------------- to_cvs', file, df0.columns)
                df0.to_csv(file, index=0, columns= ['datetime', 'time', 'volume', 'close']+self.pointColumns)
                #df1.to_csv(file1, index=1)

            if self.onlyCVS:
                df = df0[df0['powm'] !=0]
                df['code'] = self.code
                df['pid'] = self.ctpCode
                df['uid'] = uid
                df['shift'] = df['atrc']
                df['delta'] = df['midratio']

                df['mode'] = df['powm'].astype(int)
                df['options'] = df['powm1'].astype(int)
                df['createdate'] = df['datetime']
                self.Train.insertAll(df.to_dict(orient='record'))

            if self.onlyCVS: continue

            #tot = None
            tot = self.detect(df0, period=period, uid=uid)
            if tot is not None and tot['amount'] != 0:
                tot.update(
                    {
                        "method": self.method,
                        "code": self.code,
                        "period": period,
                        "uid": uid,
                        "createdate": public.getDatetime()
                    }
                )
                docs.append(tot)
        return docs
Exemple #30
0
    def total(self, dfs, dfs2=None, period=60):
        # 计算参数
        df0 = dfs[self.mCodes[0]]
        df0["rel_price"] = close = df0["close"]
        df0["datetime"] = df0.index

        s0 = self.shift[0]
        p_l = df0["p_l"] = (df0["close"] + s0)
        p_h = df0["p_h"] = (df0["close"] - s0)

        if self.isSimTickUse:
            # 调用复合apply函数计算混合参数
            close2 = dfs2[self.mCodes[0]]["close"]
            df0_1 = df0.apply(lambda row: self.k_ma(row['datetime'], row[
                'rel_price'], close2, period),
                              axis=1)
            df0 = pd.concat([df0, df0_1], axis=1)

        else:
            df0["ma"] = ma = ta.MA(close, timeperiod=period)
            df0["std"] = std = ta.STDDEV(close, timeperiod=period, nbdev=1)
            # 上下柜
            # bullWidth
            df0["bullwidth"] = width = (4 * std / ma * 100).fillna(0)
            # 近三分钟width变动
            df0["widthDelta"] = wd1 = ta.MA(width - width.shift(1),
                                            timeperiod=3).fillna(0)
            df0["slope"] = self.stand(
                ta.MA(ta.LINEARREG_SLOPE(close, timeperiod=5), timeperiod=3))
            df0["rsi"] = rsi = ta.RSI(close, timeperiod=14)

            # kdj顶点
            kdjK, kdjD = ta.STOCH(df0["high"],
                                  df0["low"],
                                  close,
                                  fastk_period=5,
                                  slowk_period=3,
                                  slowk_matype=2,
                                  slowd_period=3,
                                  slowd_matype=2)
            df0["kdj_d2"] = kdj_d2 = kdjK - kdjD
            df0["kdjm"] = kdj_d2 * kdj_d2.shift(1)
            df0["kdjm"] = df0.apply(
                lambda row: self.turn(row['kdjm'], row['kdj_d2'], 1), axis=1)

        # 相对波动
        df0['delta'] = (p_l - p_h) / df0['std']

        df1 = None
        # 循环 scale
        docs = []
        for scale in self.scaleList:
            for conds in self.iterCond():
                uid = self.uidKey % ('_'.join(
                    self.codes), str(period), str(scale), self.klineType[:-1],
                                     str(self.widthTimesPeriod) + '_' + conds)

                if self.stopTimeLine:
                    self.stopTimeDiff = self.stopTimeLine * period * int(
                        self.klineType[:-1])

                # 计算高低线值
                df0["top"], df0["lower"] = df0['ma'] + (
                    scale - self.scaleDiff) * df0['std'], df0['ma'] - (
                        scale + self.scaleDiff) * df0['std']

                df0['pow'] = df0.apply(lambda row: self.point(row), axis=1)

                if uid in self.csvList or self.codes not in self.cvsCodes:
                    file = self.Rice.basePath + '%s_%s_pre.csv' % ('_'.join(
                        self.codes), self.klineType)
                    print(uid, '---------------------------- to_cvs', file,
                          df0.columns)
                    df0.to_csv(file, index=0)
                    self.cvsCodes.append(self.codes)

                # df0.fillna(0, inplace=True)
                tot = None
                #tot = self.detect(df0, df1, period=period, uid=uid)
                if tot is not None and tot['amount'] != 0:
                    tot.update({
                        "scale": scale,
                        "code": self.codes[0],
                        "period": period,
                        "uid": uid,
                        "shift": (p_l - p_h).mean(),
                        "createdate": public.getDatetime()
                    })
                    docs.append(tot)
        return docs