第 24.1 课:实盘交易下单操作详解
⏱ 课时:1.5 小时
🎯 学习目标:掌握币安交易所的各种下单操作,理解不同订单类型的使用场景
课程概述
Freqtrade虽然自动化了交易决策,但理解底层的下单机制对于优化策略和解决问题至关重要。本课将详细介绍:
- 币安现货交易的各种订单类型
- 不同订单类型的适用场景
- 如何在Freqtrade中配置订单类型
- 实际下单操作的注意事项
第一部分:币安订单类型详解
1.1 现货交易订单类型
1.1.1 限价单(Limit Order)
定义:指定价格和数量,只在达到或优于指定价格时执行。
{
"symbol": "BTCUSDT",
"side": "BUY",
"type": "LIMIT",
"timeInForce": "GTC",
"quantity": 0.001,
"price": "30000.00"
}
特点:
✅ 优点:
- 价格可控,不会出现滑点
- 可以设置低于市价的买入价
- 可以设置高于市价的卖出价
- 适合趋势跟踪策略
❌ 缺点:
- 可能无法成交(价格未达到)
- 需要主动管理未成交订单
- 在快速市场中可能错过机会
适用场景:
- 策略有明确的买入/卖出价格
- 不急于成交,可以等待理想价格
- 需要控制交易成本
- 避免市场冲击
1.1.2 市价单(Market Order)
定义:按市场当前最优价格立即成交。
{
"symbol": "BTCUSDT",
"side": "BUY",
"type": "MARKET",
"quantity": 0.001
}
特点:
✅ 优点:
- 保证成交
- 执行速度快
- 适合紧急入场/出场
- 简单直接
❌ 缺点:
- 价格不可控,存在滑点
- 可能以较差价格成交
- 交易成本不确定
- 可能被"钓鱼"
适用场景:
- 止损订单(必须立即成交)
- 市场突破时追涨/杀跌
- 流动性好的主流币种
- 不在意小幅价格差异
1.1.3 止损限价单(Stop Loss Limit)
定义:当价格触发指定止损价后,提交限价单。
{
"symbol": "BTCUSDT",
"side": "SELL",
"type": "STOP_LOSS_LIMIT",
"timeInForce": "GTC",
"quantity": 0.001,
"price": "29500.00", // 限价单价格
"stopPrice": "29550.00" // 触发价格
}
执行逻辑:
1. 市价下跌至 29550 USDT
2. 自动触发限价卖单,价格 29500
3. 等待买家以 29500 或更好价格成交
适用场景:
- 需要精确控制止损价格
- 不想因滑点造成额外损失
- 对止损执行要求较高
1.1.4 止损市价单(Stop Loss Market)
定义:当价格触发指定止损价后,立即以市价成交。
{
"symbol": "BTCUSDT",
"side": "SELL",
"type": "STOP_LOSS_MARKET",
"quantity": 0.001,
"stopPrice": "29500.00"
}
特点:
✅ 优点:
- 保证触发后成交
- 执行速度快
- 避免进一步损失
❌ 缺点:
- 成交价格不确定
- 可能出现较大滑点
- 在快速下跌中损失可能更大
1.2 Freqtrade中的订单配置
1.2.1 基本订单类型配置
在config.json中配置:
{
"order_types": {
"entry": "limit",
"exit": "limit",
"emergency_exit": "market",
"force_exit": "market",
"force_entry": "market",
"stoploss": "market",
"stoploss_on_exchange": true,
"stoploss_on_exchange_interval": 60
}
}
配置说明:
| 参数 | 可选值 | 推荐设置 | 说明 |
|---|---|---|---|
| entry | limit/market | limit | 入场订单类型 |
| exit | limit/market | limit | 退出订单类型 |
| emergency_exit | market | market | 紧急退出订单 |
| stoploss | market/limit | market | 止损订单类型 |
| stoploss_on_exchange | true/false | true | 是否在交易所设置止损 |
| stoploss_on_exchange_interval | 数字 | 60 | 止损检查间隔(秒) |
1.2.2 高级订单配置
订单超时设置:
{
"unfilledtimeout": {
"entry": 10, // 入场订单超时(分钟)
"exit": 10, // 退出订单超时(分钟)
"exit_timeout_count": 0, // 退出超时后重试次数
"unit": "minutes"
}
}
订单设置:
{
"order_time_in_force": {
"entry": "gtc", // gtc: 有效直到取消
"exit": "gtc" // ioc: 立即成交或取消
}
}
TIF(Time In Force)选项:
-
GTC(Good Till Canceled):有效直到取消 -
IOC(Immediate Or Cancel):立即成交或取消 -
FOK(Fill Or Kill):全部成交或取消
第二部分:实际下单操作流程
2.1 限价单操作流程
2.1.1 买入限价单示例
策略场景:BTC价格在30500,策略认为30000是好的买入点。
# 在策略中,Freqtrade会自动处理下单
# 以下为理解底层API调用流程
import ccxt
exchange = ccxt.binance({
'apiKey': 'your_api_key',
'secret': 'your_secret',
'options': {
'defaultType': 'spot'
}
})
# 创建限价买单
order = exchange.create_limit_buy_order(
symbol='BTC/USDT',
amount=0.001,
price=30000.00
)
print(f"订单ID: {order['id']}")
print(f"状态: {order['status']}")
print(f"价格: {order['price']}")
print(f"数量: {order['amount']}")
输出示例:
订单ID: 123456789
状态: open
价格: 30000.00
数量: 0.001
2.1.2 订单状态监控
# 查询订单状态
def check_order_status(exchange, order_id, symbol):
order = exchange.fetch_order(order_id, symbol)
status_map = {
'open': '等待成交',
'closed': '已成交',
'canceled': '已取消',
'expired': '已过期',
'rejected': '被拒绝'
}
print(f"订单状态: {status_map.get(order['status'], order['status'])}")
print(f"已成交数量: {order.get('filled', 0)}")
print(f"剩余数量: {order.get('remaining', 0)}")
return order
# 定期检查
import time
order_id = '123456789'
while True:
order = check_order_status(exchange, order_id, 'BTC/USDT')
if order['status'] == 'closed':
print("订单已成交!")
break
elif order['status'] in ['canceled', 'expired']:
print("订单未成交")
break
time.sleep(30) # 30秒检查一次
2.2 市价单操作流程
2.2.1 紧急出场市价单
# 策略检测到强烈卖出信号
def emergency_sell(exchange, symbol, amount):
try:
# 创建市价卖单
order = exchange.create_market_sell_order(
symbol=symbol,
amount=amount
)
print(f"紧急卖出成功!")
print(f"订单ID: {order['id']}")
print(f"平均价格: {order.get('average', 'N/A')}")
print(f"成交金额: {order.get('filled', 0) * order.get('average', 0)}")
return order
except Exception as e:
print(f"卖出失败: {e}")
return None
# 调用示例
emergency_sell(exchange, 'BTC/USDT', 0.001)
2.2.2 市价单的滑点计算
def calculate_slippage(order, expected_price):
"""
计算市价单的滑点
"""
actual_price = order.get('average', 0)
if actual_price == 0:
return 0
slippage = abs(actual_price - expected_price) / expected_price * 100
return slippage
# 使用示例
order = emergency_sell(exchange, 'BTC/USDT', 0.001)
expected_price = 30000 # 策略预期价格
if order:
slippage = calculate_slippage(order, expected_price)
print(f"滑点: {slippage:.2f}%")
if slippage > 0.5:
print("⚠️ 滑点较大,需要关注")
第三部分:订单优化策略
3.1 限价单优化技巧
3.1.1 价格设置策略
def calculate_optimal_price(current_price, side, spread_percentage=0.1):
"""
计算最优限价单价格
"""
spread = current_price * (spread_percentage / 100)
if side == 'buy':
# 买入单:略低于当前价格
optimal_price = current_price - spread
else:
# 卖出单:略高于当前价格
optimal_price = current_price + spread
return round(optimal_price, 2)
# 使用示例
current_price = 30150.00
buy_price = calculate_optimal_price(current_price, 'buy')
sell_price = calculate_optimal_price(current_price, 'sell')
print(f"当前价格: {current_price}")
print(f"建议买入价: {buy_price}")
print(f"建议卖出价: {sell_price}")
3.1.2 分批下单策略
def split_order(total_amount, price, num_orders=3):
"""
将大额订单分成多个小订单
"""
order_size = total_amount / num_orders
orders = []
for i in range(num_orders):
# 为每个订单设置略有不同的价格
price_adjustment = i * 0.01 # 0.01%的价格差异
order_price = price * (1 - price_adjustment if i % 2 == 0 else 1 + price_adjustment)
orders.append({
'amount': order_size,
'price': round(order_price, 2)
})
return orders
# 使用示例
orders = split_order(0.003, 30000, 3)
for i, order in enumerate(orders):
print(f"订单 {i+1}: {order['amount']} BTC @ ${order['price']}")
3.2 止损订单优化
3.2.1 动态止损设置
def calculate_dynamic_stoploss(entry_price, current_price, atr, multiplier=2):
"""
基于ATR的动态止损
"""
if current_price > entry_price:
# 盈利时,提高止损价格
stoploss = current_price - (atr * multiplier)
else:
# 亏损时,保持原止损
stoploss = entry_price - (atr * multiplier)
return stoploss
# 使用示例
entry_price = 30000
current_price = 30500
atr = 150 # 平均真实波幅
stoploss = calculate_dynamic_stoploss(entry_price, current_price, atr)
print(f"动态止损价格: ${stoploss}")
3.2.2 追踪止损设置
def trailing_stoploss(current_price, highest_price, stoploss_pct, trailing_pct):
"""
追踪止损计算
"""
# 计算基础止损价
base_stoploss = highest_price * (1 - stoploss_pct)
# 计算追踪止损价
trailing_stoploss = current_price * (1 - trailing_pct)
# 取较高者作为止损价
final_stoploss = max(base_stoploss, trailing_stoploss)
return final_stoploss
# 使用示例
current_price = 31000
highest_price = 31500
stoploss_pct = 0.05 # 5%止损
trailing_pct = 0.03 # 3%追踪
final_stop = trailing_stoploss(current_price, highest_price, stoploss_pct, trailing_pct)
print(f"追踪止损价: ${final_stop}")
第四部分:常见问题与解决方案
4.1 订单未能成交
4.1.1 问题分析
def analyze_unfilled_order(order, current_price):
"""
分析订单未成交原因
"""
order_price = float(order['price'])
price_diff = abs(current_price - order_price) / current_price * 100
print(f"订单价格: ${order_price}")
print(f"当前价格: ${current_price}")
print(f"价格差异: {price_diff:.2f}%")
if price_diff > 1:
print("❌ 价格差异过大,建议调整订单价格")
elif order['status'] == 'expired':
print("❌ 订单已过期,建议重新下单")
elif order['status'] == 'rejected':
print("❌ 订单被拒绝,检查账户余额")
else:
print("⏳ 订单仍在等待成交")
# 使用示例
order = {'price': '29500.00', 'status': 'open'}
current_price = 30100
analyze_unfilled_order(order, current_price)
4.1.2 解决方案
def handle_unfilled_order(exchange, order, symbol, strategy='adjust'):
"""
处理未成交订单
"""
current_price = exchange.fetch_ticker(symbol)['last']
if strategy == 'adjust':
# 调整价格策略
new_price = calculate_optimal_price(current_price, order['side'])
# 取消原订单
exchange.cancel_order(order['id'], symbol)
# 创建新订单
new_order = exchange.create_limit_order(
symbol=symbol,
side=order['side'],
amount=order['remaining'],
price=new_price
)
print(f"已调整订单价格至: ${new_price}")
return new_order
elif strategy == 'market':
# 改为市价单
exchange.cancel_order(order['id'], symbol)
market_order = exchange.create_market_order(
symbol=symbol,
side=order['side'],
amount=order['remaining']
)
print("已改为市价单")
return market_order
return None
4.2 滑点控制
4.2.1 滑点监控
class SlippageMonitor:
def __init__(self, max_slippage=0.3):
self.max_slippage = max_slippage
self.slippage_history = []
def add_trade(self, expected_price, actual_price, trade_type):
slippage = abs(actual_price - expected_price) / expected_price * 100
direction = 'positive' if actual_price < expected_price else 'negative'
trade_data = {
'timestamp': time.time(),
'expected_price': expected_price,
'actual_price': actual_price,
'slippage': slippage,
'direction': direction,
'type': trade_type
}
self.slippage_history.append(trade_data)
if slippage > self.max_slippage:
print(f"⚠️ 滑点过大: {slippage:.2f}% (期望: ${expected_price}, 实际: ${actual_price})")
return trade_data
def get_average_slippage(self, trades=20):
recent_trades = self.slippage_history[-trades:]
if not recent_trades:
return 0
avg_slippage = sum(t['slippage'] for t in recent_trades) / len(recent_trades)
return avg_slippage
# 使用示例
monitor = SlippageMonitor(max_slippage=0.3)
monitor.add_trade(30000, 30090, 'buy')
monitor.add_trade(30500, 30470, 'sell')
print(f"平均滑点: {monitor.get_average_slippage():.2f}%")
4.2.2 滑点优化策略
def optimize_order_type(symbol, volatility, liquidity_score):
"""
根据市场条件优化订单类型
"""
if volatility > 0.05: # 高波动
if liquidity_score > 0.8: # 高流动性
return {
'entry': 'limit',
'exit': 'market',
'reason': '高波动环境,使用限价买入控制成本,市价卖出确保成交'
}
else: # 低流动性
return {
'entry': 'limit',
'exit': 'limit',
'reason': '高波动+低流动性,全部使用限价单避免滑点'
}
else: # 低波动
return {
'entry': 'limit',
'exit': 'limit',
'reason': '低波动环境,限价单可以更好地控制价格'
}
# 使用示例
volatility = 0.03 # 3%日波动率
liquidity_score = 0.9 # 高流动性
recommendation = optimize_order_type('BTC/USDT', volatility, liquidity_score)
print(f"推荐订单类型: {recommendation}")
第五部分:实际配置示例
5.1 保守型配置(适合新手)
{
"order_types": {
"entry": "limit",
"exit": "limit",
"emergency_exit": "market",
"stoploss": "market",
"stoploss_on_exchange": true,
"stoploss_on_exchange_interval": 60
},
"unfilledtimeout": {
"entry": 15,
"exit": 15,
"unit": "minutes"
},
"order_time_in_force": {
"entry": "gtc",
"exit": "gtc"
}
}
特点:
- 主要使用限价单,控制交易成本
- 紧急情况使用市价单
- 订单超时时间较长(15分钟)
- 适合不急于成交的策略
5.2 积极型配置(适合高频策略)
{
"order_types": {
"entry": "limit",
"exit": "market",
"emergency_exit": "market",
"stoploss": "market",
"stoploss_on_exchange": true,
"stoploss_on_exchange_interval": 30
},
"unfilledtimeout": {
"entry": 5,
"exit": 10,
"unit": "minutes"
},
"order_time_in_force": {
"entry": "ioc",
"exit": "ioc"
}
}
特点:
- 买入使用限价单控制成本
- 卖出使用市价单确保成交
- 订单超时时间短(5-10分钟)
- 适合需要快速成交的策略
5.3 混合型配置(平衡策略)
{
"order_types": {
"entry": "limit",
"exit": "limit",
"emergency_exit": "market",
"stoploss": "limit",
"stoploss_on_exchange": false
},
"unfilledtimeout": {
"entry": 10,
"exit": 10,
"unit": "minutes"
},
"order_time_in_force": {
"entry": "gtc",
"exit": "gtc"
}
}
特点:
- 全部使用限价单,精确控制价格
- 止损也使用限价单,避免滑点
- 适合对价格控制要求高的策略
📝 实践任务
任务 1:订单类型测试
使用测试网测试不同的订单类型:
- 创建限价买单,设置不同价格差
- 创建市价单,观察滑点
- 测试止损订单的执行
- 记录不同市场条件下的表现
任务 2:滑点测量
实际测量滑点:
- 记录10笔限价单的成交价与期望价
- 记录10笔市价单的滑点
- 分析滑点与市场波动的关系
- 优化你的订单配置
任务 3:订单优化
基于你的策略特点:
- 分析策略的成交需求
- 选择合适的订单类型
- 配置超时参数
- 测试优化后的配置
📌 核心要点
订单类型选择原则
限价单使用场景:
✅ 价格敏感的策略
✅ 不急于成交
✅ 需要控制交易成本
✅ 主流币种,流动性好
市价单使用场景:
✅ 紧急止损
✅ 市场突破
✅ 高流动性币种
✅ 不在意小幅价格差异
常见错误避免
| 错误 | 后果 | 正确做法 |
|---|---|---|
| 止损使用限价单 | 可能无法成交 | 止损使用市价单 |
| 限价单价格偏离太远 | 长时间不成交 | 设置合理价格差 |
| 不监控订单状态 | 错过最佳时机 | 定期检查订单状态 |
| 忽视滑点影响 | 实际收益低于预期 | 实时监控滑点 |
实盘下单检查清单
下单前检查:
□ 订单类型设置正确
□ 价格/数量合理
□ 账户余额充足
□ 市场流动性合适
下单后监控:
□ 订单状态正常
□ 成交价格合理
□ 滑点在可接受范围
□ 手续费计算正确
🎯 下节预告
第 24.2 课:止损操作详解
在下一课中,我们将学习:
- 各种止损类型的设置和使用
- 动态止损和追踪止损的实现
- 止损触发后的处理流程
- 如何优化止损策略以降低风险
理解了下单机制后,接下来学习如何有效控制风险。
Top comments (0)