示例#1
0
def button_stop_symbol(symbol):
    cmd = Command()
    cmd.command_type = CommandType.STOP_LIVE
    cmd.symbol = symbol
    request = jsonpickle.encode(cmd)
    request_producer.produce(TopicName.COMMANDS.value, request.encode('utf-8'))
    request_producer.flush()
示例#2
0
def button_request_data():
    cmd = Command()

    if strat_input.value != '':
        strat_params = str(strat_input.value).split(',')
        sc = StrategyConfiguration()
        strat_name = strat_params[0]
        if strat_name == 'TA':
            sc.type = StrategyType.TIME_AGGREGATE
        elif strat_name == 'Tick':
            sc.type = StrategyType.TICK
        elif strat_name == 'Price':
            sc.type = StrategyType.PRICE
        elif strat_name == 'Volume':
            sc.type = StrategyType.VOLUME
        elif strat_name == 'Amount':
            sc.type = StrategyType.AMOUNT
        sc.interval = float(strat_params[1])
        sc.trigger = int(strat_params[2])
        cmd.strategy_configurations.append(sc)

    if ind_input.value != '':
        ind_params = str(ind_input.value).split(',')
        ic = IndicatorConfiguration()
        ind_name = ind_params[0]
        if ind_name == 'FI':
            ic.type = IndicatorType.FI
        elif ind_name == 'MACD':
            ic.type = IndicatorType.MACD
        elif ind_name == 'CCI':
            ic.type = IndicatorType.CCI
        elif ind_name == 'RSI':
            ic.type = IndicatorType.RSI
        ic.tick = float(ind_params[1])
        ic.trigger = int(ind_params[2])
        cmd.indicator_configurations.append(ic)

    cmd.command_type = CommandType.START_LIVE
    cmd.symbol = symbol_input.value
    cmd.order_amount = amount_input.value
    request = jsonpickle.encode(cmd)
    request_producer.produce(TopicName.COMMANDS.value, request.encode('utf-8'))
    request_producer.flush()
示例#3
0
                    ind_type = IndicatorType.MACD
                elif ind == 'CCI':
                    ind_type = IndicatorType.CCI
                elif ind == 'RSI':
                    ind_type = IndicatorType.RSI
                else:
                    ind_type = 0

                for symbol in symbols:
                    print('symbol: ' + symbol)
                    start_date = from_date
                    end_date = to_date
                    for single_date in daterange(start_date, end_date):
                        str_date = single_date.strftime("%Y-%m-%d")
                        #  send start signal command
                        start_cmd = Command()
                        start_cmd.command_type = CommandType.START_RECORDING
                        start_cmd.symbol = symbol
                        start_cmd.version = args.version
                        start_cmd.from_date = str_date
                        #  for loop through strats
                        for i in range(len(strat_ticks)):
                            strat_configuration = StrategyConfiguration()
                            strat_configuration.type = start_type
                            strat_configuration.interval = int(strat_ticks[i])
                            strat_configuration.trigger = int(
                                strat_triggers[i])
                            start_cmd.strategy_configurations = [
                                strat_configuration]
                            brain.set_strategies([strat_configuration])
