def handle(self, *args, **options): last_pt = PredictionTest.objects.filter(type='mock').order_by('-created_on').first() last_trade = TradeRecommendation.objects.order_by('-created_on').first() print(last_pt.created_on) print(last_trade.created_on) # 7 hours thing is a hack for MST vs UTC timezone issues is_trader_running = last_trade.created_on > (get_time() - datetime.timedelta(hours=int(7)) - datetime.timedelta(minutes=int(15))) is_trainer_running = last_pt.created_on > (get_time() - datetime.timedelta(hours=int(7)) - datetime.timedelta(minutes=int(15))) if not is_trader_running: self.alert_email("not is_trader_running") if not is_trainer_running: self.alert_email("not is_trainer_running")
def run_predictor(self,nn_index): predictor = self.predictors[nn_index] config = self.predictor_configs[nn_index] normalize = config['type'] == 'nn' prices = predictor.get_latest_prices(normalize=normalize) prices = prices[(len(prices)-predictor.datasetinputs):(len(prices)+1)] recommend, nn_price, last_sample, projected_change_pct = predictor.predict(prices) confidence = predictor.confidence() if config['type'] == 'nn': clf = None made_by = predictor else: clf = predictor made_by = None print_and_log("(t)({})---- ({} w. {}% conf) ---- price from {} => {}({}% change); ".format(nn_index,recommend, round(confidence,0), round(last_sample,4), round(nn_price,4), int(projected_change_pct * 100.0))) tr = TradeRecommendation(symbol=config['symbol'], made_on=str(prices), made_by=made_by, clf=clf, confidence=confidence, recommendation=recommend, net_amount = -1 if recommend == 'SELL' else ( 1 if recommend == 'BUY' else 0 ), created_on_str = str(get_time().strftime('%Y-%m-%d %H:%M')) ) tr.save() self.trs[nn_index] = tr return recommend
def run_predictor(self, nn_index): predictor = self.predictors[nn_index] config = self.predictor_configs[nn_index] normalize = config['type'] == 'nn' prices = predictor.get_latest_prices(normalize=normalize) prices = prices[(len(prices) - predictor.datasetinputs):(len(prices) + 1)] recommend, nn_price, last_sample, projected_change_pct = predictor.predict( prices) confidence = predictor.confidence() if config['type'] == 'nn': clf = None made_by = predictor else: clf = predictor made_by = None print_and_log( "(t)({})---- ({} w. {}% conf) ---- price from {} => {}({}% change); " .format(nn_index, recommend, round(confidence, 0), round(last_sample, 4), round(nn_price, 4), int(projected_change_pct * 100.0))) tr = TradeRecommendation(symbol=config['symbol'], made_on=str(prices), made_by=made_by, clf=clf, confidence=confidence, recommendation=recommend, net_amount=-1 if recommend == 'SELL' else (1 if recommend == 'BUY' else 0), created_on_str=str( get_time().strftime('%Y-%m-%d %H:%M'))) tr.save() self.trs[nn_index] = tr return recommend
def optimize_view(request): #setup the_denom=request.GET.get('denom','btc_balance') denoms = ['usd_balance','btc_balance'] days_ago=request.GET.get('days_ago',False) hours_ago=request.GET.get('hours_ago',False) if not hours_ago and not days_ago: hours_ago=6 if days_ago: start_time = (timezone.now() - datetime.timedelta(days=int(days_ago))) elif hours_ago: start_time = (timezone.now() - datetime.timedelta(hours=int(hours_ago))) else: start_time = Balance.objects.order_by('created_on').first().created_on symbol = 'BTC_ETH' #get data data = {} for t in Trade.objects.filter(symbol=symbol,status='fill').order_by('-created_on').all(): date = datetime.datetime.strftime(t.created_on,'%Y-%m-%d') if not date in data.keys(): data[date] = { 'buyvol' : [], 'sellvol' : [], 'buy' : [], 'sell': [] } data[date][t.type].append(t.price) data[date][t.type+'vol'] = data[date][t.type+'vol'] + [ t.amount ] bs = Balance.objects.filter(created_on__gte=start_time).all() last_trade = TradeRecommendation.objects.order_by('-created_on').first() if last_trade: trader_last_seen = (last_trade.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M') is_trader_running = last_trade.created_on > (get_time() - datetime.timedelta(minutes=int(15))) else: trader_last_seen = None is_trader_running = False i = 0 charts = [] chartnames = [] for func in [get_trade_chart,get_trade_profitability_chart,get_directional_change_chart,get_performance_comps_chart,get_ticker_price]: i = i + 1 cht = func(bs,the_denom,symbol,start_time) charts.append(cht) chartnames.append(str(func).split()[1].replace('get_','').replace('_chart','')) options = [] return render_to_response('optimize.html',{ 'days_ago' : [1,2,3,4,5,10,15,30], 'hours_ago' : [1,2,3,6,12,24], 'getparams' : getify(request.GET), 'charts' : charts, 'chartnames' : chartnames, 'chartnamesstr' : ",".join(chartnames), 'denoms' : denoms, 'the_denom' : the_denom, 'is_trader_running' : is_trader_running, 'trader_last_seen' : trader_last_seen, })
def c_chart_view(request): #setup symbol=request.GET.get('symbol','BTC_ETH') i = 0 charts = [] chartnames = [] metas = [] symbols = Price.objects.values('symbol').distinct().order_by('symbol').values_list('symbol',flat=True) #get data pts, symbols_that_exist = get_data(request,symbol,'history_classifiertest',ClassifierTest) if len(pts) == 0: return render_to_response('notfound.html') trainer_last_seen = None try: last_pt = ClassifierTest.objects.filter(type='mock').order_by('-created_on').first() is_trainer_running = last_pt.created_on > (get_time() - datetime.timedelta(minutes=int(15))) trainer_last_seen = (last_pt.created_on- datetime.timedelta(hours=int(7))).strftime('%a %H:%M') except Exception: is_trainer_running = False meta = { 'count' : int(round(pts.count(),0)), 'avg' : round(pts.aggregate(Avg('percent_correct'))['percent_correct__avg'],0), 'median' : round(median_value(pts,'percent_correct'),0), 'max' : round(pts.aggregate(Max('percent_correct'))['percent_correct__max'],0), 'min' : round(pts.aggregate(Min('percent_correct'))['percent_correct__min'],0), } #get global chart information for parameter in ['percent_correct' ,'score']: i = i + 1 cht = get_line_chart(pts,symbol,parameter) charts.append(cht) options = [] chartnames.append("container"+str(i)) metas.append({ 'name' : parameter, 'container_class' : 'show', 'class' : "container"+str(i), 'options' : options, }) # get parameter distribution charts parameters = ['name','datasetinputs','granularity','minutes_back','timedelta_back_in_granularity_increments','time','prediction_size'] for x_axis in parameters: i = i + 1 cht = get_scatter_chart(pts,x_axis,symbol) charts.append(cht) options_dict = pts.values(x_axis).annotate(Avg('percent_correct')).annotate(Count('pk')) options = [ (x_axis, obj[x_axis], int(round(obj['percent_correct__avg'],0)), int(round(obj['pk__count'],0)) ) for obj in options_dict ] options.sort(key=lambda x: x[1]) the_max = max([option[2] for option in options]) for k in range(len(options)): options[k] = options[k] + (("max" if options[k][2] == the_max else "notmax") + " " + ("warning" if options[k][3] < 5 else "nowarning"),) chartnames.append("container"+str(i)) metas.append({ 'name' : x_axis, 'container_class' : 'show' if len(options) > 1 else 'noshow', 'class' : "container"+str(i), 'options' : options, }) #Step 3: Send the chart object to the template. return render_to_response('c_chart.html',{ 'pts' : pts.order_by('percent_correct'), 'ticker' : symbol, 'symbols' : symbols, 'meta' : meta, 'days_ago' : [1,2,3,4,5,10,15,30], 'hours_ago' : [1,2,3,6,12,24], 'getparams' : getify(request.GET), 'charts': charts, 'metas' : metas, 'chartnames' : chartnames, 'chartnamesstr' : ",".join(chartnames), 'is_trainer_running' : is_trainer_running, 'trainer_last_seen' : trainer_last_seen, 'symbols_that_exist' : symbols_that_exist, })
def optimize_view(request): #setup the_denom = request.GET.get('denom', 'btc_balance') denoms = ['usd_balance', 'btc_balance'] days_ago = request.GET.get('days_ago', False) hours_ago = request.GET.get('hours_ago', False) if not hours_ago and not days_ago: hours_ago = 6 if days_ago: start_time = (timezone.now() - datetime.timedelta(days=int(days_ago))) elif hours_ago: start_time = (timezone.now() - datetime.timedelta(hours=int(hours_ago))) else: start_time = Balance.objects.order_by('created_on').first().created_on symbol = 'BTC_ETH' #get data data = {} for t in Trade.objects.filter(symbol=symbol, status='fill').order_by('-created_on').all(): date = datetime.datetime.strftime(t.created_on, '%Y-%m-%d') if not date in data.keys(): data[date] = {'buyvol': [], 'sellvol': [], 'buy': [], 'sell': []} data[date][t.type].append(t.price) data[date][t.type + 'vol'] = data[date][t.type + 'vol'] + [t.amount] bs = Balance.objects.filter(created_on__gte=start_time).all() try: last_trade = TradeRecommendation.objects.order_by( '-created_on').first() trader_last_seen = ( last_trade.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M') is_trader_running = last_trade.created_on > ( get_time() - datetime.timedelta(minutes=int(15))) except Exception: is_trader_running = False i = 0 charts = [] chartnames = [] for func in [ get_trade_chart, get_trade_profitability_chart, get_directional_change_chart, get_performance_comps_chart, get_ticker_price ]: i = i + 1 cht = func(bs, the_denom, symbol, start_time) charts.append(cht) chartnames.append( str(func).split()[1].replace('get_', '').replace('_chart', '')) options = [] return render_to_response( 'optimize.html', { 'days_ago': [1, 2, 3, 4, 5, 10, 15, 30], 'hours_ago': [1, 2, 3, 6, 12, 24], 'getparams': getify(request.GET), 'charts': charts, 'chartnames': chartnames, 'chartnamesstr': ",".join(chartnames), 'denoms': denoms, 'the_denom': the_denom, 'is_trader_running': is_trader_running, 'trader_last_seen': trader_last_seen, })
def c_chart_view(request): #setup symbol = request.GET.get('symbol', 'BTC_ETH') i = 0 charts = [] chartnames = [] metas = [] symbols = Price.objects.values('symbol').distinct().order_by( 'symbol').values_list('symbol', flat=True) #get data pts, symbols_that_exist = get_data(request, symbol, 'history_classifiertest', ClassifierTest) if len(pts) == 0: return render_to_response('notfound.html') trainer_last_seen = None try: last_pt = ClassifierTest.objects.filter( type='mock').order_by('-created_on').first() is_trainer_running = last_pt.created_on > ( get_time() - datetime.timedelta(minutes=int(15))) trainer_last_seen = ( last_pt.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M') except Exception: is_trainer_running = False meta = { 'count': int(round(pts.count(), 0)), 'avg': round( pts.aggregate(Avg('percent_correct'))['percent_correct__avg'], 0), 'median': round(median_value(pts, 'percent_correct'), 0), 'max': round( pts.aggregate(Max('percent_correct'))['percent_correct__max'], 0), 'min': round( pts.aggregate(Min('percent_correct'))['percent_correct__min'], 0), } #get global chart information for parameter in ['percent_correct', 'score']: i = i + 1 cht = get_line_chart(pts, symbol, parameter) charts.append(cht) options = [] chartnames.append("container" + str(i)) metas.append({ 'name': parameter, 'container_class': 'show', 'class': "container" + str(i), 'options': options, }) # get parameter distribution charts parameters = [ 'name', 'datasetinputs', 'granularity', 'minutes_back', 'timedelta_back_in_granularity_increments', 'time', 'prediction_size' ] for x_axis in parameters: i = i + 1 cht = get_scatter_chart(pts, x_axis, symbol) charts.append(cht) options_dict = pts.values(x_axis).annotate( Avg('percent_correct')).annotate(Count('pk')) options = [(x_axis, obj[x_axis], int(round(obj['percent_correct__avg'], 0)), int(round(obj['pk__count'], 0))) for obj in options_dict] options.sort(key=lambda x: x[1]) the_max = max([option[2] for option in options]) for k in range(len(options)): options[k] = options[k] + ( ("max" if options[k][2] == the_max else "notmax") + " " + ("warning" if options[k][3] < 5 else "nowarning"), ) chartnames.append("container" + str(i)) metas.append({ 'name': x_axis, 'container_class': 'show' if len(options) > 1 else 'noshow', 'class': "container" + str(i), 'options': options, }) #Step 3: Send the chart object to the template. return render_to_response( 'c_chart.html', { 'pts': pts.order_by('percent_correct'), 'ticker': symbol, 'symbols': symbols, 'meta': meta, 'days_ago': [1, 2, 3, 4, 5, 10, 15, 30], 'hours_ago': [1, 2, 3, 6, 12, 24], 'getparams': getify(request.GET), 'charts': charts, 'metas': metas, 'chartnames': chartnames, 'chartnamesstr': ",".join(chartnames), 'is_trainer_running': is_trainer_running, 'trainer_last_seen': trainer_last_seen, 'symbols_that_exist': symbols_that_exist, })