|
Thanks - I tried most everything else but I guess I still haven't
got all of this in my mind. I suppose debugging R is better than
_javascript_ but on marginally. I've attached the working version if anyone needs it, but I still have to add costs and, I guess a stop loss , and so on. Stephen Choularton Ph.D., FIoD
9999 2226 0413 545 182 ![]() for insurance go to www.netinsure.com.au for markets go to www.organicfoodmarkets.com.au On 26/01/2011 2:47 PM, Aleksandr Rudnev wrote: On Tue, Jan 25, 2011 at 3:54 PM, Stephen Choularton <[email protected]> wrote:...The rules will be executed by type, in the order listed above. ... Has anyone got this problem and solved it? |
#########################################################################################################################################################################
#A simple moving average strategy to evaluate trade efficiency
#checks on SMA of 50 days and SMA of 200 days
#Author: R. Raghuraman("raghu"), Brian Peterson
#########################################################################################################################################################################
require(quantstrat)
try(rm("order_book.macross",pos=.strategy),silent=TRUE)
#get("order_book.macross",pos=.strategy)
try(rm("account.macross","portfolio.macross",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","stock.str","stratMACROSS","initDate","initEq",'start_t','end_t'),silent=TRUE)
initDate='1999-12-31'
initEq=1000000
stock.str='^AXJO' # what are we trying it on
stock.str=getSymbols(stock.str,from=as.Date(initDate)+1)
# changeable short & long MA's
# shorter periods did NOT work, eg 10/20 20/60
shortMA <- 50
longMA <- 200
stock(stock.str,currency='AUD',multiplier=1)
portfolio.st='macross'
account.st='macross'
initPortf(portfolio.st,symbols=stock.str, currency='AUD', initDate=initDate)
#initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios='macross', currency='AUD', initDate=initDate)
#initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
stratMACROSS<- strategy(portfolio.st)
# indicators
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments
= list(x=quote(Cl(mktdata)), n=shortMA),label= "shortMA" )
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments
= list(x=quote(Cl(mktdata)), n=longMA),label= "longMA")
# signals
# long side
stratMACROSS <- add.signal(strategy =
stratMACROSS,name="sigCrossover",arguments =
list(columns=c("shortMA","longMA"),relationship="gt"),label="shortMA.gt.longMA")
stratMACROSS <- add.signal(strategy =
stratMACROSS,name="sigCrossover",arguments =
list(column=c("shortMA","longMA"),relationship="lt"),label="shortMA.lt.longMA")
# short side
stratMACROSS <- add.signal(strategy =
stratMACROSS,name="sigCrossover",arguments =
list(columns=c("shortMA","longMA"),relationship="lt"),label="shortMA.lt.longMA")
stratMACROSS <- add.signal(strategy =
stratMACROSS,name="sigCrossover",arguments =
list(column=c("shortMA","longMA"),relationship="gt"),label="shortMA.gt.longMA")
# rules
# txnExp = ???
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments
= list(sigcol="shortMA.lt.longMA",sigval=TRUE, orderqty=-100,
ordertype='market', orderside='long', threshold=NULL, replace=FALSE
),type='exit')
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments
= list(sigcol="shortMA.gt.longMA",sigval=TRUE, orderqty=100,
ordertype='market', orderside='long', threshold=NULL, replace=FALSE
),type='enter')
# if you want a long/short Stops and Reverse MA cross strategy, you'd add two
more rules for the short side:
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments
= list(sigcol="shortMA.lt.longMA",sigval=TRUE, orderqty=-100,
ordertype='market', orderside='short', threshold=NULL, replace=FALSE
),type='enter')
stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments
= list(sigcol="shortMA.gt.longMA",sigval=TRUE, orderqty=100,
ordertype='market', orderside='short', threshold=NULL, replace=FALSE
),type='exit')
#getSymbols('^AXJO',currency='AUD', from=initDate)
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratMACROSS ,
portfolios=portfolio.st,verbose=TRUE))
end_t<-Sys.time()
print(end_t-start_t)
start_t<-Sys.time()
updatePortf(Portfolio='macross',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)
add_SMA(n=shortMA , on=1,col='blue')
add_SMA(n=longMA , on=1)
#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)
###############################################################################
# R (http://r-project.org/) Quantitative Strategy Model Framework
#
# Copyright (c) 2009-2010
# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua
Ulrich
#
# This library is distributed under the terms of the GNU Public License (GPL)
# for full details see the file COPYING
#
# $Id: maCross.R 374 2010-08-17 18:43:35Z braverock $
#
###############################################################################
_______________________________________________ [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.

