Пример #1
0
def get_ticks(symbol, begin_time, end_time):
    var = md.get_ticks(symbol, begin_time, end_time)
    if len(var) == 0:
        print("no ticks data for stock %s between %s and %s" %(symbol, begin_time, end_time))
        return pd.DataFrame()
    else:
        ret = tick_topd(var,'date')
        return ret
def download(exchange, instrument, date):
    dt = dateutil.parser.parse(date)

    fname = '%s-%4d%02d%02d.csv' % (instrument, dt.year, dt.month, dt.day)
    if os.path.isfile(fname):
        print('%s already existed!' % fname)
        return fname

    print('download ticks \'%s\'...' % fname)

    start = '%4d-%02d-%02d 09:00:00' % (dt.year, dt.month, dt.day)
    end = '%4d-%02d-%02d 15:00:00' % (dt.year, dt.month, dt.day)

    md.init('*****@*****.**', 'xxx')

    data = md.get_ticks('%s.%s' % (exchange, instrument), start, end)

    if len(data) == 0:
        print('download failed!')
        return None
    else:
        print('download %d ticks' % len(data))

    def secondofday(time):
        t = time.split(':')
        return int(t[0]) * 3600 + int(t[1]) * 60 + int(t[2])

    rnd = lambda x: int(x) if math.modf(x)[0] == 0 else round(x, 1)

    ticks = []
    for x in data:
        utctime = datetime.datetime.fromtimestamp(x.utc_time)
        time = utctime.strftime('%H:%M:%S')
        millis = int(int(utctime.strftime('%f')) / 1000)
        seconds = secondofday(time)
        ticks.append([
            x.sec_id, time, millis,
            int(x.cum_volume),
            rnd(x.cum_amount), x.cum_position,
            rnd(x.bids[0][0]), x.bids[0][1],
            rnd(x.asks[0][0]), x.asks[0][1], seconds
        ])

    with open(fname, 'w') as outfile:
        writer = csv.writer(outfile, delimiter=',')
        writer.writerow([
            "InstrumentID", "UpdateTime", "UpdateMillisec", "Volume",
            "Turnover", "OpenInterest", "BidPrice1", "BidVolume1", "AskPrice1",
            "AskVolume1", "SecondOfDay"
        ])
        for x in ticks:
            writer.writerow(x)

    return fname
Пример #3
0
 def gmwsd(self, code, valstr, startdate, enddate):
     # 假设startdate,enddate均为datetime/time 格式
     cols = [
         'strtime', 'open', 'high', 'low', 'close', 'volume', 'amount',
         'settle_price'
     ]
     today = dt.datetime.today()
     if 'strtime' not in valstr:
         valstr = ','.join(['strtime', valstr])
     if enddate.strftime('%Y-%m-%d') >= today.strftime(
             '%Y-%m-%d'):  # 需要取今日数据
         nowtime = dt.datetime.now()
         if nowtime.hour * 100 + nowtime.minute > 1500:  # 收盘后请求当日数据,根据tick数据构建
             head = dt.datetime(year=today.year,
                                month=today.month,
                                day=today.day,
                                hour=15)
             tail = dt.datetime(year=today.year,
                                month=today.month,
                                day=today.day,
                                hour=16)
             tickdata = md.get_ticks(code,
                                     head.strftime('%Y-%m-%d %H:%M:%S'),
                                     tail.strftime('%Y-%m-%d %H:%M:%S'))
             t = tickdata[-1]
             todaydata = [
                 t.strtime, t.open, t.high, t.low, t.last_price,
                 t.cum_volume, t.cum_amount, t.settle_price
             ]  # 将所有字段添加if needed
         else:  #收盘前请求当日数据,将返回前一日结果
             lastdata = md.get_last_dailybars(code)
             t = lastdata[0]
             todaydata = [
                 t.strtime, t.open, t.high, t.low, t.close, t.volume,
                 t.amount, t.settle_price
             ]
         todaydata = pd.DataFrame([todaydata], columns=cols)
     else:
         todaydata = pd.DataFrame()
     if startdate == enddate:  # 不再需要其他数据
         return todaydata.loc[:, valstr.split(',')]
     else:
         tempdata = md.get_dailybars(code, startdate.strftime('%Y-%m-%d'),
                                     enddate.strftime('%Y-%m-%d'))
         predata = [[
             t.strtime, t.open, t.high, t.low, t.close, t.volume, t.amount,
             t.settle_price
         ] for t in tempdata]
         predata = pd.DataFrame(predata, columns=cols)
         return predata.append(todaydata,
                               ignore_index=True).loc[:,
                                                      valstr.split(',')]
    def extract_tick_data(self, session_start, session_end, target_contract):
        return_tick_dicts = []
        ret_value = md.init("*****@*****.**", "btbxl@121208")

        current_loop_start = session_start

        while (True):
            test_tick_list = md.get_ticks(
                target_contract,
                current_loop_start.strftime(
                    stdafx.my_datetime.my_quant_time_format),
                session_end.strftime(stdafx.my_datetime.my_quant_time_format))
            if test_tick_list.__len__() <= 1:  #如果已经取不出数据,则跳出
                break
            current_final_tick = test_tick_list[test_tick_list.__len__() - 1]
            current_final_tick_moment = stdafx.my_datetime.utc_float2datetime(
                current_final_tick.utc_time)
            current_loop_start = current_final_tick_moment  #更新下一圈提数据的开始时刻
            test_tick_list.pop()
            return_tick_dicts.extend(test_tick_list)
            if current_loop_start >= session_end:  #如果下圈开始时间已经等于最终时间,则结束
                break
            pass

        tick_series = [
            my_tick(tick_to_dict(each_tick)) for each_tick in return_tick_dicts
        ]

        self.tick_profitability_walk(tick_series)
        my_tick_dict_series = [each.__dict__ for each in tick_series]

        pickle_filename = self.get_pickle_filename(target_contract,
                                                   session_start, session_end)

        pickle_file = open(pickle_filename, mode='wb')
        pickle.dump(my_tick_dict_series, pickle_file, -1)
        pickle_file.close()
        return my_tick_dict_series
