Exemplo n.º 1
0
    def loadData(self, vtSymbol):
        """加载数据"""

        df = pd.read_csv(vtSymbol + '.csv')
        for i, d in df.iterrows():
            #print(d)
            #set_trace()

            bar = VtBarData()
            bar.vtSymbol = vtSymbol
            bar.symbol = vtSymbol
            bar.open = float(d['open'])
            bar.high = float(d['high'])
            bar.low = float(d['low'])
            bar.close = float(d['close'])

            date = str(d['date'])
            if '-' in date:
                date = date.split('-')
                date = ''.join(date)
            bar.date = date
            bar.time = d['time']
            #bar.time = '00:00:00'
            bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                             '%Y%m%d %H:%M:%S')
            bar.volume = d['volume']

            barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
            barDict[bar.vtSymbol] = bar

        self.output(u'全部数据加载完成')
    def loadData(self, vtSymbol):
        """加载数据"""

        df = pd.read_csv(self.dss + 'fut/bar/min5_' + vtSymbol + '.csv')
        for i, d in df.iterrows():
            #print(d)

            bar = VtBarData()
            bar.vtSymbol = vtSymbol
            bar.symbol = vtSymbol
            bar.open = float(d['open'])
            bar.high = float(d['high'])
            bar.low = float(d['low'])
            bar.close = float(d['close'])
            bar.volume = d['volume']

            date = str(d['date'])
            bar.date = date
            bar.time = str(d['time'])
            if '-' in date:
                bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                 '%Y-%m-%d %H:%M:%S')
            else:
                bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                 '%Y%m%d %H:%M:%S')

            #bar.time = '00:00:00'
            #bar.datetime = bar.date + ' ' + bar.time
            bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S')

            self.barDict[bar.datetime] = bar
            # print(bar.__dict__)
            # break

        self.output(u'全部数据加载完成')
Exemplo n.º 3
0
    def control_in_p(self, bar):
        try:
            for k in self.spread_dict.keys():
                s0 = self.spread_dict[k][0]
                s1 = self.spread_dict[k][1]
                if s0 not in self.got_dict or s1 not in self.got_dict:
                    continue
                if self.got_dict[s0] and self.got_dict[
                        s1] and self.process_dict[k] == False:
                    self.process_dict[k] = True
                    s0 = self.signalDict[s0][0]
                    s1 = self.signalDict[s1][0]

                    bar_s = VtBarData()
                    bar_s.vtSymbol = k
                    bar_s.symbol = k
                    bar_s.exchange = s0.bar.exchange
                    bar_s.date = s0.bar.date
                    bar_s.time = s0.bar.time

                    bar_s.close = s0.bar.close - s1.bar.close
                    bar_s.AskPrice = s0.bar.AskPrice - s1.bar.BidPrice
                    bar_s.BidPrice = s0.bar.BidPrice - s1.bar.AskPrice

                    # print(bar_s.time, bar_s.vtSymbol, bar_s.close, bar_s.AskPrice, bar_s.BidPrice)

                    self.engine.lock.acquire()
                    self.engine.bar_list.append(bar_s)
                    self.engine.lock.release()

        except Exception as e:
            s = traceback.format_exc()
            to_log(s)
Exemplo n.º 4
0
    def loadData(self, dbName):
        """加载数据"""

        for vtSymbol in self.vtSymbolList:
            df = get_stk_hfq(dss, vtSymbol)
            df = df.sort_values(['date'])
            for i, d in df.iterrows():
                #print(d)
                #set_trace()

                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open'])
                bar.high = float(d['high'])
                bar.low = float(d['low'])
                bar.close = float(d['close'])
                date = d['date'].split('-')
                date = ''.join(date)
                bar.date = date
                bar.time = '00:00:00'
                bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                 '%Y%m%d %H:%M:%S')
                bar.volume = d['volume']

                barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
                barDict[bar.vtSymbol] = bar

        self.output(u'全部数据加载完成')
