I seem to be having a hard time passing values into my futures trade using CCXT and python.
Right now I have the code to open my position, SL and TP's as follows:
def open_trade(symbol, entry, leverage, take_profit, stop_loss, position_type, size):
# Load the time difference
exchange.load_time_difference()
# Assuming you already have 'exchange' defined somewhere
ticker = exchange.fetch_ticker(symbol)
current_price = ticker['last']
if position_type == 'LONG' and not (entry[1] < current_price < entry[0]):
print(f"Current price ({current_price}) is not between entry1 ({entry[1]}) and entry2 ({entry[0]}). Signal NOT executed")
return None
elif position_type == 'SHORT' and not (entry[0] < current_price < entry[1]):
print(f"Current price ({current_price}) is not between entry1 ({entry[0]}) and entry2 ({entry[1]}). Signal NOT executed")
return None
# Define additional parameters, including leverage
additional_params = {
'leverage': calc_leverage(symbol, leverage),
}
# Set leverage
exchange.fapiPrivate_post_leverage({
"symbol": symbol,
"leverage": leverage,
})
# Determine the position side based on the order type
if position_type == 'LONG':
additional_params['positionSide'] = 'LONG'
elif position_type == 'SHORT':
additional_params['positionSide'] = 'SHORT'
else:
print(f"Invalid position_type: {position_type}")
return None
# Place market order with additional parameters
order = exchange.create_order(
symbol=symbol,
type='market',
side='buy' if position_type == 'LONG' else 'sell',
amount=size,
params=additional_params,
)
position_info = order
print("Futures position information:")
print(str(position_info) + "\n")
# Add stopPrice to additional_params
additional_params['stopPrice'] = stop_loss
# Place stop loss order
stop_loss_order = exchange.create_order(
symbol=symbol,
type='STOP_MARKET',
side='sell' if position_type == 'LONG' else 'buy',
amount=size,
price=stop_loss, # This is the stop loss price
params=additional_params,
)
position_info = stop_loss_order
print("Stop Loss information:")
print(str(position_info) + "\n")
size = size/5 # Split the size into 5 take profit orders
i = 1
for tp in take_profit:
# Add stopPrice to additional_params
additional_params['stopPrice'] = tp
# Place take profit order
take_profit_order = exchange.create_order(
symbol=symbol,
type='TAKE_PROFIT_MARKET',
side='sell' if position_type == 'LONG' else 'buy',
amount=size,
price=tp, # This is the take profit price
params=additional_params,
)
position_info = take_profit_order
print('Take Profit: ' + str(i) + ' information:')
print(str(position_info) + "\n")
i += 1
Initially I had thought that this worked:
# Define additional parameters, including leverage
additional_params = {
'leverage': calc_leverage(symbol, leverage),
}
but after testing n a different signal it became clear that it did not do anything... I then went to find this post: Leverage not taken into account by Binance with CCXT in Python
but even the code provided here doesnt seem to do the trick:
markets = exchange.load_markets()
market = exchange.market(symbol)
exchange.fapiPrivate_post_leverage({
'symbol': 'symbol',
'leverage': leverage,
})
and keeps returning an error with the symbol, regardless of trying BTCUSDT, BTC/USDT, or BTC/USDT:USDT when using
ccxt.binanceusdm
or
AttributeError: 'binance' object has no attribute 'fapiPrivate_post_leverage'
when using
ccxt.binance
This was found after some other post that I saw suggested using
exchange = ccxt.binanceusdm({
'apiKey': 'blabla',
'secret': 'secret',
'enableRateLimit': True,
'options': {
'defaultType': 'future'
}
})
instead of
exchange = ccxt.binance({...
This issue is not limited to the leverage but even directly assigning a SL or TP on a trade, thus my code has 3 iterations of
order
any help will be greatly appreciated
In the documentation there is
I somehow missed than and the code works as expected now