Exemplo n.º 1
0
    def iniNode(self, full_codes):
        openMap = self.Record.getOpenMap(self.methodName,
                                         codes=full_codes,
                                         batchNum=self.batchNum)
        statusMap = self.PStatus.getStatus(self.topTableName)

        # 非CTP方式
        for map in self.used_future_Map:
            #print(map)
            key, uid = map[0], "_".join(map)
            self.procMap.new(uid)  # 初始化进程参数类

            # 初始化品种状态
            if key in openMap:
                found = True
                # CTP方式检查
                if self.isCTPUse:
                    # 检查持仓满足
                    found = self.CTP.checkPosition(openMap[key],
                                                   0,
                                                   reverse=-1,
                                                   refresh=False) == 0
                    #if key =='IH': print(openMap[key], found, res)

                if found:
                    status = statusMap[key] if key in statusMap else 0
                    self.procMap.setIni(uid, openMap[key], status=status)
                    logger.info(
                        (uid, key, self.procMap.isOpen, self.procMap.status))
Exemplo n.º 2
0
    def start(self, codes, time0, kt, shareDict):
        print("子进程启动:", self.cindex, codes, time.time() - time0)

        self.klineType = kt
        # 主力合约
        self.codes = codes
        self.mCodes = mCodes = [n + '88' for n in codes]

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

        cs0 = self.baseInfo[mCodes[0]]

        # 计算tick 导致的滑点
        sh = [self.baseInfo[d + '88']['tick_size'] for d in codes]
        self.shift = [sh[i] * self.shiftScale for i in range(len(sh))]

        # 子进程共享类
        self.Rice = tick_csv_Rice()
        self.Rice.setTimeArea(cs0["nightEnd"])
        self.Train = train_future()
        self.Total = train_total()
        self.Total.tablename = self.total_tablename

        # 查询获得N分钟K线
        dfs_l = self.Rice.kline(mCodes,
                                period=self.klineType,
                                start=self.startDate,
                                end=self.endDate,
                                pre=60)
        # 获得1分钟K线作为滚动线
        dfs = self.Rice.kline(mCodes,
                              period='1m',
                              start=self.startDate,
                              end=self.endDate,
                              pre=0)

        # 按时间截取并调整
        # dfs= self.dateAdjust(codes, dfs, sh)
        print('kline load:', mCodes, len(dfs[mCodes[0]]))

        # 根据配置文件获取最佳交易手数对
        self.iniVolume = round(
            self.iniAmount / cs0["lastPrice"] / cs0["contract_multiplier"], 0)

        # 分参数执行
        results = []
        for period in self.periodList:
            docs = self.total(dfs, dfs_l, period=period)

            if docs is None or len(docs) == 0: continue
            logger.info(
                (self.codes, period, self.klineType, len(docs), " time:",
                 time.time() - time0))
            results.extend(docs)

            # 结果写入共享阈
        self.Total.insertAll(results)
Exemplo n.º 3
0
    def start(self, codes, time0, kt, shareDict):
        print("子进程启动:", self.cindex, codes, time.time() - time0)
        self.klineType = kt
        # 主力合约
        self.codes = codes
        self.mCodes = mCodes = [n + '88' for n in codes]

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

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

        # 计算tick 导致的滑点
        sh = [self.baseInfo[d + '88']['tick_size'] for d in codes]

        # 子进程共享类
        self.Rice = tick_csv_Rice()
        self.Train = train_future()
        self.Total = train_total()
        self.Total.tablename = self.total_tablename

        # 查询获得N分钟K线
        dfs_l = self.Rice.kline(mCodes,
                                period=self.klineType,
                                start=self.startDate,
                                end=self.endDate)
        # 获得1分钟K线作为滚动线
        dfs = self.Rice.kline(mCodes,
                              period='1m',
                              start=self.startDate,
                              end=self.endDate)

        # 按时间截取并调整
        dfs = self.dateAdjust(codes, dfs, sh)
        print('kline load:', mCodes, len(dfs[mCodes[0]]), len(dfs[mCodes[1]]))

        # 根据配置文件获取最佳交易手数对
        self.iniVolume = round(
            self.iniAmount / cs0["lastPrice"] / cs0["contract_multiplier"], 0)

        # 分参数执行
        results = []
        for period in self.periodList:
            for scale in self.scaleList:
                for ss in self.shiftScaleList:

                    self.shiftScale = ss
                    self.shift = [sh[i] * ss for i in range(len(sh))]

                    doc = self.total(dfs, dfs_l, scale=scale, period=period)
                    if doc is None or doc['amount'] == 0: continue
                    logger.info((doc['uid'], doc['income'], " time:",
                                 time.time() - time0))
                    results.append(doc)
                    # 结果写入共享阈
                #shareDict.append(doc)
        self.Total.insertAll(results)