Exemplo n.º 5
0
    def _bc_loadInitBar(self, vtSymbol, initBars):
        """读取Bar数据,"""
        r = []
        #df = get_stk_hfq(self.dss, vtSymbol, begin_date=None, end_date='2019-07-18')
        df = get_stk_hfq(self.dss, vtSymbol, begin_date=None, end_date=None)
        if df is not None:
            df = df.iloc[:initBars]
            df = df.sort_values('date')
            for i, row in df.iterrows():
                d = dict(row)
                #print(d)
                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open'])
                bar.high = float(d['high'])
                bar.low = float(d['low'])
                bar.close = float(d['close'])
                date = d['date'].split('-')  #去掉字符串中间的'-'
                date = ''.join(date)
                bar.date = date
                bar.time = '00:00:00'
                bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                 '%Y%m%d %H:%M:%S')
                bar.volume = float(d['volume'])

                r.append(bar)

        return r
Exemplo n.º 6
0
def one2five(symbol):
    filename = 'min1_' + symbol + '.csv'
    df = pd.read_csv(filename)

    # 生成min5
    r5 = []
    temp_bar = []
    for i, row in df.iterrows():
        dt = row.datetime
        new_bar = VtBarData()
        new_bar.date = dt[:10]
        new_bar.time = dt[11:19]
        new_bar.open = row.open
        new_bar.high = row.high
        new_bar.low = row.low
        new_bar.close = row.close
        Generate_Bar_Min5(new_bar, temp_bar, r5)

    df_symbol = pd.DataFrame(
        r5, columns=['date', 'time', 'open', 'high', 'low', 'close', 'volume'])
    fname = 'min5_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')
Exemplo n.º 7
0
    def onBar(self, bar, minx='min1'):
        """引擎新推送过来bar,传递给每个signal"""

        # 不处理不相关的品种
        if bar.vtSymbol not in self.vtSymbolList:
            return

        if minx != 'min15':               # 本策略为min15
            return

        if self.result.date != bar.date + ' ' + bar.time:
            previousResult = self.result
            self.result = DailyResult(bar.date + ' ' + bar.time)
            self.resultList.append(self.result)
            if previousResult:
                self.result.updateClose(previousResult.closeDict)

        # 将bar推送给signal
        for signal in self.signalDict[bar.vtSymbol]:
            signal.onBar(bar, minx)
            # print(bar.vtSymbol, bar.time)

        """
        在此实现P层的业务控制逻辑
        为每一个品种来检查是否触发下单条件
        # 开始处理组合关心的bar , 尤其是品种对价差的加工和处理
        """


        if self.got_dict[self.symbol_g] == True and self.got_dict[self.symbol_d] == True:
            for signal in self.signalDict[self.dual_name]:
                for s_g in self.signalDict[self.symbol_g]:
                    price_g = s_g.bar.close
                for s_d in self.signalDict[self.symbol_d]:
                    price_d = s_d.bar.close

                price_gap = price_g - price_d
                bar_dual = VtBarData()
                bar_dual.vtSymbol = self.dual_name
                bar_dual.open = price_gap
                bar_dual.high = price_gap
                bar_dual.low = price_gap
                bar_dual.close = price_gap
                bar_dual.date = bar.date
                bar_dual.time = bar.time
                bar_dual.datetime = bar.date + ' ' + bar.time

                # 将bar推送给signal
                signal.onBar(bar_dual, minx)
                self.result.updateBar(bar_dual)

                # print(bar_dual.vtSymbol, bar_dual.datetime )

            self.got_dict[self.symbol_g] = False
            self.got_dict[self.symbol_d] = False


        self.result.updateBar(bar)
        self.result.updatePos(self.posDict)
