Example #1
0
def dual_thrust(asset, start_date, end_date, scenarios, config):
    nearby = config["nearby"]
    rollrule = config["rollrule"]
    start_d = misc.day_shift(start_date, "-2b")
    file_prefix = config["file_prefix"] + "_" + asset + "_"
    mdf = misc.nearby(asset, nearby, start_d, end_date, rollrule, "m", need_shift=True)
    mdf = backtest.cleanup_mindata(mdf, asset)
    output = {}
    for ix, s in enumerate(scenarios):
        config["win"] = s[1]
        config["k"] = s[0]
        config["m"] = s[2]
        config["f"] = s[3]
        (res, closed_trades, ts) = dual_thrust_sim(mdf, config)
        output[ix] = res
        print "saving results for scen = %s" % str(ix)
        all_trades = {}
        for i, tradepos in enumerate(closed_trades):
            all_trades[i] = strat.tradepos2dict(tradepos)
        fname = file_prefix + str(ix) + "_trades.csv"
        trades = pd.DataFrame.from_dict(all_trades).T
        trades.to_csv(fname)
        fname = file_prefix + str(ix) + "_dailydata.csv"
        ts.to_csv(fname)
    fname = file_prefix + "stats.csv"
    res = pd.DataFrame.from_dict(output)
    res.to_csv(fname)
    return
Example #2
0
def dual_thrust( asset, start_date, end_date, scenarios, config):
    nearby  = config['nearby']
    rollrule = config['rollrule']
    start_d = misc.day_shift(start_date, '-4b')
    file_prefix = config['file_prefix'] + '_' + asset + '_'
    ddf = misc.nearby(asset, nearby, start_d, end_date, rollrule, 'd', need_shift=True)
    mdf = misc.nearby(asset, nearby, start_d, end_date, rollrule, 'm', need_shift=True)
    mdf = backtest.cleanup_mindata(mdf, asset)
    #ddf = dh.conv_ohlc_freq(mdf, 'D')
    output = {}
    for ix, s in enumerate(scenarios):
        config['win'] = s[1]
        config['k'] = s[0]
        config['m'] = s[2]
        (res, closed_trades, ts) = dual_thrust_sim( ddf, mdf, config)
        output[ix] = res
        print 'saving results for scen = %s' % str(ix)
        all_trades = {}
        for i, tradepos in enumerate(closed_trades):
            all_trades[i] = strat.tradepos2dict(tradepos)
        fname = file_prefix + str(ix) + '_trades.csv'
        trades = pd.DataFrame.from_dict(all_trades).T  
        trades.to_csv(fname)
        fname = file_prefix + str(ix) + '_dailydata.csv'
        ts.to_csv(fname)
    fname = file_prefix + 'stats.csv'
    res = pd.DataFrame.from_dict(output)
    res.to_csv(fname)
    return 
Example #3
0
def aberration( asset, start_date, end_date, freqs, windows, config):
    nearby  = config['nearby']
    rollrule = config['rollrule']
    file_prefix = config['file_prefix'] + '_' + asset + '_'
    df = misc.nearby(asset, nearby, start_date, end_date, rollrule, 'm', need_shift=True)
    df = backtest.cleanup_mindata(df, asset)    
    output = {}
    for ix, freq in enumerate(freqs):
        xdf = dh.conv_ohlc_freq(df, freq)
        for iy, win in enumerate(windows):
            idx = ix*10+iy
            config['win'] = win
            (res, closed_trades, ts) = aberration_sim( xdf, config)
            output[idx] = res
            print 'saving results for scen = %s' % str(idx)
            all_trades = {}
            for i, tradepos in enumerate(closed_trades):
                all_trades[i] = strat.tradepos2dict(tradepos)
            fname = file_prefix + str(idx) + '_trades.csv'
            trades = pd.DataFrame.from_dict(all_trades).T  
            trades.to_csv(fname)
            fname = file_prefix + str(idx) + '_dailydata.csv'
            ts.to_csv(fname)
    fname = file_prefix + 'stats.csv'
    res = pd.DataFrame.from_dict(output)
    res.to_csv(fname)
    return 
