Пример #1
0
 def finally_datetime(cls, date, max_pst_days=1, bsi=False):
     """
     根据开仓时间和最长持有时间计算买出时间,适用于中国A股股票
     :param bsi: False表示按自然日计算持有的时间, True表示按交易日计算
     :param date:
     :param max_pst_days:
     :return:
     """
     try:
         time = dt.timedelta(hours=14, minutes=55)
         start = dt.datetime(date.year, date.month, date.day)
         if bsi:
             end = trading.trade_period(start, max_pst_days)
             return end + time
         else:
             end = start + dt.timedelta(days=max_pst_days)
             if trading.is_trade_day(end):
                 return end + time
             else:  # end 不是交易日
                 if trading.trade_days(start, end) > 0:  # 向前推算
                     for i in range(1, max_pst_days, 1):
                         end = end - dt.timedelta(days=1)
                         if trading.is_trade_day(end):
                             return end + time
                 else:  # 向后推算
                     for i in range(1, 11, 1):
                         end = start + dt.timedelta(days=i)
                         if trading.is_trade_day(end):
                             return end + time
             return date
     except Exception as e:
         ExceptionInfo(e)
         return date
Пример #2
0
 def trade_date(cls, datetime, market='China/Stock'):
     if market == 'China/Stock':
         tdy = dt.datetime(datetime.year, datetime.month, datetime.day)
         if trading.is_trade_day(tdy):
             open_am = dt.datetime(datetime.year, datetime.month,
                                   datetime.day, 9, 30)
             close_am = dt.datetime(datetime.year, datetime.month,
                                    datetime.day, 11, 30)
             open_pm = dt.datetime(datetime.year, datetime.month,
                                   datetime.day, 13)
             close_pm = dt.datetime(datetime.year, datetime.month,
                                    datetime.day, 15)
             if open_am <= datetime <= close_am or open_pm <= datetime < close_pm:
                 return True
             else:
                 return False
         else:
             return False
     if market == 'China/HongKong':
         pass
Пример #3
0
 def tim(tms):
     while 1:
         try:
             crt = dt.datetime.now()
             if not trading.is_trade_day(crt):
                 print('{0} today is not in business'.format(crt))
                 time.sleep(60 * 60 * 2)  # sleep two hours
                 continue
             tms['date'] = pd.datetime(crt.year, crt.month,
                                       crt.day) + tms.time
             tm = tms[tms.hour == crt.hour]
             if len(tm):
                 for i, r in tm.iterrows():
                     if crt >= r.date != r.log:
                         print('timing task run', r.date, r.log)
                         func()
                         tms.at[i, 'log'] = r.date
                         pass
                 time.sleep(60)
             else:
                 time.sleep(60 * 60)
             pass
         except Exception as ep:
             ExceptionInfo(ep)
Пример #4
0
 def is_trade_day(cls, date):
     return trading.is_trade_day(date)
Пример #5
0
    def rerun(cls, action, deep_sleep=list(), offset=None):
        """
        复盘演示
        :param action:
        :param deep_sleep:
        :param offset:
        :return:
        """
        profit_probe_period = dt.timedelta(minutes=1)
        profit_probe_next = CalfDateTime.now(offset=offset)
        _f = False  # 记录当天是否收盘
        klines = action.klines  # 需要跟踪的bar周期
        _id = cls.open_kline_update_log()  # 记录bar更新
        while 1:
            try:
                crt = CalfDateTime.now(offset=offset)
                if not trading.is_trade_day(
                        dt.datetime(crt.year, crt.month, crt.day)):
                    print('{0} today is not in business'.format(crt))
                    time.sleep(60 * 60 * 2)  # sleep two hours
                    continue
                if action.trade_day_end(crt) and _f:
                    print(fontcolor.F_GREEN + '-' * 80)
                    print('# %s Today Overview #' % action.name)
                    print('Signal:')
                    action.signals_summary()
                    print('Order:')
                    action.orders_summary()
                    print(fontcolor.F_GREEN + '-' * 80 + fontcolor.END)
                    sound_notice('close.wav').start()
                    _f = False
                # open_am <= crt <= close_am or open_pm <= crt < close_pm
                if action.trade_date(crt):
                    # 交易日盘中
                    s_d = crt - dt.timedelta(minutes=3)
                    e_d = crt + dt.timedelta(minutes=3)
                    log = kd.read_log(start_date=s_d, end_date=e_d, status=200)
                    if len(log):
                        log['_id'] = log['_id'].astype('str')
                        for i, r in log.iterrows():
                            if r.kline in klines and r['_id'] != _id[r.kline]:
                                print('this kline %s find update' % r.kline)
                                action.real(r.kline, start_time=crt)
                                print(r.kline + ' id update:' + _id[r.kline] +
                                      '-->' + r['_id'])
                                _id[r.kline] = r['_id']
                                cls.set_kline_update_log(_id)

                    if profit_probe_next <= crt:
                        print('profit probing date:{0}'.format(crt))
                        # action.probing()
                        profit_probe_next += profit_probe_period
                    _f = True
                    time.sleep(5)
                else:
                    print('{0} this datetime is not in business'.format(crt))
                    profit_probe_next = crt
                    _f = False
                    # sleep = 60 * 30
                    # if crt.hour == 9 or crt.hour == 12:
                    #     sleep = 60 * 5
                    sleep = 60 * 5 if crt.hour in deep_sleep else 60 * 30
                    time.sleep(sleep)

            except Exception as e:
                ExceptionInfo(e)