if len(old_series) == 0: return new_series if len(new_series) == 0: return old_series joint_data = pd.concat([old_series, new_series], axis=1) joint_data.columns = ["original", "new"] # fill to the left joint_data_filled_across = joint_data.bfill(1) merged_data = joint_data_filled_across["original"] return merged_data all_labels_match = _named_object("all labels match") mismatch_on_last_day = _named_object("mismatch_on_last_day") original_index_matches_new = _named_object("original index matches new") def merge_data_series_with_label_column(original_data, new_data, col_names=dict( data="PRICE", label="PRICE_CONTRACT")): """ For two pd.DataFrames with 2 columns, including a label column, update the data when the labels start consistently matching >>> s1=pd.DataFrame(dict(PRICE=[1,2,3,np.nan], PRICE_CONTRACT = ["a", "a", "b", "b"]), index=['a1','a2','a3','a4']) >>> s2=pd.DataFrame(dict(PRICE=[ 7,3,4], PRICE_CONTRACT = [ "b", "b", "b"]), index=['a2','a3','a4'])
Force1=['Force', 'Passive', 'No_Roll'], Roll_Adjusted0=['No_Roll'], Roll_Adjusted1=['Roll_Adjusted']) status_plus_position = complete_roll_state(current_roll_state, priced_position) try: allowable_states = allowed_transition[status_plus_position] except KeyError: raise Exception("State plus position %s not recognised" % status_plus_position) return allowable_states no_state_available = _named_object("No state") class rollStateData(baseData): """ Store and retrieve the roll state of a particular instrument """ def __init__(self, log=logtoscreen("rollStateData")): super().__init__(log=log) self.name = "rollStateData" def get_roll_state(self, instrument_code): state = self._get_roll_state_no_default(instrument_code) if state is no_state_available:
def update_with_multiple_prices_no_roll(self, updated_multiple_prices): """ Update adjusted prices assuming no roll has happened :param updated_multiple_prices: futuresMultiplePrices :return: updated adjusted prices """ updated_adj = update_adjusted_prices_from_multiple_no_roll( self, updated_multiple_prices) return updated_adj no_update_roll_has_occured = _named_object("Roll has occured") def update_adjusted_prices_from_multiple_no_roll(existing_adjusted_prices, updated_multiple_prices): """ Update adjusted prices assuming no roll has happened :param existing_adjusted_prices: futuresAdjustedPrices :param updated_multiple_prices: futuresMultiplePrices :return: updated adjusted prices """ last_date_in_adj = existing_adjusted_prices.index[-1] multiple_prices_as_dict = updated_multiple_prices.as_dict()
import os import pandas as pd import psutil from syscore.fileutils import html_table from syscore.dateutils import SECONDS_PER_DAY, last_run_or_heartbeat_from_date_or_none from syscore.objects import ( success, failure, _named_object, missing_data, ) process_stop = _named_object("process stop") process_no_run = _named_object("process no run") process_running = _named_object("process running") go_status = "GO" no_run_status = "NO-RUN" stop_status = "STOP" default_process_id = 0 possible_status = [go_status, no_run_status, stop_status] start_run_idx = 0 end_run_idx = 1 missing_date_str = ""
import numpy as np from syscore.genutils import sign from syscore.objects import _named_object from sysexecution.orders.base_orders import Order from sysexecution.trade_qty import tradeQuantity override_close = _named_object("Close") override_no_trading = _named_object("No trading") override_reduce_only = _named_object("Reduce only") override_none = _named_object("No override") override_dict = { override_close: 0.0, override_none: 1.0, override_no_trading: -1.0, override_reduce_only: -2.0, } _override_lookup = [] for key, value in override_dict.items(): _override_lookup.append((value, key)) def lookup_value_and_return_float_or_object(value): value_list = [entry[1] for entry in _override_lookup if entry[0] == value] if len(value_list) == 0: return value else: return value_list[0]
self._strategy_name = strategy_name @property def instrument_code(self): return self._instrument_code @property def strategy_name(self): return self._strategy_name @property def key(self): return "%s/%s" % (self.strategy_name, self.instrument_code) NO_LIMIT = _named_object("no limit") class positionLimit(object): def __init__(self, tradeable_object, position_limit: int): self._tradeable_object = tradeable_object self._position_limit = position_limit def __repr__(self): return "Position limit for %s is %s" % (str( self.key), str(self.position_limit)) @property def position_limit(self): return self._position_limit
from syscore.objects import _named_object, success from sysdata.data import baseData from syslogdiag.log import logtoscreen from sysdata.futures.contracts import futuresContract default_position = 0 no_position_available = _named_object("No position") class positionByContractData(baseData): """ Store and retrieve the position we currently have in a given futures contract """ def __init__(self, log=logtoscreen("rollStateData")): super().__init__(log=log) self._roll_dict = {} self.name = "positionByContractData" def get_position_for_instrument_and_contract_date(self, instrument_code, contract_date): contract_object = futuresContract(instrument_code, contract_date) position = self.get_position_for_contract(contract_object) return position def update_position_for_instrument_and_contract_date( self, instrument_code, contract_date, new_position):