MCPcopy
hub / github.com/owocki/pytrader / handle

Method handle

history/management/commands/scheduled_trades.py:16–61  ·  view source on GitHub ↗
(self, *args, **options)

Source from the content-addressed store, hash-verified

14 help = 'executes scheduled trades'
15
16 def handle(self, *args, **options):
17 from history.poloniex import poloniex
18 from history.models import Price
19 import time
20
21 poo = poloniex(settings.API_KEY, settings.API_SECRET)
22
23 if settings.MAKE_TRADES:
24 time.sleep(40)
25
26 for t in Trade.objects.filter(created_on__lt=datetime.datetime.now(), status='scheduled'):
27
28 # bid right below the lowest ask, or right above the highest bid so that our orders get filled
29 action = t.type
30 price = Price.objects.filter(symbol=t.symbol).order_by('-created_on').first()
31 if action == 'sell':
32 rate = price.lowestask * 0.999
33 else:
34 rate = price.highestbid * 1.001
35
36 t.price = rate
37
38 if action == 'buy':
39 try:
40 response = {} if not settings.MAKE_TRADES else poo.buy(t.symbol, rate, t.amount)
41 except Exception as e:
42 print_and_log('(st)act_upon_recommendation:buy: ' + str(e))
43 elif action == 'sell':
44 try:
45 response = {} if not settings.MAKE_TRADES else poo.sell(t.symbol, rate, t.amount)
46 except Exception as e:
47 print_and_log('(st)act_upon_recommendation:sell: ' + str(e))
48
49 t.response = response
50 t.orderNumber = response.get('orderNumber', '')
51 t.status = 'error' if response.get('error', False) else 'open'
52 t.calculatefees()
53 t.calculate_exchange_rates()
54 t.save()
55
56 ot = t.opposite_trade
57 ot.opposite_price = rate
58 ot.net_profit = ((rate * t.amount) - (ot.price * ot.amount) if action == 'sell' else
59 (ot.price * ot.amount) - (rate * t.amount)) - ot.fee_amount - t.fee_amount
60 ot.calculate_profitability_exchange_rates()
61 ot.save()

Callers

nothing calls this directly

Calls 8

poloniexClass · 0.90
print_and_logFunction · 0.90
buyMethod · 0.80
sellMethod · 0.80
calculatefeesMethod · 0.80
saveMethod · 0.45

Tested by

no test coverage detected