def updated_buffered_positions(data: dataBlob, strategy_name: str,
                               system: System):
    log = data.log

    data_optimal_positions = dataOptimalPositions(data)

    list_of_instruments = system.get_instrument_list()
    for instrument_code in list_of_instruments:
        lower_buffer, upper_buffer = get_position_buffers_from_system(
            system, instrument_code)
        position_entry = construct_position_entry(
            data=data,
            system=system,
            instrument_code=instrument_code,
            lower_buffer=lower_buffer,
            upper_buffer=upper_buffer,
        )
        instrument_strategy = instrumentStrategy(
            instrument_code=instrument_code, strategy_name=strategy_name)
        data_optimal_positions.update_optimal_position_for_instrument_strategy(
            instrument_strategy=instrument_strategy,
            position_entry=position_entry)
        log.msg(
            "New buffered positions %.3f %.3f" %
            (position_entry.lower_position, position_entry.upper_position),
            instrument_code=instrument_code,
        )
Beispiel #2
0
    def get_raw_optimal_position_data(self) -> dict:
        # This is the 'raw' data, positions pre-optimisation
        # dict of optimalPositionWithReference

        data = self.data
        strategy_name = self.strategy_name

        optimal_position_data = dataOptimalPositions(data)

        list_of_instruments = optimal_position_data.get_list_of_instruments_for_strategy_with_optimal_position(
            strategy_name, raw_positions=True)

        list_of_instrument_strategies = [
            instrumentStrategy(strategy_name=strategy_name,
                               instrument_code=instrument_code)
            for instrument_code in list_of_instruments
        ]

        raw_optimal_positions = dict([(
            instrument_strategy.instrument_code,
            optimal_position_data.
            get_current_optimal_position_for_instrument_strategy(
                instrument_strategy, raw_positions=True),
        ) for instrument_strategy in list_of_instrument_strategies])

        return raw_optimal_positions
Beispiel #3
0
    def cut_down_proposed_instrument_trade_okay(self, instrument_trade):

        strategy_name = instrument_trade.strategy_name
        instrument_code = instrument_trade.instrument_code
        proposed_trade = instrument_trade.as_single_trade_qty_or_error()

        ## want to CUT DOWN rather than bool possible trades
        ## FIXME:
        ## underneath should be using tradeQuantity and position objects
        ## these will handle abs cases plus legs if required in future
        # :FIXME
        instrument_strategy = instrumentStrategy(
            strategy_name=strategy_name, instrument_code=instrument_code)

        max_trade_ok_against_instrument_strategy = \
            self.check_if_proposed_trade_okay_against_instrument_strategy_constraint(instrument_strategy,
                                                                                    proposed_trade)
        max_trade_ok_against_instrument = \
            self.check_if_proposed_trade_okay_against_instrument_constraint(instrument_code,
                                                                            proposed_trade)

        ## FIXME THIS IS UGLY WOULD BE BETTER IF DONE INSIDE TRADE SIZE OBJECT
        mini_max_trade = sign(proposed_trade) * \
                         min([abs(max_trade_ok_against_instrument),
                        abs(max_trade_ok_against_instrument_strategy)])

        mini_max_trade = tradeQuantity(mini_max_trade)

        instrument_trade = instrument_trade.replace_required_trade_size_only_use_for_unsubmitted_trades(
            mini_max_trade)

        return instrument_trade
def actual_instrument_position(data):
    diag_positions = diagPositions(data)

    strategy_name_list = diag_positions.get_list_of_strategies_with_positions()
    strategy_name = print_menu_of_values_and_get_response(strategy_name_list)
    if strategy_name is user_exit:
        return None

    instrument_code_list = (
        diag_positions.get_list_of_instruments_for_strategy_with_position(
            strategy_name, ignore_zero_positions=False
        )
    )
    instrument_code = get_valid_code_from_list(instrument_code_list)
    if instrument_code is user_exit:
        return None
    instrument_strategy = instrumentStrategy(
        strategy_name=strategy_name, instrument_code=instrument_code
    )

    pos_series = diag_positions.get_position_df_for_instrument_strategy(
        instrument_strategy
    )
    print(pos_series)
    return None
Beispiel #5
0
    def cut_down_proposed_instrument_trade_for_position_limits(
            self, order: instrumentOrder):

        strategy_name = order.strategy_name
        instrument_code = order.instrument_code

        instrument_strategy = instrumentStrategy(
            strategy_name=strategy_name, instrument_code=instrument_code)

        max_order_ok_against_instrument_strategy = \
            self._check_if_proposed_trade_okay_against_instrument_strategy_constraint(instrument_strategy,
                                                                                      order)
        max_order_ok_against_instrument = \
            self._check_if_proposed_trade_okay_against_instrument_constraint(instrument_code,
                                                                             order)

        new_order = order.\
            single_leg_trade_qty_with_lowest_abs_value_trade_from_order_list(
            [
            max_order_ok_against_instrument,
            max_order_ok_against_instrument_strategy
            ]
        )

        return new_order