Exemplo n.º 4
0
    def pool(self, lists):
        time0 = time.time()
        pool = Pool(processes=5)

        shareDict = Manager().dict({})
        share = Manager2()
        Record = share.stock_orderForm()
        Tick = share.mon_tick()

        for k in range(0, len(lists), self.pageCount):
            codes = lists[k:k+self.pageCount]
            In = False
            for c in codes:
                if c in self.csvList:
                    In = True
                    break
            if not In: continue

            #self.start(codes, Record, Tick, int(k/self.pageCount+1), shareDict)
            try:
                pool.apply_async(self.start, (codes, Record, Tick, int(k/self.pageCount+1)), shareDict)
                pass
            except Exception as e:
                print(e)
                continue

        pool.close()
        pool.join()

        logger.info(("model_stock_pop scan finished: ", len(lists), time0, time.time()-time0))
Exemplo n.º 5
0
    def kmeans_test(self):
        """"
         执行聚类操作,并将结果保存回文档
       """
        time0 = time.time()
        obj = cluster_data()
        #return
        items = obj.get()
        logger.info("获取数据, 耗时: %s " % str(time.time()-time0))

        num = len(items)
        width = max(items[0].keys())+1

        ss = sparse.lil_matrix((num, width))
        i=0
        for item in items:
           for key in item.keys():
              if key!=0: ss[i,key] = item[key]
           i+=1

        random_state = 170
        c = KMeans(n_clusters= 4 , random_state=random_state)
        c.fit_predict(ss)
        clur = c.labels_.tolist()

        j=0
        for item in items:
            sql = obj.sql_update % (clur[j] , item[0])
            obj.update(sql)
            j += 1

        # 更新ad数据
        time.sleep(2)
        #obj.refresh()
        logger.info("聚类和更新数据, 耗时: %s" % str(time.time()-time0))
Exemplo n.º 6
0
    def start(self, codes, time0, kline=None, Base=None, Rice=None):
        print("子进程启动:", self.cindex, codes, kline)

        # 主力合约
        self.codes = codes
        self.code, self.mCode = codes[0], codes[0] + '88'

        self.mCodes = mCodes = [n + '88' for n in codes]
        # 查询获得配置 - 费率和每手单量

        self.Base = future_baseInfo()

        self.BS = {}

        for doc in self.Base.getInfo(codes):
            self.BS[doc["code"]] = self.BS[doc["code"] + '88'] = doc

        cs = [self.BS[m] for m in self.mCodes]
        self.klineType = self.BS[
            self.code]['quickkline'] if kline is None else kline

        # 子进程共享类
        self.Rice = Rice if Rice is not None else interface_Rice()
        self.Rice.setTimeArea(cs[0]["nightEnd"])

        self.Train = train_future()
        self.Total = train_total()
        self.Total.tablename = self.total_tablename
        self.Train.tablename = self.detail_tablename

        if len(self.indexCodeList) > 0:
            self.Rice.setIndexList(self.indexCodeList)

        self.adjustDates = self.Rice.getAdjustDate(self.code,
                                                   start=self.startDate)

        print(codes, self.adjustDates)

        # 查询获得N分钟K线
        dfs = self.Rice.kline(mCodes,
                              period=self.klineType,
                              start=self.startDate,
                              end=self.endDate,
                              pre=1)

        print('kline load:', mCodes, [len(dfs[m]) for m in mCodes])

        # 根据配置文件获取最佳交易手数对
        self.iniVolume = round(
            self.iniAmount / cs[0]["lastPrice"] / cs[0]["contract_multiplier"],
            0)
        if self.iniVolume == 0: self.iniVolume = 1

        # 分参数执行
        docs = self.total(dfs, period=self.periodwidth)
        if docs is None or len(docs) == 0: return

        logger.info((self.codes, self.klineType, len(docs), " time:",
                     time.time() - time0))
        self.Total.insertAll(docs)