async def listen_command_line(api, conn):
    symbol_brain_dic = {}
    loop = asyncio.get_event_loop()
    reader = asyncio.StreamReader(loop=loop)
    reader_protocol = asyncio.StreamReaderProtocol(reader)
    await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin)
    while True:
        line = await reader.readline()
        symbol = line.decode()
        symbol = symbol.rstrip('\n')
        if symbol != 'STOP' and symbol != 'STOP_ALL':
            req_symbols = symbol.split(',')
            print(req_symbols)
            for symbol in req_symbols:
                data = api.get_asset(symbol)
                if data.shortable and data.easy_to_borrow:
                    cmd = Command()
                    cmd.command_type = CommandType.START_LIVE
                    sc = StrategyConfiguration()
                    sc.type = StrategyType.TICK
                    sc.interval = 50
                    sc.trigger = 2
                    cmd.strategy_configurations.append(sc)
                    cmd.order_amount = 2000
                    if symbol not in symbol_brain_dic:
                        read_trade_fd, write_trade_fd = os.pipe()
                        read_quote_fd, write_quote_fd = os.pipe()
                        read_trade_update_fd, write_trade_update_fd \
                            = os.pipe()

                        for fd in (read_trade_fd,
                                   write_trade_fd,
                                   read_quote_fd,
                                   write_quote_fd,
                                   read_trade_update_fd,
                                   write_trade_update_fd):
                            fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK)
                        brain = LiveBrain(symbol,
                                          cmd.strategy_configurations,
                                          cmd.indicator_configurations,
                                          api,
                                          read_trade_fd,
                                          read_quote_fd,
                                          read_trade_update_fd,
                                          cmd.order_amount)
                        brain.start()

                        write_trade_pipe = os.fdopen(write_trade_fd,
                                                     'wb', buffering=0)
                        write_t_pipe, _ = await \
                            asyncio.get_event_loop().\
                            connect_write_pipe(asyncio.Protocol,
                                               write_trade_pipe)
                        # writer = asyncio.StreamWriter(write_t_pipe, protocol)
                        conn.subscribe_trades(write_t_pipe, symbol)

                        # write_quote_pipe = os.fdopen(write_quote_fd,
                        #                              'wb', buffering=0)
                        # write_q_pipe, _ = await \
                        #     asyncio.get_event_loop().\
                        #     connect_write_pipe(asyncio.Protocol,
                        #                        write_quote_pipe)
                        # conn.subscribe_quotes(write_q_pipe, symbol)

                        # write_trade_update_pipe = \
                        #     os.fdopen(write_trade_update_fd,
                        #               'wb', buffering=0)
                        # write_tu_pipe, _ = await \
                        #     asyncio.get_event_loop().\
                        #     connect_write_pipe(asyncio.Protocol,
                        #                        write_trade_update_pipe)
                        # conn.subscribe_trade_updates(write_tu_pipe, symbol)
                        symbol_brain_dic[symbol] = brain
                else:
                    print('cannot be shorted')
        elif symbol == 'STOP':
            req_symbols = symbol.split(',')
            for symbol in req_symbols:
                brain = symbol_brain_dic.get(symbol)
                if brain is not None:
                    symbol_brain_dic.pop(symbol)
                    brain.shutdown()
                    brain.terminate()
                    brain.join()
                    # conn.unsubscribe_quotes(symbol)
                    conn.unsubscribe_trades(symbol)
                    conn.unsubscribe_trade_updates(symbol)
                    time.sleep(1)
        elif symbol == 'STOP_ALL':
            for symbol in symbol_brain_dic:
                brain = symbol_brain_dic[symbol]
                symbol_brain_dic.pop(symbol)
                brain.shutdown()
                brain.terminate()
                brain.join()
                conn.unsubscribe_quotes(symbol)
                conn.unsubscribe_trades(symbol)
                conn.unsubscribe_trade_updates(symbol)
                time.sleep(1)
示例#5
0
        conn_thread = threading.Thread(target=alpaca_thread_run)
        conn_thread.daemon = True
        conn_thread.start()

        symbol_brain_dic = {}
        while True:
            symbol = input()
            if symbol != 'STOP' and symbol != 'STOP_ALL':
                req_symbols = symbol.split(',')
                print(req_symbols)
                for symbol in req_symbols:
                    data = api.get_asset(symbol)
                    # print(data)
                    if data.shortable and data.easy_to_borrow:
                        cmd = Command()
                        cmd.command_type = CommandType.START_LIVE
                        sc = StrategyConfiguration()
                        sc.type = StrategyType.TICK
                        sc.interval = 50
                        sc.trigger = 2
                        cmd.strategy_configurations.append(sc)
                        cmd.order_amount = 2000
                        if symbol not in symbol_brain_dic:
                            read_trade_pipe, write_trade_pipe = aiopipe()
                            read_quote_pipe, write_quote_pipe = aiopipe()
                            read_trade_update_pipe, write_trade_update_pipe \
                                = aiopipe()

                            # with write_quote_pipe.detach() as write_quote_pipe:
                            #     conn.subscribe_quotes(write_quote_pipe, symbol)
