コード例 #1
0
def trade_handler(s):
    #get all revelant data
    crzy_m_book = api_calls.get_trading_data(s, 'CRZY_M')
    crzy_a_book = api_calls.get_trading_data(s, 'CRZY_A')
    #decision buy all sell
    crzy_m_bid, crzy_m_ask = ticker_bid_ask(crzy_m_book)
    crzy_a_bid, crzy_a_ask = ticker_bid_ask(crzy_a_book)
    if crzy_m_bid == -1 or crzy_a_bid == -1:
        return
    if crzy_m_bid > crzy_a_ask:
        print("M BID Greater than A ask")
        print(crzy_m_bid, crzy_a_ask)
        crzy_m_vol = agg_volume(crzy_m_book, crzy_m_bid, 'bids')
        crzy_a_vol = agg_volume(crzy_a_book, crzy_a_ask, 'asks')
        volume = min(crzy_m_vol, crzy_a_vol)
        if volume > 10000:
            volume = 10000
        print(volume)
        crzy_a_params = {
            'ticker': 'CRZY_A',
            'type': 'MARKET',
            'quantity': volume,
            'action': 'BUY'
        }
        crzy_m_params = {
            'ticker': 'CRZY_M',
            'type': 'MARKET',
            'quantity': volume,
            'action': 'SELL'
        }
        s.post('http://localhost:9999/v1/orders', params=crzy_a_params)
        s.post('http://localhost:9999/v1/orders', params=crzy_m_params)
    if crzy_a_bid > crzy_m_ask:
        print("A BID Greater than M ask")
        crzy_a_vol = agg_volume(crzy_a_book, crzy_a_bid, 'bids')
        crzy_m_vol = agg_volume(crzy_m_book, crzy_m_ask, 'asks')
        volume = min(crzy_a_vol, crzy_m_vol)
        print(crzy_a_bid, crzy_m_ask)
        if volume > 10000:
            volume = 10000
        print(volume)
        crzy_a_params = {
            'ticker': 'CRZY_A',
            'type': 'MARKET',
            'quantity': volume,
            'action': 'SELL'
        }
        crzy_m_params = {
            'ticker': 'CRZY_M',
            'type': 'MARKET',
            'quantity': volume,
            'action': 'BUY'
        }
        s.post('http://localhost:9999/v1/orders', params=crzy_a_params)
        s.post('http://localhost:9999/v1/orders', params=crzy_m_params)
コード例 #2
0
 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']
コード例 #3
0
def get_max_vol(s):
    crzy_m_book = api_calls.get_trading_data(s, 'CRZY_M')
    crzy_a_book = api_calls.get_trading_data(s, 'CRZY_A')
    crzy_m_bid, crzy_m_ask = ticker_bid_ask(crzy_m_book)
    crzy_a_bid, crzy_a_ask = ticker_bid_ask(crzy_a_book)
    if crzy_m_bid == -1 or crzy_a_bid == -1:
        return
    if crzy_m_bid > crzy_a_ask:
        crzy_m_vol = agg_volume(crzy_m_book, crzy_m_bid, 'bids')
        crzy_a_vol = agg_volume(crzy_a_book, crzy_a_ask, 'asks')
        volume = min(crzy_m_vol, crzy_a_vol)
        payload = {"CRZY_A": "BUY", "CRZY_M": "SELL", "vol": volume}
    if crzy_a_bid > crzy_m_ask:
        crzy_a_vol = agg_volume(crzy_a_book, crzy_a_bid, 'bids')
        crzy_m_vol = agg_volume(crzy_m_book, crzy_m_ask, 'asks')
        volume = min(crzy_a_vol, crzy_m_vol)
        payload = {"CRZY_A": "BUY", "CRZY_M": "SELL", "vol": volume}
コード例 #4
0
def get_max_vol(s):
    crzy_m_book = api_calls.get_trading_data(s, 'CRZY_M')
    crzy_a_book = api_calls.get_trading_data(s, 'CRZY_A')
    crzy_m_bid, crzy_m_ask = ticker_bid_ask(crzy_m_book)
    crzy_a_bid, crzy_a_ask = ticker_bid_ask(crzy_a_book)
    if crzy_m_bid == -1 or crzy_a_bid == -1:
        return -1
    if crzy_m_bid > crzy_a_ask:
        crzy_m_vol = agg_volume(crzy_m_book, crzy_m_bid, 'bids')
        crzy_a_vol = agg_volume(crzy_a_book, crzy_a_ask, 'asks')
        volume = min(crzy_m_vol, crzy_a_vol)
        mid_point = (crzy_m_bid+crzy_a_ask)/2
        payload = {
            "CRZY_M": {
                "ACTION": "SELL",
                "PRICE": mid_point
                    },
            "CRZY_A": {
                "ACTION": "BUY",
                "PRICE": mid_point
            },
            'volume': volume
        }
        # print(volume//2)
        return payload
    if crzy_a_bid > crzy_m_ask:
        crzy_a_vol = agg_volume(crzy_a_book, crzy_a_bid, 'bids')
        crzy_m_vol = agg_volume(crzy_m_book, crzy_m_ask, 'asks')
        volume = min(crzy_a_vol, crzy_m_vol)
        mid_point = (crzy_a_bid+crzy_m_ask)/2
        payload = {
            "CRZY_A": {
                "ACTION": "SELL",
                "PRICE": mid_point
                    },
            "CRZY_M": {
                "ACTION": "BUY",
                "PRICE": mid_point
            },
            'volume': volume
        }
        # print(volume)
        return payload
    return -1
コード例 #5
0
 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
コード例 #6
0
    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
コード例 #7
0
    def gather_data(self, tick, tickers, s):
        if tick == 300:
            for ticker in tickers:
                self.payload[ticker]['tas'] = api_calls.get_tas(s, ticker)
                self.payload[ticker]['history'] = api_calls.get_history(
                    s, ticker)
            self.payload['trader']['tick_data'].append({
                "tick":
                tick,
                "nlv":
                api_calls.get_trader_data(s)['nlv']
            })
            self.payload['orders'] = api_calls.get_all_orders(s)
            self.db_insert()
        else:

            for ticker in tickers:
                if ticker not in self.payload:
                    self.payload[ticker] = {"tick_data": []}
                self.payload[ticker]["tick_data"].append({
                    "tick":
                    tick,
                    "book":
                    api_calls.get_trading_data(s, ticker)
                })

            self.payload['tenders']['tick_data'].append({
                "tick":
                tick,
                "tenders":
                api_calls.get_tenders(s)
            })
            securities = api_calls.get_securities(s)
            temp = {}
            for security in securities:
                temp[security['ticker']] = {
                    "VWAP": security['vwap'],
                    "Position": security['position'],
                    "Volume": security['volume']
                }
            self.payload['trader']['tick_data'].append({
                "tick":
                tick,
                "nlv":
                api_calls.get_trader_data(s)['nlv'],
                "securities_data":
                temp
            })