Exemplo n.º 7
0
    def control(self):
        Rice = interface_Rice()
        Rice.TimeArea = self.TimeArea
        Base = stock_baseInfo()
        #lists = Base.getCodes(isBound=1)
        #print(lists[0], len(lists))
        lists = self.csvList
        logger.info(('model_stock_pop scan list:', len(lists)))

        tt0 = int(time.time()) // 3
        while True:
            # 满足时间
            valid = Rice.isValidTime()
            tt, rr = int(time.time()) // 3 , int(time.time()) % 3

            if valid[0] and tt0 != tt and rr==0 :
               self.pool(lists)
               #print(time.time())
               tt0 = tt

            # 非交易日和收盘则结束
            elif not valid[1]:
                break

            time.sleep(self.timeInterval)
Exemplo n.º 8
0
    def start(self, full_codes, shareDict, CTP):
        self.shareDict = shareDict
        self.Record = future_orderForm()
        if not self.isWorking:
            self.Record.tablename = self.orderFormTest

        self.Rice = interface_Rice()
        # 基础配置信息类
        self.baseInfo = BaseInfo(full_codes, self.Rice)
        # ctp 接口类
        self.CTP = CTP if CTP is not None else interface_pyctp(use=self.isCTPUse,userkey=self.ctpuser)
        self.CTP.baseInfo = self.baseInfo
        # 进程控制类
        self.procMap = ProcessMap()

        # 设置交易对的收盘时间
        self.Rice.setTimeArea(self.baseInfo.nightEnd)
        # 按k线类型分拆组,进行K线数据调用
        self.groupByKline(full_codes)

        # 初始化节点
        try:
            self.iniNode(full_codes)
        except Exception as e:
            print('iniNode error', e)

        #return
        # 子进程启动
        full_mCodes = self.baseInfo.mainCodes #主力合约
        logger.info(("model_future_detect start: %s " % ",".join(full_mCodes), self.Rice.TimeArea))
        self.Rice.startTick(full_mCodes, callback=self.onTick)
Exemplo n.º 9
0
 def setPStatus(self, status):
     """
         更改进程状态,并写入数据库,供重新启动程序时调用
     """
     self.procMap.status = status
     method = self.topTableName
     self.PStatus.setStatus('_'.join(self.procMap.codes), method, status)
     logger.info(("setPStatus", self.procMap.codes, method, status))
Exemplo n.º 10
0
    def create(self, type=0):
        # 检测满足购买条件文件
        docs = self.spy(type)

        #计算下单文件
        orders = self.order(docs)
        # 生成VWAP,生成批量模型, 并通过Matic下单

        self.Vwap(orders)
        logger.info("批量下单完成!")
Exemplo n.º 11
0
    def baseInfo(self):
        # 清空历史数据
        #self.Base_tmp.empty()

        docs = []
        usedMon = []

        res = json.loads(self.getI(self.url_month))
        data = res["result"]["data"]
        print(data)

        for mon in data["contractMonth"]:
            # 过滤重复的月份
            if not mon in usedMon:
                usedMon.append(mon)
            else:
                continue
            doc = {
                "cateId": data["cateId"],
                "cateList": ",".join(data["cateList"]),
                "stockId": data["stockId"],
                "contractMonth": mon
            }

            # 查询到期日
            eTmp = json.loads(self.getI(self.url_day % mon))
            eDay = eTmp["result"]["data"]["expireDay"]

            # 获取上行和下行列表
            param = mon[2:].replace("-", "")

            res_up = self.extract(
                self.getI(self.url_up % (data["stockId"], param)))
            res_down = self.extract(
                self.getI(self.url_down % (data["stockId"], param)))
            k = 0

            for k in range(len(res_up)):
                if res_up[k].strip() == "": continue
                doc_new = {}
                doc_new.update(doc)
                doc_new.update({
                    "code": res_up[k][-8:],
                    "code_down": res_down[k][-8:],
                    "expireDay": eDay
                })
                docs.append(doc_new)
                k += 1

        # 添加到临时数据库
        #self.Base_tmp.insertAll(docs)
        # 更新正式库
        #self.Base_tmp.updateBase()
        logger.info(" update sh50 baseInfo")
