II Curso de Programación. Sistema Ejercicio 27

Esta semana publicamos la solución del ejercicio 27 de nuestro 2º Curso de Programación para Visual Chart 5. Este ejercicio servía de excusa para hablar acerca del indicador Ichimoku Kinko Hyo y de las señales de operativa que podemos extraer de él.

Visual Chart dispone de varias versiones del indicador Ichimoku. Para el diseño de éste sistema, se ha  utilizado la versión 2, la cual pueden encontrar en el siguiente enlace:

Ichimoku 2

Sistema Ejercicio 27: Sistema cruce de líneas Ichimoku
El indicador Ichimoku es muy interesante porque tiene varias lecturas. No obstante, en términos generales, se suele tomar como señal de compra y venta el cruce de las líneas principales Tenkan y Kijun. En el ejercicio, además, se usa el periodo Chikou como filtro de confirmación de tendencia.

En la siguiente imagen vemos ilustrada la estrategia:




En la imagen podemos observar cómo el sistema queda fuera de mercado cuando aparece un nuevo cruce pero el filtro de confirmación no lo supera. Además, el sistema lleva añadido un stop de protección para cubrir pérdidas.

Por último, incluimos un horario de operativa.

El diseño del sistema quedaría así:

Código PDV



Código VBA
(NOTA: Recuerden que el código aquí expuesto sólo incluye los métodos OnCalculateBar() y OnInitCalculate(). Tengan esto en cuenta a la hora de copiar el sistema).


'¡¡ Parameters
Dim Contratos As Long '1
Dim HoraIni As Integer '900
Dim HoraFin As Integer '1900
Dim ChikouPeriod As Integer '26
Dim TenkanSENPeriod As Integer '9
Dim KijunSENPeriod As Integer '26
Dim SenkouSPANProjection As Integer '26
Dim SenkouSPANA As Integer '52
Dim SenkouSPANB As Integer '26
Dim StopLoss As Double '75
'Parameters !!
Dim ichdata As DataIdentifier
Dim activarkijun As Boolean
Option Explicit
Public APP As SysUserApp
Implements System
Public Sub System_OnInitCalculate()
With APP
ichdata = .GetIndicatorIdentifier(ICHKH2, Data, ChikouPeriod, TenkanSENPeriod, KijunSENPeriod, SenkouSPANProjection, SenkouSPANA, SenkouSPANB, 0)
End With
End Sub
Public Sub System_OnCalculateBar(ByVal Bar As Long)
With APP
Dim tenkanact As Double
Dim kijunact As Double
Dim tenkanant As Double
Dim kijunant As Double
Dim nuevaorden As Integer
Dim pos_abierta As Integer
Dim pentrada As Double

pos_abierta = .GetMarketPosition()
pentrada = .GetEntryPrice()

tenkanact = .GetIndicatorValue(ichdata, 0, 2)
kijunact = .GetIndicatorValue(ichdata)
tenkanant = .GetIndicatorValue(ichdata, 1, 2)
kijunant = .GetIndicatorValue(ichdata, 1, 1)

If (.Time >= HoraIni And .Time < HoraFin) Then
'entradas
If (pos_abierta <> 1 And tenkanact > kijunact And tenkanant <= kijunant) Then
If (.Close() > .Close(ChikouPeriod)) Then
.Buy AtClose, Contratos
pentrada = .Close()
pos_abierta = 1
activarkijun = False
ElseIf (pos_abierta = -1) Then
.ExitShort AtClose, Contratos
pos_abierta = 0
End If
End If
If (pos_abierta <> -1 And tenkanact < kijunact And tenkanant >= kijunant) Then
If (.Close() < .Close(ChikouPeriod)) Then
.Sell AtClose, Contratos
pentrada = .Close()
pos_abierta = -1
activarkijun = False
ElseIf (pos_abierta = 1) Then
.ExitLong AtClose, Contratos
pos_abierta = 0
End If
End If

'salidas
If (pos_abierta = 1) Then
If (Not activarkijun) Then
activarkijun = (.Close() > pentrada + StopLoss)
End If
If (activarkijun) Then
.ExitLong AtStop, Contratos, kijunact
Else
.ExitLong AtStop, Contratos, pentrada - StopLoss
End If
ElseIf (pos_abierta = -1) Then
If (Not activarkijun) Then
activarkijun = (.Close() < pentrada - StopLoss)
End If
If (activarkijun) Then
.ExitShort AtStop, Contratos, kijunact
Else
.ExitShort AtStop, Contratos, pentrada + StopLoss
End If
End If
Else
If (pos_abierta = 1) Then
.ExitLong AtClose, Contratos
ElseIf (pos_abierta = -1) Then
.ExitShort AtClose, Contratos
End If
End If
End With
End Sub

Comentarios

Entradas populares de este blog

Trading Tools: Descarga de históricos para Visual Chart 6

KDJ - Indicador Stochastic %J

El indicador Relative Strength Mansfield with Index