FXF Fast in Fast out 策略是一套基于波动率的突破系统,将原始的 MetaTrader 4 专家顾问重写为 StockSharp 高级策略。策略监控可配置的时间框架,当出现大幅波动的 K 线时测量点差,并尝试通过挂单捕捉顺势延续。信号只依赖于已经收盘的 K 线,而点差过滤、下单与移动止损依靠 Level1 行情数据完成。
当当前 K 线的振幅超过预设阈值时,策略会计算最新买卖价的平均值,并将其与 K 线开盘价比较:若均价高于开盘价,则在卖价上方放置买入止损;若均价低于开盘价,则在买价下方放置卖出止损。挂单会附带止损和止盈,且可以根据账户资金和止损距离进行动态仓位管理;一旦成交,还可以启用可选的移动止损保护持仓。
交易逻辑
信号检测:只在 K 线收盘后工作,计算 (最高价-最低价) 转换成价格步进数,若不少于 VolatilitySizePoints 即认为波动足够。
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Fast In Fast Out scalping strategy: quick entry/exit using EMA and RSI.
/// </summary>
public class FxfFastInFastOutStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<int> _rsiPeriod;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int EmaPeriod
{
get => _emaPeriod.Value;
set => _emaPeriod.Value = value;
}
public int RsiPeriod
{
get => _rsiPeriod.Value;
set => _rsiPeriod.Value = value;
}
public FxfFastInFastOutStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_emaPeriod = Param(nameof(EmaPeriod), 10)
.SetGreaterThanZero()
.SetDisplay("EMA Period", "EMA period", "Indicators");
_rsiPeriod = Param(nameof(RsiPeriod), 7)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI period", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = EmaPeriod };
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
decimal? prevRsi = null;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ema, rsi, (candle, emaVal, rsiVal) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var close = candle.ClosePrice;
if (prevRsi.HasValue)
{
if (prevRsi.Value <= 50 && rsiVal > 50 && close > emaVal && Position <= 0)
BuyMarket();
else if (prevRsi.Value >= 50 && rsiVal < 50 && close < emaVal && Position >= 0)
SellMarket();
}
prevRsi = rsiVal;
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
}
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 ExponentialMovingAverage, RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class fxf_fast_in_fast_out_strategy(Strategy):
"""
FXF Fast In Fast Out: quick entry/exit using EMA and RSI.
Buys when RSI crosses above 50 and close > EMA.
Sells when RSI crosses below 50 and close < EMA.
"""
def __init__(self):
super(fxf_fast_in_fast_out_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._ema_period = self.Param("EmaPeriod", 10) \
.SetDisplay("EMA Period", "EMA period", "Indicators")
self._rsi_period = self.Param("RsiPeriod", 7) \
.SetDisplay("RSI Period", "RSI period", "Indicators")
self._prev_rsi = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(fxf_fast_in_fast_out_strategy, self).OnReseted()
self._prev_rsi = None
def OnStarted2(self, time):
super(fxf_fast_in_fast_out_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self._ema_period.Value
rsi = RelativeStrengthIndex()
rsi.Length = self._rsi_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, rsi, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _process_candle(self, candle, ema_val, rsi_val):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
ema = float(ema_val)
rsi = float(rsi_val)
if self._prev_rsi is not None:
if self._prev_rsi <= 50 and rsi > 50 and close > ema and self.Position <= 0:
self.BuyMarket()
elif self._prev_rsi >= 50 and rsi < 50 and close < ema and self.Position >= 0:
self.SellMarket()
self._prev_rsi = rsi
def CreateClone(self):
return fxf_fast_in_fast_out_strategy()