Пример #1
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)
Пример #2
0
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)
Пример #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
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)
Пример #5
0
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)
Пример #6
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)))