Exemplo n.º 8
0
    def _Generate_Bar_MinOne(self, tick, UpdateDate):
        """生成、推送、保存Bar"""
        new_bar = VtBarData()
        new_bar.date = UpdateDate
        new_bar.time = tick.UpdateTime
        new_bar.vtSymbol = tick.Instrument
        new_bar.open = tick.LastPrice
        new_bar.high = tick.LastPrice
        new_bar.low = tick.LastPrice
        new_bar.close = tick.LastPrice

        # 上一个bar存储在变量bar中,最新bar存储在变量new_bar中,
        id = new_bar.vtSymbol
        if id in self.bar_min1_dict:
            bar = self.bar_min1_dict[id]
        else:
            bar = new_bar
            self.bar_min1_dict[id] = bar
            return

        if bar.time[3:5] != new_bar.time[3:5]:
            # 将 bar的分钟改为整点,推送并保存bar
            bar.date = new_bar.date
            bar.time = new_bar.time[:-2] + '00'
            self.put_bar(bar, 'min1')

            bar.open = new_bar.open
            bar.high = new_bar.high
            bar.low = new_bar.low
            bar.close = new_bar.close
        else:
            # 分钟内,仅更新数据即可
            if bar.high < new_bar.high:
                bar.high = new_bar.high
            if bar.low > new_bar.low:
                bar.low = new_bar.low
            bar.close = new_bar.close

            bar.volume = tick.Volume
            bar.OpenInterest = tick.OpenInterest

            bar.AskPrice = tick.AskPrice
            bar.BidPrice = tick.BidPrice
            bar.AskVolume = tick.AskVolume
            bar.BidVolume = tick.BidVolume

            bar.AveragePrice = tick.AveragePrice
            bar.UpperLimitPrice = tick.UpperLimitPrice
            bar.LowerLimitPrice = tick.LowerLimitPrice
            bar.PreOpenInterest = tick.PreOpenInterest

            bar.PreSettlementPrice = tick.PreSettlementPrice
            bar.PreClosePrice = tick.PreClosePrice
            bar.OpenPrice = tick.OpenPrice
            bar.PreDelta = tick.PreDelta
            bar.CurrDelta = tick.CurrDelta

        self.bar_min1_dict[id] = bar
Exemplo n.º 9
0
    def onBar(self, bar, minx='min1'):
        """引擎新推送过来bar,传递给每个signal"""

        # 不处理不相关的品种
        if bar.vtSymbol not in self.vtSymbolList:
            return

        if minx != 'min5':  # 本策略为min5
            return

        if self.result.date != bar.date + ' ' + bar.time:
            previousResult = self.result
            self.result = DailyResult(bar.date + ' ' + bar.time)
            self.resultList.append(self.result)
            if previousResult:
                self.result.updateClose(previousResult.closeDict)

        # 将bar推送给signal
        for signal in self.signalDict[bar.vtSymbol]:
            signal.onBar(bar, minx)
            # print(bar.vtSymbol, bar.time)
        """
        在此实现P层的业务控制逻辑
        为每一个品种来检查是否触发下单条件
        # 开始处理组合关心的bar , 尤其是品种对价差的加工和处理
        """

        if self.got_dict[self.symbol_a] == True and self.got_dict[
                self.symbol_b] == True:
            for signal in self.signalDict[self.dual_name]:
                for s_a in self.signalDict[self.symbol_a]:
                    ask_price_a = float(s_a.bar.AskPrice)  # 挂卖价
                    bid_price_a = float(s_a.bar.BidPrice)  # 挂买价
                for s_b in self.signalDict[self.symbol_b]:
                    ask_price_b = float(s_b.bar.AskPrice)  # 挂卖价
                    bid_price_b = float(s_b.bar.BidPrice)  # 挂买价

                long_price = ask_price_a - bid_price_b
                short_price = bid_price_a - ask_price_b
                bar_dual = VtBarData()
                bar_dual.vtSymbol = self.dual_name
                bar_dual.high = short_price
                bar_dual.low = long_price
                bar_dual.date = bar.date
                bar_dual.time = bar.time
                bar_dual.datetime = bar.date + ' ' + bar.time

                # 将bar推送给signal
                signal.onBar(bar_dual, minx)
                self.result.updateBar(bar_dual)

                # print(bar_dual.vtSymbol, bar_dual.datetime )

            self.got_dict[self.symbol_a] = False
            self.got_dict[self.symbol_b] = False

        self.result.updateBar(bar)
        self.result.updatePos(self.posDict)
