def simulate(): ''' strategy_params=dict(top=60,btm=40,rsi=14,average=111,duration=5)) +--------+-----+-------+------+---------+---------+---------+---------+ | _score | cnt | w_pct | pr | pnl | mtm_pnl | max_equ | max_dd | +--------+-----+-------+------+---------+---------+---------+---------+ | 5.70 | 97 | 0.61 | 1.25 | 7163.57 | 7163.57 | 7608.81 | 1129.33 | +--------+-----+-------+------+---------+---------+---------+---------+ equity curve = 570x.png strategy_params=dict(top=60,btm=40,rsi=12,average=180,duration=7)) +--------+-----+-------+------+----------+----------+----------+---------+ | _score | cnt | w_pct | pr | pnl | mtm_pnl | max_equ | max_dd | +--------+-----+-------+------+----------+----------+----------+---------+ | 7.56 | 106 | 0.67 | 1.22 | 11139.79 | 11139.79 | 11158.79 | 1330.16 | +--------+-----+-------+------+----------+----------+----------+---------+ equity curve = 756x.png ''' m = RSIStrategy(name='rsi2_spy', strategy_params=dict(top=60, btm=40, rsi=12, average=180, duration=7)) s = Simulator() s.add_strategy(m) s.run(DataFeedList(['SPY.csv'], data_type='D')) s.write('rsi2_SPY_%s' % datetime.now().strftime("%Y%m%d"))
def simulate_AAPL_SPY(): m = RSIStrategy(name='rsi2_aapl_spy', strategy_params=dict(top=60, btm=40, rsi=12, average=180, duration=7)) s = Simulator() s.add_strategy(m) s.run(DataFeedList(['AAPL.csv', 'SPY.csv'], data_type='D')) s.write('rsi2_AAPL_SPY_%s' % datetime.now().strftime("%Y%m%d"))
def simulate_SPY_AAPL_composite(): ''' test to make sure that a composite file (AAPL and SPY combined) is the same as the individuals (AAPL.csv and SPY.csv separately) ''' m = RSIStrategy(name='rsi2_spy_v_aapl', strategy_params=dict(top=60, btm=40, rsi=12, average=180, duration=7)) s = Simulator() s.add_strategy(m) s.run(DataFeedList(['SPY_AAPL.csv'], data_type='D')) s.write('rsi2_SPYvAAPL_%s' % datetime.now().strftime("%Y%m%d"))
def run(self): run_id = 0 while not self.converged(): params_set = self.generate_set() for strategy_params in params_set: log.info('run_id = %d: %s' % (run_id, pprint.pformat(strategy_params))) name = '%s_%s' % (self.strategy_class.__name__, self._tempname()) s = Simulator() s.reset_on_EOD = self.reset_on_EOD ## if you want have unique portfolio names per optimizer run ## s.portfolio.name = "".join(['portfolio_',name]) s.verbose = self.verbose s.add_strategy( self.strategy_class(name, strategy_setup=self.strategy_setup, strategy_params=strategy_params)) self.data_feed.reset() summary = s.run(self.data_feed) self.score(summary, run_id) run_id += 1 self.dump(display=self.display_dump)
from Simulator import Simulator from RetraceStrategy import RetraceStrategy from DataFeed import DataFeedList if __name__ == '__main__': r = RetraceStrategy('tiger', strategy_params=dict(average=150, duration=20, momentum=40)) d = DataFeedList(['tiger_data.csv'], data_type='D') s = Simulator() s.add_strategy(r) s.run(d) s.write('tiger_150_20_40') s.show()
def run(self, count=50): log.info('strategy: %s' % self.strategy_class.__name__) log.info('strategy_params: %s' % self.strategy_params) self.best = None self.shelf = {} for i in xrange(count): s = Simulator() s.reset_on_EOD = self.reset_on_EOD name = '%s_%s' % (self.strategy_class.__name__, self._tempname()) s.add_strategy( self.strategy_class(name, strategy_setup=self.strategy_setup, strategy_params=self.strategy_params)) self.data_feed.reset() log.info('\n%d/%d\nStart Strategy= %s' % (i + 1, count, name)) summary = s.run(self.data_feed) log.info('End Strategy= %s' % name) key = (summary['cnt'], summary['pnl'], summary['mtm_pnl'], summary['max_dd']) self.record[key] += 1 if not self.best or (self.record[key] >= self.best[1]): self.best = (key, self.record[key], name) self.shelf[key] = s.portfolio.storage group = [] columns = ['score', 'cnt', 'trades', 'pnl', 'mtm_pnl', 'max_dd'] p = PrettyTable(columns) for k, v in self.record.iteritems(): u = dict(cnt=k[0], mtm_pnl=k[2], max_dd=k[3]) score = fitness_function(u) u.update({'score': score, 'trades': k[0], 'pnl': k[1]}) group.append(u) p.add_row([score, v, k[0], k[1], k[2], k[3]]) ## tack on summary information df = pandas.DataFrame(group) means = [] devs = [] for c in columns: if c in ['score', 'pnl', 'mtm_pnl', 'max_dd']: p.float_format[c] = '0.2' p.align[c] = 'r' m = df[c].mean() means.append(m) ## calc % of dispersion relative to avg pnl devs.append(df[c].std() / m) elif c in ['trades']: means.append(' ') devs.append(' ') else: means.append('AVG') devs.append('DSP') p.add_row(means) p.add_row(devs) log.info('\n%s' % p) log.info('Best Simulation: Strategy= %s' % self.best[2]) ## return dataframe - just in case you want further analysis self.df_out = df
from Simulator import Simulator from RetraceStrategy import RetraceStrategy from DataFeed import DataFeedList from MStrategy2 import MStrategy2 s = Simulator() ''' s.add_strategy(RetraceStrategy('R2',strategy_params = dict(average=141,momentum=21,duration=15))) #data_feed = DataFeedList(['daily.SPY.csv','daily.SPY.csv'],data_type='D') data_feed = DataFeedList(['daily.SPY.csv','daily.SPY.csv'],data_type='D') s.run(data_feed) s.write('R2') ## testing DataFeedBar serial files strat = MStrategy2('M2',strategy_params = dict(length=20,duration=15)) #strat.capture_data = True s.add_strategy(strat) s.reset_on_EOD = False data_feed = DataFeedList(['20081210.SPY.1min.csv','20081211.SPY.1min.csv','20081212.SPY.1min.csv'],data_type='B') s.run(data_feed) #strat.dump_data().to_csv('M2_data_dump.csv') s.write('M2') ''' ## testing DataFeedIntraday serial files strat = MStrategy2('M2', strategy_params=dict(length=30, duration=50)) #strat.capture_data = True s.add_strategy(strat) s.reset_on_EOD = False #data_feed = DataFeedList(['20081210.SPY.30s.csv','20081211.SPY.30s.csv'],data_type='I') #data_feed = DataFeedList(['combo.SPY.30s.csv'],data_type='I')