Added market order and limit order
This commit is contained in:
@@ -76,7 +76,13 @@ async def contract_long(tg_id, message, margin_mode):
|
|||||||
starting_quantity = float(data_main_stgs['starting_quantity'])
|
starting_quantity = float(data_main_stgs['starting_quantity'])
|
||||||
max_risk_percent = float(data_risk_management_stgs['max_risk_deal'])
|
max_risk_percent = float(data_risk_management_stgs['max_risk_deal'])
|
||||||
loss_profit = float(data_risk_management_stgs['price_loss'])
|
loss_profit = float(data_risk_management_stgs['price_loss'])
|
||||||
takeProfit= float(data_risk_management_stgs['price_profit'])
|
takeprofit= float(data_risk_management_stgs['price_profit'])
|
||||||
|
commission_fee = float(data_risk_management_stgs.get('commission_fee', 0))
|
||||||
|
takeProfit_raw = takeprofit
|
||||||
|
takeProfit = takeProfit_raw - commission_fee # уменьшаем TP на комиссию
|
||||||
|
|
||||||
|
if takeProfit < 0:
|
||||||
|
takeProfit = 0
|
||||||
|
|
||||||
# Инициализация переменных
|
# Инициализация переменных
|
||||||
next_quantity = starting_quantity
|
next_quantity = starting_quantity
|
||||||
@@ -144,9 +150,11 @@ async def contract_long(tg_id, message, margin_mode):
|
|||||||
await info_access_open_deal(message, SYMBOL, data_main_stgs['trading_mode'], margin_mode, data_main_stgs['size_leverage'], next_quantity)
|
await info_access_open_deal(message, SYMBOL, data_main_stgs['trading_mode'], margin_mode, data_main_stgs['size_leverage'], next_quantity)
|
||||||
|
|
||||||
except exceptions.InvalidRequestError as e:
|
except exceptions.InvalidRequestError as e:
|
||||||
|
logging.error(f"Неверно указана торговая пара: {e}")
|
||||||
await message.answer('Недостаточно баланса')
|
await message.answer('Недостаточно баланса')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await message.answer('Непредвиденная оишбка')
|
logging.error(f"Ошибка при совершении сделки: {e}")
|
||||||
|
await message.answer('⚠️ Ошибка при совершении сделки')
|
||||||
|
|
||||||
async def contract_short(tg_id, message, margin_mode):
|
async def contract_short(tg_id, message, margin_mode):
|
||||||
api_key = await rq.get_bybit_api_key(tg_id)
|
api_key = await rq.get_bybit_api_key(tg_id)
|
||||||
@@ -183,7 +191,13 @@ async def contract_short(tg_id, message, margin_mode):
|
|||||||
starting_quantity = float(data_main_stgs['starting_quantity'])
|
starting_quantity = float(data_main_stgs['starting_quantity'])
|
||||||
max_risk_percent = float(data_risk_management_stgs['max_risk_deal'])
|
max_risk_percent = float(data_risk_management_stgs['max_risk_deal'])
|
||||||
loss_profit = float(data_risk_management_stgs['price_loss'])
|
loss_profit = float(data_risk_management_stgs['price_loss'])
|
||||||
takeProfit = float(data_risk_management_stgs['price_profit'])
|
takeprofit = float(data_risk_management_stgs['price_profit'])
|
||||||
|
commission_fee = float(data_risk_management_stgs.get('commission_fee', 0))
|
||||||
|
takeProfit_raw = takeprofit
|
||||||
|
takeProfit = takeProfit_raw - commission_fee # уменьшаем TP на комиссию
|
||||||
|
|
||||||
|
if takeProfit < 0:
|
||||||
|
takeProfit = 0
|
||||||
|
|
||||||
# Инициализация переменных
|
# Инициализация переменных
|
||||||
next_quantity = starting_quantity
|
next_quantity = starting_quantity
|
||||||
@@ -249,6 +263,58 @@ async def contract_short(tg_id, message, margin_mode):
|
|||||||
await info_access_open_deal(message, SYMBOL, data_main_stgs['trading_mode'], margin_mode, data_main_stgs['size_leverage'], next_quantity)
|
await info_access_open_deal(message, SYMBOL, data_main_stgs['trading_mode'], margin_mode, data_main_stgs['size_leverage'], next_quantity)
|
||||||
|
|
||||||
except exceptions.InvalidRequestError as e:
|
except exceptions.InvalidRequestError as e:
|
||||||
|
logging.error(f"Error in open_deal: {e}")
|
||||||
await message.answer('Недостаточно баланса')
|
await message.answer('Недостаточно баланса')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await message.answer('Непредвиденная оишбка')
|
logging.error(f"Error in open_deal: {e}")
|
||||||
|
await message.answer('⚠️ Ошибка при совершении сделки')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def open_market_order(tg_id, message, api_key, secret_key):
|
||||||
|
data_main_stgs = await rq.get_user_main_settings(tg_id)
|
||||||
|
trading_mode = data_main_stgs['trading_mode']
|
||||||
|
margin_mode = data_main_stgs.get('margin_type')
|
||||||
|
|
||||||
|
if trading_mode == 'Long':
|
||||||
|
await contract_long(tg_id, message, margin_mode)
|
||||||
|
elif trading_mode == 'Short':
|
||||||
|
await contract_short(tg_id, message, margin_mode)
|
||||||
|
else:
|
||||||
|
await message.answer("Неизвестный режим торговли: выберите Long или Short.")
|
||||||
|
|
||||||
|
|
||||||
|
async def open_limit_order(tg_id, message, price, api_key, secret_key):
|
||||||
|
data_main_stgs = await rq.get_user_main_settings(tg_id)
|
||||||
|
trading_mode = data_main_stgs['trading_mode']
|
||||||
|
margin_mode = data_main_stgs.get('margin_type')
|
||||||
|
|
||||||
|
|
||||||
|
client = HTTP(
|
||||||
|
api_key=api_key,
|
||||||
|
api_secret=secret_key
|
||||||
|
)
|
||||||
|
|
||||||
|
symbol = await rq.get_symbol(tg_id)
|
||||||
|
qty = float(data_main_stgs['starting_quantity'])
|
||||||
|
side = 'Buy' if trading_mode == 'Long' else 'Sell'
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = client.place_order(
|
||||||
|
category='linear',
|
||||||
|
symbol=symbol,
|
||||||
|
side=side,
|
||||||
|
orderType='Limit',
|
||||||
|
qty=qty,
|
||||||
|
price=price,
|
||||||
|
timeInForce='GTC',
|
||||||
|
orderLinkId=f"order_{int(time.time())}"
|
||||||
|
)
|
||||||
|
if response.get('retCode') == 0:
|
||||||
|
await message.answer(f"Limit ордер открыт: {side} {qty} {symbol} по цене {price}")
|
||||||
|
else:
|
||||||
|
await message.answer(f"Ошибка открытия ордера: {response.get('retMsg')}")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Ошибка при открытии лимитного ордера: {e}")
|
||||||
|
await message.answer("Ошибка при открытии ордера")
|
Reference in New Issue
Block a user