Skip to content

Commit

Permalink
Chore: Make release 1.0.91
Browse files Browse the repository at this point in the history
  • Loading branch information
martinroberson committed Jun 28, 2024
1 parent 9f07207 commit 51a7ff1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
15 changes: 14 additions & 1 deletion gs_quant/backtests/strategy_systematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@
ISO_FORMAT = r"^([0-9]{4})-([0-9]{2})-([0-9]{2})$"


class StrategySystematic:
class StrategySystematicBase(metaclass=ABCMeta):
@abstractmethod
def backtest(
self,
start: datetime.date = None,
end: datetime.date = datetime.date.today() - datetime.timedelta(days=1),
is_async: bool = False,
measures: Iterable[FlowVolBacktestMeasure] = (FlowVolBacktestMeasure.ALL_MEASURES,),
correlation_id: str = None
) -> Union[Backtest, BacktestResult]:
...


class StrategySystematic(StrategySystematicBase):
"""Equity back testing systematic strategy"""

def __init__(self,
Expand Down
53 changes: 53 additions & 0 deletions gs_quant/backtests/strategy_systematic_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Copyright 2019 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
"""

from typing import Union, Iterable, Tuple

from gs_quant.backtests import TradeInMethod, StrategySystematic
from gs_quant.common import Currency
from gs_quant.instrument import EqOption, EqVarianceSwap, Instrument
from gs_quant.target.backtests import BacktestTradingQuantityType, DeltaHedgeParameters, BacktestSignalSeriesItem, \
EquityMarketModel


class StrategySystematicFactory:
@staticmethod
def get(underliers: Union[Instrument, Iterable[Instrument]],
quantity: float = 1,
quantity_type: Union[BacktestTradingQuantityType, str] = BacktestTradingQuantityType.notional,
trade_in_method: Union[TradeInMethod, str] = TradeInMethod.FixedRoll,
roll_frequency: str = None,
scaling_method: str = None,
index_initial_value: float = 0.0,
delta_hedge: DeltaHedgeParameters = None,
name: str = None,
cost_netting: bool = False,
currency: Union[Currency, str] = Currency.USD,
trade_in_signals: Tuple[BacktestSignalSeriesItem, ...] = None,
trade_out_signals: Tuple[BacktestSignalSeriesItem, ...] = None,
market_model: Union[EquityMarketModel, str] = EquityMarketModel.SFK,
roll_date_mode: str = None,
expiry_date_mode: str = None,
cash_accrual: bool = True):
supported_eq_inst = (EqOption, EqVarianceSwap)
if (isinstance(underliers, Instrument) and isinstance(underliers, supported_eq_inst)) or \
isinstance(underliers, Iterable) and all(isinstance(u, supported_eq_inst) for u in underliers):
return StrategySystematic(underliers, quantity, quantity_type, trade_in_method, roll_frequency,
scaling_method, index_initial_value, delta_hedge, name, cost_netting,
currency, trade_in_signals, trade_out_signals, market_model, roll_date_mode,
expiry_date_mode, cash_accrual)
else:
raise NotImplementedError('StrategySystematic only implemented for equity underliers')
9 changes: 6 additions & 3 deletions gs_quant/risk/result_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
from typing import Iterable, Optional, Union

from gs_quant.base import InstrumentBase, RiskKey
from gs_quant.common import RiskMeasure, AssetClass, RiskMeasureType
from gs_quant.risk.measures import PnlExplain

from .core import DataFrameWithInfo, ErrorValue, UnsupportedValue, FloatWithInfo, SeriesWithInfo, StringWithInfo, \
sort_values, MQVSValidatorDefnsWithInfo, MQVSValidatorDefn
from gs_quant.common import RiskMeasure, AssetClass, RiskMeasureType

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -219,9 +218,13 @@ def fixing_table_handler(result: dict, risk_key: RiskKey, _instrument: Instrumen

def simple_valtable_handler(result: dict, risk_key: RiskKey, _instrument: InstrumentBase,
request_id: Optional[str] = None) -> DataFrameWithInfo:
def get_value(value):
handler = result_handlers.get(value.get('$type'))
return handler(value, risk_key, _instrument, request_id)

raw_res = result['rows']
# simplevaltable's values contain all the information on units which needs to be extracted into the dataframe
df = DataFrameWithInfo([(res['label'], res['value']['val']) for res in raw_res], risk_key=risk_key,
df = DataFrameWithInfo([(res['label'], get_value(res['value'])) for res in raw_res], risk_key=risk_key,
request_id=request_id, unit=raw_res[0]['value'].get('unit'))
df.columns = ['label', 'value']
return df
Expand Down
16 changes: 8 additions & 8 deletions gs_quant/target/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,21 @@ class EqAutoroll(Instrument):
@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(unsafe_hash=True, repr=False)
class EqBarrier(Instrument):
underlier: Optional[str] = field(default=None, metadata=field_metadata)
underlier: Optional[Union[float, str]] = field(default=None, metadata=field_metadata)
buy_sell: Optional[BuySell] = field(default=None, metadata=field_metadata)
option_type: Optional[OptionType] = field(default=None, metadata=field_metadata)
expiration_date: Optional[str] = field(default=None, metadata=field_metadata)
strike_price: Optional[float] = field(default=None, metadata=field_metadata)
barrier_start_date: Optional[str] = field(default=None, metadata=field_metadata)
barrier_end_date: Optional[str] = field(default=None, metadata=field_metadata)
barrier_level: Optional[float] = field(default=None, metadata=field_metadata)
expiration_date: Optional[Union[datetime.date, str]] = field(default=None, metadata=field_metadata)
strike_price: Optional[Union[float, str]] = field(default=None, metadata=field_metadata)
barrier_start_date: Optional[Union[datetime.date, str]] = field(default=None, metadata=field_metadata)
barrier_end_date: Optional[Union[datetime.date, str]] = field(default=None, metadata=field_metadata)
barrier_level: Optional[Union[float, str]] = field(default=None, metadata=field_metadata)
knock_up_or_down: Optional[UpDown] = field(default=None, metadata=field_metadata)
barrier_frequency: Optional[str] = field(default=None, metadata=field_metadata)
knock_in_or_out: Optional[InOut] = field(default=None, metadata=field_metadata)
number_of_options: Optional[float] = field(default=None, metadata=field_metadata)
multiplier: Optional[float] = field(default=None, metadata=field_metadata)
premium: Optional[float] = field(default=None, metadata=field_metadata)
premium_settlement_date: Optional[str] = field(default=None, metadata=field_metadata)
premium: Optional[Union[float, str]] = field(default=None, metadata=field_metadata)
premium_settlement_date: Optional[Union[datetime.date, str]] = field(default=None, metadata=field_metadata)
currency: Optional[str] = field(default=None, metadata=field_metadata)
asset_class: Optional[AssetClass] = field(init=False, default=AssetClass.Equity, metadata=field_metadata)
type_: Optional[AssetType] = field(init=False, default=AssetType.Barrier, metadata=config(field_name='type', exclude=exclude_none))
Expand Down

0 comments on commit 51a7ff1

Please sign in to comment.