Exemplo n.º 12
0
    def start(self):
        self.uid = "510050_XSHG_%s" % self.period
        self.tickParam = None
        self.iniNode()

        logger.info(('etf50 - ma stage start', self.uid))
        self.Rice.startTick(self.codes,
                            callback=self.onTick,
                            source='kline',
                            kPeriod=self.period,
                            kmap=self.ktypeMap)
Exemplo n.º 13
0
 def iniNode(self):
     doc = self.Record.getPosition(method=self.method)
     if doc is not None:
         self.currentVol, self.currentPositionType, self.preNode = doc[
             'vol'], doc['pos'], doc
         if self.method == 'bull' and abs(doc['mode']) in [3, 4]:
             self.status = np.sign(doc['pos'])
     else:
         self.preNode = None
         self.status = 0
     logger.info(('current status: vol - pos - status:', self.currentVol,
                  self.currentPositionType, self.status))
Exemplo n.º 14
0
    def start(self, full_codes, Rice=None, CTP=None):
        # print(full_codes)
        self.Record = future_orderForm()
        self.PStatus = future_status()

        if not self.isWorking: self.Record.tablename = self.orderFormTest

        if self.isTickSave:
            self.Tick = mon_tick()

        self.Rice = interface_Rice() if Rice is None else Rice

        # 基础配置信息类
        self.baseInfo = BaseInfo(full_codes, self.Rice)

        # ctp 接口类
        self.CTP = interface_pyctp(
            use=self.isCTPUse, userkey=self.ctpuser) if CTP is None else CTP
        self.CTP.baseInfo = self.baseInfo

        # 进程控制类
        self.procMap = ProcessMap()

        # 趋势预测
        self.trendMap = self.Record.trendMap(self.relativeMethods)

        # 指数期货
        self.indexList = [c[0] for c in self.indexCodeList]

        # 设置交易对的收盘时间
        self.Rice.setTimeArea(self.baseInfo.nightEnd)

        if len(self.indexCodeList) > 0:
            self.Rice.setIndexList(self.indexCodeList)

        # 按k线类型分拆组,进行K线数据调用
        self.groupByKline(full_codes)

        # 初始化节点
        self.iniNode(full_codes)

        # 子进程启动
        full_mCodes = self.baseInfo.mainCodes  # 主力合约

        logger.info(("model_future_detect start: %s " % ",".join(full_codes),
                     self.Rice.TimeArea))

        self.Rice.startTick(full_mCodes,
                            kmap=self.kTypeMap,
                            timesleep=self.timeInterval,
                            source=self.sourceType,
                            callback=self.onTick)
Exemplo n.º 15
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()
Exemplo n.º 16
0
    def baseUpdate(self):
        df = rq.all_instruments(type='Future', date=public.getDate())
        u = df["underlying_symbol"].unique()

        FT = future_baseInfo()
        # 已有code及结束时间
        exists = FT.exists()
        exCodes = [c[0] for c in exists]
        endMap = {}
        for c in exists:
            endMap[c[0]] = c[1]

        docs = []
        mu = self.getMain(u)
        i = 0

        for d in u:
            n = df[df["underlying_symbol"] ==
                   d].loc[:, FT.keylist[2:]].values[-1].tolist()
            doc = FT.set([d, 0] + n)

            detais = rq.instruments(mu[i])
            # tick_size
            doc["tick_size"] = detais.tick_size()
            doc['contract_multiplier'] = detais.contract_multiplier
            doc['margin_rate'] = detais.margin_rate

            # 结束时间
            hh = detais.trading_hours.split(',')
            hs = hh[0].split('-')
            if hs[0][0:2] != '09':
                doc["nightEnd"] = hs[1]
            else:
                doc["nightEnd"] = hh[-1].split('-')[1]

            if d not in exCodes:
                doc['product'] = 'Commodity'
                docs.append(doc)
            else:
                # 更新结束时间
                if doc["nightEnd"] != endMap[d][0:len(doc["nightEnd"])]:
                    print(d, endMap[d][0:len(doc["nightEnd"])],
                          doc["nightEnd"])

                FT.setMulti(doc)
            i += 1

        if len(docs) > 0:
            FT.insertAll(docs)
            logger.info(('future base update finished, ', docs))