Пример #5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gmsdk import md, to_dict

md.init('*****@*****.**', '123456')
# 提取tick数据
r = md.get_ticks(
    'SHSE.600000',
    '2016-01-04 09:00:00',
    '2016-01-04 12:00:00',
)
print('get_ticks: ', len(r))

#提取tick快照
r = md.get_last_ticks('SHSE.600000')
print('get_last_ticks: ', len(r))

#提取最新N笔tick数据
r = md.get_last_n_ticks(
    'SHSE.600000', 10)
print('get_last_n_ticks(10): ', len(r))

#提起一分钟分时数据(freq=60 seconds)
r = md.get_bars(
    'SHSE.600000',
    60,
    '2015-12-01 09:30:00',
    '2015-12-10 09:31:00',
)
print('get_bars: ', len(r))
Пример #6
0
from gmsdk import md

md.init('*****@*****.**', '123456')

r = md.get_ticks(
    'CFFEX.IF1512,CFFEX.IF1512',
    '2015-03-19 11:29:00',
    '2015-03-19 12:00:00',
)
print('get_ticks: ', len(r))

r = md.get_last_ticks('CFFEX.IF1512,')
print('get_last_ticks: ', len(r))

r = md.get_last_n_ticks(
    'CFFEX.IF1512', 10)
print('get_last_n_ticks(10): ', len(r))

r = md.get_bars(
    'CFFEX.IF1512',
    60,
    '2015-05-01 09:30:00',
    '2015-05-10 09:31:00',
)
print('get_bars: ', len(r))

r = md.get_last_bars('CFFEX.IF1512,', 60)
print('get_last_bars: ', len(r))

r = md.get_last_n_bars(
    'CFFEX.IF1512',
Пример #7
0
def get_ticks(symbol, begin_time, end_time):
    var = md.get_ticks(symbol, begin_time, end_time)
    ret = tick_topd(var,'date')
    return ret
Пример #8
0
def get_ticks(symbol, begin_time, end_time):
    var = md.get_ticks(symbol, begin_time, end_time)
    ret = tick_topd(var, 'date')
    return ret
Пример #9
0
from gmsdk import md

ret = md.init("13601380996",
              "it@iZ23psatkqsZ",
              2,
              "SHFE.RU1709.bar.60")

ticks = md.get_ticks("SHSE.600000,SZSE.000001",
                     "2017-06-01 09:30:00",
                     "2017-06-01 09:31:00")

print(ticks)

ret = md.init("13601380996",
              "it@iZ23psatkqsZ",
              4,
              "SHFE.ru1709.tick")


ti = md.get_ticks("SHFE.ru1709.tick","2017-06-01 09:30:00","2017-06-01 09:31:00")

print("aaa!")
print(ti)