count = 0 def status(msg, order): global TMX global endTime totalDelta = 0.0 totalVega = 0.0 for key in msg['trader_state']['positions']: quant = msg['trader_state']['positions'][key] print key, quant try: K = int(key[1:-1]) except: continue v = vols[K] if vols[K] == 0.0 or vols[K] == None: v = 20 c = mibian.BS([TMX, K, 0.0, (endTime-time.time())/15.0], volatility = v) type = key[-1] if type == 'P': totalDelta += (c.putDelta)*quant else: totalDelta += (c.callDelta)*quant totalVega += c.vega*quant print 'Total delta: ' + str(totalDelta*100) print 'Total vega: ' + str(totalVega*100) t.onMarketUpdate = upd t.onTraderUpdate = status t.run()
} pending_orders.append(new_order) except: print('Unable to parse headline: Unknown error') DEBUG = True algo_bot = None if len(sys.argv) >= 4: algo_bot = TradersBot(host=sys.argv[1], id=sys.argv[2], password=sys.argv[3]) # DEBUG = False CANCEL_TRADES = False else: algo_bot = TradersBot('127.0.0.1', 'trader0', 'trader0') algo_bot.onAckRegister = onAckRegister algo_bot.onMarketUpdate = onMarketUpdate algo_bot.onTraderUpdate = onTraderUpdate algo_bot.onTrade = onTrade algo_bot.onAckModifyOrders = onAckModifyOrders algo_bot.onNews = onNews if not DEBUG: def f(*args): return None print = f algo_bot.run()
def trade_method(msg, order): global MARKET print(MARKET['T85C']['price']) trade_dict = msg['trades'] for trade in trade_dict: security = MARKET[trade["ticker"]] security['price'] = trade["price"] # security['vol'] = calc_vol(security['type'] == 'C', trade['price'], 100, security['strike'], exp_time(), INTEREST_RATE) # Buys or sells in a random quantity every time it gets an update # You do not need to buy/sell here def trader_update_method(msg, order): global MARKET positions = msg['trader_state']['positions'] for security in positions.keys(): if random.random() < 0.5: quant = 10*random.randint(1, 10) order.addBuy(security, quantity=quant,price=MARKET[security]['price']) else: quant = 10*random.randint(1, 10) order.addSell(security, quantity=quant,price=MARKET[security]['price']) t.onAckRegister = ack_register_method t.onMarketUpdate = market_update_method t.onTraderUpdate = trader_update_method t.onTrade = trade_method #t.onAckModifyOrders = ack_modify_orders_method #t.onNews = news_method t.run()
elif ticker == 'USDJPY': if len(price_list['EURJPY'])>0: val = 1/price_list['EURUSD'][-1]*price_list['EURJPY'][-1] if val<(1-5*dx)*price: return {'trade':'yes','USDJPY':('sell',amount(val,price)),'EURUSD':('sell',amount(val,price)),'EURJPY':('buy',amount(val,price))} elif val>(1+5*dx)*price: return {'trade':'yes','USDJPY':('buy',amount(val,price)),'EURUSD':('buy',amount(val,price)),'EURJPY':('sell',amount(val,price))} if len(price_list['CHFJPY'])>0: val = price_list['USDCHF'][-1]*price_list['CHFJPY'][-1] if val<(1-5*dx)*price: return {'trade':'yes','USDJPY':('sell',amount(val,price)),'USDCHF':('buy',amount(val,price)),'CHFJPY':('buy',amount(val,price))} elif val>(1+5*dx)*price: return {'trade':'yes','USDJPY':('buy',amount(val,price)),'USDCHF':('sell',amount(val,price)),'CHFJPY':('sell',amount(val,price))} return {'trade':'no'} def amount(val, price): amt = abs(val-price)/price amt *= (20/(5*dx)) return floor(amt) t.onMarketUpdate = on_update t.onTrade = trade t.onTraderUpdate = get_info t.run()
counter += 1 print('Orders cleared!') time_last_clear = time.time() pnl_deltas.append((msg['trader_state']['default_pnl'] - current_pnl)) pnl_deltas = pnl_deltas[1:] total_delta = sum(pnl_deltas) current_pnl = msg['trader_state']['default_pnl'] print(current_pnl) if DELTASHIFT == 1 and time.time() - time_last_threshold_update > 5: # if total_delta < 25 and total_delta > -100: # # not making enough money, narrow in # arb_threshold *= 0.95 # print('THRESHOLD CHANGE: ' + str(arb_threshold)) # time_last_threshold_update = time.time() if total_delta < -100: # losing too much money, push out arb_threshold *= 1.1 print('THRESHOLD CHANGE: ' + str(arb_threshold)) time_last_threshold_update = time.time() t.onTraderUpdate = adjust_params ## onTrade def update_orderbook(msg, TradersOrder): global orderbook ### RUN BOT t.run()
def black_scholes_call(S, K, t, T, r, sigma): d1 = np.log(S / K) + (r + .5 * sigma**2) * (T - t) d1 = d1 / (sigma * np.sqrt(T - t)) d2 = d1 - sigma * np.sqrt(T - t) return stats.norm.cdf(d1) * S - stats.norm.cdf(d2) * K * np.exp(-1 * r * (T - t)) def black_scholes_put(S, K, t, T, r, sigma): return black_scholes_call(S, K, t, T, r, sigma) + K * np.exp(-1 * r * (T - t)) - S # S = 100 # K = 100 # sigma = .2 # r = .01 # flag = 'p' # t = .5 # print(black_scholes_put(S,K,t,1,r,sigma)) # p = black_scholes.black_scholes(flag,S,K,t,r,sigma) # print(p) t.onMarketUpdate = on_update t.onTrade = trade t.onTraderUpdate = curr_pos t.run()