-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcustom_order_form_handler.py
More file actions
246 lines (200 loc) · 9.6 KB
/
custom_order_form_handler.py
File metadata and controls
246 lines (200 loc) · 9.6 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import json
import os
import portalocker
from datetime import datetime
from enum import Enum
from typing import Dict, Any, List
class OrderStatus(Enum):
PENDING = 'PENDING' # ORDER READY TO READ AND ORDER KRAKEN NOT YET FINALIZED
HOLDING = 'HOLDING'
EXITED = 'EXITED'
WAITING = 'WAITING' # ENTRY CONDITION WAITING
CANCELED = 'CANCELED'
# Lists for order statuses (using string values)
ACTIVE_ORDER_STATUSES_VALUES = [
OrderStatus.WAITING.value, OrderStatus.PENDING.value, OrderStatus.HOLDING.value]
INACTIVE_ORDER_STATUSES_VALUES = [
OrderStatus.EXITED.value, OrderStatus.CANCELED.value]
ALL_ORDER_STATUSES_VALUES = ACTIVE_ORDER_STATUSES_VALUES + \
INACTIVE_ORDER_STATUSES_VALUES
class StrategyDataHandler:
def __init__(self, strategy_name: str, base_dir='.'):
self.strategy_name = strategy_name
script_dir = os.path.dirname(os.path.abspath(__file__))
self.base_dir = os.path.join(script_dir, base_dir)
self.ensure_base_dir()
self.live_trades_file = f"LIVE_TRADES_LOG_{self.strategy_name}.json"
self.completed_trades_file = f"COMPLETED_TRADES_LOG_{self.strategy_name}.json"
def ensure_base_dir(self):
if not os.path.exists(self.base_dir):
os.makedirs(self.base_dir)
def get_order_file_path(self) -> str:
return os.path.join(self.base_dir, self.live_trades_file)
def get_completed_trades_file_path(self) -> str:
return os.path.join(self.base_dir, self.completed_trades_file)
def read_file_with_lock(self, file_path: str) -> Any:
with portalocker.Lock(file_path, 'r', timeout=10) as file:
try:
return json.load(file)
except json.JSONDecodeError:
return []
def write_file_with_lock(self, file_path: str, data: Any):
with portalocker.Lock(file_path, 'w', timeout=10) as file:
json.dump(data, file, indent=4)
def read_strategy_data(self) -> Dict[str, Any]:
file_path = self.get_order_file_path()
if os.path.exists(file_path):
return self.read_file_with_lock(file_path)
return {}
def save_strategy_data(self, data: Dict[str, Any]) -> None:
sorted_data = dict(sorted(data.items(), key=lambda item: item[0]))
file_path = self.get_order_file_path()
with portalocker.Lock(file_path, 'r+', timeout=10) as file:
try:
strategy_data = json.load(file)
except json.JSONDecodeError:
strategy_data = {}
for pair, pair_data in sorted_data.items():
strategy_data = self.move_data_from_active_to_completed(pair, pair_data, strategy_data)
file.seek(0)
file.truncate()
json.dump(strategy_data, file, indent=4)
def read_completed_trades(self) -> List[Dict[str, Any]]:
file_path = self.get_completed_trades_file_path()
if os.path.exists(file_path):
return self.read_file_with_lock(file_path)
return []
def save_completed_trades(self, data: List[Dict[str, Any]]) -> None:
file_path = self.get_completed_trades_file_path()
self.write_file_with_lock(file_path, data)
def move_data_from_active_to_completed(self, pair, data, strategy_data):
if data['status'] in [OrderStatus.EXITED.value, OrderStatus.CANCELED.value]:
exited_trade = strategy_data.pop(pair, None)
if exited_trade:
exited_trade['status'] = OrderStatus.EXITED.value
self.add_completed_trade(pair, exited_trade)
else:
strategy_data[pair] = data
return strategy_data
def update_strategy_data(self, pair, data):
file_path = self.get_order_file_path()
with portalocker.Lock(file_path, 'r+', timeout=10) as file:
try:
strategy_data = json.load(file)
strategy_data = self.move_data_from_active_to_completed(pair, data, strategy_data)
file.seek(0)
file.truncate()
json.dump(strategy_data, file, indent=4)
except json.JSONDecodeError:
# Handle empty or corrupted file
strategy_data = {pair: data}
json.dump(strategy_data, file, indent=4)
def add_completed_trade(self, pair, trade_data):
file_path = self.get_completed_trades_file_path()
with portalocker.Lock(file_path, 'r+', timeout=10) as file:
try:
completed_trades = json.load(file)
if not isinstance(completed_trades, list):
completed_trades = []
completed_trades.append({pair: trade_data})
file.seek(0)
file.truncate()
json.dump(completed_trades, file, indent=4)
except json.JSONDecodeError:
# Handle empty or corrupted file
completed_trades = [{pair: trade_data}]
json.dump(completed_trades, file, indent=4)
# outdated uses dict for cmpleted rtades
# import json
# import os
# import portalocker
# from datetime import datetime
# from enum import Enum
# from typing import Dict, Any
# class OrderStatus(Enum):
# PENDING = 'PENDING' # ORDER READY TO READ AND ORDER KRAKEN NOT YET FINALIZED
# HOLDING = 'HOLDING'
# EXITED = 'EXITED'
# WAITING = 'WAITING' # ENTRY CONDITION WAITING
# CANCELED = 'CANCELED'
# # Lists for order statuses (using string values)
# ACTIVE_ORDER_STATUSES_VALUES = [
# OrderStatus.WAITING.value, OrderStatus.PENDING.value, OrderStatus.HOLDING.value]
# INACTIVE_ORDER_STATUSES_VALUES = [
# OrderStatus.EXITED.value, OrderStatus.CANCELED.value]
# ALL_ORDER_STATUSES_VALUES = ACTIVE_ORDER_STATUSES_VALUES + \
# INACTIVE_ORDER_STATUSES_VALUES
# class StrategyDataHandler:
# def __init__(self, strategy_name: str, base_dir='.'):
# self.strategy_name = strategy_name
# script_dir = os.path.dirname(os.path.abspath(__file__))
# self.base_dir = os.path.join(script_dir, base_dir)
# self.ensure_base_dir()
# self.live_trades_file = f"LIVE_TRADES_LOG_{self.strategy_name}.json"
# self.completed_trades_file = f"COMPLETED_TRADES_LOG_{self.strategy_name}.json"
# def ensure_base_dir(self):
# if not os.path.exists(self.base_dir):
# os.makedirs(self.base_dir)
# def get_order_file_path(self) -> str:
# return os.path.join(self.base_dir, self.live_trades_file)
# def get_completed_trades_file_path(self) -> str:
# return os.path.join(self.base_dir, self.completed_trades_file)
# def read_file_with_lock(self, file_path: str) -> Dict[str, Any]:
# with portalocker.Lock(file_path, 'r', timeout=10) as file:
# try:
# return json.load(file)
# except json.JSONDecodeError:
# return {}
# def write_file_with_lock(self, file_path: str, data: Dict[str, Any]):
# with portalocker.Lock(file_path, 'w', timeout=10) as file:
# json.dump(data, file, indent=4)
# def read_strategy_data(self) -> Dict[str, Any]:
# file_path = self.get_order_file_path()
# if os.path.exists(file_path):
# return self.read_file_with_lock(file_path)
# return {}
# def save_strategy_data(self, data: Dict[str, Any]) -> None:
# sorted_data = dict(sorted(data.items(), key=lambda item: item[0]))
# file_path = self.get_order_file_path()
# self.write_file_with_lock(file_path, sorted_data)
# def read_completed_trades(self) -> Dict[str, Any]:
# file_path = self.get_completed_trades_file_path()
# if os.path.exists(file_path):
# return self.read_file_with_lock(file_path)
# return {}
# def save_completed_trades(self, data: Dict[str, Any]) -> None:
# sorted_data = dict(sorted(data.items(), key=lambda item: item[0]))
# file_path = self.get_completed_trades_file_path()
# self.write_file_with_lock(file_path, sorted_data)
# def update_strategy_data(self, pair, data):
# file_path = self.get_order_file_path()
# with portalocker.Lock(file_path, 'r+', timeout=10) as file:
# try:
# strategy_data = json.load(file)
# if data['status'] in [OrderStatus.EXITED.value, OrderStatus.CANCELED.value]:
# exited_trade = strategy_data.pop(pair, None)
# if exited_trade:
# exited_trade['status'] = OrderStatus.EXITED.value
# self.add_completed_trade(pair, exited_trade)
# else:
# strategy_data[pair] = data
# file.seek(0)
# file.truncate()
# json.dump(strategy_data, file, indent=4)
# except json.JSONDecodeError:
# # Handle empty or corrupted file
# strategy_data = {pair: data}
# json.dump(strategy_data, file, indent=4)
# def add_completed_trade(self, pair, trade_data):
# file_path = self.get_completed_trades_file_path()
# with portalocker.Lock(file_path, 'r+', timeout=10) as file:
# try:
# completed_trades = json.load(file)
# completed_trades[pair] = trade_data
# file.seek(0)
# file.truncate()
# json.dump(completed_trades, file, indent=4)
# except json.JSONDecodeError:
# # Handle empty or corrupted file
# completed_trades = {pair: trade_data}
# json.dump(completed_trades, file, indent=4)