def fisher_swing(asset, start_date, end_date, freqs, windows, config):
    nearby = config["nearby"]
    rollrule = config["rollrule"]
    file_prefix = config["file_prefix"] + "_" + asset + "_"
    df = misc.nearby(asset, nearby, start_date, end_date, rollrule, "m", need_shift=True)
    df = backtest.cleanup_mindata(df, asset)
    output = {}
    for ix, freq in enumerate(freqs):
        xdf = dh.conv_ohlc_freq(df, freq)
        for iy, win in enumerate(windows):
            idx = ix * 10 + iy
            config["win"] = win
            config["freq"] = freq
            (res, closed_trades, ts) = fisher_swing_sim(df, xdf, config)
            output[idx] = res
            print "saving results for scen = %s" % str(idx)
            all_trades = {}
            for i, tradepos in enumerate(closed_trades):
                all_trades[i] = strat.tradepos2dict(tradepos)
            fname = file_prefix + str(idx) + "_trades.csv"
            trades = pd.DataFrame.from_dict(all_trades).T
            trades.to_csv(fname)
            fname = file_prefix + str(idx) + "_dailydata.csv"
            ts.to_csv(fname)
    fname = file_prefix + "stats.csv"
    res = pd.DataFrame.from_dict(output)
    res.to_csv(fname)
    return
Example #5
0
def r_breaker( asset, start_date, end_date, scenarios, freqs, config):
    nearby  = config['nearby']
    rollrule = config['rollrule']
    start_d = misc.day_shift(start_date, '-1b')
    file_prefix = config['file_prefix'] + '_' + asset + '_'
    ddf = misc.nearby(asset, nearby, start_date, end_date, rollrule, 'd', need_shift=True)
    mdf = misc.nearby(asset, nearby, start_date, end_date, rollrule, 'm', need_shift=True)
    mdf = backtest.cleanup_mindata(mdf, asset)
    #ddf = dh.conv_ohlc_freq(mdf, 'D')
    output = {}
    for ix, freq in enumerate(freqs):
        if freq !='1min':
            df = dh.conv_ohlc_freq(mdf, freq)
        else:
            df = mdf
        for iy, k in enumerate(scenarios):
            idx = ix*10+iy
            config['k'] = k
            (res, closed_trades, ts) = r_breaker_sim( ddf, df, config)
            output[idx] = res
            print 'saving results for scen = %s' % str(idx)
            all_trades = {}
            for i, tradepos in enumerate(closed_trades):
                all_trades[i] = strat.tradepos2dict(tradepos)
            fname = file_prefix + str(idx) + '_trades.csv'
            trades = pd.DataFrame.from_dict(all_trades).T  
            trades.to_csv(fname)
            fname = file_prefix + str(idx) + '_dailydata.csv'
            ts.to_csv(fname)
    fname = file_prefix + 'stats.csv'
    res = pd.DataFrame.from_dict(output)
    res.to_csv(fname)
    return 
Example #6
0
def fisher_swing(asset, start_date, end_date, freqs, windows, config):
    nearby = config['nearby']
    rollrule = config['rollrule']
    file_prefix = config['file_prefix'] + '_' + asset + '_'
    df = misc.nearby(asset,
                     nearby,
                     start_date,
                     end_date,
                     rollrule,
                     'm',
                     need_shift=True)
    df = backtest.cleanup_mindata(df, asset)
    output = {}
    for ix, freq in enumerate(freqs):
        xdf = dh.conv_ohlc_freq(df, freq)
        for iy, win in enumerate(windows):
            idx = ix * 10 + iy
            config['win'] = win
            config['freq'] = freq
            (res, closed_trades, ts) = fisher_swing_sim(df, xdf, config)
            output[idx] = res
            print 'saving results for scen = %s' % str(idx)
            all_trades = {}
            for i, tradepos in enumerate(closed_trades):
                all_trades[i] = strat.tradepos2dict(tradepos)
            fname = file_prefix + str(idx) + '_trades.csv'
            trades = pd.DataFrame.from_dict(all_trades).T
            trades.to_csv(fname)
            fname = file_prefix + str(idx) + '_dailydata.csv'
            ts.to_csv(fname)
    fname = file_prefix + 'stats.csv'
    res = pd.DataFrame.from_dict(output)
    res.to_csv(fname)
    return