Exemplo n.º 10
0
    def worker_1450(self):
        """盘中推送bar"""
        to_log('in TradeEngine.worker_1450')

        for p in self.portfolio_list:
            for vtSymbol in p.vtSymbolList:
                #print(vtSymbol)
                df = None
                i = 0
                while df is None and i < 2:
                    try:
                        i += 1
                        df = ts.get_realtime_quotes(vtSymbol)
                    except Exception as e:
                        print('error get_realtime_quotes')
                        print(e)
                        time.sleep(0.1)

                if df is None:
                    continue

                code = vtSymbol
                df1 = None
                i = 0
                while df1 is None and i < 2:
                    try:
                        i += 1
                        df1 = get_adj_factor(self.dss, code)
                    except Exception as e:
                        print('error adj_factor ' + code)
                        print(e)
                        time.sleep(0.1)
                if df1 is None:
                    continue

                factor = float(df1.at[0, 'adj_factor'])
                d = df.loc[0, :]
                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open']) * factor
                bar.high = float(d['high']) * factor
                bar.low = float(d['low']) * factor
                bar.close = float(d['price']) * factor
                bar.close_bfq = float(d['price'])
                date = d['date'].split('-')  #去掉字符串中间的'-'
                date = ''.join(date)
                bar.date = date
                bar.time = '00:00:00'
                bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                 '%Y%m%d %H:%M:%S')
                bar.volume = float(d['volume'])

                p.onBar(bar)

        send_email(self.dss, 'worker_1450处理完毕', '')
Exemplo n.º 11
0
    def _Generate_Bar(self, tick):
        """生成、推送、保存Bar"""
        id = tick.Instrument
        if id in self.bar_dict:
            bar = self.bar_dict[id]
        else:
            bar = VtBarData()

        today = time.strftime('%Y-%m-%d', time.localtime())
        min = ''.join(tick.UpdateTime)
        #print('before ',min)
        min = min[:-2] + '00'
        #print('after  ',min)

        if bar.time != min:
            if len(bar.time) > 0:
                df = pd.DataFrame([bar.__dict__])
                cols = [
                    'date', 'time', 'open', 'high', 'low', 'close', 'volume'
                ]
                df = df[cols]

                # send bar to port
                self.send_bar(str(bar.__dict__))

                #self.put_bar(df, id)

                # save bar
                # 出现了怪的问题,发布了多条重复的tick,但分种线的加工不应该出现此问题,多线程 ? ? ?
                fname = self.dss + 'fut/bar/min1_' + self.tradeDay + '_' + id + '.csv'
                if os.path.exists(fname):
                    df.to_csv(fname, index=False, mode='a', header=False)
                else:
                    df.to_csv(fname, index=False, mode='a')
            bar.date = today
            bar.time = min
            bar.vtSymbol = tick.Instrument
            bar.open = tick.LastPrice
            bar.high = tick.LastPrice
            bar.low = tick.LastPrice
            bar.close = tick.LastPrice
            bar.volume = tick.Volume
        else:
            if bar.high < tick.LastPrice:
                bar.high = tick.LastPrice
            if bar.low > tick.LastPrice:
                bar.low = tick.LastPrice
            bar.close = tick.LastPrice
            bar.volume = tick.Volume

        self.bar_dict[id] = bar
Exemplo n.º 12
0
    def loadData(self):
        """加载数据"""
        for vtSymbol in self.symbol_list:
            filename = get_dss() + 'fut/bar/min1_' + vtSymbol + '.csv'

            df = pd.read_csv(filename)
            for i, d in df.iterrows():
                # print(d)
                # print('here')

                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open'])
                bar.high = float(d['high'])
                bar.low = float(d['low'])
                bar.close = float(d['close'])
                bar.volume = d['volume']

                dt = d['datetime']

                bar.date = dt[:4] + dt[5:7] + dt[8:10]
                bar.time = dt[11:19]
                bar.datetime = bar.date + ' ' + bar.time
                # print(bar.datetime, bar.date, bar.time)
                # return

                # date = str(d['date'])
                # bar.date = date
                # bar.time = str(d['time'])
                # if '-' in date:
                #     bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S')
                # else:
                #     bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S')
                #
                # #bar.time = '00:00:00'
                # #bar.datetime = bar.date + ' ' + bar.time
                # bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S')

                barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
                barDict[bar.vtSymbol] = bar
                # break

        self.output(u'全部数据加载完成')
