def test_main():
    counter = 0
    time = datetime.datetime.today()
    prev_tick = -1
    trader = dynamic.trade_handler(tickers)
    with requests.Session() as s:
        s.headers.update(API_KEY)
        tick = api_calls.get_tick(s)
        status = api_calls.get_status(s)
        # while status == "ACTIVE":
        #     print('Waiting for test to complete')
        #     status = api_calls.get_status(s)
        #     sleep(1)
        #     pass
        # status = api_calls.get_status(s)
        # print('Finished Waiting test to complete')
        while counter <= test_counter:
            #checks if the trading platform is running, if it is we do the trades
            if status == "ACTIVE":
                #this insures we only execute the trade_handler once a tick
                if prev_tick != tick:
                    prev_tick = tick
                    trader.tick_handler(s,tick)
                    # checks if we've finished the test, if we have we will increment the counter by 1
                    print(tick)
                tick = api_calls.get_tick(s)
                status = api_calls.get_status(s)
                if tick == 300:
                    trader.tick_handler(s,tick)
                    counter += 1
                    time = datetime.datetime.today()
                    print('Completed', counter, 'Full test')
                    #sleeps 1 seconds to ensure same tick isn't executed again
                    sleep(3)
                    #updates tick and status which should be INACTIVE now
                    tick = api_calls.get_tick(s)
                    status = api_calls.get_status(s)
                    print(tick)
                    print(status)
            #if trading platform becomes inactive
            else:            
                tick = api_calls.get_tick(s)
                status = api_calls.get_status(s)
                sleep(1)
                print("Trading Platform not active")
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