Example #7
0
def get_cont_data(asset,
                  start_date,
                  end_date,
                  freq='1m',
                  nearby=1,
                  rollrule='-10b'):
    if nearby == 0:
        mdf = mysqlaccess.load_min_data_to_df('fut_min',
                                              asset,
                                              start_date,
                                              end_date,
                                              minid_start=300,
                                              minid_end=2114,
                                              database='hist_data')
        mdf['contract'] = asset
    else:
        mdf = misc.nearby(asset,
                          nearby,
                          start_date,
                          end_date,
                          rollrule,
                          'm',
                          need_shift=True,
                          database='hist_data')
    mdf = backtest.cleanup_mindata(mdf, asset)
    xdf = dh.conv_ohlc_freq(mdf,
                            freq,
                            extra_cols=['contract'],
                            bar_func=dh.bar_conv_func2)
    return xdf
Example #8
0
def conv_db_htick2min(db_table,
                      inst_file,
                      out_table='hist_fut_min',
                      database='hist_data',
                      dstep=10):
    conf_dict = {}
    instIDs = []
    if inst_file == '':
        instIDs = get_col_dist_values(database + '.' + db_table, 'instID', {})
        conf_dict = {'instIDs': instIDs}
        try:
            inst_file = 'instID_file.json'
            with open(inst_file, 'w') as ofile:
                json.dump(conf_dict, ofile)
        except:
            pass
    else:
        with open(inst_file, 'r') as infile:
            conf_dict = json.load(infile)
        instIDs = conf_dict['instIDs']
    dbconfig = {
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'database': database
    }
    cnx = mysqlconn.connect(**dbconfig)
    for inst in instIDs:
        field_dict = {'instID': "\'" + inst + "\'"}
        datestr_list = get_col_dist_values(database + '.' + db_table, 'date',
                                           field_dict)
        mdata = pd.DataFrame()
        prod = misc.inst2product(inst)
        exch = misc.inst2exch(inst)
        num_run = (len(datestr_list) + dstep - 1) / dstep
        for idx in range(num_run):
            s_idx = idx * dstep
            e_idx = min((idx + 1) * dstep - 1, len(datestr_list) - 1)
            sdate = datetime.datetime.strptime(datestr_list[s_idx],
                                               "%Y-%m-%d").date()
            edate = datetime.datetime.strptime(datestr_list[e_idx],
                                               "%Y-%m-%d").date()
            df = load_hist_tick(db_table, inst, sdate, edate)
            mdf = conv_ohlc_freq(df, '1Min')
            mdf['min_id'] = (
                (mdf.index.hour + 6) % 24) * 100 + mdf.index.minute
            mdf = backtest.cleanup_mindata(mdf, prod)
            mdf.index.name = 'datetime'
            mdf['instID'] = inst
            mdf['exch'] = exch
            mdf = mdf.reset_index()
            mdf.set_index(['instID', 'exch', 'datetime'], inplace=True)
            mdf.to_sql(name=out_table,
                       flavor='mysql',
                       con=cnx,
                       if_exists='append')
            cnx.commit()
            print inst, sdate, edate, len(mdf)
    cnx.close()
    return
