分形权重振荡器策略
概述
该策略是对 "Exp_Fractal_WeightOscillator" 专家的移植,通过组合四个
振荡器(RSI、资金流量指数、威廉指标 %R 和 DeMarker)生成一个平滑的
综合信号。策略在所选时间框架上计算指标,并将综合振荡器与
HighLevel / LowLevel 两条水平线比较,在趋势跟随或逆趋势模式下
触发多空交易。所有逻辑均使用 StockSharp 的高级 API 实现。
指标结构
- RSI:应用于配置的价格源。
- MFI:基于所选价格和K线成交量计算。
- Williams %R:使用K线高/低/收价。
- DeMarker:根据高低价重建,并采用简单的移动平均平滑。
- 平滑移动平均:可选的后处理器(SMA、EMA、SMMA 或 LWMA)。
综合振荡器是上述四个指标的加权平均值。HighLevel 与
LowLevel 定义超买/超卖区域;SignalBar 决定在信号判断时回溯的
已完成K线数量,用于控制信号延迟。
交易逻辑
TrendMode = Direct(顺势)
- 做多 / 平空:当振荡器从
LowLevel之上跌至LowLevel(需要BuyOpenEnabled和SellCloseEnabled为 true)。 - 做空 / 平多:当振荡器从
HighLevel之下升至HighLevel(需要SellOpenEnabled和BuyCloseEnabled为 true)。
TrendMode = Counter(逆势)
- 做多 / 平空:当振荡器向上突破
HighLevel。 - 做空 / 平多:当振荡器向下跌破
LowLevel。
信号在 SignalBar 指定的已完成K线上评估。反向开仓时使用
Volume + |Position|,以先平掉旧仓位。
风险管理
开仓后会根据 StopLossPoints 与 TakeProfitPoints 计算固定价位的
止损/止盈(以 MinPriceStep 为单位)。每根完成的K线都会检查高低价
是否触及这些价位,一旦命中立即平仓并重置风险参数。
参数
| 名称 | 说明 |
|---|---|
TrendMode |
选择顺势或逆势模式。 |
SignalBar |
评估信号时回溯的已完成K线数量。 |
Period |
RSI、MFI、Williams %R、DeMarker 的基础周期。 |
SmoothingLength |
平滑移动平均的窗口长度。 |
SmoothingMethod |
移动平均类型(None、Sma、Ema、Smma、Lwma)。 |
RsiPrice、MfiPrice |
组成指标使用的价格源。 |
MfiVolume |
MFI 的成交量类型(Tick 与 Real 都使用K线总量)。 |
RsiWeight、MfiWeight、WprWeight、DeMarkerWeight |
各子指标的权重。 |
HighLevel、LowLevel |
振荡器的上/下阈值。 |
BuyOpenEnabled、SellOpenEnabled |
是否允许开多/开空。 |
BuyCloseEnabled、SellCloseEnabled |
是否允许在反向信号时平仓。 |
StopLossPoints、TakeProfitPoints |
止损与止盈(单位为价格步长,0 表示禁用)。 |
CandleType |
计算所用K线的类型/周期。 |
Volume (策略属性) |
开仓手数;反手时自动加上当前仓位绝对值。 |
使用说明
SignalBar = 1重现原始EA的行为,使用最新完成的K线进行判断。 增加该值可以让策略在更旧的K线上确认信号。SmoothingMethod可关闭平滑(None),或选择不同的移动平均方式 以贴近MQL版本。- MFI 计算始终使用数据源提供的K线总成交量。因为 StockSharp 标准
K线默认不区分逐笔数量,
Tick与Real两个选项使用相同数据。 - 源码中的注释全部为英文,以符合仓库要求。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
using StockSharp.Algo.Candles;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on the Fractal Weight Oscillator indicator.
/// Combines RSI, MFI, Williams %R and DeMarker into a smoothed oscillator
/// and trades level crossings in direct or counter-trend mode.
/// </summary>
public class FractalWeightOscillatorStrategy : Strategy
{
/// <summary>
/// Volume source used for the MFI component.
/// </summary>
public enum MfiVolumeTypes
{
/// <summary>
/// Tick volume.
/// </summary>
Tick,
/// <summary>
/// Real traded volume.
/// </summary>
Real
}
public enum TrendModes
{
/// <summary>
/// Follow the trend.
/// </summary>
Direct,
/// <summary>
/// Trade against the trend.
/// </summary>
Counter
}
public enum SmoothingMethods
{
/// <summary>
/// No smoothing.
/// </summary>
None,
/// <summary>
/// Simple Moving Average.
/// </summary>
Sma,
/// <summary>
/// Exponential Moving Average.
/// </summary>
Ema,
/// <summary>
/// Smoothed Moving Average.
/// </summary>
Smma,
/// <summary>
/// Linear Weighted Moving Average.
/// </summary>
Lwma
}
public enum AppliedPrice
{
None,
Open,
High,
Low,
Close,
Median,
Typical,
Weighted,
Simple,
Quarter,
TrendFollow0,
TrendFollow1,
DeMark
}
private readonly StrategyParam<TrendModes> _trendMode;
private readonly StrategyParam<int> _signalBar;
private readonly StrategyParam<int> _period;
private readonly StrategyParam<int> _smoothingLength;
private readonly StrategyParam<SmoothingMethods> _smoothingMethod;
private readonly StrategyParam<AppliedPrice> _rsiPrice;
private readonly StrategyParam<AppliedPrice> _mfiPrice;
private readonly StrategyParam<MfiVolumeTypes> _mfiVolumeType;
private readonly StrategyParam<decimal> _rsiWeight;
private readonly StrategyParam<decimal> _mfiWeight;
private readonly StrategyParam<decimal> _wprWeight;
private readonly StrategyParam<decimal> _deMarkerWeight;
private readonly StrategyParam<decimal> _highLevel;
private readonly StrategyParam<decimal> _lowLevel;
private readonly StrategyParam<bool> _buyOpenEnabled;
private readonly StrategyParam<bool> _sellOpenEnabled;
private readonly StrategyParam<bool> _buyCloseEnabled;
private readonly StrategyParam<bool> _sellCloseEnabled;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private readonly StrategyParam<DataType> _candleType;
private RelativeStrengthIndex _rsi = null!;
private RelativeStrengthIndex _williams = null!;
private DecimalLengthIndicator _smoother;
private SimpleMovingAverage _deMaxSma = null!;
private SimpleMovingAverage _deMinSma = null!;
private readonly List<decimal> _oscillatorHistory = new();
private decimal _previousHigh;
private decimal _previousLow;
private bool _hasPreviousCandle;
private readonly Queue<decimal> _positiveFlow = new();
private readonly Queue<decimal> _negativeFlow = new();
private decimal _previousTypical;
private bool _hasPreviousTypical;
private decimal _positiveSum;
private decimal _negativeSum;
private decimal _entryPrice;
private decimal? _stopPrice;
private decimal? _takePrice;
private DateTime _currentTime;
/// <summary>
/// Trading direction mode.
/// </summary>
public TrendModes TrendMode
{
get => _trendMode.Value;
set => _trendMode.Value = value;
}
/// <summary>
/// Number of closed bars used for signal evaluation.
/// </summary>
public int SignalBar
{
get => _signalBar.Value;
set => _signalBar.Value = value;
}
/// <summary>
/// Base period for all component oscillators.
/// </summary>
public int Period
{
get => _period.Value;
set => _period.Value = value;
}
/// <summary>
/// Smoothing window applied to the combined oscillator.
/// </summary>
public int SmoothingLength
{
get => _smoothingLength.Value;
set => _smoothingLength.Value = value;
}
/// <summary>
/// Type of moving average used for smoothing.
/// </summary>
public SmoothingMethods SmoothingMethod
{
get => _smoothingMethod.Value;
set => _smoothingMethod.Value = value;
}
/// <summary>
/// Applied price source for RSI calculation.
/// </summary>
public AppliedPrice RsiPrice
{
get => _rsiPrice.Value;
set => _rsiPrice.Value = value;
}
/// <summary>
/// Applied price source for MFI calculation.
/// </summary>
public AppliedPrice MfiPrice
{
get => _mfiPrice.Value;
set => _mfiPrice.Value = value;
}
/// <summary>
/// Volume type used by the MFI component.
/// </summary>
public MfiVolumeTypes MfiVolume
{
get => _mfiVolumeType.Value;
set => _mfiVolumeType.Value = value;
}
/// <summary>
/// Weight of the RSI contribution.
/// </summary>
public decimal RsiWeight
{
get => _rsiWeight.Value;
set => _rsiWeight.Value = value;
}
/// <summary>
/// Weight of the MFI contribution.
/// </summary>
public decimal MfiWeight
{
get => _mfiWeight.Value;
set => _mfiWeight.Value = value;
}
/// <summary>
/// Weight of the Williams %R contribution.
/// </summary>
public decimal WprWeight
{
get => _wprWeight.Value;
set => _wprWeight.Value = value;
}
/// <summary>
/// Weight of the DeMarker contribution.
/// </summary>
public decimal DeMarkerWeight
{
get => _deMarkerWeight.Value;
set => _deMarkerWeight.Value = value;
}
/// <summary>
/// Upper threshold of the oscillator.
/// </summary>
public decimal HighLevel
{
get => _highLevel.Value;
set => _highLevel.Value = value;
}
/// <summary>
/// Lower threshold of the oscillator.
/// </summary>
public decimal LowLevel
{
get => _lowLevel.Value;
set => _lowLevel.Value = value;
}
/// <summary>
/// Allow opening long positions.
/// </summary>
public bool BuyOpenEnabled
{
get => _buyOpenEnabled.Value;
set => _buyOpenEnabled.Value = value;
}
/// <summary>
/// Allow opening short positions.
/// </summary>
public bool SellOpenEnabled
{
get => _sellOpenEnabled.Value;
set => _sellOpenEnabled.Value = value;
}
/// <summary>
/// Allow closing long positions on opposite signals.
/// </summary>
public bool BuyCloseEnabled
{
get => _buyCloseEnabled.Value;
set => _buyCloseEnabled.Value = value;
}
/// <summary>
/// Allow closing short positions on opposite signals.
/// </summary>
public bool SellCloseEnabled
{
get => _sellCloseEnabled.Value;
set => _sellCloseEnabled.Value = value;
}
/// <summary>
/// Stop-loss distance expressed in instrument points.
/// </summary>
public int StopLossPoints
{
get => _stopLossPoints.Value;
set => _stopLossPoints.Value = value;
}
/// <summary>
/// Take-profit distance expressed in instrument points.
/// </summary>
public int TakeProfitPoints
{
get => _takeProfitPoints.Value;
set => _takeProfitPoints.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of the strategy.
/// </summary>
public FractalWeightOscillatorStrategy()
{
_trendMode = Param(nameof(TrendMode), TrendModes.Direct)
.SetDisplay("Trend Mode", "Follow trend or counter-trend", "Trading");
_signalBar = Param(nameof(SignalBar), 1)
.SetNotNegative()
.SetDisplay("Signal Bar", "Offset for signal evaluation", "Trading");
_period = Param(nameof(Period), 30)
.SetGreaterThanZero()
.SetDisplay("Period", "Length for component oscillators", "Indicators");
_smoothingLength = Param(nameof(SmoothingLength), 30)
.SetGreaterThanZero()
.SetDisplay("Smoothing Length", "Window for smoothing", "Indicators");
_smoothingMethod = Param(nameof(SmoothingMethod), SmoothingMethods.Smma)
.SetDisplay("Smoothing Method", "Moving average type for smoothing", "Indicators");
_rsiPrice = Param(nameof(RsiPrice), AppliedPrice.Close)
.SetDisplay("RSI Price", "Applied price for RSI", "Indicators");
_mfiPrice = Param(nameof(MfiPrice), AppliedPrice.Typical)
.SetDisplay("MFI Price", "Applied price for MFI", "Indicators");
_mfiVolumeType = Param(nameof(MfiVolume), MfiVolumeTypes.Tick)
.SetDisplay("MFI Volume", "Volume source for MFI", "Indicators");
_rsiWeight = Param(nameof(RsiWeight), 1m)
.SetGreaterThanZero()
.SetDisplay("RSI Weight", "Weight of RSI component", "Weights");
_mfiWeight = Param(nameof(MfiWeight), 1m)
.SetGreaterThanZero()
.SetDisplay("MFI Weight", "Weight of MFI component", "Weights");
_wprWeight = Param(nameof(WprWeight), 1m)
.SetGreaterThanZero()
.SetDisplay("WPR Weight", "Weight of Williams %R component", "Weights");
_deMarkerWeight = Param(nameof(DeMarkerWeight), 1m)
.SetGreaterThanZero()
.SetDisplay("DeMarker Weight", "Weight of DeMarker component", "Weights");
_highLevel = Param(nameof(HighLevel), 60m)
.SetDisplay("High Level", "Upper oscillator threshold", "Trading");
_lowLevel = Param(nameof(LowLevel), 40m)
.SetDisplay("Low Level", "Lower oscillator threshold", "Trading");
_buyOpenEnabled = Param(nameof(BuyOpenEnabled), true)
.SetDisplay("Enable Long Entries", "Allow buying", "Trading");
_sellOpenEnabled = Param(nameof(SellOpenEnabled), true)
.SetDisplay("Enable Short Entries", "Allow selling", "Trading");
_buyCloseEnabled = Param(nameof(BuyCloseEnabled), true)
.SetDisplay("Close Long", "Allow long exit on signals", "Trading");
_sellCloseEnabled = Param(nameof(SellCloseEnabled), true)
.SetDisplay("Close Short", "Allow short exit on signals", "Trading");
_stopLossPoints = Param(nameof(StopLossPoints), 1000)
.SetNotNegative()
.SetDisplay("Stop Loss (pts)", "Stop-loss distance in points", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 2000)
.SetNotNegative()
.SetDisplay("Take Profit (pts)", "Take-profit distance in points", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for processing", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_oscillatorHistory.Clear();
_previousHigh = 0m;
_previousLow = 0m;
_hasPreviousCandle = false;
_positiveFlow.Clear();
_negativeFlow.Clear();
_previousTypical = 0m;
_hasPreviousTypical = false;
_positiveSum = 0m;
_negativeSum = 0m;
_entryPrice = 0m;
_stopPrice = null;
_takePrice = null;
_currentTime = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_rsi = new RelativeStrengthIndex { Length = Period };
_williams = new RelativeStrengthIndex { Length = Period };
_deMaxSma = new SMA { Length = Period };
_deMinSma = new SMA { Length = Period };
_smoother = CreateSmoother(SmoothingMethod, SmoothingLength);
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
_currentTime = candle.OpenTime;
// indicators are processed manually below
var rsiInput = GetPrice(candle, RsiPrice);
var rsiValue = _rsi.Process(new DecimalIndicatorValue(_rsi, rsiInput, candle.OpenTime) { IsFinal = true });
var mfiInput = GetPrice(candle, MfiPrice);
var wprValue = _williams.Process(new DecimalIndicatorValue(_williams, candle.ClosePrice, candle.OpenTime) { IsFinal = true });
if (!rsiValue.IsFinal || !wprValue.IsFinal)
return;
var mfi = CalculateMfi(candle, mfiInput);
if (mfi is null)
return;
var deMarker = CalculateDeMarker(candle);
if (deMarker is null)
return;
var rsi = rsiValue.GetValue<decimal>();
var mfiValue = mfi.Value;
var wpr = wprValue.GetValue<decimal>();
var totalWeight = RsiWeight + MfiWeight + WprWeight + DeMarkerWeight;
if (totalWeight <= 0m)
return;
var weighted = (RsiWeight * rsi
+ MfiWeight * mfiValue
+ WprWeight * wpr
+ DeMarkerWeight * (deMarker.Value * 100m)) / totalWeight;
var smoothed = ApplySmoothing(weighted);
if (smoothed is null)
return;
_oscillatorHistory.Add(smoothed.Value);
TrimHistory();
if (_oscillatorHistory.Count < SignalBar + 2)
return;
var currentIndex = _oscillatorHistory.Count - 1 - SignalBar;
if (currentIndex <= 0)
return;
var current = _oscillatorHistory[currentIndex];
var previous = _oscillatorHistory[currentIndex - 1];
CheckRisk(candle);
var crossBelowLow = previous > LowLevel && current <= LowLevel;
var crossAboveHigh = previous < HighLevel && current >= HighLevel;
var openBuy = false;
var closeBuy = false;
var openSell = false;
var closeSell = false;
if (TrendMode == TrendModes.Direct)
{
if (crossBelowLow)
{
openBuy = BuyOpenEnabled;
closeSell = SellCloseEnabled;
}
if (crossAboveHigh)
{
openSell = SellOpenEnabled;
closeBuy = BuyCloseEnabled;
}
}
else
{
if (crossBelowLow)
{
openSell = SellOpenEnabled;
closeBuy = BuyCloseEnabled;
}
if (crossAboveHigh)
{
openBuy = BuyOpenEnabled;
closeSell = SellCloseEnabled;
}
}
if (closeBuy && Position > 0)
{
SellMarket();
ResetRisk();
}
if (closeSell && Position < 0)
{
BuyMarket();
ResetRisk();
}
if (openBuy && Position <= 0)
{
var volume = Volume + Math.Abs(Position);
BuyMarket();
SetRiskLevels(candle, Sides.Buy);
}
else if (openSell && Position >= 0)
{
var volume = Volume + Math.Abs(Position);
SellMarket();
SetRiskLevels(candle, Sides.Sell);
}
}
private decimal? ApplySmoothing(decimal value)
{
if (_smoother is null)
return value;
var smoothed = _smoother.Process(new DecimalIndicatorValue(_smoother, value, _currentTime) { IsFinal = true });
return smoothed.IsFinal ? smoothed.GetValue<decimal>() : null;
}
private decimal? CalculateDeMarker(ICandleMessage candle)
{
if (!_hasPreviousCandle)
{
_previousHigh = candle.HighPrice;
_previousLow = candle.LowPrice;
_hasPreviousCandle = true;
return null;
}
var deMax = Math.Max(candle.HighPrice - _previousHigh, 0m);
var deMin = Math.Max(_previousLow - candle.LowPrice, 0m);
_previousHigh = candle.HighPrice;
_previousLow = candle.LowPrice;
var deMaxValue = _deMaxSma.Process(new DecimalIndicatorValue(_deMaxSma, deMax, _currentTime) { IsFinal = true });
var deMinValue = _deMinSma.Process(new DecimalIndicatorValue(_deMinSma, deMin, _currentTime) { IsFinal = true });
if (!deMaxValue.IsFinal || !deMinValue.IsFinal)
return null;
var maxAvg = deMaxValue.GetValue<decimal>();
var minAvg = deMinValue.GetValue<decimal>();
var denom = maxAvg + minAvg;
if (denom == 0m)
return 0.5m;
return maxAvg / denom;
}
private decimal? CalculateMfi(ICandleMessage candle, decimal price)
{
var volume = GetVolume(candle);
if (!_hasPreviousTypical)
{
_previousTypical = price;
_hasPreviousTypical = true;
_positiveFlow.Clear();
_negativeFlow.Clear();
_positiveSum = 0m;
_negativeSum = 0m;
return null;
}
var flow = price * volume;
var positive = price > _previousTypical ? flow : 0m;
var negative = price < _previousTypical ? flow : 0m;
_previousTypical = price;
_positiveSum += positive;
_negativeSum += negative;
_positiveFlow.Enqueue(positive);
_negativeFlow.Enqueue(negative);
if (_positiveFlow.Count > Period)
{
_positiveSum -= _positiveFlow.Dequeue();
_negativeSum -= _negativeFlow.Dequeue();
}
if (_positiveFlow.Count < Period)
return null;
if (_negativeSum == 0m)
return 100m;
var ratio = _positiveSum / _negativeSum;
return 100m - 100m / (1m + ratio);
}
private void TrimHistory()
{
var maxSize = SignalBar + Math.Max(Period, SmoothingLength) + 5;
if (_oscillatorHistory.Count <= maxSize)
return;
var remove = _oscillatorHistory.Count - maxSize;
_oscillatorHistory.RemoveRange(0, remove);
}
private void CheckRisk(ICandleMessage candle)
{
if (Position > 0)
{
if (_stopPrice is decimal stop && candle.LowPrice <= stop)
{
SellMarket();
ResetRisk();
return;
}
if (_takePrice is decimal take && candle.HighPrice >= take)
{
SellMarket();
ResetRisk();
}
}
else if (Position < 0)
{
if (_stopPrice is decimal stop && candle.HighPrice >= stop)
{
BuyMarket();
ResetRisk();
return;
}
if (_takePrice is decimal take && candle.LowPrice <= take)
{
BuyMarket();
ResetRisk();
}
}
}
private void SetRiskLevels(ICandleMessage candle, Sides side)
{
_entryPrice = candle.ClosePrice;
var step = Security?.PriceStep ?? 0m;
if (step <= 0m)
{
_stopPrice = null;
_takePrice = null;
return;
}
_stopPrice = StopLossPoints > 0
? side == Sides.Buy
? _entryPrice - step * StopLossPoints
: _entryPrice + step * StopLossPoints
: null;
_takePrice = TakeProfitPoints > 0
? side == Sides.Buy
? _entryPrice + step * TakeProfitPoints
: _entryPrice - step * TakeProfitPoints
: null;
}
private void ResetRisk()
{
_entryPrice = 0m;
_stopPrice = null;
_takePrice = null;
}
private decimal GetPrice(ICandleMessage candle, AppliedPrice price)
{
return price switch
{
AppliedPrice.Open => candle.OpenPrice,
AppliedPrice.High => candle.HighPrice,
AppliedPrice.Low => candle.LowPrice,
AppliedPrice.Median => (candle.HighPrice + candle.LowPrice) / 2m,
AppliedPrice.Typical => (candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m,
AppliedPrice.Weighted => (candle.HighPrice + candle.LowPrice + 2m * candle.ClosePrice) / 4m,
AppliedPrice.Simple => (candle.OpenPrice + candle.ClosePrice) / 2m,
AppliedPrice.Quarter => (candle.OpenPrice + candle.ClosePrice + candle.HighPrice + candle.LowPrice) / 4m,
AppliedPrice.TrendFollow0 => candle.ClosePrice > candle.OpenPrice ? candle.HighPrice
: candle.ClosePrice < candle.OpenPrice ? candle.LowPrice
: candle.ClosePrice,
AppliedPrice.TrendFollow1 => candle.ClosePrice > candle.OpenPrice
? (candle.HighPrice + candle.ClosePrice) / 2m
: candle.ClosePrice < candle.OpenPrice
? (candle.LowPrice + candle.ClosePrice) / 2m
: candle.ClosePrice,
AppliedPrice.DeMark => CalculateDeMarkPrice(candle),
_ => candle.ClosePrice,
};
}
private static decimal CalculateDeMarkPrice(ICandleMessage candle)
{
var res = candle.HighPrice + candle.LowPrice + candle.ClosePrice;
if (candle.ClosePrice < candle.OpenPrice)
res = (res + candle.LowPrice) / 2m;
else if (candle.ClosePrice > candle.OpenPrice)
res = (res + candle.HighPrice) / 2m;
else
res = (res + candle.ClosePrice) / 2m;
return ((res - candle.LowPrice) + (res - candle.HighPrice)) / 2m;
}
private decimal GetVolume(ICandleMessage candle)
{
return candle.TotalVolume;
}
private static DecimalLengthIndicator CreateSmoother(SmoothingMethods method, int length)
{
return method switch
{
SmoothingMethods.None => null,
SmoothingMethods.Sma => new SMA { Length = length },
SmoothingMethods.Ema => new EMA { Length = length },
SmoothingMethods.Smma => new SmoothedMovingAverage { Length = length },
SmoothingMethods.Lwma => new WeightedMovingAverage { Length = length },
_ => new SmoothedMovingAverage { Length = length }
};
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from System import Decimal
from StockSharp.Algo.Indicators import RelativeStrengthIndex, SimpleMovingAverage, ExponentialMovingAverage, SmoothedMovingAverage, WeightedMovingAverage
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
# Trend mode constants
TREND_DIRECT = 0
TREND_COUNTER = 1
class fractal_weight_oscillator_strategy(Strategy):
def __init__(self):
super(fractal_weight_oscillator_strategy, self).__init__()
self._trend_mode = self.Param("TrendMode", TREND_DIRECT)
self._signal_bar = self.Param("SignalBar", 1)
self._period = self.Param("Period", 30)
self._smoothing_length = self.Param("SmoothingLength", 30)
self._smoothing_method = self.Param("SmoothingMethod", 3) # 0=None,1=SMA,2=EMA,3=SMMA,4=LWMA
self._rsi_weight = self.Param("RsiWeight", 1.0)
self._mfi_weight = self.Param("MfiWeight", 1.0)
self._wpr_weight = self.Param("WprWeight", 1.0)
self._de_marker_weight = self.Param("DeMarkerWeight", 1.0)
self._high_level = self.Param("HighLevel", 60.0)
self._low_level = self.Param("LowLevel", 40.0)
self._buy_open_enabled = self.Param("BuyOpenEnabled", True)
self._sell_open_enabled = self.Param("SellOpenEnabled", True)
self._buy_close_enabled = self.Param("BuyCloseEnabled", True)
self._sell_close_enabled = self.Param("SellCloseEnabled", True)
self._stop_loss_points = self.Param("StopLossPoints", 1000)
self._take_profit_points = self.Param("TakeProfitPoints", 2000)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15)))
self._oscillator_history = []
self._previous_high = 0.0
self._previous_low = 0.0
self._has_previous_candle = False
self._positive_flow = []
self._negative_flow = []
self._previous_typical = 0.0
self._has_previous_typical = False
self._positive_sum = 0.0
self._negative_sum = 0.0
self._entry_price = 0.0
self._stop_price = None
self._take_price = None
@property
def TrendMode(self):
return self._trend_mode.Value
@TrendMode.setter
def TrendMode(self, value):
self._trend_mode.Value = value
@property
def SignalBar(self):
return self._signal_bar.Value
@SignalBar.setter
def SignalBar(self, value):
self._signal_bar.Value = value
@property
def Period(self):
return self._period.Value
@Period.setter
def Period(self, value):
self._period.Value = value
@property
def RsiWeight(self):
return self._rsi_weight.Value
@RsiWeight.setter
def RsiWeight(self, value):
self._rsi_weight.Value = value
@property
def MfiWeight(self):
return self._mfi_weight.Value
@MfiWeight.setter
def MfiWeight(self, value):
self._mfi_weight.Value = value
@property
def WprWeight(self):
return self._wpr_weight.Value
@WprWeight.setter
def WprWeight(self, value):
self._wpr_weight.Value = value
@property
def DeMarkerWeight(self):
return self._de_marker_weight.Value
@DeMarkerWeight.setter
def DeMarkerWeight(self, value):
self._de_marker_weight.Value = value
@property
def HighLevel(self):
return self._high_level.Value
@HighLevel.setter
def HighLevel(self, value):
self._high_level.Value = value
@property
def LowLevel(self):
return self._low_level.Value
@LowLevel.setter
def LowLevel(self, value):
self._low_level.Value = value
@property
def BuyOpenEnabled(self):
return self._buy_open_enabled.Value
@BuyOpenEnabled.setter
def BuyOpenEnabled(self, value):
self._buy_open_enabled.Value = value
@property
def SellOpenEnabled(self):
return self._sell_open_enabled.Value
@SellOpenEnabled.setter
def SellOpenEnabled(self, value):
self._sell_open_enabled.Value = value
@property
def BuyCloseEnabled(self):
return self._buy_close_enabled.Value
@BuyCloseEnabled.setter
def BuyCloseEnabled(self, value):
self._buy_close_enabled.Value = value
@property
def SellCloseEnabled(self):
return self._sell_close_enabled.Value
@SellCloseEnabled.setter
def SellCloseEnabled(self, value):
self._sell_close_enabled.Value = value
@property
def StopLossPoints(self):
return self._stop_loss_points.Value
@StopLossPoints.setter
def StopLossPoints(self, value):
self._stop_loss_points.Value = value
@property
def TakeProfitPoints(self):
return self._take_profit_points.Value
@TakeProfitPoints.setter
def TakeProfitPoints(self, value):
self._take_profit_points.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(fractal_weight_oscillator_strategy, self).OnStarted2(time)
self._oscillator_history = []
self._previous_high = 0.0
self._previous_low = 0.0
self._has_previous_candle = False
self._positive_flow = []
self._negative_flow = []
self._previous_typical = 0.0
self._has_previous_typical = False
self._positive_sum = 0.0
self._negative_sum = 0.0
self._entry_price = 0.0
self._stop_price = None
self._take_price = None
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self.Period
self._williams_rsi = RelativeStrengthIndex()
self._williams_rsi.Length = self.Period
self._de_max_sma = SimpleMovingAverage()
self._de_max_sma.Length = self.Period
self._de_min_sma = SimpleMovingAverage()
self._de_min_sma.Length = self.Period
self._smoother = self._create_smoother(int(self._smoothing_method.Value), int(self._smoothing_length.Value))
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self.ProcessCandle).Start()
def ProcessCandle(self, candle):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
high = float(candle.HighPrice)
low = float(candle.LowPrice)
open_price = float(candle.OpenPrice)
typical = (high + low + close) / 3.0
volume = float(candle.TotalVolume)
rsi_result = process_float(self._rsi, candle.ClosePrice, candle.OpenTime, True)
wpr_result = process_float(self._williams_rsi, candle.ClosePrice, candle.OpenTime, True)
if not rsi_result.IsFinal or not wpr_result.IsFinal:
return
rsi_val = float(rsi_result)
wpr_val = float(wpr_result)
mfi = self._calculate_mfi(typical, volume)
demarker = self._calculate_demarker(candle)
if mfi is None or demarker is None:
return
total_weight = float(self.RsiWeight) + float(self.MfiWeight) + float(self.WprWeight) + float(self.DeMarkerWeight)
if total_weight <= 0.0:
return
weighted = (float(self.RsiWeight) * rsi_val +
float(self.MfiWeight) * mfi +
float(self.WprWeight) * wpr_val +
float(self.DeMarkerWeight) * (demarker * 100.0)) / total_weight
smoothed = self._apply_smoothing(weighted, candle.OpenTime)
if smoothed is None:
return
self._oscillator_history.append(smoothed)
self._trim_history()
signal_bar = int(self.SignalBar)
if len(self._oscillator_history) < signal_bar + 2:
return
current_index = len(self._oscillator_history) - 1 - signal_bar
if current_index <= 0:
return
current = self._oscillator_history[current_index]
previous = self._oscillator_history[current_index - 1]
self._check_risk(candle)
high_lvl = float(self.HighLevel)
low_lvl = float(self.LowLevel)
cross_below_low = previous > low_lvl and current <= low_lvl
cross_above_high = previous < high_lvl and current >= high_lvl
open_buy = False
close_buy = False
open_sell = False
close_sell = False
if self.TrendMode == TREND_DIRECT:
if cross_below_low:
open_buy = self.BuyOpenEnabled
close_sell = self.SellCloseEnabled
if cross_above_high:
open_sell = self.SellOpenEnabled
close_buy = self.BuyCloseEnabled
else:
if cross_below_low:
open_sell = self.SellOpenEnabled
close_buy = self.BuyCloseEnabled
if cross_above_high:
open_buy = self.BuyOpenEnabled
close_sell = self.SellCloseEnabled
if close_buy and self.Position > 0:
self.SellMarket()
self._reset_risk()
if close_sell and self.Position < 0:
self.BuyMarket()
self._reset_risk()
if open_buy and self.Position <= 0:
self.BuyMarket()
self._set_risk_levels(close, True)
elif open_sell and self.Position >= 0:
self.SellMarket()
self._set_risk_levels(close, False)
def _calculate_mfi(self, typical, volume):
if not self._has_previous_typical:
self._previous_typical = typical
self._has_previous_typical = True
self._positive_flow = []
self._negative_flow = []
self._positive_sum = 0.0
self._negative_sum = 0.0
return None
flow = typical * volume
positive = flow if typical > self._previous_typical else 0.0
negative = flow if typical < self._previous_typical else 0.0
self._previous_typical = typical
self._positive_sum += positive
self._negative_sum += negative
self._positive_flow.append(positive)
self._negative_flow.append(negative)
period = int(self.Period)
if len(self._positive_flow) > period:
self._positive_sum -= self._positive_flow.pop(0)
self._negative_sum -= self._negative_flow.pop(0)
if len(self._positive_flow) < period:
return None
if self._negative_sum == 0.0:
return 100.0
ratio = self._positive_sum / self._negative_sum
return 100.0 - 100.0 / (1.0 + ratio)
def _calculate_demarker(self, candle):
high = float(candle.HighPrice)
low = float(candle.LowPrice)
if not self._has_previous_candle:
self._previous_high = high
self._previous_low = low
self._has_previous_candle = True
return None
de_max = max(high - self._previous_high, 0.0)
de_min = max(self._previous_low - low, 0.0)
self._previous_high = high
self._previous_low = low
de_max_result = process_float(self._de_max_sma, Decimal(de_max), candle.OpenTime, True)
de_min_result = process_float(self._de_min_sma, Decimal(de_min), candle.OpenTime, True)
if not de_max_result.IsFinal or not de_min_result.IsFinal:
return None
max_avg = float(de_max_result)
min_avg = float(de_min_result)
denom = max_avg + min_avg
if denom == 0.0:
return 0.5
return max_avg / denom
def _trim_history(self):
period = int(self.Period)
smoothing_len = int(self._smoothing_length.Value)
signal_bar = int(self.SignalBar)
max_size = signal_bar + max(period, smoothing_len) + 5
if len(self._oscillator_history) > max_size:
remove = len(self._oscillator_history) - max_size
self._oscillator_history = self._oscillator_history[remove:]
def _check_risk(self, candle):
high = float(candle.HighPrice)
low = float(candle.LowPrice)
if self.Position > 0:
if self._stop_price is not None and low <= self._stop_price:
self.SellMarket()
self._reset_risk()
return
if self._take_price is not None and high >= self._take_price:
self.SellMarket()
self._reset_risk()
elif self.Position < 0:
if self._stop_price is not None and high >= self._stop_price:
self.BuyMarket()
self._reset_risk()
return
if self._take_price is not None and low <= self._take_price:
self.BuyMarket()
self._reset_risk()
def _set_risk_levels(self, close, is_long):
self._entry_price = close
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 0.0
if step <= 0.0:
self._stop_price = None
self._take_price = None
return
sl_pts = int(self.StopLossPoints)
tp_pts = int(self.TakeProfitPoints)
if sl_pts > 0:
if is_long:
self._stop_price = close - step * sl_pts
else:
self._stop_price = close + step * sl_pts
else:
self._stop_price = None
if tp_pts > 0:
if is_long:
self._take_price = close + step * tp_pts
else:
self._take_price = close - step * tp_pts
else:
self._take_price = None
def _create_smoother(self, method, length):
if method == 0:
return None
elif method == 1:
ind = SimpleMovingAverage()
ind.Length = length
return ind
elif method == 2:
ind = ExponentialMovingAverage()
ind.Length = length
return ind
elif method == 3:
ind = SmoothedMovingAverage()
ind.Length = length
return ind
elif method == 4:
ind = WeightedMovingAverage()
ind.Length = length
return ind
else:
ind = SmoothedMovingAverage()
ind.Length = length
return ind
def _apply_smoothing(self, value, time):
if self._smoother is None:
return value
result = process_float(self._smoother, Decimal(value), time, True)
if result.IsFinal:
return float(result)
return None
def _reset_risk(self):
self._entry_price = 0.0
self._stop_price = None
self._take_price = None
def OnReseted(self):
super(fractal_weight_oscillator_strategy, self).OnReseted()
self._oscillator_history = []
self._previous_high = 0.0
self._previous_low = 0.0
self._has_previous_candle = False
self._positive_flow = []
self._negative_flow = []
self._previous_typical = 0.0
self._has_previous_typical = False
self._positive_sum = 0.0
self._negative_sum = 0.0
self._reset_risk()
def CreateClone(self):
return fractal_weight_oscillator_strategy()