Hi Brian

Thanks for that.

Still trying to make sure I have a handle on this. I am working on the macd.R demo example but have started from 31/12/2010 to make the data easier to eyeball.

Obviously I am trying to see the stop work and I notice that a 95% threshold should do this in August when it produces a stop figure of $255.30375 and it can be seen below the closing price falls below this on 2010-08-11

2010-08-02 260.44 262.59 259.62 261.85 15287700 261.85 0.33850430 2010-08-03 261.01 263.26 259.42 261.93 14916200 261.93 0.42744640 2010-08-04 262.84 264.28 260.31 262.98 15013400 262.98 0.52431485 2010-08-05 261.73 263.18 260.55 261.70 10324900 261.70 0.55460413 2010-08-06 259.78 261.49 257.63 260.09 15889200 260.09 0.52246936 2010-08-09 261.48 262.15 259.57 261.75 10826000 261.75 0.54227647 2010-08-10 259.85 260.45 257.55 259.41 16140000 259.41 0.47975631 2010-08-11 255.40 255.69 249.81 250.19 22144800 250.19 0.14151831 2010-08-12 246.69 253.10 246.12 251.79 19104300 251.79 -0.07671837 2010-08-13 251.65 251.88 249.09 249.10 12673900 249.10 -0.33112468 2010-08-16 247.58 250.01 246.62 247.64 11372500 247.64 -0.57338811

The order book looks like this:


> getOrderBook(portfolio.st)
$macd
$macd$AAPL
           Order.Qty Order.Price Order.Type  Order.Side Order.Threshold
2009-12-31 "0"       NA          "init"      "long"     "0"
2010-03-08 "-100"    "213.603"   "stoplimit" "long"     "0.975"
2010-03-08 "100"     "219.08"    "market"    "long"     NA
2010-07-15 "-100"    "251.45"    "market"    "long"     NA
2010-08-02 "-100"    "255.30375" "stoplimit" "long"     "0.975"
2010-08-02 "100"     "261.85"    "market"    "long"     NA
2010-08-16 "-100"    "247.64"    "market"    "long"     NA
2010-09-14 "-100"    "261.3585"  "stoplimit" "long"     "0.975"
2010-09-14 "100"     "268.06"    "market"    "long"     NA
           Order.Status Order.StatusTime Order.Set Txn.Fees
2009-12-31 "closed"     "2009-12-31"     "1"       "0"
2010-03-08 "replaced"   "2010-08-02"     NA        "0"
2010-03-08 "closed"     "2010-03-09"     NA        "0"
2010-07-15 "closed"     "2010-07-16"     NA        "0"
2010-08-02 "replaced"   "2010-09-14"     NA        "0"
2010-08-02 "closed"     "2010-08-03"     NA        "0"
2010-08-16 "closed"     "2010-08-17"     NA        "0"
2010-09-14 "open"       NA               NA        "0"
2010-09-14 "closed"     "2010-09-15"     NA        "0"

but the actual transactions like this:


> getTxns(Portfolio=portfolio.st, Symbol=stock.str)
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
2009-12-31       0      0.00        0         0         0.00
2010-03-08     100    219.08        0     21908       219.08
2010-07-15    -100    251.45        0    -25145       251.45
2010-08-02     100    261.85        0     26185       261.85
2010-08-16    -100    247.64        0    -24764       247.64
2010-09-14     100    268.06        0     26806       268.06
           Net.Txn.Realized.PL
2009-12-31                   0
2010-03-08                   0
2010-07-15                3237
2010-08-02                   0
2010-08-16               -1421
2010-09-14

Can anyone tell me what I am doing wrong?



Stephen Choularton Ph.D., FIoD


On 24/02/2011 9:55 PM, Brian G. Peterson wrote:
On 02/24/2011 03:31 AM, Stephen Choularton wrote:
Hi and thanks.

  I had noticed these but I am having difficulty in understand the
'mechanics'.

  I notice the column names in mktdata are:

AAPL.Open     AAPL.High     AAPL.Low     AAPL.Close     AAPL.Volume
AAPL.Adjusted macd     signal     signal.gt.zero     signal.lt.zero


