def rerun_existing_simulations(): total_count = MySimulationCondition.objects.all().count() for counter, condition in enumerate(MySimulationCondition.objects.filter(strategy=2)): logger.debug('%s: %d/%d'%(inspect.stack()[1][3], counter, total_count)) # simulation run backtesting_simulation_consumer.delay(cPickle.dumps(condition), is_update=True)
def form_valid(self, form): # persist simulation conditions condition, created = MySimulationCondition.objects.get_or_create( **form.cleaned_data) # run simulation for 1st time if not MyPosition.objects.filter(simulation=condition): # MySimulationCondition is not JSON serializable # so we use cPickle to pass object to background queue task backtesting_simulation_consumer.delay(cPickle.dumps(condition)) return HttpResponseRedirect(reverse_lazy('simulation_result_list')) else: return HttpResponseRedirect(reverse_lazy('condition_detail', kwargs={'pk': condition.id}))
def batch_simulation_daily_return(date_range, strategies = [1,2], capital = 10000, per_trade = 1000): sources = [1, 2, 3, 5] strategy_values = [1] # get 8211 related sectors stock_8211 = MyStock.objects.filter(symbol__startswith="8821") sectors = [None] + reduce(lambda x, y: x + y, [list(s.mysector_set.all()) for s in stock_8211]) # simulation run conditions = [] for (source, strategy, strategy_value, sector) in itertools.product(sources, strategies, strategy_values, sectors): for (start,end) in date_range: # we only try strategy 2 with source 1 if strategy == 2 and source != 1: continue # we only try strategy 5 with sector if sector and source != 5: continue # simulation parameters logger.debug(source, strategy, strategy_value, start, end, sector) # cutoffs have different meanings based on strategy if strategy == 1: step = 25 buy_cutoff = range(0, 100, step) sell_cutoff = [b + step for b in buy_cutoff] cutoffs = zip(buy_cutoff, sell_cutoff) elif strategy == 2: buy_cutoff = range(1,6,1) sell_cutoff = range(1,6,1) cutoffs = itertools.product(buy_cutoff, sell_cutoff) # cutoffs = [(1,1)] # Set up simulation condition objects based on # combination of specified parameters. Note that # the count of this matrix increases dramatically # if we expand this parameter list. for (buy_cutoff, sell_cutoff) in cutoffs: condition, created = MySimulationCondition.objects.get_or_create( data_source=source, sector=sector, data_sort=1, # descending strategy=strategy, strategy_value=strategy_value, start=start, end=end, capital=capital, per_trade=per_trade, buy_cutoff=buy_cutoff, sell_cutoff=sell_cutoff ) conditions.append(condition) # simulation run! for condition in conditions: # if condition.data_source == 1 and strategy == 2: # MySimulationCondition is not json-able, # using python pickle instead. The downside of this is that # we are relying on a python-specif data format. # But it is safe in this context. if strategy == 2: # buy low sell high # Set is_update=True will remove all existing results first # and then rerun all simulations. This is necessary # because SP500 is gettting new data each day. backtesting_simulation_consumer.delay( cPickle.dumps(condition), is_update=True) else: # alpha # Because computing alpha index values are very time consuming, # so we are to skip existing results to save time. Ideally # we should set is_update=True to recompute from a clean sheet. backtesting_simulation_consumer.delay( cPickle.dumps(condition), is_update=False)