Exemplo n.º 17
0
    def iniNode(self, full_codes):
        openMap = self.Record.getOpenMap(self.methodName,
                                         codes=full_codes,
                                         batchNum=1)

        # 非CTP方式
        for map in self.used_future_Map:
            key, uid = map[0], "_".join(map)
            self.procMap.new(uid)  # 初始化进程参数类

            # 初始化品种状态
            if key in openMap:
                self.procMap.setIni(uid, openMap[key])
                logger.info(
                    (uid, key, self.procMap.isOpen, self.procMap.status))
Exemplo n.º 18
0
    def start(self, full_codes, shareDict):
        # 每个进程单独初始化对象
        self.Record = future_orderForm()
        self.Rice = interface_Rice()
        self.Base = future_baseInfo()
        #self.CTP = interface_pyctp()

        self.shareDict = shareDict
        self.full_codes = full_codes

        # 子进程中共享变量
        self.isOpen, self.batchId, self.preNode, self.isStop = {}, {}, {}, {}

        # 配置文件
        for doc in self.Base.getInfo(full_codes):
            self.config[doc["code"]] = doc

        # 设置交易对的收盘时间
        self.Rice.setTimeArea(doc["nightEnd"])

        # 查询主力代码,并存入字典
        full_mCodes = self.Rice.getMain(full_codes)
        self.mCodeMap = {}
        for i in range(len(full_codes)):
            self.mCodeMap[full_codes[i]] = full_mCodes[i]

        # 初始化订单状态和batchID
        self.kTypeMap = {}
        openMap = self.Record.getOpenMap(self.methodName, full_codes)

        for map in self.future_Map:
            if map[0] not in full_codes or map[1] not in full_codes: continue
            self.iniNode(map, openMap)

            # 按k线时间框类型初始化 kTypeMap dict,
            ktype = int(map[4])

            if not ktype in self.kTypeMap.keys():
                self.kTypeMap[ktype] = []

            for i in [0, 1]:
                if not self.mCodeMap[map[i]] in self.kTypeMap[ktype]:
                    self.kTypeMap[ktype].append(self.mCodeMap[map[i]])

        # 子进程启动
        logger.info(("model_future_detect start: %s " % ",".join(full_mCodes),
                     self.Rice.TimeArea))
        self.Rice.startTick(full_mCodes, callback=self.onTick)
Exemplo n.º 19
0
    def checkResult(self):
        c0 = ['direction', 'hands']
        c1 = ['Direction', 'VolumeTotalOriginal']
        t, w = 0, 0

        times = 5
        # 等待处理中的记录
        if self.ordersWaits is not None:
            while len(self.ordersWaits) > 0:
                if w > times: break
                logger.info(
                    ('-------- order waiting.....', self.ordersWaits, w))
                w += 1
                time.sleep(3)

        for d0 in self.ordersSend:
            if not (d0['session'] == self.session
                    and d0['front'] == self.front):
                continue
            for d1 in self.ordersResponse:
                #print('------------return--------------', d1)

                if d0['symbol'] == d1['InstrumentID'].decode('gbk') and [
                        d0[k] for k in c0
                ] == [d1[k] for k in c1]:
                    d0.update({
                        "status":
                        d1['status'],
                        "price":
                        d1['LimitPrice'],
                        "hands":
                        d1['volume'],
                        "orderID":
                        d1["OrderRef"].decode('gbk').strip(),
                        "vol":
                        d1['volume'] *
                        self.baseInfo.att(d0['symbol'], 'contract_multiplier')
                    })
                    t += 1
                    break

        logger.info(
            ('--------orderresult compare input-out', len(self.ordersSend),
             len(self.ordersResponse), 'match:', t))

        # 输出: 未匹配订单数,订单返回信息
        return (len(self.ordersSend) -
                t) if len(self.ordersWaits) == 0 else 4, self.ordersSend
