hey there, this is the function i created in my script, for some reason when a trailing stop order is placed right i am trying to figure it out why it is not being updated or moving when the price is in the desired direction i set it to. i dont get it why .the all purpose of trailing stop is to move within the callback rate once the price goes up isnt it ? please help on this… i understand that the trailing stop order is for minimzing loses if the price goes in the expected direction and saving profits. if i set it to 0.1% call back this means the callback will be 0.1% moves each time it goes up or down. i think i understand that but still it doesnt seem to be properly works in futures .
def place_order_ta_stp_trailing_stop(self, side, amount, trailing_percentage=1.02, stop_loss_percentage=10.0,take_profit_percentage=1.02):
try:
# Fetch the current market price
current_price = self.get_last_price()
if current_price is None:
logging.error('Failed to fetch current market price')
return None
# Calculate the limit order price as 0.00000001% above the current market price
limit_order_price = current_price * (1 + 0.000000000000000000000000000000001) if side == 'BUY' else current_price * (1 - 0.000000000000000000000000000000001)
# Place the limit order
order = self.exchange.create_order(
symbol=self.pair,
type='LIMIT',
side=side,
price=limit_order_price,
amount=amount,
)
logging.info(f"Limit order placed at {limit_order_price}: {order}")
# Adjust the execution price based on the limit order price
execution_price = float(order['price'])
# Determine closing side based on the order side
closing_side = 'BUY' if side == 'SELL' else 'SELL'
# Calculate callbackRate for trailing stop, stop loss price, and take profit price
callback_rate = trailing_percentage
stop_loss_price = execution_price * (
1 - stop_loss_percentage / 100) if side == 'BUY' else execution_price * (
1 + stop_loss_percentage / 100)
take_profit_price = execution_price * (
1 + take_profit_percentage / 100) if side == 'BUY' else execution_price * (
1 - take_profit_percentage / 100)
# Create TRAILING_STOP_MARKET order with specified callback rate and activation price
self.exchange.create_order(
symbol=self.pair,
type='TRAILING_STOP_MARKET',
side=closing_side,
amount=amount,
params={
'callbackRate': trailing_percentage,
'activationPrice': execution_price,
'reduceOnly': 'True'
}
)
logging.info(
f"Trailing stop order set for {side} position with callback rate of {trailing_percentage}% at activation price {execution_price}")
# Create STOP_MARKET order for fixed stop loss
self.exchange.create_order(
symbol=self.pair,
type='STOP_MARKET',
side=closing_side,
amount=amount,
params={
'stopPrice': stop_loss_price,
'reduceOnly': 'True'
}
)
logging.info(f"Fixed stop loss order set for {side} position at price {stop_loss_price}")
# Create TAKE_PROFIT_MARKET order for take profit
self.exchange.create_order(
symbol=self.pair,
type='TAKE_PROFIT_MARKET',
side=closing_side,
amount=amount,
params={
'stopPrice': take_profit_price,
'reduceOnly': 'True'
}
)
logging.info(f"Take profit order set for {side} position at price {take_profit_price}")
return order
except Exception as err:
logging.error(f'An error occurred: {err}')
return None