カスタムシグナルオシレーター
2つの価格シグナルの差を使用する戦略。オシレーターがゼロを上抜けするとロング、下抜けするとショートに入る。ロングのみモードが有効な場合、マイナスクロスでポジションを決済する。
詳細
- エントリー条件: オシレーターがゼロをクロスする。
- ロング/ショート: 両方向またはロングのみ。
- エグジット条件: 逆シグナルまたはロングのみモードでのゼロクロス。
- ストップ: いいえ。
- デフォルト値:
LongOnly= falseCandleType= TimeSpan.FromMinutes(5)
- フィルター:
- カテゴリ: オシレーター
- 方向: 両方
- インジケーター: なし
- ストップ: いいえ
- 複雑さ: 基本
- 時間軸: イントラデイ (5m)
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Custom Signal Oscillator strategy.
/// The oscillator is the difference between two price signals of the candle, its close and its open.
/// A cross above zero goes long and a cross below zero goes short, reversing an opposite position.
/// In long-only mode a cross below zero only closes the long.
/// </summary>
public class CustomSignalOscillatorStrategy : Strategy
{
private readonly StrategyParam<bool> _longOnly;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevOscillator;
/// <summary>
/// Trade only long positions.
/// </summary>
public bool LongOnly
{
get => _longOnly.Value;
set => _longOnly.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public CustomSignalOscillatorStrategy()
{
_longOnly = Param(nameof(LongOnly), false)
.SetDisplay("Long Only", "Trade only long positions", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevOscillator = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevOscillator = null;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var oscillator = candle.ClosePrice - candle.OpenPrice;
var prev = _prevOscillator;
_prevOscillator = oscillator;
if (prev is not decimal prevOscillator)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var crossUp = prevOscillator <= 0m && oscillator > 0m;
var crossDown = prevOscillator >= 0m && oscillator < 0m;
if (crossUp && Position <= 0)
{
BuyMarket(Volume + Math.Abs(Position));
}
else if (crossDown)
{
if (LongOnly)
{
if (Position > 0)
SellMarket(Position);
}
else if (Position >= 0)
{
SellMarket(Volume + Math.Abs(Position));
}
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class custom_signal_oscillator_strategy(Strategy):
"""
Custom Signal Oscillator strategy.
The oscillator is the difference between two price signals of the candle, its close and its open.
A cross above zero goes long and a cross below zero goes short, reversing an opposite position.
In long-only mode a cross below zero only closes the long.
"""
def __init__(self):
super(custom_signal_oscillator_strategy, self).__init__()
self._long_only = self.Param("LongOnly", False) \
.SetDisplay("Long Only", "Trade only long positions", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_oscillator = None
@property
def candle_type(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.candle_type)]
def OnReseted(self):
super(custom_signal_oscillator_strategy, self).OnReseted()
self._prev_oscillator = None
def OnStarted2(self, time):
super(custom_signal_oscillator_strategy, self).OnStarted2(time)
self._prev_oscillator = None
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
oscillator = candle.ClosePrice - candle.OpenPrice
prev = self._prev_oscillator
self._prev_oscillator = oscillator
if prev is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
cross_up = prev <= 0 and oscillator > 0
cross_down = prev >= 0 and oscillator < 0
if cross_up and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif cross_down:
if self._long_only.Value:
if self.Position > 0:
self.SellMarket(self.Position)
elif self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
def CreateClone(self):
return custom_signal_oscillator_strategy()