Example #9
0
 def prepare_data_env(self, inst, mid_day = True):
     if  self.instruments[inst].ptype == instrument.ProductType.Option:
         return
     if self.daily_data_days > 0 or mid_day:
         self.logger.debug('Updating historical daily data for %s' % self.scur_day.strftime('%Y-%m-%d'))
         daily_start = workdays.workday(self.scur_day, -self.daily_data_days, CHN_Holidays)
         daily_end = self.scur_day
         self.day_data[inst] = mysqlaccess.load_daily_data_to_df('fut_daily', inst, daily_start, daily_end)
         df = self.day_data[inst]
         if len(df) > 0:
             self.instruments[inst].price = df['close'][-1]
             self.instruments[inst].last_update = 0
             self.instruments[inst].prev_close = df['close'][-1]
             for fobj in self.day_data_func[inst]:
                 ts = fobj.sfunc(df)
                 df[ts.name]= pd.Series(ts, index=df.index)
     if self.min_data_days > 0 or mid_day:
         self.logger.debug('Updating historical min data for %s' % self.scur_day.strftime('%Y-%m-%d'))
         d_start = workdays.workday(self.scur_day, -self.min_data_days, CHN_Holidays)
         d_end = self.scur_day
         min_start = int(self.instruments[inst].start_tick_id/1000)
         min_end = int(self.instruments[inst].last_tick_id/1000)+1
         mindata = mysqlaccess.load_min_data_to_df('fut_min', inst, d_start, d_end, minid_start=min_start, minid_end=min_end, database = 'blueshale')
         mindata = backtest.cleanup_mindata(mindata, self.instruments[inst].product)
         self.min_data[inst][1] = mindata
         if len(mindata)>0:
             min_date = mindata.index[-1].date()
             if (len(self.day_data[inst].index)==0) or (min_date > self.day_data[inst].index[-1]):
                 ddf = data_handler.conv_ohlc_freq(mindata, 'd')
                 self.cur_day[inst]['open'] = float(ddf.open[-1])
                 self.cur_day[inst]['close'] = float(ddf.close[-1])
                 self.cur_day[inst]['high'] = float(ddf.high[-1])
                 self.cur_day[inst]['low'] = float(ddf.low[-1])
                 self.cur_day[inst]['volume'] = int(ddf.volume[-1])
                 self.cur_day[inst]['openInterest'] = int(ddf.openInterest[-1])
                 self.cur_min[inst]['datetime'] = pd.datetime(*mindata.index[-1].timetuple()[0:-3])
                 self.cur_min[inst]['open'] = float(mindata.ix[-1,'open'])
                 self.cur_min[inst]['close'] = float(mindata.ix[-1,'close'])
                 self.cur_min[inst]['high'] = float(mindata.ix[-1,'high'])
                 self.cur_min[inst]['low'] = float(mindata.ix[-1,'low'])
                 self.cur_min[inst]['volume'] = self.cur_day[inst]['volume']
                 self.cur_min[inst]['openInterest'] = self.cur_day[inst]['openInterest']
                 self.cur_min[inst]['min_id'] = int(mindata.ix[-1,'min_id'])
                 self.instruments[inst].price = float(mindata.ix[-1,'close'])
                 self.instruments[inst].last_update = 0
                 self.logger.debug('inst=%s tick data loaded for date=%s' % (inst, min_date))
             for m in self.min_data_func[inst]:
                 if m != 1:
                     self.min_data[inst][m] = data_handler.conv_ohlc_freq(self.min_data[inst][1], str(m)+'min')
                 df = self.min_data[inst][m]
                 for fobj in self.min_data_func[inst][m]:
                     ts = fobj.sfunc(df)
                     df[ts.name]= pd.Series(ts, index=df.index)
Example #10
0
def conv_db_htick2min(db_table, inst_file, out_table = 'hist_fut_min', database = 'hist_data', dstep = 10):
    conf_dict = {}
    instIDs = []
    if inst_file == '':
        instIDs =  get_col_dist_values(database + '.' + db_table, 'instID',{})
        conf_dict = {'instIDs': instIDs}
        try:
            inst_file = 'instID_file.json'
            with open(inst_file, 'w') as ofile:
                json.dump(conf_dict, ofile)
        except:
            pass
    else:
        with open(inst_file, 'r') as infile:
            conf_dict = json.load(infile)
        instIDs = conf_dict['instIDs']
    dbconfig = {'user': '******', 'password':'******', 'host':'localhost', 'database': database}
    cnx = mysqlconn.connect(**dbconfig)
    for inst in instIDs:
        field_dict = {'instID': "\'"+inst+"\'"}
        datestr_list = get_col_dist_values(database + '.' + db_table, 'date', field_dict)
        mdata = pd.DataFrame()
        prod = misc.inst2product(inst)
        exch = misc.inst2exch(inst)
        num_run = (len(datestr_list)+dstep-1)/dstep
        for idx in range(num_run):
            s_idx = idx * dstep
            e_idx = min((idx + 1) *dstep - 1, len(datestr_list)-1)
            sdate = datetime.datetime.strptime(datestr_list[s_idx], "%Y-%m-%d").date()
            edate = datetime.datetime.strptime(datestr_list[e_idx], "%Y-%m-%d").date()
            df = load_hist_tick(db_table, inst, sdate, edate)
            mdf = conv_ohlc_freq(df, '1Min')
            mdf['min_id'] =  ((mdf.index.hour + 6) % 24) * 100 + mdf.index.minute
            mdf = backtest.cleanup_mindata(mdf, prod)
            mdf.index.name = 'datetime'
            mdf['instID'] = inst
            mdf['exch'] = exch
            mdf = mdf.reset_index()
            mdf.set_index(['instID', 'exch', 'datetime'], inplace = True)
            mdf.to_sql(name = out_table, flavor = 'mysql', con = cnx, if_exists='append')
            cnx.commit()
            print inst, sdate, edate, len(mdf)
    cnx.close()
    return
