Estrategia de Reversión del Cuerpo de Vela Go
Estrategia basada en el indicador Go que promedia el tamaño del cuerpo de la vela. Abre una posición larga cuando el cuerpo suavizado de la vela cruza por debajo de cero después de ser positivo y abre una posición corta en el cruce opuesto. Las posiciones existentes se cierran con señales opuestas.
Detalles
- Criterios de entrada: cambio de signo del SMA del cuerpo (positivo a negativo para largo, negativo a positivo para corto)
- Largo/Corto: Ambos
- Criterios de salida: cambio de signo opuesto del SMA del cuerpo
- Stops: No
- Valores predeterminados:
Period= 174CandleType= 1 hora
- Filtros:
- Categoría: Reversión
- Dirección: Largo y Corto
- Indicadores: SMA
- Stops: No
- Complejidad: Básico
- Marco temporal: Intradía
- Estacionalidad: No
- Redes neuronales: No
- Divergencia: No
- Nivel de riesgo: Medio
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;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on smoothed candle body direction.
/// Smooths (close-open) with SMA, trades on sign changes.
/// </summary>
public class GoCandleBodyReversalStrategy : Strategy
{
private readonly StrategyParam<int> _period;
private readonly StrategyParam<DataType> _candleType;
private ExponentialMovingAverage _bodySma;
private int _prevSign;
public int Period { get => _period.Value; set => _period.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public GoCandleBodyReversalStrategy()
{
_period = Param(nameof(Period), 30)
.SetGreaterThanZero()
.SetDisplay("Period", "SMA period for candle body", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "Parameters");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_bodySma = null;
_prevSign = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_bodySma = new ExponentialMovingAverage { Length = Period };
Indicators.Add(_bodySma);
// Use a warmup EMA bound to close price
var warmup = new ExponentialMovingAverage { Length = Period };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(warmup, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal _warmupVal)
{
if (candle.State != CandleStates.Finished)
return;
var body = candle.ClosePrice - candle.OpenPrice;
var maResult = _bodySma.Process(new DecimalIndicatorValue(_bodySma, body, candle.OpenTime) { IsFinal = true });
if (!maResult.IsFormed)
return;
var value = maResult.GetValue<decimal>();
var sign = value > 0 ? 1 : value < 0 ? -1 : 0;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevSign = sign;
return;
}
if (_prevSign == 0)
{
_prevSign = sign;
return;
}
// Body direction turns negative (bearish reversal) -> sell
if (sign < 0 && _prevSign > 0 && Position >= 0)
SellMarket();
// Body direction turns positive (bullish reversal) -> buy
else if (sign > 0 && _prevSign < 0 && Position <= 0)
BuyMarket();
_prevSign = sign;
}
}
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
from indicator_extensions import *
class go_candle_body_reversal_strategy(Strategy):
def __init__(self):
super(go_candle_body_reversal_strategy, self).__init__()
self._period = self.Param("Period", 30) \
.SetDisplay("Period", "SMA period for candle body", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "Parameters")
self._body_sma = None
self._prev_sign = 0
@property
def period(self):
return self._period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(go_candle_body_reversal_strategy, self).OnReseted()
self._body_sma = None
self._prev_sign = 0
def OnStarted2(self, time):
super(go_candle_body_reversal_strategy, self).OnStarted2(time)
self._body_sma = ExponentialMovingAverage()
self._body_sma.Length = self.period
self.Indicators.Add(self._body_sma)
warmup = ExponentialMovingAverage()
warmup.Length = self.period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(warmup, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def process_candle(self, candle, _warmup_val):
if candle.State != CandleStates.Finished:
return
body = float(candle.ClosePrice) - float(candle.OpenPrice)
ma_result = process_float(self._body_sma, body, candle.OpenTime, True)
if not ma_result.IsFormed:
return
value = float(ma_result)
if value > 0:
sign = 1
elif value < 0:
sign = -1
else:
sign = 0
if self._prev_sign == 0:
self._prev_sign = sign
return
if sign < 0 and self._prev_sign > 0 and self.Position >= 0:
self.SellMarket()
elif sign > 0 and self._prev_sign < 0 and self.Position <= 0:
self.BuyMarket()
self._prev_sign = sign
def CreateClone(self):
return go_candle_body_reversal_strategy()