-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
155 lines (137 loc) · 4.91 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
from django.db import models
from zulutrade_api import ZTrade
from django.conf import settings
class Trade(models.Model):
TRADE_CHOICES = (
("cancelled", "Cancelled"),
("filled", "Filled"),
("trading", "Trading"),
)
status = models.CharField(max_length=75, choices = TRADE_CHOICES)
currency = models.CharField(max_length=10)
lots = models.FloatField(default=0.01)
trade_id = models.CharField(max_length=75, blank=True, null=True)
is_buy = models.BooleanField(default=False)
take_profit = models.FloatField(null=True, blank=True)
stop_loss = models.FloatField(null=True, blank=True)
requested_price = models.FloatField(null=True, blank=True)
trailing_stop = models.IntegerField(default=20)
conditional_stop = models.IntegerField(default=20)
created_at = models.DateTimeField(auto_now = True)
modified_at = models.DateTimeField(auto_now_add = True)
trade_list = []
pending_list = []
def set_trades(self, trade_res):
trade_res = trade_res
self.trade_list = trade_res["openPositions"]
self.pending_list = trade_res["openOrders"]
return self.trade_list, self.pending_list
def get_trade_obj(self):
return_value = None
hit_trade = False
for item in self.trade_list:
if ("%s" % item["currencyName"]).strip() == ("%s" % self.currency).strip():
return_value = item
self.trade_id = item["uniqueId"]
self.save()
hit_trade = True
if not hit_trade:
for item in self.pending_list:
if ("%s" % item["currencyName"]).strip() == ("%s" % self.currency).strip():
return_value = item
self.trade_id = item["uniqueId"]
self.save()
return return_value
def start_pending(self):
trade = ZTrade()
trade.currency = self.currency
trade.take_profit = self.take_profit
trade.stop_loss = self.stop_loss
trade.requested_price = self.requested_price
trade.trailing_stop = self.trailing_stop
trade.conditional_stop = self.conditional_stop
if self.lots:
trade.lots = self.lots
else:
trade.lots = settings.TRADE_LOTS
trade.buy = self.is_buy
trade.start_pending()
self.status = "trading"
self.trade_id = trade.trade_id
self.save()
def stop_pending(self):
trade = ZTrade()
trade.trade_id = self.trade_id
trade.currency = self.currency
if self.lots:
trade.lots = self.lots
else:
trade.lots = settings.TRADE_LOTS
trade.take_profit = self.take_profit
trade.stop_loss = self.stop_loss
trade.trailing_stop = self.trailing_stop
trade.conditional_stop = self.conditional_stop
trade.buy = self.is_buy
self.status = "filled"
trade.stop_pending()
self.trade_id = None
self.save()
def start_market(self):
trade = ZTrade()
trade.currency = self.currency
trade.take_profit = self.take_profit
trade.stop_loss = self.stop_loss
trade.trailing_stop = self.trailing_stop
trade.conditional_stop = self.conditional_stop
if self.lots:
trade.lots = self.lots
else:
trade.lots = settings.TRADE_LOTS
trade.buy = self.is_buy
trade.start_market()
self.status = "trading"
self.trade_id = trade.trade_id
self.save()
def stop_market(self):
trade = ZTrade()
trade.trade_id = self.trade_id
trade.currency = self.currency
if self.lots:
trade.lots = self.lots
else:
trade.lots = settings.TRADE_LOTS
trade.take_profit = self.take_profit
trade.stop_loss = self.stop_loss
trade.trailing_stop = self.trailing_stop
trade.conditional_stop = self.conditional_stop
trade.buy = self.is_buy
self.status = "filled"
trade.stop_market()
self.trade_id = None
self.save()
def stop_market_provider(self):
trade = ZTrade()
trade.trade_id = self.trade_id
trade.currency = self.currency
if self.lots:
trade.lots = self.lots
else:
trade.lots = settings.TRADE_LOTS
trade.take_profit = self.take_profit
trade.stop_loss = self.stop_loss
trade.trailing_stop = self.trailing_stop
trade.conditional_stop = self.conditional_stop
trade.buy = self.is_buy
self.status = "filled"
trade.stop_market_provider()
self.trade_id = None
self.save()
def get_instruments(self):
trade = ZTrade()
trade.sleep = False
return trade.get_instruments()
def get_stream(self):
trade = ZTrade()
trade.sleep = False
return trade.get_stream()