コード例 #1
0
ファイル: run_port_selector.py プロジェクト: ajmal017/Genesis
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--sharpe', help='minimum sharpe', default=1)
    parser.add_argument('-t', '--train_result', default='SM0_21')
    parser.add_argument('-p', '--portfolio_name', default='smin1')
    args = parser.parse_args()

    sharpe = float(args.sharpe)
    train_result_dir = os.path.join(CommonDir.train_output_dir,
                                    args.train_result)

    instMaster = InstMaster()
    json_ = instMaster.getInstrumentJson()
    selected_inst_json = []
    for inst in json_['instruments']:
        symbol = inst['canonical_name']

        fp = os.path.join(train_result_dir, symbol, 'optimize', 'final',
                          'strategy.csv')
        if os.path.exists(fp) == False:
            continue

        stgy_df = pd.read_csv(fp)
        if stgy_df['sharpe'].iloc[0] >= sharpe:
            inst['pos'] = 0
            inst['order_size'] = 1
            inst['pnl'] = 0
            inst['bounds'] = [1, 30]
            selected_inst_json.append(inst)
            print(fp)

    if len(selected_inst_json) > 0:
        instMaster.writePortfolioJson(args.portfolio_name, selected_inst_json)
コード例 #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-a',
                        '--max_allocation',
                        help='max allocation',
                        default=10000000)
    parser.add_argument('-j', '--pname', default='smin0')
    parser.add_argument('--tag', default='SM0_21')
    args = parser.parse_args()

    date = '2021-01-07'
    max_allocation = args.max_allocation
    pname = args.pname
    instMaster = InstMaster()
    inst_json = instMaster.getInstrumentJson()

    inst_update = []
    for idx, inst in enumerate(inst_json[pname]):
        print(inst['symbol'])
        ss_df = pdr.get_data_yahoo(inst['symbol'], start=date, end=date)
        ss_df['d'] = ss_df.index
        ss_df = ss_df[ss_df['d'] == date]
        lprice = ss_df['Close'].iloc[0]
        max_alloc = int(max_allocation / lprice)
        inst['bounds'] = [1, max_alloc]
        inst_update.append(inst)

    inst_json[pname] = inst_update
    print(json.dumps(inst_json, indent=2))
    instMaster.writePortfolioJson(pname, inst_json)
コード例 #3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--type', help='intra, inter', default='inter')
    parser.add_argument('-i',
                        '--intruments',
                        help='instruments_kospi200, instruments_sp500',
                        default='instruments_kospi200')
    args = parser.parse_args()

    output_dir = '/home/smin/StockEngine/data'
    start_date = '2016-01-01'
    end_date = '2020-12-25'
    instMaster = InstMaster(args.type, args.instruments)
    inst_json = instMaster.getInstrumentJson()

    if instMaster.type() == 0:
        for inst in inst_json['instruments']:
            ss_df = pdr.get_data_yahoo(inst['symbol'],
                                       start=start_date,
                                       end=end_date)
            fp = '{}.csv'.format(
                os.path.join(output_dir, inst['canonical_name']))
            ss_df.to_csv(fp)
            print(fp)
    else:
        for inst in inst_json['instruments']:
            symbol_ = inst['symbol']
            ts = TimeSeries(key='YOUR_API_KEY', output_format='pandas')
            data, meta_data = ts.get_intraday(symbol=symbol_,
                                              interval='1min',
                                              outputsize='full')
            os.makedirs(
                os.path.join(CommonDir.sampled_us_dir, '{}'.format(symbol_)))

            data.to_csv(symbol and date.csv)