Example #11
0
 def prepare_data_env(self, inst, mid_day=True):
     if self.instruments[inst].ptype == instrument.ProductType.Option:
         return
     if self.daily_data_days > 0 or mid_day:
         #self.logger.debug('Updating historical daily data for %s' % self.scur_day.strftime('%Y-%m-%d'))
         daily_start = workdays.workday(self.scur_day,
                                        -self.daily_data_days, CHN_Holidays)
         daily_end = self.scur_day
         ddf = mysqlaccess.load_daily_data_to_df('fut_daily',
                                                 inst,
                                                 daily_start,
                                                 daily_end,
                                                 index_col=None)
         if len(ddf) > 0:
             self.instruments[inst].price = ddf['close'].iloc[-1]
             self.instruments[inst].last_update = 0
             self.instruments[inst].prev_close = ddf['close'].iloc[-1]
             for fobj in self.day_data_func[inst]:
                 ts = fobj.sfunc(ddf)
                 if type(ts).__name__ == 'Series':
                     if ts.name in ddf.columns:
                         self.logger.warning(
                             'TimeSeries name %s is already in the columns for inst = %s'
                             % (ts.name, inst))
                     ddf[ts.name] = ts
                 elif type(ts).__name__ == 'DataFrame':
                     for col_name in ts.columns:
                         if col_name in ddf.columns:
                             self.logger.warning(
                                 'TimeSeries name %s is already in the columns for inst = %s'
                                 % (col_name, inst))
                         ddf[col_name] = ts[col_name]
         self.day_data[inst] = data_handler.DynamicRecArray(dataframe=ddf)
     if self.min_data_days > 0 or mid_day:
         #self.logger.debug('Updating historical min data for %s' % self.scur_day.strftime('%Y-%m-%d'))
         d_start = workdays.workday(self.scur_day, -self.min_data_days,
                                    CHN_Holidays)
         d_end = self.scur_day
         min_start = int(self.instruments[inst].start_tick_id / 1000)
         min_end = int(self.instruments[inst].last_tick_id / 1000) + 1
         mdf = mysqlaccess.load_min_data_to_df('fut_min',
                                               inst,
                                               d_start,
                                               d_end,
                                               minid_start=min_start,
                                               minid_end=min_end,
                                               database='blueshale',
                                               index_col=None)
         mdf = backtest.cleanup_mindata(mdf,
                                        self.instruments[inst].product,
                                        index_col=None)
         mdf['bar_id'] = self.conv_bar_id(mdf['min_id'], inst)
         if len(mdf) > 0:
             min_date = mdf['date'].iloc[-1]
             if (len(self.day_data[inst]) == 0) or (
                     min_date > self.day_data[inst].data['date'][-1]):
                 ddf = data_handler.conv_ohlc_freq(mdf, 'd', index_col=None)
                 self.cur_day[inst]['open'] = float(ddf.open[-1])
                 self.cur_day[inst]['close'] = float(ddf.close[-1])
                 self.cur_day[inst]['high'] = float(ddf.high[-1])
                 self.cur_day[inst]['low'] = float(ddf.low[-1])
                 self.cur_day[inst]['volume'] = int(ddf.volume[-1])
                 self.cur_day[inst]['openInterest'] = int(
                     ddf.openInterest[-1])
                 self.cur_min[inst]['datetime'] = pd.datetime(
                     *mdf['datetime'].iloc[-1].timetuple()[0:-3])
                 self.cur_min[inst]['date'] = mdf['date'].iloc[-1]
                 self.cur_min[inst]['open'] = float(mdf['open'].iloc[-1])
                 self.cur_min[inst]['close'] = float(mdf['close'].iloc[-1])
                 self.cur_min[inst]['high'] = float(mdf['high'].iloc[-1])
                 self.cur_min[inst]['low'] = float(mdf['low'].iloc[-1])
                 self.cur_min[inst]['volume'] = self.cur_day[inst]['volume']
                 self.cur_min[inst]['openInterest'] = self.cur_day[inst][
                     'openInterest']
                 self.cur_min[inst]['min_id'] = int(mdf['min_id'].iloc[-1])
                 self.cur_min[inst]['bar_id'] = self.conv_bar_id(
                     self.cur_min[inst]['min_id'], inst)
                 self.instruments[inst].price = float(mdf['close'].iloc[-1])
                 self.instruments[inst].last_update = 0
                 #self.logger.debug('inst=%s tick data loaded for date=%s' % (inst, min_date))
             if 1 not in self.min_data_func[inst]:
                 self.min_data[inst][1] = data_handler.DynamicRecArray(
                     dataframe=mdf)
             for m in sorted(self.min_data_func[inst]):
                 if m != 1:
                     bar_func = lambda ts: self.conv_bar_id(ts, inst)
                     mdf_m = data_handler.conv_ohlc_freq(
                         mdf,
                         str(m) + 'min',
                         index_col=None,
                         bar_func=bar_func,
                         extra_cols=['bar_id'])
                 else:
                     mdf_m = mdf
                 for fobj in self.min_data_func[inst][m]:
                     ts = fobj.sfunc(mdf_m)
                     if type(ts).__name__ == 'Series':
                         if ts.name in mdf_m.columns:
                             self.logger.warning(
                                 'TimeSeries name %s is already in the columns for inst = %s'
                                 % (ts.name, inst))
                         mdf_m[ts.name] = ts
                     elif type(ts).__name__ == 'DataFrame':
                         for col_name in ts.columns:
                             if col_name in mdf_m.columns:
                                 self.logger.warning(
                                     'TimeSeries name %s is already in the columns for inst = %s'
                                     % (col_name, inst))
                             mdf_m[col_name] = ts[col_name]
                 self.min_data[inst][m] = data_handler.DynamicRecArray(
                     dataframe=mdf_m)