Exemplo n.º 20
0
    def start(self, codes, klinePeriod, time0, shareDict):
        print("子进程启动:", codes, time.time()-time0)

        self.Rice = interface_Rice()
        self.Train = train_future()
        self.Train.tablename = self.detail_tablename
        self.codes = codes
        self.Base = future_baseInfo()
        self.Total = train_total()
        self.Total.tablename = self.total_tablename

        # 主力合约
        self.mCodes = mCodes = [n +'88' for n in codes]
        self.klinePeriod = klinePeriod

        # 查询获得分钟K线
        dfs = self.Rice.kline(mCodes, period= klinePeriod, start=self.startDate, end=self.endDate)
        print("kline load", self.mCodes)
        # 查询获得配置 - 费率和每手单量

        i= 0
        for doc in self.Base.getInfo(codes):
            self.baseInfo[doc["code"]+'88'] = doc
            self.shift = doc["tick_size"] * self.shiftRatio
            i+=1

        # 分参数执行
        res = []
        for period in self.periodList:
            for scale in self.scaleList:
                for deltaline in self.widthDeltaLineList:
                    self.widthDeltaLine = deltaline
                    doc = self.total(dfs[mCodes[0]], scale=scale, period=period)
                    if doc is None: continue
                    doc.update({
                        "code": codes[0],
                    #    "code1": codes[1],
                    })
                    logger.info((doc['uid'], doc['count'], doc['income'], " time:", time.time() - time0))
                    res.append(doc)
                # 结果写入共享阈
                #shareDict.append(doc)

        self.Total.insertAll(res)
Exemplo n.º 21
0
    def start(self, full_codes):
        # print(full_codes)
        self.Record = future_orderForm()
        if not self.isCTPUse:
            self.Record.tablename = 'future_orderForm_1'

        self.time0 = time.time()

        if self.isTickSave:
            self.Tick = mon_tick()

        self.Rice = interface_Rice()
        # 基础配置信息类
        self.baseInfo = BaseInfo(full_codes, self.Rice)

        # ctp 接口类
        # self.CTP =  interface_pyctp(use=self.isCTPUse, baseInfo=self.baseInfo, userkey=self.ctpuser)

        # 进程控制类
        self.procMap = ProcessMap()

        # 设置交易对的收盘时间
        self.Rice.setTimeArea(self.baseInfo.nightEnd)

        self.indexList = []
        if len(self.indexCodeList) > 0:
            self.Rice.setIndexList(self.indexCodeList)
            self.indexList = [c[0] for c in self.indexCodeList]

        # 按k线类型分拆组,进行K线数据调用
        self.groupByKline(full_codes)
        # 初始化节点
        self.iniNode(full_codes)
        # return
        # 子进程启动
        full_mCodes = self.baseInfo.mainCodes  # 主力合约
        logger.info(
            ("%s start: %s" % (self.__class__.__name__, ",".join(full_mCodes)),
             self.Rice.TimeArea))

        self.Rice.startTick(full_mCodes,
                            kmap=self.kTypeMap,
                            source='combin',
                            callback=self.onTick)
Exemplo n.º 22
0
    def start(self):
        self.uid = "510050-XSHG_15_15_0.5_2.0"
        map = self.uid.split('_')

        self.timePeriods = int(map[1])
        self.klineType = int(map[2])
        self.period = str(self.klineType) + 'm'
        self.powline = float(map[3])
        self.ktypeMap = {self.codes[0]: int(map[2])}
        self.turnline = float(map[4])
        self.tickParam, self.status = None, 0
        self.iniNode()

        logger.info(('etf50 - bull_trend stage start', self.uid))
        self.Rice.startTick(self.codes,
                            callback=self.onTick,
                            source='kline',
                            kPeriod=self.period,
                            kmap=self.ktypeMap)
