Description Usage Arguments Details Value
This function executes overnight trades based on a dataframe of returns predictions and actual returns for a number of securities. Multiple securities may be traded on the same date. The function allows for a maximum number of simultaneous positions, a maximum portfolio weight per position and long and short thresholds tested against the predicted return (yhat) to trade only those securities with the highest return potential.
1 2 3 4 | trade_overnight(predmat, maxposn = 10, maxweight = 0.25,
longthresh = 0.01, shortthresh = -100, dolvolname = NA,
dolvolthresh = 1e+06, dateseries = NA, datecol = NA, yhatcol = "yhat",
retcol = "rets")
|
predmat |
A dataframe or xts matrix containing at minumum a date column, a prediction column (yhat) and an overnight returns colum. The names of these columns should be specified with the datecol, yhatcol and retcol arguments. Other columns are considered ID columns and are passed through to the trade details xts that is returned. Such columns may contain, for example, the ticker symbols. |
maxposn |
The maximum number of equally weighted simultaneous positions to take for an overnight trade. |
maxweight |
The maximum weight given to any trade. This is important to limit how large a position will be when only one or very stocks are traded during one day. |
longthresh |
The threshold above which a long trade is taken. This threshold is compared with the yhatcol value for this decision. |
shortthresh |
The threshold below which a short trade is taken. This threshold is compared with the yhatcol value for this decision. NOT YET IMPLEMENTED! |
dolvolname |
The column name in predmat that corresponds to the dollar volume traded for the stock. Typically, this will be a 3 month rolling daily dollar volume average. It is used to filter out stocks that are thinly traded by eliminating trades when the dollar volume is less than dolvolthresh. |
dolvolthresh |
The dollar volume threshold used to filter out stock trades below this number. |
dateseries |
An optional xts series (such as SPY or GSPC) that has all dates the market is open. This is used to fill in dates not present in predmat to carry forward the equity curve. This way, the resulting equity curve can be plotted without any big time frames missing. Default is NA (no dates provided) |
yhatcol |
The column name in predmat with the predicted values (yhat). |
retcol |
The column name in predmat with the overnight returns. Note that the assumed overnight return starts on the current date (close of market) to the next day's sale. The user must ensure these returns are properly lagged to ensure there is no lookahead bias. |
datacol |
The column name in predmat containing the dates. If predmat is an xts matrix, this should be set to NA which will result in taking the xts matrix dates instead. |
A dataframe with at least three columns must be provided: the actual overnight return, a return estimate (yhat) generated by some model, and the date such trade would have been executed. Other columns may be included for trade identification purposes, such as the ticker symbol. Each row corresponds to a potential stock trade, so there may be many rows with different stocks on the same date.
The long threshold and short thresholds are used to test yhat and select only those securities where yhat >= long_thresh or yhat <= short_thresh for long and short trades respectively.
When more trades are available than maxposn, the trades are sorted by the absolute value of yhat in descending order, then the top maxposn trades are selected. This allows similar and fair treatment of long and short trades. If a long only strategy is desired, simply set short_thresh to a large negative number.
During the trade sort, identical values of yhat are not reordered, per the "shell" method in function sort. See ?sort.
Returns a list of three items:
$summary An xts matrix with the resulting equity curve. The xts matrix index will either correspond to the predmat dates only (dateseries = NA), or an outer merge of the predmat dates with the dates in dateseries, properly subsetted at the start and ending boundaries of predmat.
In addition to the equity curve, the matrix includes a column for the number of trades performed on each date (Ntrades), and the total number of stocks available to trade on each date (allstocks).
$returns A single column xts matrix of the equity curve returns.
$trade_details A dataframe containing the details of each trade executed.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.