Example #12
0
 def prepare_data_env(self, inst, mid_day=True):
     if self.instruments[inst].ptype == instrument.ProductType.Option:
         return
     if self.daily_data_days > 0 or mid_day:
         self.logger.debug('Updating historical daily data for %s' %
                           self.scur_day.strftime('%Y-%m-%d'))
         daily_start = workdays.workday(self.scur_day,
                                        -self.daily_data_days, CHN_Holidays)
         daily_end = self.scur_day
         self.day_data[inst] = mysqlaccess.load_daily_data_to_df(
             'fut_daily', inst, daily_start, daily_end)
         df = self.day_data[inst]
         print inst
         if len(df) > 0:
             self.instruments[inst].price = df['close'][-1]
             self.instruments[inst].last_update = 0
             self.instruments[inst].prev_close = df['close'][-1]
             for fobj in self.day_data_func[inst]:
                 ts = fobj.sfunc(df)
                 df[ts.name] = pd.Series(ts, index=df.index)
     if self.min_data_days > 0 or mid_day:
         self.logger.debug('Updating historical min data for %s' %
                           self.scur_day.strftime('%Y-%m-%d'))
         d_start = workdays.workday(self.scur_day, -self.min_data_days,
                                    CHN_Holidays)
         d_end = self.scur_day
         min_start = int(self.instruments[inst].start_tick_id / 1000)
         min_end = int(self.instruments[inst].last_tick_id / 1000) + 1
         mindata = mysqlaccess.load_min_data_to_df('fut_min',
                                                   inst,
                                                   d_start,
                                                   d_end,
                                                   minid_start=min_start,
                                                   minid_end=min_end,
                                                   database='blueshale')
         mindata = backtest.cleanup_mindata(mindata,
                                            self.instruments[inst].product)
         self.min_data[inst][1] = mindata
         if len(mindata) > 0:
             min_date = mindata.index[-1].date()
             if (len(self.day_data[inst].index)
                     == 0) or (min_date > self.day_data[inst].index[-1]):
                 ddf = data_handler.conv_ohlc_freq(mindata, 'd')
                 self.cur_day[inst]['open'] = float(ddf.open[-1])
                 self.cur_day[inst]['close'] = float(ddf.close[-1])
                 self.cur_day[inst]['high'] = float(ddf.high[-1])
                 self.cur_day[inst]['low'] = float(ddf.low[-1])
                 self.cur_day[inst]['volume'] = int(ddf.volume[-1])
                 self.cur_day[inst]['openInterest'] = int(
                     ddf.openInterest[-1])
                 self.cur_min[inst]['datetime'] = pd.datetime(
                     *mindata.index[-1].timetuple()[0:-3])
                 self.cur_min[inst]['open'] = float(mindata.ix[-1, 'open'])
                 self.cur_min[inst]['close'] = float(mindata.ix[-1,
                                                                'close'])
                 self.cur_min[inst]['high'] = float(mindata.ix[-1, 'high'])
                 self.cur_min[inst]['low'] = float(mindata.ix[-1, 'low'])
                 self.cur_min[inst]['volume'] = self.cur_day[inst]['volume']
                 self.cur_min[inst]['openInterest'] = self.cur_day[inst][
                     'openInterest']
                 self.cur_min[inst]['min_id'] = int(mindata.ix[-1,
                                                               'min_id'])
                 self.instruments[inst].price = float(mindata.ix[-1,
                                                                 'close'])
                 self.instruments[inst].last_update = 0
                 self.logger.debug('inst=%s tick data loaded for date=%s' %
                                   (inst, min_date))
             for m in self.min_data_func[inst]:
                 if m != 1:
                     self.min_data[inst][m] = data_handler.conv_ohlc_freq(
                         self.min_data[inst][1],
                         str(m) + 'min')
                 df = self.min_data[inst][m]
                 for fobj in self.min_data_func[inst][m]:
                     ts = fobj.sfunc(df)
                     df[ts.name] = pd.Series(ts, index=df.index)