Exemplo n.º 13
0
    def control_in_p(self, bar):
        try:
            for k in self.spread_dict.keys():
                s0 = self.spread_dict[k][0]
                s1 = self.spread_dict[k][1]
                if s0 not in self.got_dict or s1 not in self.got_dict:
                    continue
                if self.got_dict[s0] and self.got_dict[
                        s1] and self.process_dict[k] == False:
                    self.process_dict[k] = True
                    s0 = self.bar_dict[s0]
                    s1 = self.bar_dict[s1]

                    bar_s = VtBarData()
                    bar_s.vtSymbol = k
                    bar_s.symbol = k
                    bar_s.exchange = s0.exchange
                    bar_s.date = s0.date
                    bar_s.time = s0.time

                    bar_s.close = s0.close - s1.close
                    bar_s.AskPrice = s0.AskPrice - s1.BidPrice
                    bar_s.BidPrice = s0.BidPrice - s1.AskPrice

                    # print(bar_s.time, bar_s.vtSymbol, bar_s.close, bar_s.AskPrice, bar_s.BidPrice)
                    df = pd.DataFrame([bar_s.__dict__])
                    cols = [
                        'date', 'time', 'open', 'high', 'low', 'close',
                        'volume'
                    ]
                    df = df[cols]
                    fname = get_dss() + 'fut/bar/min1_' + k + '.csv'
                    if os.path.exists(fname):
                        df.to_csv(fname, index=False, mode='a', header=False)
                    else:
                        df.to_csv(fname, index=False, mode='a')

                    return bar_s
        except Exception as e:
            s = traceback.format_exc()
            to_log(s)

        return None
Exemplo n.º 14
0
def _Generate_Bar_MinOne(tick, temp_bar, r, today):
    """
    从tick加工生成bar_min1
    temp_bar:存储上一个bar
    r:存储数据加工结果
    today:tradeDay
    """

    new_bar = VtBarData()
    new_bar.date = today
    new_bar.time = tick.UpdateTime
    new_bar.open = tick.LastPrice
    new_bar.high = tick.LastPrice
    new_bar.low =  tick.LastPrice
    new_bar.close = tick.LastPrice

    # 上一个bar存储在变量bar中,最新bar存储在变量new_bar中。
    if temp_bar != []:
        bar = temp_bar.pop()
    else:
        bar = new_bar
        temp_bar.append(bar)
        return

    # if bar.time[3:5] != new_bar.time[3:5] :
    if bar.time[:5] != new_bar.time[:5] :
        # 将 bar的分钟改为整点,推送并保存bar
        bar.date = new_bar.date
        bar.time = new_bar.time[:-2] + '00'
        r.append( [bar.date, bar.time, bar.open, bar.high, bar.low, bar.close, 0] )
        bar.open = new_bar.open
        bar.high = new_bar.high
        bar.low = new_bar.low
        bar.close = new_bar.close
    else:
        # 更新数据
        if bar.high < new_bar.high:
            bar.high = new_bar.high
        if bar.low > new_bar.low:
            bar.low =  new_bar.low
        bar.close = new_bar.close

    temp_bar.append(bar)
    def loadData(self):
        """加载数据"""
        for vtSymbol in self.symbol_list:
            #filename = get_dss( )+ 'fut/bar/min5_' + vtSymbol + '.csv'
            #filename = get_dss( ) + 'fut/bar/' + self.minx + '_' + vtSymbol + '.csv'
            #filename = get_dss( ) + 'backtest/bar/' + self.minx + '_' + vtSymbol + '.csv'
            filename = get_dss(
            ) + 'backtest/fut/' + vtSymbol + '/' + self.minx + '_' + vtSymbol + '.csv'

            df = pd.read_csv(filename)
            for i, d in df.iterrows():
                #print(d)

                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open'])
                bar.high = float(d['high'])
                bar.low = float(d['low'])
                bar.close = float(d['close'])
                bar.volume = d['volume']

                date = str(d['date'])
                bar.date = date
                bar.time = str(d['time'])
                if '-' in date:
                    bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                     '%Y-%m-%d %H:%M:%S')
                else:
                    bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                     '%Y%m%d %H:%M:%S')

                #bar.time = '00:00:00'
                #bar.datetime = bar.date + ' ' + bar.time
                bar.datetime = datetime.strftime(bar.datetime,
                                                 '%Y%m%d %H:%M:%S')

                barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
                barDict[bar.vtSymbol] = bar
                # break

            print(vtSymbol + '全部数据加载完成,数据量:' + str(len(df)))
    def _Generate_Bar_MinOne(self, tick, UpdateDate):
        """生成、推送、保存Bar"""
        new_bar = VtBarData()
        new_bar.date = UpdateDate
        new_bar.time = tick.UpdateTime
        new_bar.vtSymbol = tick.Instrument
        new_bar.open = tick.LastPrice
        new_bar.high = tick.LastPrice
        new_bar.low = tick.LastPrice
        new_bar.close = tick.LastPrice

        id = new_bar.vtSymbol
        if id in self.bar_min1_dict:
            bar = self.bar_min1_dict[id]
        else:
            bar = new_bar
            self.bar_min1_dict[id] = bar
            return

        if bar.time[3:5] != new_bar.time[3:5]:
            # 将 bar的分钟改为整点,推送并保存bar
            bar.date = new_bar.date
            bar.time = new_bar.time[:-2] + '00'
            self.put_bar(bar, 'min1')
            self._Generate_Bar_Min5(bar)
            self._Generate_Bar_Min15(bar)
            self.save_bar(bar, 'min1')

            bar.open = new_bar.open
            bar.high = new_bar.high
            bar.low = new_bar.low
            bar.close = new_bar.close
        else:
            # 更新数据
            if bar.high < new_bar.high:
                bar.high = new_bar.high
            if bar.low > new_bar.low:
                bar.low = new_bar.low
            bar.close = new_bar.close

        self.bar_min1_dict[id] = bar
