def setUp(self):
     
     import strategy_tester.market_data as md
     import strategy_tester.trade as td
     import strategy_tester.Portfolio as pt
     import strategy_tester.TradeStrategyClasses as tsc
     
     import rpy2.robjects as ro
     from rpy2.robjects.packages import importr
     zoo = importr('zoo')
           
     #Create moving average market data type
     AORD = ro.r('read.table("~/Documents/R/StrategyTester/Data/AORD.csv",header=T, sep=",")')
     AORD = zoo.as_zoo(AORD)#, order_by = ro.r.rownames(AORD))
     MAD1 = md.simple_ma_md(AORD,'AORD.Close', 10, 20)
     
     #Create empty cash trade
     trade_cash =td.Trade("Cash","Cash", 0)
     #Setup empty cash portfolio
     port1 = pt.Portfolio("port1")
     port1.add_trade(trade_cash)
     [td.name for td in port1.trades]
     
     #Setup MA trade Strategy
     #Note have checked that time 58 will have 'buy' signal
     self.trade_strat = tsc.MA_Trade_Strategy(MAD1,port1,59)
Esempio n. 2
0
 def test_MD_simpleMovingAverage(self):
     
     #Test the moving averages are contructed with length >= 1
     sma = simple_ma_md(self.SP500)
     sma.generateTradeSig(seriesName = 'SP500', 
                          sig_index = 'Adj_Close', 
                          short_win = 10, 
                          long_win = 20)
     
     rm = pd.rolling_mean(sma['Adj_Close'],20)
     self.assertTrue((sma['MAl'][20:] == rm[20:]).all(), 'simpleMAshort not working')
Esempio n. 3
0
 
 #Load data from db
 dr = dbr.DBReader(conString)
 dataFull = dr.readSeries('AAPL')
 print dataFull.head()
 plot = dataFull['Adj_Close'].plot()
 
 #Change Date Range
 d1 = datetime(2012,1,1).date()
 d2 = datetime(2012,6,1).date()
 
 data = dataFull.loc[d1:d2]
 print data.head()
 
 #Generate trade sigs for simple moving average
 sma = md.simple_ma_md(data)
 sma.generateTradeSig('AAPL',
                      'Adj_Close',
                      short_win = 10,
                      long_win = 20)
 sma.plot()
 
 #Generate trade sigs for exponentially weighted moving ave 
 ema = md.exponential_ma_md(data)
 ema.generateTradeSig('AAPL',
                      'Adj_Close',
                      short_span = 10,
                      long_span = 20)
 ema.plot()
 
 
Esempio n. 4
0
#Create Portfolio and value
port_test = pt.Portfolio("port_test")
port_test.add_trade(trade_eq)
print port_test.trades
port_test.add_trade(trade_cash) 
print port_test.trades

ps_test = pt.PortfolioSlice(port_test, MD1, 1)
ps_test.price()
sum(ps_test.value())

#Test Trade Strategy
#Crossover in the moving averages
reload(tsc)
reload(td)
MAD1 = md.simple_ma_md(AORD,'AORD.Close', 10, 20)
MAD1.core_data['MAl'][0:30]

MAD1.core_data['AORD.Close'][56:100].plot(style = 'k--')
MAD1.core_data['MAl'][56:100].plot(style = 'k')
MAD1.core_data['MAs'][56:100].plot(style = 'k')

#plt.show()

MADSlice0 = md.market_data_slice(MAD1,20)
MADSlice1 = md.market_data_slice(MAD1,58)
MADSlice0.data[['MAl','MAs']] == [5644.1750000000011, 5713.830]
MADSlice1.data[['MAl','MAs']]
MADSlice0.data['MAl'] == 5644.1750000000011
x = MADSlice0.data['MAl']
#Test updateSignal