def change_position_limit_for_instrument_strategy(data):
    view_position_limit(data)
    data_position_limits = dataPositionLimits(data)
    strategy_name = get_valid_strategy_name_from_user(data,
                                                      allow_all=False,
                                                      source="positions")
    instrument_code = get_valid_instrument_code_from_user(data,
                                                          allow_all=False)
    new_position_limit = get_and_convert(
        "New position limit?",
        type_expected=int,
        allow_default=True,
        default_value=-1,
        default_str="No limit",
    )

    instrument_strategy = instrumentStrategy(instrument_code=instrument_code,
                                             strategy_name=strategy_name)

    if new_position_limit == -1:
        data_position_limits.delete_position_limit_for_instrument_strategy(
            instrument_strategy)
    else:
        new_position_limit = abs(new_position_limit)
        data_position_limits.set_position_limit_for_instrument_strategy(
            instrument_strategy, new_position_limit)
Beispiel #7
0
def get_fills_for_instrument(data, instrument_code, strategy_name):
    data_orders = dataOrders(data)
    instrument_strategy = instrumentStrategy(instrument_code=instrument_code, strategy_name=strategy_name)

    list_of_fills = data_orders.get_fills_history_for_instrument_strategy(instrument_strategy)

    return list_of_fills
Beispiel #8
0
 def _element_object_from_row(dfrow):
     instrument_strategy = instrumentStrategy(
         strategy_name=dfrow[KEY_STRATEGY_NAME],
         instrument_code=dfrow[KEY_INSTRUMENT_CODE],
     )
     return instrumentStrategyPosition(dfrow[KEY_POSITION],
                                       instrument_strategy)
Beispiel #9
0
    def get_current_optimal_position_for_strategy_and_instrument(
        self, strategy_name, instrument_code
    ):
        # FIX ME REMOVE
        instrument_strategy = instrumentStrategy(strategy_name=strategy_name,
                                                 instrument_code=instrument_code)

        return self.get_current_optimal_position_for_instrument_strategy(instrument_strategy)
Beispiel #10
0
    def get_position_df_for_strategy_and_instrument(
        self, strategy_name:str, instrument_code:str
    ):
        #FIXME THINK ABOUT REMOVING
        instrument_strategy = instrumentStrategy(strategy_name=strategy_name, instrument_code=instrument_code)
        position_df = self.get_position_df_for_instrument_strategy_object(instrument_strategy)

        return position_df
Beispiel #11
0
    def update_instrument_strategy_limit_with_new_limit(
            self, strategy_name, instrument_code, period_days, new_limit):
        #FIXME DELETE REPLACE WITH '2'
        instrument_strategy = instrumentStrategy(
            strategy_name=strategy_name, instrument_code=instrument_code)

        self.update_instrument_strategy_limit_with_new_limit2(
            instrument_strategy, period_days, new_limit)
Beispiel #12
0
 def get_current_position_for_strategy_and_instrument(
         self, strategy_name, instrument_code):
     #FIXME THINK ABOUT REMOVING
     instrument_strategy = instrumentStrategy(
         strategy_name=strategy_name, instrument_code=instrument_code)
     position = self.get_current_position_for_instrument_strategy(
         instrument_strategy)
     return position
Beispiel #13
0
def get_trade_df_for_instrument(data, instrument_code, strategy_name):
    data_orders = dataOrders(data)
    instrument_strategy = instrumentStrategy(instrument_code=instrument_code, strategy_name=strategy_name)

    list_of_trades = data_orders.get_fills_history_for_instrument_strategy(instrument_strategy)
    list_of_trades_as_pd_df = list_of_trades.as_pd_df()

    return list_of_trades_as_pd_df
Beispiel #14
0
    def update_override_for_strategy_instrument(self, strategy_name,
                                                instrument_code, new_override):
        # FIXME REMOVE
        instrument_strategy = instrumentStrategy(
            strategy_name=strategy_name, instrument_code=instrument_code)

        self.update_override_for_instrument_strategy(instrument_strategy,
                                                     new_override)
Beispiel #15
0
    def set_abs_position_limit_for_strategy_instrument(self, strategy_name,
                                                       instrument_code,
                                                       new_position_limit):

        #FIXME DELETE
        instrument_strategy = instrumentStrategy(
            strategy_name=strategy_name, instrument_code=instrument_code)
        self.set_position_limit_for_instrument_strategy(
            instrument_strategy, new_position_limit)
def get_current_position_for_instrument_code(
        data_backtest, data, instrument_code):
    diag_positions = diagPositions(data)
    strategy_name = data_backtest.strategy_name
    instrument_strategy = instrumentStrategy(strategy_name=strategy_name, instrument_code=instrument_code)

    current_position = diag_positions.get_current_position_for_instrument_strategy(instrument_strategy)

    return current_position