Exemplo n.º 23
0
    def getKline(self, codes, ktype=1, key='_0', num=300):
        # 每分钟更新K线
        mins, unit = ktype, 'm'
        if str(key).find('d') > -1:
            unit = 'd'
            mins = ktype * 24 * 60

        now = int(time.time() / 60 / mins)
        #
        time0 = time.time()
        if (key not in self.HisKline.keys()) or (self.HisKlineTime[key] !=
                                                 now):
            self.HisKline[key] = self.kline(codes,
                                            period=str(ktype) + unit,
                                            pre=num)
            self.HisKlineTime[key] = now
            logger.info(("%s alter new %s kline" % (key, str(ktype) + unit),
                         now, codes, ' 耗时:', time.time() - time0))
        return self.HisKline[key]
Exemplo n.º 24
0
    def debugT(self, str, n=1000, param=None):
        self.itemCount += 1
        if str != '' and self.itemCount % n == 0:
            logger.info((self.itemCount, str))

        #print(self.isTickSave)

        if not self.isTickSave: return

        code, method = self.procMap.codes[0], self.procMap.currentUid.split(
            "_")[-1]
        #print(self.csvCodes)
        if param is not None and (code in self.cvsCodes
                                  or self.cvsCodes == []):
            # param = param.to_dict()
            param['code'] = code
            param['method'] = method
            param['isopen'] = self.procMap.isOpen
            param['status'] = self.procMap.status

            for key in self.tickIntList:
                if key in param: param[key] = int(param[key])

            # 初始化
            if code not in self.cvsMap:
                self.cvsMap[code] = [param]
                self.cvsMap['c_' + code] = 1

            else:
                self.cvsMap['c_' + code] += 1
                c, t = self.cvsMap[code], self.cvsMap['c_' + code]

                if len(c) > 2:
                    if self.isTickSave:
                        self.Tick.col.insert_many(self.cvsMap[code])

                    self.cvsMap[code] = []

                elif t % self.tickInterval == 0:
                    #print(param)
                    self.cvsMap[code].append(param)
Exemplo n.º 25
0
    def orderCheck(self, cur, param):
        isRun = False
        # 开仓

        if self.isOpen[self.uid] == 0:

            #if param["delta"] > self.deltaLimit: return None

            # 已关闭的交易对只平仓, 不再开仓
            if self.codes in self.noneUsed: return None

            # 大于上线轨迹
            if param["p_h"] > param["top"]:
                self.isOpen[self.uid] = -1
                isRun = True

            # 低于下轨线
            elif param["p_l"] < param["lower"]:
                self.isOpen[self.uid] = 1
                isRun = True

        # 平仓
        else:
            # 回归ma则平仓  或  超过24分钟 或到收盘时间 强制平仓
            io = self.isOpen[self.uid]
            if io * ((param["p_h"] if io == 1 else param["p_l"]) -
                     param["ma"]) >= 0:

                self.isOpen[self.uid] = 0
                isRun = True

        # 分钟线
        self.tmp += 1
        if self.tmp % 1000 == 1:
            #print(self.codes, cur, param)
            pass

        if isRun:
            self.order(cur, self.isOpen[self.uid], param)
            logger.info(
                [" purchase record: ", self.codes, self.isOpen[self.uid]])
Exemplo n.º 26
0
    def Vwap(self, docs):
        v_inter, v_rate = int(self.iniKey('vwap_interval')), float(
            self.iniKey('vwap_volume_rate'))
        No = 0
        # 根据深度数据,自动批量下单
        while True:
            # 总共需要卖出的手数 用于卖出
            total_v = sum([doc["volume"] for doc in docs if doc["type"] == -1])

            # 总共需要买入金额 用于买入
            total_a = sum([doc["amount"] for doc in docs if doc["type"] == 1])

            if total_v <= 0 and total_a <= 0:
                break
            cur_docs, docs = self.vwap_volume(docs, v_rate)
            # 下单
            self.Matic.order(cur_docs)
            No += 1
            logger.info("批量下单批次号: %s" % No)
            # 下单间隔时间
            time.sleep(5)