コード例 #4
0
ファイル: run_portfolio.py プロジェクト: ajmal017/Genesis
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        '--tradingPeriod',
                        help='tradingPeriod',
                        default=126)
    parser.add_argument('-t', '--sim_type', default=1)
    parser.add_argument('-b', '--begin_date', default='2010-01-01')
    parser.add_argument('-e', '--end_date', default='2020-12-01')
    parser.add_argument('-j', '--pname', default='instruments')
    parser.add_argument('--tag', default='SM0_21')
    args = parser.parse_args()

    sim_output = CommonDir.sim_output_dir
    portfolio_config_dir = CommonDir.train2_output_dir
    trading_period = int(args.tradingPeriod)
    begin_date = args.begin_date
    end_date = args.end_date
    sim_type = int(args.sim_type)
    prtf_name = args.pname

    tickers = InstMaster().getTickerList(prtf_name)
    result_dfs = pd.DataFrame()
    result_detail_df = pd.DataFrame()
    for train_dir in tickers:
        config_fp = os.path.join(portfolio_config_dir, args.tag, train_dir,
                                 'optimize', 'final', 'strategy.csv')
        if os.path.exists(config_fp) == False:
            continue

        symbol = train_dir
        data_frame_df = pd.read_csv(
            os.path.join(CommonDir.sampled_dir, '{}.csv'.format(symbol)))
        sim = Simulator(data_frame_df, config_fp, begin_date, end_date, symbol,
                        trading_period, sim_type)
        result_df, rdfs = sim.run()

        if result_df.shape[0] > 0:
            result_dfs = result_dfs.append(result_df)
            result_detail_df = result_detail_df.append(rdfs)
        else:
            print('{} has no result '.format(train_dir))

    summary_df = summary_portfolio(result_detail_df, prtf_name, trading_period)
    summary_df.to_csv(
        os.path.join(
            sim_output,
            'summary_{}_{}_{}.csv'.format(prtf_name,
                                          begin_date.replace('-', ''),
                                          end_date.replace('-', ''))))
    result_dfs.to_csv(
        os.path.join(
            sim_output, 'sim_{}_{}_{}.csv'.format(prtf_name,
                                                  begin_date.replace('-', ''),
                                                  end_date.replace('-', ''))))

    display(summary_df)
    display(result_dfs)
コード例 #5
0
ファイル: update_portfolio.py プロジェクト: ajmal017/Genesis
def updateBuy(pname, ticker, qty, price, date, cprice):
    instMaster = InstMaster()
    portfolio_json = instMaster.getInstrumentJson()[pname]
    
    ticker_idx = 0
    for idx, s in enumerate(portfolio_json):
        if s['symbol'] == ticker:
            ticker_idx = idx
            break
    
    t_json = portfolio_json[ticker_idx]
#     portfolio_json[ticker_idx]['pnl'] = t_json['pnl'] + (cprice - price)* qty if 'pnl' in t_json else (cprice - price)* qty  
    portfolio_json[ticker_idx]['pos'] = t_json['pos'] + qty if 'pos' in t_json else qty
    orders = t_json['orders'] if 'orders' in t_json else []
    o = { 'price':price, 'qty':qty, 'date':date }
    orders .append(o)
    portfolio_json[ticker_idx]['orders'] = orders
    instMaster.writePortfolioJson(pname, portfolio_json)
コード例 #6
0
ファイル: run_trainer.py プロジェクト: ajmal017/Genesis
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-j',
        '--train_config',
        help='training config json',
        default=
        '/home/smin/eclipse-workspace/IntraStrategy/models/US_model.json')
    args = parser.parse_args()

    if os.path.exists(args.train_config) == False:
        print('{} does not exist'.format(args.train_config))
        return

    json_ = ReadFile.read_json(args.train_config)
    tradingPeriod = int(json_['period'])
    sim_type = int(json_['sim_type'])
    begin_date = json_['begin_date']
    end_date = json_['end_date']
    intra_trading = not json_['daily']
    train_output_dir = CommonDir.train_output_dir
    if sim_type == 1:
        train_output_dir = CommonDir.train2_output_dir

    train_output_dir = os.path.join(train_output_dir, json_['output_dir'])
    instMaster = InstMaster(json_['daily'], json_['instruments'])
    cnt = multiprocessing.cpu_count() - 3
    symbols = instMaster.getInstNames()

    symbols = ['testdata']  #TODO testing
    sampled_dir = '/home/smin/dataDir'  #TODO sampling directory

    for s in symbols:
        run_simOptimizer(os.path.join(sampled_dir, s),
                         os.path.join(train_output_dir,
                                      '{}'.format(s)), tradingPeriod, s,
                         sim_type, begin_date, end_date, json_['obj_func'],
                         copy.deepcopy(json_), intra_trading)
    print('end of optimization')