Example #13
0
def hist_realized_vol_by_product(prodcode,
                                 start_d,
                                 end_d,
                                 periods=12,
                                 tenor='-1m',
                                 writeDB=False):
    cont_mth, exch = mysqlaccess.prod_main_cont_exch(prodcode)
    contlist = contract_range(prodcode, exch, cont_mth, start_d, end_d)
    exp_dates = [get_opt_expiry(cont, inst2contmth(cont)) for cont in contlist]
    data = {
        'is_dtime': True,
        'data_column': 'close',
        'data_freq': '30min',
        'xs': [0.5, 0.25, 0.75],
        'xs_names': ['atm', 'v25', 'v75'],
        'xs_func': 'bs_delta_to_ratio',
        'rehedge_period': 1,
        'term_tenor': tenor,
        'database': 'hist_data'
    }
    option_input = {
        'otype': True,
        'rd': 0.0,
        'rf': 0.0,
        'end_vol': 0.0,
        'ref_vol': 0.5,
        'pricer_func': 'bsopt.BSOpt',
        'delta_func': 'bsopt.BSDelta',
        'is_dtime': data['is_dtime'],
    }
    freq = data['data_freq']
    for cont, expiry in zip(contlist, exp_dates):
        expiry_d = expiry.date()
        if expiry_d > end_d:
            break
        p_str = '-' + str(int(tenor[1:-1]) * periods) + tenor[-1]
        d_start = day_shift(expiry_d, p_str)
        if freq == 'd':
            df = mysqlaccess.load_daily_data_to_df('fut_daily',
                                                   cont,
                                                   d_start,
                                                   expiry_d,
                                                   database=data['database'],
                                                   index_col=None)
        else:
            mdf = mysqlaccess.load_min_data_to_df('fut_min',
                                                  cont,
                                                  d_start,
                                                  expiry_d,
                                                  minid_start=300,
                                                  minid_end=2115,
                                                  database=data['database'],
                                                  index_col=None)
            mdf = backtest.cleanup_mindata(mdf, prodcode, index_col=None)
            mdf['bar_id'] = dh.bar_conv_func2(mdf['min_id'])
            df = dh.conv_ohlc_freq(mdf,
                                   freq,
                                   bar_func=dh.bar_conv_func2,
                                   extra_cols=['bar_id'],
                                   index_col=None)
        option_input['expiry'] = expiry
        data['dataframe'] = df
        vol_df = realized_termstruct(option_input, data)
        print cont, expiry_d, vol_df