Exemplo n.º 27
0
    def start(self, codes, klinePeriod, time0, shareDict):
        print("子进程启动:", codes, time.time()-time0)

        self.Rice = interface_Rice()
        self.Train = train_future()
        self.Base = future_baseInfo()
        self.Total = train_total()
        self.Total.tablename = self.total_tablename
        self.Train.tablename = self.detail_tablename

        # 主力合约
        self.mCodes = codes
        self.klinePeriod = klinePeriod

        # 查询获得分钟K线
        dfs = self.Rice.kline(codes, period= klinePeriod, start=self.startDate, end=self.endDate)

        print("kline load", self.mCodes)
        # 查询获得配置 - 费率和每手单量
        self.shift = 0.001 * self.shiftRatio

        # 分参数执行
        res = []
        for period in self.periodList:
            for scale in self.scaleList:
                for deltaline in self.widthDeltaLineList:
                    self.widthDeltaLine = deltaline
                    doc = self.total(dfs[codes[0]], scale=scale, period=period)

                    if doc is None: continue
                    doc.update({
                        "code": codes[0],
                    #    "code1": codes[1],
                    })
                    logger.info((doc,  " time:", time.time()-time0))
                    res.append(doc)
                # 结果写入共享阈
                #shareDict.append(doc)

        self.Total.insertAll(res)
Exemplo n.º 28
0
    def filterCodes(self):
        # 查询每周统计表
        Top = train_total()
        Top.tablename = self.topTableName
        self.future_Map = Top.last_top(num=self.topNumbers,
                                       maxsames=self.sameLimit,
                                       minRate=self.minRate)
        num0 = len(self.future_Map)

        Result = future_orderForm()

        # 添加top10
        codesList = [[c[0], c[1]] for c in self.future_Map]
        for map in Result.topCodes():
            if not map[0:2] in codesList:
                self.future_Map.append(map)
        num1 = len(self.future_Map) - num0

        # 添加未平仓,并不再开仓
        codesList = [[c[0], c[1]] for c in self.future_Map]
        for map in Result.currentOpenCodes(method=self.methodName):
            print(map)
            if not map[0:2] in codesList:
                self.future_Map.append(map)
                self.noneUsed.append(map[0:2])

        logger.info(
            ('总监控币对:', len(self.future_Map), ' 新列表:', num0, ' top10:', num1))
        logger.info(([n for n in self.future_Map]))
        logger.info(('暂停币对:', len(self.noneUsed), [n for n in self.noneUsed]))
Exemplo n.º 29
0
    def start(self, codes, time0, kline=None, Base=None, Rice=None):
        print("子进程启动:", self.cindex, codes, kline)
        self.subInit(codes, time0, kline=kline, Base=Base, Rice=Rice)

        self.klineType = kline
        self.mCodes = [c + '88' for c in codes]

        print(self.mCodes)
        # 查询获得N分钟K线
        dfs = self.Rice.kline(self.mCodes,
                              period=self.klineType,
                              start=self.startDate,
                              end=self.endDate,
                              pre=1)

        # 分参数执行
        docs = self.total(dfs, period=self.period)
        if docs is None or len(docs) == 0: return

        logger.info((self.codes, self.klineType, len(docs), " time:",
                     time.time() - time0))
        self.Total.insertAll(docs)
Exemplo n.º 30
0
    def start(self, full_codes, pid, shareDict={}):
        time0 = time.time()
        self.used_stocks = full_codes
        self.Record = stock_orderForm()

        if not self.isWorking:
            self.Record.tablename = self.orderFormTest

        if self.isTickSave:
            self.Tick = mon_tick()

        self.Rice = interface_Rice(isSub=True)
        # 进程控制类
        self.procMap = ProcessMap()

        self.shareDict = shareDict
        self.shareDict = self.iniShare(full_codes)

        # 初始化节点
        self.iniNode(full_codes)
        orders = self.onTick(self.Rice.snap(full_codes))

        if len(orders) > 0:
            # 文件提交
            if self.isWorking:
                G = GuangFa_interface()
                G.stage = self.methodName
                res = G.order(orders)
                logger.info(([(d['code'], d['vol'], d['isBuy'], d['status'])
                              for d in res]))

                self.Record.insertAll(res)

        # 子进程启动
        #logger.info(("model_stock_pop: subprocess finished", pid, " orders:", len(orders), " buy:", len([d['code'] for d in orders if d['isBuy']>0]), time.time()-time0))

        self.Record.closecur()
        self.Record = None