Blonde Trader 策略
Blonde Trader 是从 MQL 移植而来的网格交易策略。它监控价格相对近期极值的偏离程度,并在触发条件时建立头寸并挂出一组挂单。
思路
- 计算最近 Period X 根 K 线的最高价和最低价。
- 当当前价格低于最近最高价超过 Limit 个跳动点时,按市价买入并在下方按 Grid 间距挂出一系列 Buy Limit 单。
- 当当前价格高于最近最低价超过 Limit 个跳动点时,按市价卖出并在上方按 Grid 间距挂出一系列 Sell Limit 单。
- 当累计利润达到 Amount 时,关闭所有头寸和挂单。
- 另外,如果价格朝有利方向运行了 LockDown 个跳动点,会在成本价附近放置保护性止损单。
参数
| 名称 | 说明 |
|---|---|
PeriodX |
计算极值的回溯周期长度。 |
Limit |
当前价格距离极值的最小跳动点数。 |
Grid |
网格挂单之间的跳动点间隔。 |
Amount |
以账户货币计的目标利润。 |
LockDown |
价格获利一定跳动点后移动止损到保本价。 |
CandleType |
用于分析的 K 线类型。 |
指标
Highest– 追踪窗口内的最高价。Lowest– 追踪窗口内的最低价。
订单逻辑
- 多头信号:
- 按市价买入默认数量。
- 在入场价下方按 Grid 间隔挂出四个 Buy Limit,每个订单的数量翻倍。
- 空头信号:
- 按市价卖出默认数量。
- 在入场价上方按相同间隔挂出四个 Sell Limit,数量同样逐次翻倍。
- 当
PnL达到 Amount 时,所有头寸与挂单立即关闭。 - 若
LockDown大于零且价格朝有利方向运行到指定跳动点,则在成本价附近放置一张保护性的止损单。
备注
该策略演示了基本的网格交易思想,使用了高层 API 特性:SubscribeCandles、指标绑定以及 BuyMarket、SellLimit、SellStop 等简化下单函数。
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Blonde Trader strategy. Buys when price pulls back from recent high,
/// sells when price bounces from recent low. Uses Highest/Lowest indicators.
/// </summary>
public class BlondeTraderStrategy : Strategy
{
private readonly StrategyParam<int> _lookback;
private readonly StrategyParam<decimal> _threshold;
private readonly StrategyParam<DataType> _candleType;
private decimal _entryPrice;
public int Lookback { get => _lookback.Value; set => _lookback.Value = value; }
public decimal Threshold { get => _threshold.Value; set => _threshold.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BlondeTraderStrategy()
{
_lookback = Param(nameof(Lookback), 20)
.SetGreaterThanZero()
.SetDisplay("Lookback", "Period for Highest/Lowest", "General");
_threshold = Param(nameof(Threshold), 0.002m)
.SetDisplay("Threshold", "Min distance ratio from extreme", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = Lookback };
var lowest = new Lowest { Length = Lookback };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
var price = candle.ClosePrice;
var range = high - low;
if (range <= 0 || high == 0)
return;
var distFromHigh = (high - price) / high;
var distFromLow = (price - low) / price;
// Buy signal: price pulled back from high by at least threshold
if (distFromHigh > Threshold && Position <= 0)
{
BuyMarket();
_entryPrice = price;
}
// Sell signal: price bounced up from low by at least threshold
else if (distFromLow > Threshold && Position >= 0)
{
SellMarket();
_entryPrice = price;
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class blonde_trader_strategy(Strategy):
def __init__(self):
super(blonde_trader_strategy, self).__init__()
self._lookback = self.Param("Lookback", 20) \
.SetDisplay("Lookback", "Period for Highest/Lowest", "General")
self._threshold = self.Param("Threshold", 0.002) \
.SetDisplay("Threshold", "Min distance ratio from extreme", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._entry_price = 0.0
@property
def lookback(self):
return self._lookback.Value
@property
def threshold(self):
return self._threshold.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(blonde_trader_strategy, self).OnReseted()
self._entry_price = 0.0
def OnStarted2(self, time):
super(blonde_trader_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.lookback
lowest = Lowest()
lowest.Length = self.lookback
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def OnProcess(self, candle, high, low):
if candle.State != CandleStates.Finished:
return
price = float(candle.ClosePrice)
high_val = float(high)
low_val = float(low)
range_val = high_val - low_val
if range_val <= 0 or high_val == 0:
return
dist_from_high = (high_val - price) / high_val
dist_from_low = (price - low_val) / price
if dist_from_high > self.threshold and self.Position <= 0:
self.BuyMarket()
self._entry_price = price
elif dist_from_low > self.threshold and self.Position >= 0:
self.SellMarket()
self._entry_price = price
def CreateClone(self):
return blonde_trader_strategy()