77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import ccxt
|
||
|
||
# 使用您的配置
|
||
CONFIG = {
|
||
'apiKey': 'rFdbMs40biKImtwGnQ',
|
||
'secret': 'JOuEiGT1uCX9l2qbn9uZuZrVFraAZAA59mqY',
|
||
'verbose': False,
|
||
'enableRateLimit': True,
|
||
'enableDemoTrading': True,
|
||
# 'defaultType': 'swap', # 'swap', 'future', 'option', 'spot'
|
||
# 'options': {
|
||
# 'defaultType': 'swap', # 使用合约交易
|
||
# 'fetchMarkets': {
|
||
# 'types': ['linear'],
|
||
# },
|
||
# 'defaultSubType': 'linear', # 'linear', 'inverse'
|
||
# },
|
||
}
|
||
|
||
def place_market_order(symbol, side, amount):
|
||
"""
|
||
在Bybit下市价单
|
||
|
||
Args:
|
||
symbol: 交易对,例如 'BTC/USDT'
|
||
side: 方向 'buy' 或 'sell'
|
||
amount: 数量
|
||
"""
|
||
try:
|
||
# 创建交易所实例
|
||
exchange = ccxt.bybit({
|
||
'apiKey': CONFIG['apiKey'],
|
||
'secret': CONFIG['secret'],
|
||
'sandbox': False, # 使用实盘,如果是测试网改为 True
|
||
'verbose': CONFIG['verbose'],
|
||
'enableRateLimit': CONFIG['enableRateLimit'],
|
||
})
|
||
exchange.enable_demo_trading(True) # 模拟交易
|
||
exchange.set_position_mode(True) # 设置双向持仓
|
||
# 下市价单
|
||
order = exchange.create_order(
|
||
symbol=symbol,
|
||
type='market',
|
||
side=side,
|
||
amount=amount,
|
||
params={
|
||
'timeInForce': 'FOK', # 对于市价单,FOK可能不适用
|
||
'positionIdx': 1 # 0: 单向模式
|
||
} # 额外参数
|
||
)
|
||
|
||
print("订单创建成功!")
|
||
print(f"订单ID: {order['id']}")
|
||
print(f"状态: {order['status']}")
|
||
print(f"数量: {order['amount']}")
|
||
print(f"成交数量: {order['filled']}")
|
||
|
||
return order
|
||
|
||
except Exception as e:
|
||
print(f"下单失败: {e}")
|
||
return None
|
||
|
||
# 使用示例
|
||
if __name__ == "__main__":
|
||
# 示例:市价买入 0.001 BTC
|
||
symbol = 'BTC/USDT:USDT'
|
||
side = 'buy' # 'buy' 或 'sell'
|
||
amount = 0.001
|
||
|
||
order_result = place_market_order(symbol, side, amount)
|
||
|
||
if order_result:
|
||
print("\n订单详情:")
|
||
print(order_result)
|
||
# for key, value in order_result.items():
|
||
# print(f"{key}: {value}") |