Quantstrat version of a reddit post:

http://www.reddit.com/r/algotrading/comments/wrblb/not_enough_code_on_this_sub_check_out_my_first/

Two rules:

1. Buy when SMA(10) > SMA(100)

2. Hold Cash Otherwise

*** THIS IS NOT INVESTMENT ADVICE, I AM NOT YOUR BROKER, ADVISOR, FUND MANAGER OR HAVE ANY DUTY OF CARE, FIDUCIARY DUTY OR ANYTHING SIMILAR. I AM NOT LIABLE FOR ANY LOSSES ***

As always Credit is due to the respective authors of the packages and the methodologies/inspiration.

#clear workspace (for testing) rm ( list = ls ( ) ) #load libraries library ( quantmod ) library ( PerformanceAnalytics ) library ( blotter ) library ( FinancialInstrument ) try ( library ( quantstrat ) ) #clear portfolio and acct not needed due to the clearing workspace but here incase you don't use it. suppressWarnings ( rm ( "account.stocky" , "portfolio.stocky" , pos=.blotter ) ) suppressWarnings ( rm ( "order_book.stocky" , pos=.strategy ) ) suppressWarnings ( rm ( stocky ) ) #if your stock is different you need to change (initdate,initportf,addposlimit, chart.posn) symbol = "SPY" #Set up currencies currency ( "USD" ) #define stock change spy to your stock of choice stock ( symbol , currency= "USD" , multiplier = 1 ) ####################################################### Get Data ################################################# getSymbols ( symbol , src= "yahoo" , from = "1900-01-01" , to= "2012-06-01" ) #Set Initial Date and Equity, note change SPY to your stock of choice. initDate = start ( SPY ) initEq = 10000 ########################## Set up portfolio orders and Acct ####################################### #change SPY to your stock choice initPortf ( name= "stocky" , "SPY" , initPosQty= 0 , initDate=initDate , currency= "USD" ) initAcct ( "stocky" , portfolios= "stocky" , initDate=initDate , initEq=initEq ) initOrders ( "stocky" , symbols = symbol , initDate=initDate ) #position limits #change "SPY" to your stock choice addPosLimit ( "stocky" , "SPY" , timestamp =initDate , maxpos= 100 , minpos= 0 ) #Set up Strategy stratstocky <- strategy ( "stocky" ) ##############################FUNCTIONS################################# # PUT YOUR CUSTOM Indicator here MAcross <- function ( x ) { step1 <- SMA ( x , n= 10 ) step2 <- SMA ( x , n= 100 ) step3 <- step1 - step2 return ( step3 ) } ########################indicators############################# stratstocky <- add.indicator ( strategy = stratstocky , name = "MAcross" , arguments = list ( x = quote ( Cl ( mktdata ) ) ) , label = "macrossed" ) ################################################ Signals ############################# #long sig stratstocky <- add.signal ( strategy = stratstocky , name = "sigThreshold" , arguments = list ( threshold = 0 , column = "macrossed" , relationship = "gte" , cross = TRUE ) , label = "Buytime" ) stratstocky <- add.signal ( strategy = stratstocky , name = "sigThreshold" , arguments = list ( threshold = 0 , column = "macrossed" , relationship = "lt" , cross = TRUE ) , label = "cashtime" ) ######################################## Rules ################################################# #Entry Rule Long stratstocky <- add.rule ( stratstocky , name = "ruleSignal" , arguments = list ( sigcol = "Buytime" , sigval = TRUE , orderqty = 100 , ordertype = "market" , orderside = "long" , pricemethod = "market" , replace = FALSE , TxnFees = - 1 , osFUN = osMaxPos ) , type = "enter" , path.dep = TRUE , label = "Entry" ) #Exit Rules #Exit stratstocky <- add.rule ( stratstocky , name = "ruleSignal" , arguments = list ( sigcol = "cashtime" , sigval = TRUE , orderqty = "all" , ordertype = "market" , orderside = "long" , pricemethod = "market" , replace = FALSE , TxnFees = - 1 ) , type = "exit" , path.dep = TRUE , label = "Exit" ) ############################## Apply Strategy ############################################## out <- applyStrategy ( strategy=stratstocky , portfolios= "stocky" ) updatePortf ( "stocky" ) ############################# Portfolio Return Characterics ################################ #get portfolio data portRet <- PortfReturns ( "stocky" ) portRet $ Total <- rowSums ( portRet , na.rm= TRUE ) charts.PerformanceSummary ( portRet $ Total ) tradeStats ( "stocky" ) [ , c ( "Symbol" , "Num.Trades" , "Net.Trading.PL" , "maxDrawdown" ) ] #change SPY to your stock choice chart.Posn ( "stocky" , "SPY" ) results1 <- getTxns ( "stocky" , "SPY" ) #plot(results1$Net.Txn.Realized.PL)

Created by Pretty R at inside-R.org





