| 635 | |
| 636 | @staff_member_required |
| 637 | def optimize_view(request): |
| 638 | |
| 639 | # setup |
| 640 | the_denom = request.GET.get('denom', 'btc_balance') |
| 641 | denoms = ['usd_balance', 'btc_balance'] |
| 642 | days_ago = request.GET.get('days_ago', False) |
| 643 | hours_ago = request.GET.get('hours_ago', False) |
| 644 | if not hours_ago and not days_ago: |
| 645 | hours_ago = 6 |
| 646 | if days_ago: |
| 647 | start_time = (timezone.now() - datetime.timedelta(days=int(days_ago))) |
| 648 | elif hours_ago: |
| 649 | start_time = (timezone.now() - datetime.timedelta(hours=int(hours_ago))) |
| 650 | else: |
| 651 | start_time = Balance.objects.order_by('created_on').first().created_on |
| 652 | symbol = 'BTC_ETH' |
| 653 | |
| 654 | # get data |
| 655 | data = {} |
| 656 | for t in Trade.objects.filter(symbol=symbol, status='fill').order_by('-created_on').all(): |
| 657 | date = datetime.datetime.strftime(t.created_on, '%Y-%m-%d') |
| 658 | if date not in data.keys(): |
| 659 | data[date] = {'buyvol': [], 'sellvol': [], 'buy': [], 'sell': []} |
| 660 | data[date][t.type].append(t.price) |
| 661 | data[date][t.type+'vol'] = data[date][t.type+'vol'] + [t.amount] |
| 662 | bs = Balance.objects.filter(created_on__gte=start_time).all() |
| 663 | |
| 664 | last_trade = TradeRecommendation.objects.order_by('-created_on').first() |
| 665 | if last_trade: |
| 666 | trader_last_seen = (last_trade.created_on - datetime.timedelta(hours=int(7))).strftime('%a %H:%M') |
| 667 | is_trader_running = last_trade.created_on > (get_time() - datetime.timedelta(minutes=int(15))) |
| 668 | else: |
| 669 | trader_last_seen = None |
| 670 | is_trader_running = False |
| 671 | |
| 672 | i = 0 |
| 673 | charts = [] |
| 674 | chartnames = [] |
| 675 | for func in [get_trade_chart, get_trade_profitability_chart, get_directional_change_chart, |
| 676 | get_performance_comps_chart, get_ticker_price]: |
| 677 | i = i + 1 |
| 678 | cht = func(bs, the_denom, symbol, start_time) |
| 679 | charts.append(cht) |
| 680 | chartnames.append(str(func).split()[1].replace('get_', '').replace('_chart', '')) |
| 681 | |
| 682 | return render_to_response('optimize.html', { |
| 683 | 'days_ago': [1, 2, 3, 4, 5, 10, 15, 30], |
| 684 | 'hours_ago': [1, 2, 3, 6, 12, 24], |
| 685 | 'getparams': getify(request.GET), |
| 686 | 'charts': charts, |
| 687 | 'chartnames': chartnames, |
| 688 | 'chartnamesstr': ",".join(chartnames), |
| 689 | 'denoms': denoms, |
| 690 | 'the_denom': the_denom, |
| 691 | 'is_trader_running': is_trader_running, |
| 692 | 'trader_last_seen': trader_last_seen, |
| 693 | }) |