Wrapper function to automatically rebalance a portfolio. Requires a portfolio_model
input (see create_portfolio_model()
or read_portfolio_model()
), a trading connection and a data connection as a minimum (see, for example, alpaca_connect()
although others may be supported in the future).
balance_portfolio(
portfolio_model,
trading_connection,
pricing_connection,
min_order_size = 1000,
max_order_size = 10000,
daily_vol_pct_limit = 0.02,
pricing_spread_tolerance = 0.02,
pricing_overrides = NULL,
trader_life = 30,
exit_if_market_closed = TRUE,
resubmit_interval = 5,
buy_only = FALSE,
verbose = TRUE
)
a portfolio model, as created by create_portfolio_model()
a connection to a trading backend
a connection to a data backend
minimum trader order size
maximum trader order size
trader()
function will not trade more than this percentage of a symbol's trailing 10 day average daily volume
limit order setting function will not set a price if the bid-ask spread for a symbol is greater than this spread
options. Use this to set your own symbol price limits
duration in seconds that the trader()
should keep trading before timing out
should the trader()
quit if the market is closed
duration in seconds that a trader()
should keep an order in the market before cancelling it and resubmitting at an updated limit price
TRUE/FALSE flag to indicate whether the trader()
should limit itself to only buy orders
TRUE/FALSE flag to indicate if the function should emit messages
a list containing a portfolio_balanced
value (TRUE/FALSE), a data frame indicating the drift
of each symbol, and a data frame detailing the trades
attempted.
if (FALSE) {
t_conn <- alpaca_connect('paper',
Sys.getenv("ALPACA_PAPER_KEY"),
Sys.getenv("ALPACA_PAPER_SECRET"))
d_conn <- alpaca_connect('data',
Sys.getenv("ALPACA_LIVE_KEY"),
Sys.getenv("ALPACA_LIVE_SECRET"))
portfolio_model <- read_portfolio_model(system.file(package='rblncr','extdata/sample_portfolio.yaml'))
balance_portfolio(portfolio_model,
t_conn,
d_conn,
verbose = F)
}