Hedging Simulations
Backtesting and Trading/Hedging simulation
Bruno.BackTest.buy
— Functionbuy(
fin_obj::FinancialInstrument,
number,
holdings,
pricing_model,
trasaction_cost;
kwargs...
)
buy(
fin_obj::Widget,
number,
holdings,
pricing_model,
trasaction_cost;
kwargs...
)
Records buying a specified number of fin_obj
in a holdings dictionary based on the given pricing_model. To be used in strategy()
functions to define trading and hedging strategies.
Arguments
fin_obj
: the financial object to be bought. Can be a subtype of FinancialInstrument or Widgetnumber
: number of objects to be boughtholdings
: dictionary with all holdings of widgets and financial instruments (generally supplied by strategy_returns() function)pricing_model
: Model subtype to be used to define buy pricetransaction_cost
: total transaction costs for the transactionkwargs
: pass through for any keyword arguments needed by thepricing_model
inprice!()
function
Bruno.BackTest.sell
— Functionsell(
fin_obj::FinancialInstrument,
number,
holdings,
pricing_model,
trasaction_cost;
kwargs...
)
sell(
fin_obj::Widget,
number,
holdings,
pricing_model,
trasaction_cost;
kwargs...
)
Records selling a specified number of fin_obj
in a holdings dictionary based on the given pricing_model. To be used in strategy()
functions to define trading and hedging strategies.
Arguments
fin_obj
: the financial object to be sold. Can be a subtype of FinancialInstrument or Widgetnumber
: number of objects to be soldholdings
: dictionary with all holdings of widgets and financial instruments (generally supplied by strategy_returns() function)pricing_model
: Model subtype to be used to define sell pricetransaction_cost
: total transaction costs for the transactionkwargs
: pass through for any keyword arguments needed by thepricing_model
inprice!()
function
Bruno.BackTest.strategy
— Functionstrategy(
fin_obj::FinancialInstrument,
pricing_model,
strategy_model,
holdings,
step;
kwargs...
)
function to be used with strategy_returns()
function. strategy()
function defines buying and selling behavior for a given trading or hedging strategy. To use with strategy_returns()
function, define a new Hedging
subtype to allow for dispatch and write a new method for strategy()
dispatcing off this new type in the strategy_mode
argument. See package documentation for more information.
Arguments
fin_obj
: the FinancialInstrument the strategy is defined forpricing_model
: the pricing model used by theprice!()
function to price the FinancialInstrumentholdings
: the dictionary of current holdings of FinancialInstruments and base assets supplied by thestrategy_returns()
functionstep
: the step number out ofn_timesteps
thestrategy_returns
is currently executing
strategy(
fin_obj::FinancialInstrument,
pricing_model,
strategy_model,
holdings,
step;
kwargs...
)
function to be used with strategy_returns()
function. strategy()
function defines buying and selling behavior for a given trading or hedging strategy. To use with strategy_returns()
function, define a new Hedging
subtype to allow for dispatch and write a new method for strategy()
dispatcing off this new type in the strategy_mode
argument. See package documentation for more information.
Arguments
obj_array::Vector{<:FinancialInstrument}
: the vector of FinancialInstruments the strategy runs onpricing_model
: the pricing model used by theprice!()
function to price the FinancialInstrumentholdings
: the dictionary of current holdings of FinancialInstruments and base assets supplied by thestrategy_returns()
functionstep
: the step number out ofn_timesteps
thestrategy_returns
is currently executing
Bruno.BackTest.strategy_returns
— Functionstrategy_returns(
obj::FinancialInstrument,
pricing_model,
strategy_type,
future_prices,
n_timesteps,
timesteps_per_period,
cash_injection = 0.0,
fin_obj_count = 0.0,
widget_count = 0.0,
pay_int_rate = 0.0,
hold_return_int_rate = 0.0;
kwargs...
)
a simulating environment to test trading or hedging strategies for given interest rates and and prices. To be used by providing a new method for the strategy()
function which defines the trading strategy.
Returns the dollar cumulative return from the strategy, the time-series of all holdings the strategy, and the updated object array.
Arguments
obj
: financial instrument the trading or hedging strategy runs onpricing_model
:Model
subtype that defines how to price theobj
strategy_type
:Hedging
subtype that thestrategy()
function dispatches off. Must provide a new subtype for newstrategy()
methodsfuture_prices
: vector of future prices for the underlyingWidget
asset of obj to run strategy onn_timesteps
: number of timesteps to test the strategy ontimesteps_per_period
: for the size of a timestep in the data, the number of
time steps for a given period of time, cannot be negative. For example, if the period of interest is a year, and daily stock data is used, timesteps_per_period=252
. Must be positive.
cash_injection
: amount of cash owned when starting the strategyfin_obj_count
: amount of financial instruments owned when starting the strategywidget_count
: amount of underlyingWidget
owned when starting the strategypay_int_rate
: the continuous interest rate payed on negative cash balanceshold_return_int_rate
: the continous interest rate earned on positive cash balanceskwargs
: pass through for keyword arguments needed byprice!()
orstrategy()
functions
Example
# make the Widget and FinancialInstrument to be used
stock = Stock(; prices=[99, 97, 90, 83, 83, 88, 88, 89, 97, 100], name="stock", timesteps_per_period=252)
call = EuroCallOption(stock, 110; maturity=.5, label="call", risk_free_rate=.02)
# make future_prices array
future_prices = [100, 104, 109, 105, 108, 108, 101, 101, 104, 110]
fin_obj_count = 2
widget_count = 3
pay_int_rate = .05
hold_return_int_rate = .02
cumulative_return, ts_holdings, obj = strategy_returns(
call,
BlackScholes,
Naked,
future_prices,
10,
252,
10.0,
fin_obj_count,
widget_count,
pay_int_rate,
hold_return_int_rate;
transaction_cost = 0.0
)
strategy_returns(
objs::Vector{<:FinancialInstrument},
pricing_model,
strategy_type,
future_prices,
n_timesteps,
timesteps_per_period,
cash_injection = 0.0,
fin_obj_count,
widget_count,
pay_int_rate = 0.0,
hold_return_int_rate = 0.0;
kwargs...
)
a simulating environment to test trading or hedging strategies for multiple financial instruments for given interest rates and prices. To be used by providing a new method for the strategy()
function which defines the trading strategy.
Returns the dollar cumulative return from the strategy, the time-series of all holdings the strategy, and the updated object array.
Arguments
objs::Vector{<:FinancialInstrument}
: vector of financial instruments the trading or hedging strategy runs onpricing_model
:Model
subtype that defines how to price theobj
strategy_type
:Hedging
subtype that thestrategy()
function dispatches off. Must provide a new subtype for newstrategy()
methodsfuture_prices
: dictionary of vectors of future prices for underlyingWidget
assets used in financial instruments inobjs
Note: dictionary keys must be the widget.name
field string for each base asset
n_timesteps
: number of timesteps to test the strategy ontimesteps_per_period
: for the size of a timestep in the data, the number of
time steps for a given period of time, cannot be negative. For example, if the period of interest is a year, and daily stock data is used, timesteps_per_period=252
. Must be positive.
cash_injection
: amount of cash owned when starting the strategyfin_obj_count
: dictionary of amounts of financial instruments owned when starting the strategy
Note: dictionary keys must be the FinancialInstrument.label
field string for each financial instrument
widget_count
: dictionary of amounts of base assets used in financial instruments owned when starting the strategy
Note: dictionary keys must be the Widget.name
field string for each base asset
pay_int_rate
: the continuous interest rate payed on negative cash balanceshold_return_int_rate
: the continous interest rate earned on positive cash balanceskwargs
: pass through for keyword arguments needed byprice!()
orstrategy()
functions
Example
# make the widgets and FinancialInstruments to be used
stock = Stock(; prices=[99, 97, 90, 83, 83, 88, 88, 89, 97, 100], name="stock", timesteps_per_period=252)
stock2 = Stock(; prices=[66, 61, 70, 55, 65, 63, 57, 55, 53, 68], name="stock2", timesteps_per_period=252)
call = EuroCallOption(stock, 110; maturity=.5, label="call", risk_free_rate=.02)
call2 = EuroCallOption(stock2, 70; maturity=1, label="call2", risk_free_rate=.02)
objs = [call, call2]
# make a Dict with future_prices for each widget
future_prices = Dict(
"stock" => [100, 104, 109, 105, 108, 108, 101, 101, 104, 110],
"stock2" => [67, 74, 73, 67, 67, 75, 69, 71, 69, 70]
)
# make dictionaries for the starting amounts held of each Widget and FinancialInstrument
fin_obj_count = Dict("call" => 1.0, "call2" => 2)
widget_count = Dict("stock" => 2.0, "stock2" => 3)
cash_injection = 0.0
pay_int_rate = 0.08
hold_return_int_rate = 0.02
cumulative_return, ts_holdings, obj_array = strategy_returns(
objs,
BlackScholes,
Naked,
future_prices,
10,
252,
cash_injection,
fin_obj_count,
widget_count,
pay_int_rate,
hold_return_int_rate
)