コード例 #7
0
ファイル: update_portfolio.py プロジェクト: ajmal017/Genesis
def updateSell(pname, ticker, qty, price, date, cprice):
    instMaster = InstMaster()
    portfolio_json = instMaster.getInstrumentJson()[pname]

    ticker_idx = 0
    for idx, s in enumerate(portfolio_json):
        if s['symbol'] == ticker:
            ticker_idx = idx
            break
    
    t_json = portfolio_json[ticker_idx]
#     orders = sorted(t_json['orders'], key=lambda k:k['price'], reverse=True) # sort worst
    orders = sorted(t_json['orders'], key=lambda k:k['price'])  # sort best 
    portfolio_json[ticker_idx]['pos'] = t_json['pos'] - qty
    for idx, o in enumerate(orders):
        portfolio_json[ticker_idx]['pnl'] = t_json['pnl'] + (price - o['price'])* o['qty'] if 'pnl' in t_json else (price - o['price'])* o['qty'] 
        qty = qty - o['qty']
        orders[idx]['qty'] = 0
        if qty == 0:
            break
        
    portfolio_json[ticker_idx]['orders'] = [ o for o in orders if o['qty'] > 0 ]
    instMaster.writePortfolioJson(pname, portfolio_json)
コード例 #8
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-d',
                        '--trd_date',
                        help='trading_date',
                        default='2021-01-15')
    parser.add_argument('-j', '--pname', default='smin0')
    parser.add_argument('--tag', default='SM0_21')
    args = parser.parse_args()

    date = args.trd_date
    pname = args.pname
    instMaster = InstMaster()
    inst_json = instMaster.getInstrumentJson()
    portfolio_config_dir = os.path.join(CommonDir.train2_output_dir, args.tag)

    today_orders = pd.DataFrame(columns=[
        'date', 'type', 'ticker', 'ticker_id', 'is_buy', 'price', 'qty',
        'start_price'
    ])

    for inst in inst_json[pname]:
        ticker = inst['canonical_name']
        ss_df = pdr.get_data_yahoo(inst['symbol'], start=date, end=date)
        ss_df['d'] = ss_df.index
        ss_df = ss_df[ss_df['d'] == date]
        config_fp = os.path.join(portfolio_config_dir, ticker, 'optimize',
                                 'final', 'strategy.csv')
        if os.path.exists(config_fp) == False:
            continue
        config_df = pd.read_csv(config_fp)

        c_edge = config_df['c_edge'].iloc[0]
        s_edge = config_df['s_edge'].iloc[0]
        f_edge = config_df['f_edge'].iloc[0]
        pos = inst['pos'] if 'pos' in inst else 0
        orders = inst['orders'] if 'orders' in inst else []
        realized_pnl = inst['pnl'] if 'pnl' in inst else 0
        order_size = inst['order_size'] if 'order_size' in inst else 1
        lprice = ss_df['Close'].iloc[0]

        if pos == 0:
            propBid = lprice - s_edge * instMaster.getTickSize(lprice)
            propAsk = propBid + c_edge * instMaster.getTickSize(lprice)
            today_orders.loc[today_orders.shape[0]] = [
                date, 'normal', inst['symbol'], inst['canonical_name'], True,
                propBid, order_size, lprice
            ]
            today_orders.loc[today_orders.shape[0]] = [
                date, 'stop', inst['symbol'], inst['canonical_name'], False,
                propAsk, order_size, lprice
            ]
        else:
            cov_notion = 0
            cov_qty = 0
            low_price = np.NAN
            for o in inst['orders']:
                cov_qty = cov_qty + o['qty']
                cov_notion = cov_notion + o['qty'] * o['price']
                if np.isnan(low_price) or low_price > o['price']:
                    low_price = o['price']

            propAsk = cov_notion / cov_qty + c_edge * instMaster.getTickSize(
                lprice)
            buy_price = low_price - f_edge * instMaster.getTickSize(lprice)
            propBid = lprice if buy_price > lprice else buy_price
            today_orders.loc[today_orders.shape[0]] = [
                date, 'normal', inst['symbol'], inst['canonical_name'], True,
                propBid, inst['order_size'], lprice
            ]
            today_orders.loc[today_orders.shape[0]] = [
                date, 'stop', inst['symbol'], inst['canonical_name'], False,
                propAsk, o['qty'], lprice
            ]

    display(today_orders)
    today_orders.to_csv(
        os.path.join(CommonDir.trading_dir, '{}.{}.csv'.format(pname, date)))