Exemplo n.º 17
0
    def loadData(self):
        """加载数据"""

        for vtSymbol in self.vtSymbol_list:
            # print(vtSymbol)
            test_list = ['IF2011', 'IO2011-C-4700', 'IO2011-P-4700']
            # test_list = ['m2101', 'RM101', 'MA101', 'p2101']
            if vtSymbol in test_list:

                fn = get_dss( )+ 'fut/bar/min1_' + vtSymbol + '.csv'
                if os.path.exists(fn):
                    df = pd.read_csv(fn)
                    for i, d in df.iterrows():
                        # print(i)

                        bar = VtBarData()
                        bar.vtSymbol = vtSymbol
                        bar.symbol = vtSymbol
                        bar.open = float(d['open'])
                        bar.high = float(d['high'])
                        bar.low = float(d['low'])
                        bar.close = float(d['close'])
                        bar.volume = d['volume']

                        date = str(d['date'])
                        bar.date = date
                        bar.time = str(d['time'])
                        if '-' in date:
                            bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S')
                        else:
                            bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S')

                        bar.datetime = datetime.strftime(bar.datetime, '%Y-%m-%d %H:%M:%S')

                        # barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
                        # barDict[bar.vtSymbol] = bar
                        if bar.datetime in self.dataDict:
                            self.dataDict[bar.datetime][bar.vtSymbol] = bar
                        else:
                            self.dataDict[bar.datetime] = {}
                            self.dataDict[bar.datetime][bar.vtSymbol] = bar
Exemplo n.º 18
0
    def loadData(self):
        """加载数据"""
        for vtSymbol in self.vtSymbol_list:
            fname = vtSymbol + '.csv'
            #print(fname)
            df = pd.read_csv(fname,dtype='str')
            df = df[df.date>datetime.strftime(self.startDt, '%Y%m%d')]
            df = df[df.date<datetime.strftime(self.endDt, '%Y%m%d')]
            df = df.sort_values(by=['date','time'])
            #print(df)

            for i, row in df.iterrows():
                d = dict(row)
                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open'])
                bar.high = float(d['high'])
                bar.low = float(d['low'])
                bar.close = float(d['close'])
                bar.volume = d['volume']

                date = str(d['date'])
                bar.date = date
                bar.time = str(d['time'])
                if '-' in date:
                    bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y-%m-%d %H:%M:%S')
                else:
                    bar.datetime = datetime.strptime(bar.date + ' ' + bar.time, '%Y%m%d %H:%M:%S')

                #bar.datetime = datetime.strftime(bar.datetime, '%Y%m%d %H:%M:%S')


                barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
                barDict[bar.vtSymbol] = bar

            self.output(u'%s数据加载完成,总数据量:%s' %(vtSymbol, len(df)))

        self.output(u'全部数据加载完成')
