Skip to content

Commit 1365d6f

Browse files
committed
Edit trade API
1 parent 5688234 commit 1365d6f

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

API/APIServer.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, trade_handler):
5757
super().__init__(trade_handler)
5858
self.parser = reqparse.RequestParser()
5959
self.parser.add_argument('action', type=str, help='close|start|pause')
60+
self.parser.add_argument('data', type=dict)
6061

6162
def get(self, id):
6263
return Response(response=ConfigLoader.get_json_str(self.th.get_strategy_by_id(id).trade),
@@ -67,7 +68,7 @@ def delete(self, id):
6768
strategies = self.get_strategies(id)
6869

6970
if not strategies:
70-
return APIResult.ErrorResult(101, msg='No strategies were found')
71+
return APIResult.ErrorResult(101, msg='No strategies were found'), 404
7172

7273
# for strategy in strategies:
7374
# self.th.remove_trade_by_strategy(strategy, True)
@@ -90,12 +91,12 @@ def post(self, id=None):
9091
action = args['action']
9192

9293
if not action:
93-
return APIResult.ErrorResult(100, msg='No "action" was provided')
94+
return APIResult.ErrorResult(100, msg='No "action" was provided'), 403
9495

9596
strategies = self.get_strategies(id)
9697

9798
if not strategies:
98-
return APIResult.ErrorResult(101, msg='No strategies were found')
99+
return APIResult.ErrorResult(101, msg='No strategies were found'), 404
99100

100101
action = action.lower()
101102

@@ -108,6 +109,20 @@ def post(self, id=None):
108109

109110
for strategy in strategies:
110111
strategy.paused = paused
112+
elif action == 'add':
113+
ids = []
114+
try:
115+
trade_json = args['data']
116+
trades = ConfigLoader.load_trade_list_from_obj(trade_json)
117+
for trade in trades:
118+
ids.append(trade.id)
119+
self.th.updated_trade(trade)
120+
self.th.fire_trade_updated(trade, True)
121+
except Exception as e:
122+
# return Response(APIResult.ErrorResult(100, e), 500, mimetype='application/json')
123+
return json.dumps(APIResult.ErrorResult(100, str(e))), 500
124+
125+
return APIResult.OKResult(ids), 201
111126

112127
return APIResult.OKResult()
113128

Bot/ConfigLoader.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ def _rename_trade_file(self, path, file_name, trades):
8686
def load_trade_list_fromfile(self, file_path):
8787
with open(file_path, 'r') as f:
8888
trade_obj = json.load(f)
89-
if ConfigLoader.PARENT_ELEMENT in trade_obj:
90-
return [Trade(**to) for to in trade_obj[ConfigLoader.PARENT_ELEMENT]]
91-
if ConfigLoader.ALT_PARENT_ELEMENT in trade_obj:
92-
return [Trade(**trade_obj[ConfigLoader.ALT_PARENT_ELEMENT])]
89+
return ConfigLoader.load_trade_list_from_obj(trade_obj)
90+
91+
@classmethod
92+
def load_trade_list_from_json(cls, s):
93+
trade_obj = json.loads(s)
94+
return cls.load_trade_list_from_obj(trade_obj)
95+
96+
@classmethod
97+
def load_trade_list_from_obj(cls, trade_obj):
98+
if ConfigLoader.PARENT_ELEMENT in trade_obj:
99+
return [Trade(**to) for to in trade_obj[ConfigLoader.PARENT_ELEMENT]]
100+
if ConfigLoader.ALT_PARENT_ELEMENT in trade_obj:
101+
return [Trade(**trade_obj[ConfigLoader.ALT_PARENT_ELEMENT])]
102+
93103

94104
def save_trades(self, saver, trades):
95105
if isinstance(trades, list):

Bot/TradeHandler.py

+4
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,7 @@ def updated_trade(self, trade: Trade):
307307
def confirm_socket_msg_rcvd(self):
308308
self.socket_message_rcvd = True
309309
self.logInfo('WebSocket message received')
310+
311+
def fire_trade_updated(self, trade, need_cloud_sync):
312+
if self.order_updated_handler:
313+
self.order_updated_handler(trade, need_cloud_sync)

0 commit comments

Comments
 (0)