Skip to content

Commit

Permalink
♻️ ccxt execute_order refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mraniki committed Sep 9, 2024
1 parent 2ac8937 commit d2d6c9f
Showing 1 changed file with 62 additions and 20 deletions.
82 changes: 62 additions & 20 deletions cefi/handler/ccxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,40 +161,82 @@ async def execute_order(self, order_params):
"""
try:
action = order_params.get("action")
instrument = await self.replace_instrument(order_params.get("instrument"))
action = order_params["action"]
instrument = await self.replace_instrument(order_params["instrument"])
quantity = order_params.get("quantity", self.trading_risk_amount)
logger.debug("quantity {}", quantity)
amount = await self.get_order_amount(
quantity=quantity,
instrument=instrument,
is_percentage=self.trading_risk_percentage,
)
params = {
"stopLoss": {
"triggerPrice": order_params.get("stop_loss"),
# "price": order_params.get("action") * 0.98,
},
"takeProfit": {
"triggerPrice": order_params.get("take_profit"),
# "price": order_params.get("action") * 0.98,
},
"stopLoss": {"triggerPrice": order_params.get("stop_loss")},
"takeProfit": {"triggerPrice": order_params.get("take_profit")},
}
logger.debug("amount {}", amount)
pre_order_checks = await self.pre_order_checks(order_params)
logger.debug("pre_order_checks {}", pre_order_checks)

if amount and pre_order_checks:
if order := self.client.create_order(
if amount and await self.pre_order_checks(order_params):
order = self.client.create_order(
symbol=instrument,
type=self.ordertype,
side=action,
amount=amount,
params=params,
):
return await self.get_trade_confirmation(order, instrument, action)
)
return await self.get_trade_confirmation(order, instrument, action)
return f"Error executing {self.name}"

except Exception as e:
logger.error("{} Error {}", self.name, e)
return f"Error executing {self.name}"


# async def execute_order(self, order_params):
# """
# Execute order

# Args:
# order_params (dict):
# action(str)
# instrument(str)
# quantity(int)

# Returns:
# trade_confirmation(dict)

# """
# try:
# action = order_params.get("action")
# instrument = await self.replace_instrument(order_params.get("instrument"))
# quantity = order_params.get("quantity", self.trading_risk_amount)
# logger.debug("quantity {}", quantity)
# amount = await self.get_order_amount(
# quantity=quantity,
# instrument=instrument,
# is_percentage=self.trading_risk_percentage,
# )
# params = {
# "stopLoss": {
# "triggerPrice": order_params.get("stop_loss"),
# # "price": order_params.get("action") * 0.98,
# },
# "takeProfit": {
# "triggerPrice": order_params.get("take_profit"),
# # "price": order_params.get("action") * 0.98,
# },
# }
# logger.debug("amount {}", amount)
# pre_order_checks = await self.pre_order_checks(order_params)
# logger.debug("pre_order_checks {}", pre_order_checks)

# if amount and pre_order_checks:
# if order := self.client.create_order(
# symbol=instrument,
# type=self.ordertype,
# side=action,
# amount=amount,
# params=params,
# ):
# return await self.get_trade_confirmation(order, instrument, action)
# return f"Error executing {self.name}"

# except Exception as e:
# logger.error("{} Error {}", self.name, e)
# return f"Error executing {self.name}"

0 comments on commit d2d6c9f

Please sign in to comment.