最終価格戦略
この戦略は、最終取引価格がユーザー定義の間隔だけ離れたときに、最良の買い値または売り値に指値注文を出します。Level1の板情報の更新と約定情報を監視してエントリーを判断します。
詳細
- エントリー条件:
- ロング: 最終価格 ≥ 最良売値 + 間隔。
- ショート: 最終価格 ≤ 最良買値 - 間隔。
- ロング/ショート: 両方向。
- エグジット条件:
- 逆シグナル、または許可された取引セッション外。
- ストップ: ストップロスのみ。
- デフォルト値:
Interval= 400Min Volume= 1Max Volume= 900000Spread= 200Volume= 1Stop Loss= 400
- 取引セッション:
- 10:05:40 – 13:54:30
- 14:08:30 – 15:44:30
- 16:05:30 – 18:39:30
- 19:15:10 – 23:44:30
- フィルター:
- カテゴリ: ブレイクアウト
- 方向: 両方
- インジケーター: なし
- ストップ: はい
- 複雑さ: 中級
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy that enters when price moves away from a moving average by
/// a configurable distance, expecting mean reversion.
/// </summary>
public class LastPriceStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<decimal> _distancePct;
private decimal _entryPrice;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int MaPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
public decimal DistancePct
{
get => _distancePct.Value;
set => _distancePct.Value = value;
}
public LastPriceStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_maPeriod = Param(nameof(MaPeriod), 20)
.SetDisplay("MA Period", "Moving average period", "Parameters");
_distancePct = Param(nameof(DistancePct), 0.5m)
.SetDisplay("Distance %", "Percent distance from MA to trigger entry", "Parameters");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_entryPrice = 0;
var ma = new ExponentialMovingAverage { Length = MaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal maValue)
{
if (candle.State != CandleStates.Finished)
return;
var price = candle.ClosePrice;
var threshold = maValue * DistancePct / 100m;
// Exit: price returned to MA
if (Position > 0 && price >= maValue)
{
SellMarket();
_entryPrice = 0;
}
else if (Position < 0 && price <= maValue)
{
BuyMarket();
_entryPrice = 0;
}
// Entry: price moved away from MA
if (Position == 0)
{
if (price < maValue - threshold)
{
BuyMarket();
_entryPrice = price;
}
else if (price > maValue + threshold)
{
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class last_price_strategy(Strategy):
def __init__(self):
super(last_price_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._ma_period = self.Param("MaPeriod", 20) \
.SetDisplay("MA Period", "Moving average period", "Parameters")
self._distance_pct = self.Param("DistancePct", 0.5) \
.SetDisplay("Distance %", "Percent distance from MA to trigger entry", "Parameters")
self._entry_price = 0.0
@property
def candle_type(self):
return self._candle_type.Value
@property
def ma_period(self):
return self._ma_period.Value
@property
def distance_pct(self):
return self._distance_pct.Value
def OnReseted(self):
super(last_price_strategy, self).OnReseted()
self._entry_price = 0.0
def OnStarted2(self, time):
super(last_price_strategy, self).OnStarted2(time)
self._entry_price = 0.0
ma = ExponentialMovingAverage()
ma.Length = self.ma_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ma, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ma)
self.DrawOwnTrades(area)
def process_candle(self, candle, ma_value):
if candle.State != CandleStates.Finished:
return
ma_value = float(ma_value)
price = float(candle.ClosePrice)
threshold = ma_value * float(self.distance_pct) / 100.0
# Exit: price returned to MA
if self.Position > 0 and price >= ma_value:
self.SellMarket()
self._entry_price = 0.0
elif self.Position < 0 and price <= ma_value:
self.BuyMarket()
self._entry_price = 0.0
# Entry: price moved away from MA
if self.Position == 0:
if price < ma_value - threshold:
self.BuyMarket()
self._entry_price = price
elif price > ma_value + threshold:
self.SellMarket()
self._entry_price = price
def CreateClone(self):
return last_price_strategy()