when I have stoplimit live.     I can see where everything but 'signal'
comes from.  Can anyone advise what puts it there ?  I was guessing
add.signal calls did this.

It would be more appropriate to say that these columns are added when applySignals is called inside applyStrategy, and in turn executes the signal function sigThreshold. For porting to a moving average strategy, you'd probably want to use sigCrossover-based signals instead, because the value of the MA is not known beforehand.

As should be obvious from the demo code and the documentation, the columns you asked about are are the label= parameters from your signals.

In the indicator function, we do not specify a label, because the MACD() function from TTR adds the labels 'macd' and 'signal' to its output.

In the signal generators, we specify a label as 'signal.gt.zero' and 'signal.lt.zero' when we define the signals with add.signal so that we know what the 'sigcol' will be for our later rules. These labels aer arbitrary, and were chosen to indicate what the signal stands for, but could just as easily have been called 'blue' and 'brown'.

As the documents describe, what you are doing up front with all the add.* functions is just building the strategy object, nothing is 'added to mktdata' until you've called applyStrategy on a portfolio (which in turn calls applyIndicators and applySignals in a vectorized way and applyRules in a path-dependent manner).

  - Brian




-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1204 / Virus Database: 1435/3464 - Release Date: 02/23/11


 
 require(quantstrat)

 try(rm("order_book.macd",pos=.strategy),silent=TRUE)

 try(rm("account.macd","portfolio.macd",pos=.blotter),silent=TRUE)

 
try(rm("account.st","portfolio.st","stock.str","stratMACD","initDate","initEq",'start_t','end_t'),silent=TRUE)

 stock.str='AAPL' # what are we trying it on

 #MA parameters for MACD
 fastMA = 12 

 slowMA = 26 

 signalMA = 9

 maType="EMA"

 currency('USD')

 stock(stock.str,currency='USD',multiplier=1)

 #or use fake data
 #stock.str='sample_matrix' # what are we trying it on
 #data(sample_matrix)                 # data included in package xts
 #sample_matrix<-as.xts(sample_matrix)
 
 
 initDate='2009-12-31'

 initEq=1000000

 portfolio.st='macd'

 account.st='macd'

 initPortf(portfolio.st,symbols=stock.str, initDate=initDate)

 initAcct(account.st,portfolios=portfolio.st, initDate=initDate)

 initOrders(portfolio=portfolio.st,initDate=initDate)

 stratMACD <- strategy(portfolio.st)

 # indicator
 stratMACD <- add.indicator(strategy = stratMACD, name = "MACD", arguments = 
list(x=quote(Cl(mktdata))) )

 # signal
 stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = 
list(column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")

 # signal
 stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = 
list(column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")

 # opening rule
 stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = 
list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=100, ordertype='market', 
orderside='long', threshold=NULL),type='enter')

 # stoplimit rule
 stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = 
list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100, ordertype='stoplimit', 
orderside='long', threshold=.975,tmult=TRUE),type='risk')

 # alternately, use a trailing order rule
 # stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = 
list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100, 
ordertype='stoptrailing', orderside='long', 
threshold=.9,tmult=TRUE),type='risk')
 
 
 # closing rule
 stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = 
list(sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', ordertype='market', 
orderside='long', threshold=NULL),type='exit')

 getSymbols(stock.str,from=initDate)

 start_t<-Sys.time()

 out<-try(applyStrategy(strategy=stratMACD , 
portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, 
nSig=signalMA,maType=maType)))

 end_t<-Sys.time()

 print(end_t-start_t)


 start_t<-Sys.time()

 
updatePortf(Portfolio=portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))

 end_t<-Sys.time()

 print("trade blotter portfolio update:")

 print(end_t-start_t)

 chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)

 plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA,maType="EMA"))

#get/save some data

write.table(mktdata, 'c:/r/logs/mktdata.tbl', col.name=TRUE)

print ('order book')


getOrderBook(portfolio.st)

print('transactions')


getTxns(Portfolio=portfolio.st, Symbol=stock.str)

print('account')
  
getAccount(portfolio.st)

_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should 
go.

Reply via email to