Skip to content

Commit

Permalink
added optimal position data
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Carver committed Nov 20, 2023
1 parent 2c3a50f commit 68154c7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
60 changes: 60 additions & 0 deletions sysdata/parquet/parquet_optimal_positions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

from syscore.exceptions import missingData
from sysdata.parquet.parquet_access import ParquetAccess
from sysdata.production.optimal_positions import optimalPositionData
from syslogging.logger import *

from sysobjects.production.tradeable_object import (
instrumentStrategy,
listOfInstrumentStrategies,
)

import pandas as pd

OPTIMAL_POSITION_COLLECTION = "optimal_positions"


class parquetOptimalPositionData(optimalPositionData):
def __init__(self, parquet_access: ParquetAccess, log=get_logger("parquetOptimalPositionData")):

super().__init__(log=log)
self._parquet = parquet_access

def __repr__(self):
return "parquetOptimalPositionData"

@property
def parquet(self):
return self._parquet

def get_list_of_instrument_strategies_with_optimal_position(
self,
) -> listOfInstrumentStrategies:

raw_list_of_instrument_strategies = self.parquet.get_all_identifiers_with_data_type(data_type=OPTIMAL_POSITION_COLLECTION)
list_of_instrument_strategies = [
instrumentStrategy.from_key(key)
for key in raw_list_of_instrument_strategies
]

return listOfInstrumentStrategies(list_of_instrument_strategies)

def get_optimal_position_as_df_for_instrument_strategy(
self, instrument_strategy: instrumentStrategy
) -> pd.DataFrame:

try:
ident = instrument_strategy.key
df_result = self.parquet.read_data_given_data_type_and_identifier(data_type=OPTIMAL_POSITION_COLLECTION, identifier=ident)
except:
raise missingData

return df_result

def write_optimal_position_as_df_for_instrument_strategy_without_checking(
self,
instrument_strategy: instrumentStrategy,
optimal_positions_as_df: pd.DataFrame,
):
ident = instrument_strategy.key
self.parquet.write_data_given_data_type_and_identifier(data_type=OPTIMAL_POSITION_COLLECTION, identifier=ident, data_to_write=optimal_positions_as_df)
26 changes: 22 additions & 4 deletions sysinit/transfer/backup_arctic_to_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sysdata.parquet.parquet_multiple_prices import parquetFuturesMultiplePricesData
from sysdata.parquet.parquet_spotfx_prices import parquetFxPricesData
from sysdata.parquet.parquet_spreads import parquetSpreadsForInstrumentData
from sysdata.parquet.parquet_optimal_positions import parquetOptimalPositionData

from sysdata.csv.csv_futures_contracts import csvFuturesContractData
from sysdata.csv.csv_contract_position_data import csvContractPositionData
Expand Down Expand Up @@ -85,7 +86,9 @@ def backup_arctic_to_parquet():
do = true_if_answer_is_yes("Time series of spread costs?")
if do:
backup_spreads_to_parquet(backup_data)
#backup_optimal_positions(backup_data)
do = true_if_answer_is_yes("optimal positions?")
if do:
backup_optimal_positions(backup_data)
#backup_roll_state_data(backup_data)


Expand All @@ -106,7 +109,7 @@ def get_data_blob(logname):
parquetFuturesAdjustedPricesData,
#csvFuturesContractData,
parquetFxPricesData,
#csvOptimalPositionData,
parquetOptimalPositionData,
#csvRollStateData,
#csvSpreadCostData,
parquetSpreadsForInstrumentData,
Expand Down Expand Up @@ -430,10 +433,25 @@ def backup_optimal_positions(data):
)
except missingData:
continue
data.csv_optimal_position.write_optimal_position_as_df_for_instrument_strategy_without_checking(

try:
parquet_data = data.parquet_optimal_position.get_optimal_position_as_df_for_instrument_strategy(
instrument_strategy
)
except missingData:
parquet_data=[]

if len(parquet_data)>=len(arctic_data):
data.log.debug("skipping already written")

data.parquet_optimal_position.write_optimal_position_as_df_for_instrument_strategy_without_checking(
instrument_strategy, arctic_data
)
data.log.debug("Backed up %s optimal position data" % str(instrument_strategy))
parquet_data = data.parquet_optimal_position.get_optimal_position_as_df_for_instrument_strategy(
instrument_strategy
)

data.log.debug("Backed up %s optimal position data was %s now %s" % (str(instrument_strategy), str(arctic_data), str(parquet_data)))


def backup_spread_cost_data(data):
Expand Down

0 comments on commit 68154c7

Please sign in to comment.