Exemplo n.º 19
0
    def worker_1450(self):
        """盘中推送bar"""
        to_log('in TradeEngine.worker_1450')

        print('begin worker_1450')
        r, dt = self.is_trade_day()
        if r == False:
            return

        self.currentDt = dt
        for vtSymbol in self.vtSymbolList:
            #print(vtSymbol)
            df = None
            i = 0
            while df is None and i < 3:
                try:
                    i += 1
                    df = ts.get_realtime_quotes(vtSymbol)
                except Exception as e:
                    print('error get_realtime_quotes')
                    print(e)
                    time.sleep(1)

            if df is None:
                continue

            code = vtSymbol
            df1 = None
            i = 0
            while df1 is None and i < 3:
                try:
                    i += 1
                    df1 = get_adj_factor(self.dss, code)
                except Exception as e:
                    print('error adj_factor')
                    print(e)
                    time.sleep(1)
            if df1 is None:
                continue

            factor = float(df1.at[0, 'adj_factor'])
            d = df.loc[0, :]
            bar = VtBarData()
            bar.vtSymbol = vtSymbol
            bar.symbol = vtSymbol
            bar.open = float(d['open']) * factor
            bar.high = float(d['high']) * factor
            bar.low = float(d['low']) * factor
            bar.close = float(d['price']) * factor
            bar.close_bfq = float(d['price'])
            date = d['date'].split('-')  #去掉字符串中间的'-'
            date = ''.join(date)
            bar.date = date
            bar.time = '00:00:00'
            bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                             '%Y%m%d %H:%M:%S')
            bar.volume = float(d['volume'])

            barDict = self.dataDict.setdefault(bar.datetime, OrderedDict())
            barDict[bar.vtSymbol] = bar

            self.portfolio.onBar(bar)

        send_email(self.dss, 'worker_1450处理完毕', '')
Exemplo n.º 20
0
def save_bar(r1, symbol):
    # 保存min1
    df_symbol = pd.DataFrame(r1, columns=['date','time','open','high','low','close','volume'])
    fname = get_dss() + 'fut/bar/min1_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 根据min1的结果集生成minx
    g5 = BarGenerator('min5')
    g15 = BarGenerator('min15')
    g30 = BarGenerator('min30')
    g_day = BarGenerator('day')
    for row in r1:
        new_bar = VtBarData()
        new_bar.vtSymbol = symbol
        new_bar.date = row[0]
        new_bar.time = row[1]
        new_bar.open = row[2]
        new_bar.high = row[3]
        new_bar.low =  row[4]
        new_bar.close = row[5]
        g5.update_bar(new_bar)
        g15.update_bar(new_bar)
        g30.update_bar(new_bar)
        g_day.update_bar(new_bar)

    # 保存min5
    r5 = g5.r_dict[symbol]
    df_symbol = pd.DataFrame(r5, columns=['date','time','open','high','low','close','volume'])
    fname = get_dss() + 'fut/bar/min5_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 保存min15
    r15 = g15.r_dict[symbol]
    df_symbol = pd.DataFrame(r15, columns=['date','time','open','high','low','close','volume'])
    fname = get_dss() + 'fut/bar/min15_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 保存min30
    r30 = g30.r_dict[symbol]
    df_symbol = pd.DataFrame(r30, columns=['date','time','open','high','low','close','volume'])
    fname = get_dss() + 'fut/bar/min30_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 保存day
    r_day = g_day.r_dict[symbol]
    df_symbol = pd.DataFrame(r_day, columns=['date','time','open','high','low','close','volume'])
    fname = get_dss() + 'fut/bar/day_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')
