重心 OSMA 策略
该策略利用 重心 OSMA 振荡器来识别潜在的趋势反转。 振荡器将简单和加权移动平均相乘,并进行两次平滑处理以跟踪方向变化。 当指标在下跌后向上转折时,策略平掉空头并可选择开多; 当指标在上涨后向下转折时,策略平掉多头并可选择开空。
工作原理
- 使用收盘价作为自定义指标输入。
- 指标计算过程:
- 计算周期为
Period的简单移动平均 (SMA); - 计算相同周期的加权移动平均 (
WMA); - 将两个平均值相乘;
- 使用
SmoothPeriod1与SmoothPeriod2进行两次额外平滑。
- 计算周期为
- 交易规则:
- 若前一个值低于其前值且当前值高于前一个值,说明振荡器向上转折,关闭空头并可开多;
- 若前一个值高于其前值且当前值低于前一个值,说明振荡器向下转折,关闭多头并可开空;
StopLoss与TakeProfit以价格单位定义止损和止盈。
参数
Period– SMA 与 WMA 的基础周期;SmoothPeriod1– 第一次平滑的周期;SmoothPeriod2– 第二次平滑的周期;StopLoss– 止损距离,0 表示禁用;TakeProfit– 止盈距离,0 表示禁用;BuyPosOpen– 是否允许开多;SellPosOpen– 是否允许开空;BuyPosClose– 是否在卖出信号时关闭多头;SellPosClose– 是否在买入信号时关闭空头;CandleType– 用于计算的K线类型(时间框)。
说明
- 仅提供 C# 版本,Python 文件夹故意缺失。
- 修改代码时请使用制表符缩进。
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>
/// Center of Gravity OSMA strategy.
/// Uses SMA vs WMA difference (center of gravity concept) direction changes for signals.
/// </summary>
public class CenterOfGravityOsmaStrategy : Strategy
{
private readonly StrategyParam<int> _period;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevOsma;
private decimal _prevPrevOsma;
private int _count;
public int Period { get => _period.Value; set => _period.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public CenterOfGravityOsmaStrategy()
{
_period = Param(nameof(Period), 10)
.SetGreaterThanZero()
.SetDisplay("Period", "Calculation period", "Indicator");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevOsma = 0;
_prevPrevOsma = 0;
_count = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SimpleMovingAverage { Length = Period };
var wma = new WeightedMovingAverage { Length = Period };
SubscribeCandles(CandleType)
.Bind(sma, wma, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal smaValue, decimal wmaValue)
{
if (candle.State != CandleStates.Finished)
return;
var osma = smaValue - wmaValue;
_count++;
if (_count < 3)
{
_prevPrevOsma = _prevOsma;
_prevOsma = osma;
return;
}
// Buy when OSMA turns up
var turnUp = _prevOsma < _prevPrevOsma && osma > _prevOsma;
// Sell when OSMA turns down
var turnDown = _prevOsma > _prevPrevOsma && osma < _prevOsma;
if (turnUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (turnDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevPrevOsma = _prevOsma;
_prevOsma = osma;
}
}
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 SimpleMovingAverage, WeightedMovingAverage
from StockSharp.Algo.Strategies import Strategy
class center_of_gravity_osma_strategy(Strategy):
def __init__(self):
super(center_of_gravity_osma_strategy, self).__init__()
self._period = self.Param("Period", 10) \
.SetDisplay("Period", "Calculation period", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._prev_osma = 0.0
self._prev_prev_osma = 0.0
self._count = 0
@property
def Period(self):
return self._period.Value
@Period.setter
def Period(self, value):
self._period.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(center_of_gravity_osma_strategy, self).OnStarted2(time)
sma = SimpleMovingAverage()
sma.Length = self.Period
wma = WeightedMovingAverage()
wma.Length = self.Period
self.SubscribeCandles(self.CandleType) \
.Bind(sma, wma, self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle, sma_value, wma_value):
if candle.State != CandleStates.Finished:
return
osma = float(sma_value) - float(wma_value)
self._count += 1
if self._count < 3:
self._prev_prev_osma = self._prev_osma
self._prev_osma = osma
return
turn_up = self._prev_osma < self._prev_prev_osma and osma > self._prev_osma
turn_down = self._prev_osma > self._prev_prev_osma and osma < self._prev_osma
if turn_up and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif turn_down and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_prev_osma = self._prev_osma
self._prev_osma = osma
def OnReseted(self):
super(center_of_gravity_osma_strategy, self).OnReseted()
self._prev_osma = 0.0
self._prev_prev_osma = 0.0
self._count = 0
def CreateClone(self):
return center_of_gravity_osma_strategy()