Skip to content

Commit

Permalink
fix unexpected side effect #931
Browse files Browse the repository at this point in the history
  • Loading branch information
rob committed Jul 3, 2023
1 parent e006e80 commit dedacf9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
36 changes: 30 additions & 6 deletions sysexecution/stack_handler/roll_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def create_force_roll_orders(
roll_spread_info = get_roll_spread_information(data, instrument_code)
type_of_roll = flat_roll_or_close_near_contract(data, instrument_code)
instrument_order = create_instrument_roll_order(
data=data,
roll_spread_info=roll_spread_info,
instrument_code=instrument_code,
type_of_roll=type_of_roll,
Expand Down Expand Up @@ -316,6 +317,7 @@ def get_roll_spread_information(


def create_instrument_roll_order(
data: dataBlob,
roll_spread_info: rollSpreadInformation,
instrument_code: str,
type_of_roll: named_object,
Expand All @@ -326,7 +328,9 @@ def create_instrument_roll_order(
)
else:
instrument_order = create_instrument_roll_order_closing_priced_contract(
roll_spread_info=roll_spread_info, instrument_code=instrument_code
data=data,
roll_spread_info=roll_spread_info,
instrument_code=instrument_code,
)

return instrument_order
Expand All @@ -353,10 +357,13 @@ def create_instrument_roll_order_for_flat_roll(


def create_instrument_roll_order_closing_priced_contract(
data: dataBlob,
roll_spread_info: rollSpreadInformation,
instrument_code: str,
) -> instrumentOrder:
strategy = ROLL_PSEUDO_STRATEGY
strategy = get_strategy_name_with_largest_position_for_instrument(
data=data, instrument_code=instrument_code
)
position_priced = roll_spread_info.position_in_priced
trade = -position_priced
instrument_order = instrumentOrder(
Expand All @@ -373,6 +380,21 @@ def create_instrument_roll_order_closing_priced_contract(
return instrument_order


def get_strategy_name_with_largest_position_for_instrument(
data: dataBlob, instrument_code: str
) -> str:
diag_positions = diagPositions(data)
all_instrument_positions = (
diag_positions.get_all_current_strategy_instrument_positions()
)

return (
all_instrument_positions.strategy_name_with_largest_abs_position_for_instrument(
instrument_code
)
)


def create_contract_roll_orders(
data: dataBlob,
roll_spread_info: rollSpreadInformation,
Expand All @@ -386,7 +408,9 @@ def create_contract_roll_orders(
return missing_order

if type_of_roll is roll_state_is_close_near_contract:
contract_orders = create_contract_orders_close_first_contract(roll_spread_info)
contract_orders = create_contract_orders_close_first_contract(
roll_spread_info=roll_spread_info, instrument_order=instrument_order
)

elif diag_positions.is_roll_state_force(instrument_code):
contract_orders = create_contract_orders_spread(roll_spread_info)
Expand All @@ -408,13 +432,13 @@ def create_contract_roll_orders(


def create_contract_orders_close_first_contract(
roll_spread_info: rollSpreadInformation,
roll_spread_info: rollSpreadInformation, instrument_order: instrumentOrder
) -> listOfOrders:
strategy = ROLL_PSEUDO_STRATEGY
strategy = instrument_order.strategy_name

first_order = contractOrder(
strategy,
roll_spread_info.instrument_code,
instrument_order.instrument_code,
roll_spread_info.priced_contract_id,
-roll_spread_info.position_in_priced,
reference_price=roll_spread_info.reference_price_priced_contract,
Expand Down
48 changes: 44 additions & 4 deletions sysobjects/production/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,38 @@ def as_pd_df(self) -> pd.DataFrame:
def _as_set_of_dicts(self) -> dict:
# start with
output_dict = self._id_column_dict()
positions_column = [position.position for position in self]

output_dict[KEY_POSITION] = positions_column
output_dict[KEY_POSITION] = self._list_of_positions()

return output_dict

def _id_column_dict(self) -> dict:
id_column_list = [str(position.tradeable_object) for position in self]
id_column_list = self._list_of_ids()
id_column_dict = {KEY_TRADEABLE_OBJECT: id_column_list}
return id_column_dict

def tradeable_object_with_largest_abs_position(self):
idx_of_largest_position = self._idx_of_largest_position()
list_of_tradeable_objects = self._list_of_tradeable_objects()
return list_of_tradeable_objects[idx_of_largest_position]

def _idx_of_largest_position(self) -> int:
list_of_positions = self._list_of_positions()
abs_list_of_positions = [abs(position) for position in list_of_positions]
return abs_list_of_positions.index(max(abs_list_of_positions))

def _list_of_positions(self) -> list:
return [position.position for position in self]

def _list_of_ids(self) -> list:
list_of_tradeable_objects = self._list_of_tradeable_objects()
id_column_list = [
str(tradeable_object) for tradeable_object in list_of_tradeable_objects
]
return id_column_list

def _list_of_tradeable_objects(self) -> list:
return [position.tradeable_object for position in self]


KEY_INSTRUMENT_CODE = "instrument_code"

Expand Down Expand Up @@ -258,6 +279,25 @@ def _element_object_from_row(dfrow):

return list_of_positions

def strategy_name_with_largest_abs_position_for_instrument(
self, instrument_code: str
) -> str:
positions_for_instrument = self.subset_for_instrument(instrument_code)
tradeable_object = (
positions_for_instrument.tradeable_object_with_largest_abs_position()
)

return tradeable_object.strategy_name

def subset_for_instrument(
self, instrument_code: str
) -> "listOfInstrumentStrategyPositions":
subset_list = [
position for position in self if position.instrument_code == instrument_code
]

return listOfInstrumentStrategyPositions(subset_list)

def _id_column_dict(self) -> dict:
instrument_code_list = [str(position.instrument_code) for position in self]
strategy_name_list = [str(position.strategy_name) for position in self]
Expand Down

0 comments on commit dedacf9

Please sign in to comment.