-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rob Carver
committed
Nov 20, 2023
1 parent
2939dd4
commit 225390e
Showing
3 changed files
with
180 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import pandas as pd | ||
|
||
from sysobjects.contracts import futuresContract, listOfFuturesContracts | ||
from sysdata.parquet.parquet_access import ParquetAccess | ||
|
||
from sysdata.production.historic_contract_positions import contractPositionData | ||
|
||
from syscore.exceptions import missingData | ||
|
||
from syslogging.logger import * | ||
|
||
CONTRACT_POSITION_COLLECTION = "contract_positions" | ||
|
||
|
||
class parquetContractPositionData(contractPositionData): | ||
def __init__(self, parquet_access: ParquetAccess, log=get_logger("parquetContractPositionData")): | ||
|
||
super().__init__(log=log) | ||
|
||
self._parquet = parquet_access | ||
|
||
def __repr__(self): | ||
return "parquetContractPositionData" | ||
|
||
@property | ||
def parquet(self): | ||
return self._parquet | ||
|
||
def _write_updated_position_series_for_contract_object( | ||
self, contract_object: futuresContract, updated_series: pd.Series | ||
): | ||
## overwrites what is there without checking | ||
ident = contract_object.key | ||
updated_data_as_df = pd.DataFrame(updated_series) | ||
updated_data_as_df.columns = ["position"] | ||
|
||
self.parquet.write_data_given_data_type_and_identifier(data_to_write=updated_data_as_df, identifier=ident, data_type=CONTRACT_POSITION_COLLECTION) | ||
|
||
def _delete_position_series_for_contract_object_without_checking( | ||
self, contract_object: futuresContract | ||
): | ||
ident = contract_object.key | ||
self.parquet.delete_data_given_data_type_and_identifier(data_type=CONTRACT_POSITION_COLLECTION, identifier=ident) | ||
|
||
def get_position_as_series_for_contract_object( | ||
self, contract_object: futuresContract | ||
) -> pd.Series: | ||
keyname = contract_object.key | ||
try: | ||
pd_df = self.parquet.read_data_given_data_type_and_identifier(data_type=CONTRACT_POSITION_COLLECTION, identifier=keyname) | ||
except: | ||
raise missingData | ||
|
||
return pd_df.iloc[:, 0] | ||
|
||
def get_list_of_contracts(self) -> listOfFuturesContracts: | ||
## doesn't remove zero positions | ||
list_of_keys = self.parquet.get_all_identifiers_with_data_type(data_type=CONTRACT_POSITION_COLLECTION) | ||
list_of_futures_contract = [ | ||
futuresContract.from_key(key) for key in list_of_keys | ||
] | ||
|
||
return listOfFuturesContracts(list_of_futures_contract) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pandas as pd | ||
|
||
from sysobjects.production.tradeable_object import ( | ||
listOfInstrumentStrategies, | ||
instrumentStrategy, | ||
) | ||
from sysdata.parquet.parquet_access import ParquetAccess | ||
from sysdata.production.historic_strategy_positions import strategyPositionData | ||
from syscore.exceptions import missingData | ||
|
||
from syslogging.logger import * | ||
|
||
STRATEGY_POSITION_COLLECTION = "strategy_positions" | ||
|
||
|
||
class parquetStrategyPositionData(strategyPositionData): | ||
def __init__(self, parquet_access: ParquetAccess, log=get_logger("parquetStrategyPositionData")): | ||
|
||
super().__init__(log=log) | ||
|
||
self._parquet = parquet_access | ||
|
||
def __repr__(self): | ||
return "parquetStrategyPositionData" | ||
|
||
@property | ||
@property | ||
def parquet(self): | ||
return self._parquet | ||
|
||
def get_list_of_instrument_strategies(self) -> listOfInstrumentStrategies: | ||
list_of_keys = self.parquet.get_all_identifiers_with_data_type(data_type=STRATEGY_POSITION_COLLECTION) | ||
list_of_instrument_strategies = [ | ||
instrumentStrategy.from_key(key) for key in list_of_keys | ||
] | ||
|
||
return listOfInstrumentStrategies(list_of_instrument_strategies) | ||
|
||
def _write_updated_position_series_for_instrument_strategy_object( | ||
self, instrument_strategy: instrumentStrategy, updated_series: pd.Series | ||
): | ||
|
||
ident = instrument_strategy.key | ||
updated_data_as_df = pd.DataFrame(updated_series) | ||
updated_data_as_df.columns = ["position"] | ||
|
||
self.parquet.write_data_given_data_type_and_identifier(data_to_write=updated_data_as_df, identifier=ident, data_type=STRATEGY_POSITION_COLLECTION) | ||
|
||
def _delete_position_series_for_instrument_strategy_object_without_checking( | ||
self, instrument_strategy: instrumentStrategy | ||
): | ||
ident = instrument_strategy.key | ||
self.parquet.delete_data_given_data_type_and_identifier(data_type=STRATEGY_POSITION_COLLECTION, identifier=ident) | ||
|
||
def get_position_as_series_for_instrument_strategy_object( | ||
self, instrument_strategy: instrumentStrategy | ||
) -> pd.Series: | ||
|
||
keyname = instrument_strategy.key | ||
try: | ||
pd_df = self.parquet.read_data_given_data_type_and_identifier(data_type=STRATEGY_POSITION_COLLECTION, identifier=keyname) | ||
except: | ||
raise missingData | ||
|
||
return pd_df.iloc[:, 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters