[R-SIG-Finance] Stock Model Question!

Brian G. Peterson brian at braverock.com
Wed Sep 28 12:02:20 CEST 2011


On Wed, 2011-09-28 at 11:25 +0200, Anna Dunietz wrote:
> 
> I am interested in plotting my portfolio's performance and don't
> exactly know how to do it.  I have built a (not-so-great) model - the
> data below is found below.  I used information on each individual S&P
> 500 stock over the past ~10 years (RSI, moving averages, price
> changes, and price).  Selling the stock occurs when a stop loss or
> take profit is signaled.  The model was built without using the
> blotter or quantstrat packages - it was built with a which function.
> The code is quite long. However, if you think it will be helpful, I
> can always attach it.  I would like inital equity to be $100000,
> and I would like to invest $25000 per buy signal.  I hope this is
> clear enough.  If not, please let me know!
> 
> Thanks!
> Anna
> 
> 
>        BuyDate         Equity BuyPrice Action   SellDate SellPrice
> PerformancePerc.
> 51  2001-07-02  FCX.UN.Equity   5.3600   LONG 2001-07-11    5.9450
>   0.10914179
> 122 2001-07-02  TIE.UN.Equity   2.2875   LONG 2001-07-17    2.6050
>   0.13879781
> 82  2001-07-13    L.UN.Equity  19.2867   LONG 2001-07-20   18.2667
>  -0.05288619 
<...>

Anna,

Of course posting a reproducible example *always* helps.

You have all the data to create a bar chart of returns for each of your
investments, but not a time series of returns. I'll assume you're
looking for a time series chart, for which you must construct a time
series.

To use any of the open source package tools (in R or any other
language!) for backtesting investment models, you'll need either a list
of transactions, or a set of periodic returns.

It seems that you are close to generating this from your code, but
you've munged everything together on one line.  Your output is a nice
summary of actions and results, but insufficient for charting.

It's nice that your summary table includes 'PerformancePerc', and you
could of course use that to generate a total performance if those are
log returns (which would be additive), but your data does not have
enough information to construct a time series of returns from.

You have a few options, which may be easier or harder based on your
other code which you have not shared:

- subset your input asset return time series for times when you are in
the market with each of your instruments, (invert if short), turn each
of those subsets into log returns if they aren't already, apply weights
if necessary, cbind, add each row for portfolio return, and plot your
portfolio return column using charts.PerformanceSummary or similar.

- extract your transactions into a time series by asset.  Feed those
transactions into blotter or equivalent.  If using blotter, use
extractReturns to get your returns out, and use
charts.PerformanceSummary to or equivalent to chart.

- probably least appealing, rewrite your model using one of the packages
 
The key is that to chart your returns, you need the time series with
returns at each point in time, not just the summary output.  Given the
data you've posted, I can't unmix your mixture.  The good news is that
you probably still have all the components in your code, you'll just
need to add some of them to your output.

I'll note that the reason we/I wrote all these packages was to eliminate
most of the issues I ran into every time I wrote a custom one-off model,
because all those standard bits would be done, and I wouldn't have to
redo or adapt custom code each time.  Every model has custom code, of
course, but the idea was to maximise reuse of the common bits and focus
coding on the bits that are specific to one model, to the degree
possible. Of course, you give up something in return, often speed of
execution (though blotter/quantstrat can be very fast, your which
statement could be faster), or some degreee of flexibility.

You indicated that your code is 'quite long'.  It seems likely that your
indicators, signals, and rules for a(ny) model using "RSI, moving
averages, price changes, and price" could be recast in far less code
using quantstrat, though that of course is not necessary, and I trust my
pointers above will point you in the right direction.

Regards,

   - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list