Пример #1
0
def main(trader):
    
    check_frequency = 60 
    current = trader.get_last_trade_time()
    start_time = datetime.combine(current, dt.time(10,0,0))
    end_time = datetime.combine(current, dt.time(15,30,0))


    processes = []


    while trader.get_last_trade_time() < start_time:

        print(f"Checking for market open at {trader.get_last_trade_time()}")
        sleep(check_frequency)

    processes.extend(run_processes(trader, end_time))

    while trader.get_last_trade_time() < end_time: 
        print(f"Waiting for Market Close @ {trader.get_last_trade_time()}")
        sleep(check_frequency)


    stop_processes(processes)

    for order in trader.get_waiting_list():
        trader.submit_cancellation(order)

    
    for t in tickers:
        item = trader.get_portfolio_item(t)
        if item.get_shares() > 0:
            # orders_placed = place_orders(shift.Order.Type.MARKET_SELL, t, int(item.get_shares() / 100))
            # # sell = shift.Order(shift.Order.Type.MARKET_SELL,t, int(item.get_shares() / 100)) # Order size in 100's of shares, strictly as an int
            # # trader.submit_order(sell)
            print(f'CLOSING LONG {t}')
            # sleep(1)
            sell_long(t, trader)
            sleep(1)
    sleep(10)
    for t in tickers:
        item = trader.get_portfolio_item(t)
        if item.get_shares() < 0:
            # orders_placed = place_orders(shift.Order.Type.MARKET_BUY, t, int(-1* item.get_shares() / 100))
            # buy = shift.Order(shift.Order.Type.MARKET_BUY,t, int(-1* item.get_shares() / 100)) # Order size in 100's of shares, strictly as an int
            # trader.submit_order(buy)
            print(f'CLOSING SHORT {t}')
            buy_back_shorted(t, trader)
            sleep(1)

    sleep(15)
    utils.print_portfolio_information(trader)
    utils.print_all_submitted_order(trader)
    print(trader.get_last_trade_time())
def longer(trader: shift.Trader, tickers):
    today = trader.get_last_trade_time()
    endTime = dt.time(15, 50, 0)
    dayEnd = dt.datetime.combine(today, endTime)
    tickSize = 0.01
    bp = (trader.get_portfolio_summary().get_total_bp())
    dpa = bp / float(len(tickers))

    for t in tickers:
        lastPrice = trader.get_last_price(t)
        while lastPrice == 0:
            lastPrice = trader.get_last_price(t)
        s = int((dpa / lastPrice) / 100)

        for i in range(0, s):
            # limitBuyPrice = lastPrice - tickSize
            limit_sell = shift.Order(shift.Order.Type.MARKET_BUY, t, 1)
            trader.submit_order(limit_sell)

    while trader.get_last_trade_time() < dayEnd:
        time.sleep(1)

    for order in trader.get_waiting_list():
        trader.submit_cancellation(order)

    for t in tickers:
        print(t)
        lastPrice = trader.get_last_price(t)
        item = trader.get_portfolio_item(t)
        if item.get_shares() > 0:
            sell = shift.Order(
                shift.Order.Type.MARKET_SELL, t,
                int(item.get_shares() /
                    100))  # Order size in 100's of shares, strictly as an int
            trader.submit_order(sell)
        elif item.get_shares() < 0:
            buy = shift.Order(
                shift.Order.Type.MARKET_BUY, t,
                int(-1 * item.get_shares() /
                    100))  # Order size in 100's of shares, strictly as an int
            trader.submit_order(buy)
            print(f'submitted buy for {t}')

    time.sleep(15)
    utils.print_portfolio_information(trader)
    utils.print_all_submitted_order(trader)
    print(trader.get_last_trade_time())

    return
def routine_summary(trader: shift.Trader, end_time):
    # today = trader.get_last_trade_time()
    # endTime = dt.time(9, 50, 0)
    # dayEnd = dt.datetime.combine(today, endTime)
    # rightNow = trader.get_last_trade_time()
    while trader.get_last_trade_time() < end_time:
        print(f"----------{trader.get_last_trade_time()}-------------")
        utils.print_all_submitted_order(trader)
        utils.print_portfolio_information(trader)
        # print("-----–ASK–-----")
        # utils.print_ask_order_book(trader)
        # print("------BID–-----")
        # utils.print_bid_order_book(trader)
        ct = dt.datetime.now()
        print("current time:-", ct)
        time.sleep(3)
    return 0
def main(trader):
    manager = Manager()
    list_of_shared_dicts = manager.list()
    list_of_shared_dicts.append({})
    state = list_of_shared_dicts[0]
    keys_in_state = [candles_key]
    for key in keys_in_state:
        state[key] = manager.dict()
        for ticker in tickers:
            if key == candles_key:
                state[key].setdefault(ticker, [])

    check_frequency = 60
    current = trader.get_last_trade_time()
    start_time = datetime.combine(current, dt.time(9, 52, 0))
    end_time = datetime.combine(current, dt.time(15, 30, 0))

    processes = []

    for ticker in tickers:
        # 1 thread per ticker getting price data & saving it
        processes.append(
            Thread(target=build_candles,
                   args=(ticker, trader, state, end_time)))

        # run_save_prices(ticker, trader, state)

    for process in processes:
        process.start()

    print(f"{len(processes)} processes created for run_save_prices")

    while trader.get_last_trade_time() < start_time:

        print(f"Checking for market open at {trader.get_last_trade_time()}")
        sleep(check_frequency)

    processes.extend(run_processes(trader, state, end_time))

    while trader.get_last_trade_time() < end_time:
        print(f"Waiting for Market Close @ {trader.get_last_trade_time()}")
        sleep(check_frequency)

    stop_processes(processes)

    for order in trader.get_waiting_list():
        trader.submit_cancellation(order)

    for t in tickers:
        item = trader.get_portfolio_item(t)
        if item.get_shares() > 0:
            # orders_placed = place_orders(shift.Order.Type.MARKET_SELL, t, int(item.get_shares() / 100))
            # # sell = shift.Order(shift.Order.Type.MARKET_SELL,t, int(item.get_shares() / 100)) # Order size in 100's of shares, strictly as an int
            # # trader.submit_order(sell)
            print(f'CLOSING LONG {t}')
            # sleep(1)
            sell_long(t, trader)
            sleep(1)
    sleep(10)
    for t in tickers:
        item = trader.get_portfolio_item(t)
        if item.get_shares() < 0:
            # orders_placed = place_orders(shift.Order.Type.MARKET_BUY, t, int(-1* item.get_shares() / 100))
            # buy = shift.Order(shift.Order.Type.MARKET_BUY,t, int(-1* item.get_shares() / 100)) # Order size in 100's of shares, strictly as an int
            # trader.submit_order(buy)
            print(f'CLOSING SHORT {t}')
            buy_back_shorted(t, trader)
            sleep(1)

    sleep(15)
    utils.print_portfolio_information(trader)
    utils.print_all_submitted_order(trader)
    print(trader.get_last_trade_time())