示例#6
0
def button_request_data():
    cmd = Command()
    for i in range(len(strategy_data_source.data['name'])):
        if (int(strategy_data_source.data['On/Off'][i]) == 1):
            sc = StrategyConfiguration()
            sc.interval = float(strategy_data_source.data['interval'][i])
            sc.trigger = int(strategy_data_source.data['trigger'][i])
            if strategy_data_source.data['name'][i] == 'Time Aggregate':
                sc.type = StrategyType.TIME_AGGREGATE
            elif strategy_data_source.data['name'][i] == 'Tick':
                sc.type = StrategyType.TICK
            elif strategy_data_source.data['name'][i] == 'Price':
                sc.type = StrategyType.PRICE
            elif strategy_data_source.data['name'][i] == 'Volume':
                sc.type = StrategyType.VOLUME
            elif strategy_data_source.data['name'][i] == 'Amount':
                sc.type = StrategyType.AMOUNT
            cmd.strategy_configurations.append(sc)

    for i in range(len(indicator_data_source.data['name'])):
        if (int(indicator_data_source.data['On/Off'][i]) == 1):
            debug_banner.text = "indicator found"
            ic = IndicatorConfiguration()
            ic.tick = float(indicator_data_source.data['interval'][i])
            ic.trigger = int(indicator_data_source.data['trigger'][i])
            ic.param1 = int(indicator_data_source.data['param1'][i])
            ic.param2 = int(indicator_data_source.data['param2'][i])
            ic.param3 = int(indicator_data_source.data['param3'][i])
            if indicator_data_source.data['name'][i] == 'Force Index':
                ic.type = IndicatorType.FI
            elif indicator_data_source.data['name'][i] == 'MACD':
                ic.type = IndicatorType.MACD
            elif indicator_data_source.data['name'][i] == 'CCI':
                ic.type = IndicatorType.CCI
            elif indicator_data_source.data['name'][i] == 'RSI':
                ic.type = IndicatorType.RSI
            cmd.indicator_configurations.append(ic)

    if mode_radio_button_group.active == 0:
        cmd.command_type = CommandType.HISTORICAL
    else:
        cmd.command_type = CommandType.LIVE
    cmd.symbol = symbol_input.value
    cmd.from_date = from_date_picker.value
    cmd.to_date = to_date_picker.value

    # order configuration
    if len(buy_order_strategy_checkbox.active) > 0:
        buy_order_configuration = OrderConfiguration()
        buy_order_configuration.order_strategy_type = OrderStrategyType.BUY
        if buy_order_type_button_group.active == 0:
            buy_order_configuration.initial_order_type = \
                InitialOrderType.MARKET
        else:
            buy_order_configuration.initial_order_type = \
                InitialOrderType.LIMIT
            buy_order_configuration.initial_order_limit_market_delta = \
                buy_limit_input.value

        if buy_order_stop_loss_button_group.active == 1:
            buy_order_configuration.stop_loss_order_strategy = \
                StopLossOrderStrategy.TRAIL
            buy_order_configuration.stop_loss_order_trail_price_delta_trigger \
                = buy_order_trail_threshold_input.value
            buy_order_configuration.stop_loss_order_trail_price_change \
                = buy_order_trail_amount_input.value
        else:
            buy_order_configuration.stop_loss_order_strategy \
                = StopLossOrderStrategy.FIXED

        buy_order_configuration.stop_loss_order_stop_delta_initial_order \
            = buy_stop_loss_stop_price_input.value
        buy_order_configuration.stop_loss_order_limit_delta_stop_price \
            = buy_stop_loss_limit_price_input.value
        buy_order_configuration.order_size = buy_order_qty_input.value
        cmd.order_configurations.append(buy_order_configuration)

    if len(sell_order_strategy_checkbox.active) > 0:
        sell_order_configuration = OrderConfiguration()
        sell_order_configuration.order_strategy_type = OrderStrategyType.SELL
        if sell_order_type_button_group.active == 0:
            sell_order_configuration.initial_order_type = \
                InitialOrderType.MARKET
        else:
            sell_order_configuration.initial_order_type = \
                InitialOrderType.LIMIT
            sell_order_configuration.initial_order_limit_market_delta = \
                sell_limit_input.value

        if buy_order_stop_loss_button_group.active == 1:
            sell_order_configuration.stop_loss_order_strategy = \
                StopLossOrderStrategy.TRAIL
            sell_order_configuration.stop_loss_order_trail_price_delta_trigger\
                = sell_order_trail_threshold_input.value
            sell_order_configuration.stop_loss_order_trail_price_change \
                = sell_order_trail_amount_input.value
        else:
            sell_order_configuration.stop_loss_order_strategy = \
                StopLossOrderStrategy.FIXED

        sell_order_configuration.stop_loss_order_stop_delta_initial_order \
            = sell_stop_loss_stop_price_input.value
        sell_order_configuration.stop_loss_order_limit_delta_stop_price \
            = sell_stop_loss_limit_price_input.value
        sell_order_configuration.order_size = sell_order_qty_input.value
        cmd.order_configurations.append(sell_order_configuration)

        request = jsonpickle.encode(cmd)
        request_producer.produce(TopicName.COMMANDS.value,
                                 request.encode('utf-8'))
        request_producer.flush()