Exemple #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)
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)
Exemple #3
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)
Exemple #4
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)