Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Starting with the second question </p> <pre><code>&gt; s &lt;- getSymbols('SPY') &gt; nrow(s) NULL &gt; class(s) [1] "character" &gt; s.data &lt;- get(s) &gt; class(s.data) [1] "xts" "zoo" &gt; nrow(s.data) [1] 1635 </code></pre> <p>So if you want to work on the actual <code>xts</code> object you need to use <code>get</code>.</p> <p>About your first question - i don't think you really need to pull the data as a vector - the <code>xts</code> object is an array indexed by date and it's easy to work with. If you still want to get the data you can use </p> <pre><code>closing.prices &lt;- coredata(Cl(s)) </code></pre> <p>Now, to get you started with simple back testing of strategies i will suggest working in the following steps </p> <p>define your strategy. 2. create an array or add a column to your xts object that will represent your position for each day. 1 for long, 0 for no position and -1 for short (later on you can play with the number for leverage). 3. multiply each days return with the position and you'll get your strategy return vector. 4. examine the results - my recommendation is <code>PerformanceAnalytics</code>.</p> <p>simple strategy - buy when close over SMA20 , sell under</p> <pre><code>library(quantmod) library(PerformanceAnalytics) s &lt;- get(getSymbols('SPY'))["2012::"] s$sma20 &lt;- SMA(Cl(s) , 20) s$position &lt;- ifelse(Cl(s) &gt; s$sma20 , 1 , -1) myReturn &lt;- lag(s$position) * dailyReturn(s) charts.PerformanceSummary(cbind(dailyReturn(s),myReturn)) </code></pre> <p>and this is what you'll get </p> <p><img src="https://i.stack.imgur.com/O2xg3.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload