GitHub で見る
BlauTvi戦略
この戦略はMQL5エキスパートExp_BlauTVIをStockSharpの高レベル戦略に変換したものです。**Blau True Volume Index(TVI)**を使用してティックボリュームフローの反転を検出します。
アイデア
True Volume IndexはアップティックとダウンティックをFR分離し、3つの指数移動平均で平滑化します。最終値は-100から+100の間で変動し、買い手または売り手の優勢を表します。戦略はインジケーターが下落後に上向きに転換するとロングポジションを開き、上昇後に下向きに転換するとショートポジションを開きます。反対方向の既存ポジションはクローズされます。
パラメーター
Length1 – アップティックとダウンティックの第1平滑化期間。
Length2 – 第2平滑化期間。
Length3 – TVIに適用される最終平滑化期間。
CandleType – 計算に使用するローソク足の種類(デフォルト:4時間足)。
Allow Buy Open – ロングポジションのオープンを有効化。
Allow Sell Open – ショートポジションのオープンを有効化。
Allow Buy Close – 売りシグナル発生時のロングポジションクローズを有効化。
Allow Sell Close – 買いシグナル発生時のショートポジションクローズを有効化。
Enable Stop Loss – ポイント単位のストップロス保護を使用。
Stop Loss – ストップロスの値(ポイント)。
Enable Take Profit – ポイント単位のテイクプロフィット保護を使用。
Take Profit – テイクプロフィットの値(ポイント)。
Volume – 注文ボリューム(ロット)。
シグナル
- 買い – 前のTVI値がその前より低く、現在のTVI値が前の値より大きい場合。有効な場合、既存のショートポジションがクローズされます。
- 売り – 前のTVI値がその前より高く、現在のTVI値が前の値より小さい場合。有効な場合、既存のロングポジションがクローズされます。
完成したローソク足のみが処理され、すべての計算にはローソク足のティックボリュームが使用されます。ストップロスとテイクプロフィットはオプションで、価格ポイントで表されます。
注意事項
戦略は高レベルAPIを使用します:ローソク足をサブスクライブし、ExponentialMovingAverageインスタンスで内部的にインジケーターを計算し、BuyMarketおよびSellMarketメソッドでポジションを管理します。チャートには戦略が執行した取引とともにTVIインジケーターが表示されます。
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>
/// Strategy based on smoothed momentum direction changes (Blau TVI concept).
/// Uses triple-smoothed EMA direction changes for signals.
/// </summary>
public class BlauTviStrategy : Strategy
{
private readonly StrategyParam<int> _length;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevEma;
private decimal _prevPrevEma;
private int _count;
public int Length { get => _length.Value; set => _length.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BlauTviStrategy()
{
_length = Param(nameof(Length), 12)
.SetGreaterThanZero()
.SetDisplay("Length", "Smoothing length", "Indicator");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevEma = 0;
_prevPrevEma = 0;
_count = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = Length };
SubscribeCandles(CandleType)
.Bind(ema, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal emaValue)
{
if (candle.State != CandleStates.Finished)
return;
_count++;
if (_count < 3)
{
_prevPrevEma = _prevEma;
_prevEma = emaValue;
return;
}
var turnUp = _prevEma < _prevPrevEma && emaValue > _prevEma;
var turnDown = _prevEma > _prevPrevEma && emaValue < _prevEma;
if (turnUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (turnDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevPrevEma = _prevEma;
_prevEma = emaValue;
}
}
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
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class blau_tvi_strategy(Strategy):
def __init__(self):
super(blau_tvi_strategy, self).__init__()
self._length = self.Param("Length", 12) \
.SetDisplay("Length", "Smoothing length", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_ema = 0.0
self._prev_prev_ema = 0.0
self._count = 0
@property
def Length(self):
return self._length.Value
@Length.setter
def Length(self, value):
self._length.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(blau_tvi_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self.Length
self.SubscribeCandles(self.CandleType) \
.Bind(ema, self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle, ema_value):
if candle.State != CandleStates.Finished:
return
ema_val = float(ema_value)
self._count += 1
if self._count < 3:
self._prev_prev_ema = self._prev_ema
self._prev_ema = ema_val
return
turn_up = self._prev_ema < self._prev_prev_ema and ema_val > self._prev_ema
turn_down = self._prev_ema > self._prev_prev_ema and ema_val < self._prev_ema
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_ema = self._prev_ema
self._prev_ema = ema_val
def OnReseted(self):
super(blau_tvi_strategy, self).OnReseted()
self._prev_ema = 0.0
self._prev_prev_ema = 0.0
self._count = 0
def CreateClone(self):
return blau_tvi_strategy()