Exemplo n.º 21
0
    def worker_1450(self):
        """盘中推送bar"""
        to_log('in TradeEngine.worker_1450')

        for p in self.portfolio_list:
            for vtSymbol in p.vtSymbolList:
                #print(vtSymbol)
                df = None
                i = 0
                while df is None and i < 2:
                    try:
                        i += 1
                        df = ts.get_realtime_quotes(vtSymbol)
                    except Exception as e:
                        print('error get_realtime_quotes')
                        print(e)
                        time.sleep(0.1)

                if df is None:
                    to_log('ignore ' + vtSymbol)
                    continue

                adj_factor = None
                df1 = None
                i = 0
                while df1 is None and i < 2:
                    try:
                        i += 1
                        df1 = get_adj_factor(self.dss, vtSymbol)
                        adj_factor = float(df1.at[0, 'adj_factor'])
                    except Exception as e:
                        print('error adj_factor ' + vtSymbol)
                        print(e)
                        time.sleep(0.1)
                if df1 is None:
                    to_log('ignore ' + vtSymbol)
                    continue

                hfq_factor = get_hfq_factor(self.dss, vtSymbol)
                factor = hfq_factor
                if adj_factor is not None:
                    if abs((adj_factor - hfq_factor) /
                           adj_factor) > 0.01:  # 差异大,今天有除权
                        factor = adj_factor

                d = df.loc[0, :]
                bar = VtBarData()
                bar.vtSymbol = vtSymbol
                bar.symbol = vtSymbol
                bar.open = float(d['open']) * factor
                bar.high = float(d['high']) * factor
                bar.low = float(d['low']) * factor
                bar.close = float(d['price']) * factor
                bar.close_bfq = float(d['price'])
                date = d['date'].split('-')  #去掉字符串中间的'-'
                date = ''.join(date)
                bar.date = date
                bar.time = '00:00:00'
                bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,
                                                 '%Y%m%d %H:%M:%S')
                bar.volume = float(d['volume'])

                #to_log(vtSymbol+' '+d['price']+' * '+str(factor)+' = '+str(bar.close))

                p.onBar(bar)

        send_email(self.dss, 'worker_1450处理完毕', '')
Exemplo n.º 22
0
def one2x(pz, symbol, filename):
    df = pd.read_csv(filename)

    # 生成minx
    g5 = BarGenerator('min5')
    g15 = BarGenerator('min15')
    g30 = BarGenerator('min30')
    g_day = BarGenerator('day')
    for i, row in df.iterrows():
        new_bar = VtBarData()
        new_bar.vtSymbol = symbol
        new_bar.date = row.date
        new_bar.time = row.time
        new_bar.open = row.open
        new_bar.high = row.high
        new_bar.low = row.low
        new_bar.close = row.close
        g5.update_bar(new_bar)
        g15.update_bar(new_bar)
        g30.update_bar(new_bar)
        g_day.update_bar(new_bar)

    # 保存min5
    r5 = g5.r_dict[symbol]
    df_symbol = pd.DataFrame(
        r5, columns=['date', 'time', 'open', 'high', 'low', 'close', 'volume'])
    fname = get_dss() + 'backtest/fut/' + pz + '/min5_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 保存min15
    r15 = g15.r_dict[symbol]
    df_symbol = pd.DataFrame(
        r15,
        columns=['date', 'time', 'open', 'high', 'low', 'close', 'volume'])
    fname = get_dss() + 'backtest/fut/' + pz + '/min15_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 保存min30
    r30 = g30.r_dict[symbol]
    df_symbol = pd.DataFrame(
        r30,
        columns=['date', 'time', 'open', 'high', 'low', 'close', 'volume'])
    fname = get_dss() + 'backtest/fut/' + pz + '/min30_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')

    # 保存day
    r_day = g_day.r_dict[symbol]
    df_symbol = pd.DataFrame(
        r_day,
        columns=['date', 'time', 'open', 'high', 'low', 'close', 'volume'])
    fname = get_dss() + 'backtest/fut/' + pz + '/day_' + symbol + '.csv'
    if os.path.exists(fname):
        df_symbol.to_csv(fname, index=False, mode='a', header=False)
    else:
        df_symbol.to_csv(fname, index=False, mode='a')