def microtrade(self, ticker, action, price, session,vol):
     book = api_calls.get_trading_data(session, ticker)
     #if we want to sell, check is buying price has stayed same or gone higher
     quantity = 0
     if action == "SELL":
         if price >= book['bids'][0]['price']:
             bid_price = book['bids'][0]['price']
             for i in book['bids']:
                 if i['price'] == bid_price:
                     quantity += (i['quantity'] - i['quantity_filled'])
             if quantity >=25000:
                 quantity = 25000
             resp = api_calls.send_order(session, ticker, min(quantity,abs(vol)), "LIMIT", "SELL", bid_price)
             if resp != -1:
                 return resp['order_id']
     elif action == "BUY":
         if price <= book['asks'][0]['price']:
             ask_price = book['asks'][0]['price']
             for i in book['asks']:
                 if i['price'] == ask_price:
                     quantity = i['quantity'] - i['quantity_filled']
                 if quantity >=25000:
                     quantity = 25000
             resp = api_calls.send_order(session, ticker, min(quantity,abs(vol)), "LIMIT", "BUY", ask_price)
             if resp != -1:
                 return resp['order_id']
 def unwind_handler(self, tick, session):
     print('hit unwind_handler')
     print(self.data)
     ticker_price = {'TAME': -1, 'CRZY': -1}
     #first sell required amount to clear in required time
     for ticker in self.tickers:
         book = api_calls.get_trading_data(session, ticker)
         if self.data[ticker]["time_to_sell"] != 0 and (
                 self.trade_limit[ticker] == False):
             amount_required = self.data[ticker][
                 'current_stock'] // self.data[ticker]["time_to_sell"]
             print("Amount Required", amount_required)
             if amount_required > 0:
                 api_calls.send_order(session, ticker, abs(amount_required),
                                      "MARKET", "SELL", 0)
                 self.data[ticker]["time_to_sell"] -= 1
                 self.data[ticker]['current_stock'] -= amount_required
                 ticker_price[ticker] = book['bids'][0]['price']
             elif amount_required < 0:
                 api_calls.send_order(session, ticker, abs(amount_required),
                                      "MARKET", "BUY", 0)
                 self.data[ticker]["time_to_sell"] -= 1
                 self.data[ticker]['current_stock'] -= amount_required
                 ticker_price[ticker] = book['asks'][0]['price']
         else:
             pass
     for ticker in self.tickers:
         if self.data[ticker]["time_to_sell"] != 0 and (
                 self.trade_limit[ticker] == False):
             curr_stock = self.data[ticker]['current_stock']
             if curr_stock > 0:
                 amount = self.microtrade(ticker, "SELL",
                                          self.data[ticker]['tender_price'],
                                          session, curr_stock,
                                          ticker_price[ticker])
                 if amount != None:
                     print('SOLD additional', amount)
                     print("prev time", self.data[ticker]['time_to_sell'])
                     self.data[ticker]['time_to_sell'] = (
                         (curr_stock - amount) /
                         curr_stock) * self.data[ticker]['time_to_sell']
             elif curr_stock < 0:
                 amount = self.microtrade(ticker, "BUY",
                                          self.data[ticker]['tender_price'],
                                          session, curr_stock,
                                          ticker_price[ticker])
                 print('SOLD additional', amount)
                 print("prev time", self.data[ticker]['time_to_sell'])
                 if amount != None:
                     print('SOLD additional', amount)
                     print("prev time", self.data[ticker]['time_to_sell'])
                     self.data[ticker]['time_to_sell'] = (
                         (curr_stock - amount) /
                         curr_stock) * self.data[ticker]['time_to_sell']
         else:
             pass
    def microtrade(self, ticker, action, price, session, vol, prev_price):
        book = api_calls.get_trading_data(session, ticker)
        #if we want to sell, check is buying price has stayed same or gone higher
        quantity = 0
        if action == "SELL":
            if price >= book['bids'][0]['price']:
                print('additional scalp')
                bid_price = book['bids'][0]['price']
                for i in book['bids']:
                    if i['price'] == bid_price:
                        quantity += (i['quantity'] - i['quantity_filled'])
                if ticker == "TAME":
                    if quantity >= 10000:
                        quantity = 10000
                elif ticker == "CRZY":
                    if quantity >= 25000:
                        quantity = 25000
                o_id = api_calls.send_order(session, ticker,
                                            min(quantity, abs(vol)), "LIMIT",
                                            "SELL", bid_price)['order_id']
                order_exist = False
                while (order_exist == False):
                    order_exist = api_calls.order_exists(session, o_id)
                if api_calls.is_filled(session, o_id):
                    return min(quantity, abs(vol))
                else:
                    api_calls.cancel_order(session, o_id)
                    return 0

        elif action == "BUY":
            if price <= book['asks'][0]['price']:
                print('additional scalp')
                ask_price = book['asks'][0]['price']
                for i in book['asks']:
                    if i['price'] == ask_price:
                        quantity = i['quantity'] - i['quantity_filled']
                    if ticker == "TAME":
                        if quantity >= 10000:
                            quantity = 10000
                    elif ticker == "CRZY":
                        if quantity >= 25000:
                            quantity = 25000
                o_id = api_calls.send_order(session, ticker,
                                            min(quantity, abs(vol)), "LIMIT",
                                            "BUY", ask_price)
                order_exist = False
                while (order_exist == False):
                    order_exist = api_calls.order_exists(session, o_id)
                if api_calls.is_filled(session, o_id):
                    return min(quantity, abs(vol))
                else:
                    api_calls.cancel_order(session, o_id)
                    return 0
 def unwind_handler(self, tick, session):
     print('hit unwind_handler')
     print(self.data)
     #first sell required amount to clear in required time
     for ticker in self.tickers:
         if self.data[ticker]["time_to_sell"]!=0 and (self.trade_limit[ticker]==False):
             amount_required = self.data[ticker]['current_stock']//self.data[ticker]["time_to_sell"]
             print("Amount Required", amount_required)
             if amount_required>0:
                 api_calls.send_order(session, ticker, abs(amount_required), "MARKET", "SELL", 0)
                 self.data[ticker]["time_to_sell"] -=1
             elif amount_required<0:
                 api_calls.send_order(session, ticker, abs(amount_required), "MARKET", "BUY", 0)
                 self.data[ticker]["time_to_sell"] -=1
         else:
             pass
