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
)

Arguments

portfolio_model

a portfolio model, as created by create_portfolio_model()

trading_connection

a connection to a trading backend

pricing_connection

a connection to a data backend

min_order_size

minimum trader order size

max_order_size

maximum trader order size

daily_vol_pct_limit

trader() function will not trade more than this percentage of a symbol's trailing 10 day average daily volume

pricing_spread_tolerance

limit order setting function will not set a price if the bid-ask spread for a symbol is greater than this spread

pricing_overrides

options. Use this to set your own symbol price limits

trader_life

duration in seconds that the trader() should keep trading before timing out

exit_if_market_closed

should the trader() quit if the market is closed

resubmit_interval

duration in seconds that a trader() should keep an order in the market before cancelling it and resubmitting at an updated limit price

buy_only

TRUE/FALSE flag to indicate whether the trader() should limit itself to only buy orders

verbose

TRUE/FALSE flag to indicate if the function should emit messages

Value

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.

Examples

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)
                             }