Beispiel #17
0
def instrument_strategy_with_raw_tag(
    instrument_strategy: instrumentStrategy, ) -> instrumentStrategy:
    original_strategy_name = copy(instrument_strategy.strategy_name)
    strategy_name = strategy_name_with_raw_tag(original_strategy_name)

    new_instrument_strategy = instrumentStrategy(
        strategy_name=strategy_name,
        instrument_code=instrument_strategy.instrument_code)

    return new_instrument_strategy
Beispiel #18
0
def get_position_series_for_instrument_strategy(
        data, instrument_code, strategy_name):
    diag_positions = diagPositions(data)
    instrument_strategy = instrumentStrategy(strategy_name=strategy_name, instrument_code=instrument_code)

    pos_series = diag_positions.get_position_df_for_instrument_strategy(instrument_strategy)
    if pos_series is missing_data:
        return pd.Series()

    return pd.Series(pos_series.position)
    def get_optimal_positions(self) -> optimalPositions:
        data = self.data
        strategy_name = self.strategy_name

        optimal_position_data = dataOptimalPositions(data)

        list_of_instruments = optimal_position_data.get_list_of_instruments_for_strategy_with_optimal_position(
            strategy_name)

        list_of_instrument_strategies = [
            instrumentStrategy(strategy_name=strategy_name,
                               instrument_code=instrument_code)
            for instrument_code in list_of_instruments
        ]

        optimal_positions = dict([(
            instrument_strategy.instrument_code,
            optimal_position_data.
            get_current_optimal_position_for_instrument_strategy(
                instrument_strategy),
        ) for instrument_strategy in list_of_instrument_strategies])

        ref_dates = dict([
            (instrument_code, opt_position.date)
            for instrument_code, opt_position in optimal_positions.items()
        ])

        upper_positions = dict([
            (instrument_code, opt_position.upper_position)
            for instrument_code, opt_position in optimal_positions.items()
        ])
        lower_positions = dict([
            (instrument_code, opt_position.lower_position)
            for instrument_code, opt_position in optimal_positions.items()
        ])

        reference_prices = dict([
            (instrument_code, opt_position.reference_price)
            for instrument_code, opt_position in optimal_positions.items()
        ])

        reference_contracts = dict([
            (instrument_code, opt_position.reference_contract)
            for instrument_code, opt_position in optimal_positions.items()
        ])

        optimal_positions = optimalPositions(
            upper_positions=upper_positions,
            lower_positions=lower_positions,
            reference_prices=reference_prices,
            reference_contracts=reference_contracts,
            ref_dates=ref_dates,
        )
        return optimal_positions
def _from_trade_limit_dict_to_required_dict(trade_limit_dict: dict) -> dict:
    if INSTRUMENT_STRATEGY_KEY in trade_limit_dict.keys():
        ## NEW STYLE
        return trade_limit_dict

    ## OLD STYLE
    instrument_strategy = instrumentStrategy(instrument_code=trade_limit_dict.pop(LEGACY_INSTRUMENT_KEY),
                                             strategy_name=trade_limit_dict.pop(LEGACY_STRATEGY_KEY))
    trade_limit_dict[INSTRUMENT_STRATEGY_KEY] = instrument_strategy.key

    return trade_limit_dict
def _from_result_dict_to_isd(result_dict: dict) -> instrumentStrategyKeyAndDays:
    if INSTRUMENT_STRATEGY_KEY in result_dict.keys():
        ## NEW STYLE
        instrument_strategy_key = result_dict[INSTRUMENT_STRATEGY_KEY]
    else:
        ## LEGACY
        instrument_strategy = instrumentStrategy(strategy_name=result_dict[LEGACY_STRATEGY_KEY],
                                                 instrument_code=result_dict[LEGACY_INSTRUMENT_KEY])
        instrument_strategy_key = instrument_strategy.key

    return instrumentStrategyKeyAndDays(instrument_strategy_key, result_dict[PERIOD_KEY])
Beispiel #22
0
def get_reduce_only_keys(data: dataBlob, strategy_name: str,
                         list_of_instruments: list) -> list:
    no_trade_keys = [
        instrument_code for instrument_code in list_of_instruments
        if get_override_for_instrument_strategy(
            data,
            instrument_strategy=instrumentStrategy(
                instrument_code=instrument_code, strategy_name=strategy_name),
        ) == REDUCE_ONLY_OVERRIDE
    ]

    return no_trade_keys