def liquidate(s):
    #get all orders
    #cancel all orders
    orders = api_calls.get_orders(s,"OPEN")
    while len(orders)>0:
        for order in orders:
            api_calls.cancel_order(s, order['order_id'])
        orders = api_calls.get_orders(s,"OPEN")
    print('hit liquidate, no open orders found')
    securities = api_calls.get_securities(s)
    position = securities[0]['position']
    if position !=0:
        print('position', position)
        if position >0:
            max_bid = max(securities[0]['bid'], securities[1]['bid'])
            for security in securities:
                if security['bid'] == max_bid:
                    api_calls.send_order(s, security['ticker'], security['position'], "MARKET", "SELL", 0)
                    print('sent offsetting sell order')
                    break
        else:
            min_ask = min(securities[0]['ask'], securities[1]['ask'])
            for security in securities:
                if security['ask'] == min_ask:
                    api_calls.send_order(s, security['ticker'], -security['position'], "MARKET", "BUY", 0)
                    print('sent offsetting buy order')
                    break
    securities = api_calls.get_securities(s)
    position = securities[0]['position']
    if position != 0:
        time.sleep(1)
        if api_calls.get_status(s) == "ACTIVE":
            securities = api_calls.get_securities(s)
            position = securities[0]['position']
            if position !=0:
                print('position', position)
                if position >0:
                    max_bid = max(securities[0]['bid'], securities[1]['bid'])
                    for security in securities:
                        if security['bid'] == max_bid:
                            try:
                                api_calls.send_order(s, security['ticker'], security['position'], "MARKET", "SELL", 0)
                            except:
                                print('order failed')
                            print('sent offsetting sell order')
                            break
                else:
                    min_ask = min(securities[0]['ask'], securities[1]['ask'])
                    for security in securities:
                        if security['ask'] == min_ask:
                            try:
                                api_calls.send_order(s, security['ticker'], -security['position'], "MARKET", "BUY", 0)
                            except:
                                print('order failed')
                            print('sent offsetting buy order')
                            break
def tick_handler(s, tick,tickers):
    payload = get_max_vol(s)
    if payload == -1:
        return
    target_volume = payload['volume']
    curr_tick = tick
    continue_arb = True
    while target_volume!=0 and curr_tick == tick and continue_arb == True:
        #make the order
        if target_volume >=10000:
            # print("Arbiting 10000")
            o_id1 = api_calls.send_order(s, "CRZY_M", 10000, "LIMIT", payload['CRZY_M']['ACTION'], payload['CRZY_M']['PRICE'])['order_id']
            o_id2 = api_calls.send_order(s, "CRZY_A", 10000, "LIMIT", payload['CRZY_A']['ACTION'], payload['CRZY_A']['PRICE']  )['order_id']
            target_volume -= 10000
        else: 
            # print("Arbiting", target_volume)
            o_id1 = api_calls.send_order(s, "CRZY_M", target_volume, "LIMIT", payload['CRZY_M']['ACTION'], payload['CRZY_M']['PRICE']  )['order_id']
            o_id2 = api_calls.send_order(s, "CRZY_A", target_volume, "LIMIT", payload['CRZY_A']['ACTION'], payload['CRZY_A']['PRICE']  )['order_id']
            target_volume = 0
        #wait for trades to be be on the order books
        order1_exist = False
        order2_exist = False
        while (order1_exist == False) and (order2_exist == False):
            order1_exist = api_calls.order_exists(s, o_id1)
            order2_exist = api_calls.order_exists(s, o_id2)
        time.sleep(0.1)
        #wait for order to be completed
        order1_filled = False
        order2_filled = False
        order1_filled = api_calls.is_filled(s,o_id1)
        order2_filled = api_calls.is_filled(s,o_id2)
        if order1_filled and order2_filled:
            continue_arb = True
            # print("ORDERS ALL FILLED")
        else:
            while (order1_filled == False) and (order2_filled == False):
                order1_filled = api_calls.is_filled(s,o_id1)
                order2_filled = api_calls.is_filled(s,o_id2)
            # print('after filled')
            api_calls.cancel_order(s,o_id1)
            api_calls.cancel_order(s,o_id2)
        
            #get positions and liquidate
            liquidate(s)
            continue_arb = False
示例#7
0
 def trading(self, ticker, tick, session):
     #for each time period, sell x units
     #assume volume is the same
     rem_ticks = (299 - tick)
     action = None
     print(self.data[ticker]['current_stock'])
     if self.data[ticker]['current_stock'] > 0:
         action = "SELL"
     elif self.data[ticker]['current_stock'] < 0:
         action = "BUY"
     if self.data[ticker]['current_stock'] != 0:
         if rem_ticks == 0:
             api_calls.send_order(session, ticker,
                                  abs(self.data[ticker]['current_stock']),
                                  "MARKET", action, 0)
         else:
             api_calls.send_order(
                 session, ticker,
                 abs(self.data[ticker]['current_stock'] // rem_ticks),
                 "MARKET", action, 0)