Beispiel #23
0
def get_maximum_position_contracts(
        data, strategy_name: str,
        list_of_instruments: list) -> portfolioWeights:

    maximum_position_contracts = dict([(
        instrument_code,
        get_maximum_position_contracts_for_instrument_strategy(
            data,
            instrument_strategy=instrumentStrategy(
                strategy_name=strategy_name, instrument_code=instrument_code)),
    ) for instrument_code in list_of_instruments])

    return portfolioWeights(maximum_position_contracts)
def update_strategy_instrument_override(data):
    view_overrides(data)
    update_overrides = updateOverrides(data)
    instrument_code = get_valid_instrument_code_from_user(data)
    strategy_name = get_valid_strategy_name_from_user(data=data,
                                                      source="positions")
    instrument_strategy = instrumentStrategy(instrument_code=instrument_code,
                                             strategy_name=strategy_name)
    new_override = get_overide_object_from_user()
    ans = input("Are you sure? (y/other)")
    if ans == "y":
        update_overrides.update_override_for_instrument_strategy(
            instrument_strategy=instrument_strategy, new_override=new_override)
def get_last_position_update_for_strategy_instrument(data, strategy_name,
                                                     instrument_code):
    op = dataOptimalPositions(data)
    instrument_strategy = instrumentStrategy(instrument_code=instrument_code,
                                             strategy_name=strategy_name)

    pos_data = op.get_optimal_position_as_df_for_instrument_strategy(
        instrument_strategy)
    if pos_data is missing_data:
        return None
    last_update = pos_data.index[-1]
    key = "%s/%s" % (strategy_name, instrument_code)

    return genericUpdate(key, last_update)
Beispiel #26
0
    def get_list_of_perc_pandl_series_for_strategy_all_instruments(
            self, strategy_name: str):
        instrument_list = get_list_of_instruments_held_for_a_strategy(
            self.data, strategy_name)
        if len(instrument_list) == 0:
            return missing_data, missing_data

        pandl_list = [
            self.perc_pandl_series_for_strategy_instrument_vs_total_capital(
                instrumentStrategy(strategy_name, instrument_code))
            for instrument_code in instrument_list
        ]

        return instrument_list, pandl_list
def backup_strategy_position_data(data):
    strategy_list = get_list_of_strategies(data)
    instrument_list = (data.mongo_contract_position.
                       get_list_of_instruments_with_any_position())
    for strategy_name in strategy_list:
        for instrument_code in instrument_list:
            instrument_strategy = instrumentStrategy(
                strategy_name=strategy_name, instrument_code=instrument_code)
            mongo_data = data.mongo_strategy_position.get_position_as_df_for_instrument_strategy_object(
                instrument_strategy)
            if mongo_data is missing_data:
                continue
            data.csv_strategy_position.write_position_df_for_instrument_strategy(
                instrument_strategy, mongo_data)
            data.log.msg("Backed up %s %s strategy position data" %
                         (instrument_code, strategy_name))
Beispiel #28
0
def reset_limit_for_instrument_strategy(data):
    trade_limits = dataTradeLimits(data)
    instrument_code = get_valid_instrument_code_from_user(data)
    period_days = get_and_convert(
        "Period of days?", type_expected=int, allow_default=True, default_value=1
    )
    strategy_name = get_valid_strategy_name_from_user(data=data, source="positions")

    ans = input("Reset means trade 'clock' will restart. Are you sure? (y/other)")
    if ans == "y":
        instrument_strategy = instrumentStrategy(
            instrument_code=instrument_code, strategy_name=strategy_name
        )
        trade_limits.reset_instrument_strategy_limit(
            instrument_strategy=instrument_strategy, period_days=period_days
        )
Beispiel #29
0
def write_optimised_positions_data_for_code(
    data: dataBlob,
    strategy_name: str,
    instrument_code: str,
    optimised_position_entry: optimalPositionWithDynamicCalculations,
):

    data_optimal_positions = dataOptimalPositions(data)
    instrument_strategy = instrumentStrategy(instrument_code=instrument_code,
                                             strategy_name=strategy_name)

    data.log.msg(
        "Adding optimal position for %s: %s" %
        (str(instrument_strategy), optimised_position_entry.verbose_repr()))
    data_optimal_positions.update_optimal_position_for_instrument_strategy(
        instrument_strategy=instrument_strategy,
        position_entry=optimised_position_entry)
Beispiel #30
0
    def get_dict_of_actual_positions_for_strategy(self,
                                                  strategy_name: str) -> dict:
        list_of_instruments = self.get_list_of_instruments_for_strategy_with_position(
            strategy_name)
        list_of_instrument_strategies = [
            instrumentStrategy(strategy_name=strategy_name,
                               instrument_code=instrument_code)
            for instrument_code in list_of_instruments
        ]

        actual_positions = dict([(
            instrument_strategy.instrument_code,
            self.get_current_position_for_instrument_strategy(
                instrument_strategy),
        ) for instrument_strategy in list_of_instrument_strategies])

        return actual_positions