II Curso de Programación. Sistema Tema 7.

Continuamos con la publicación de los sistemas de ejemplo que estamos elaborando a través de nuestro 2º Curso de Programación para Visual Chart 5.

Sistema Tema 7: Sistema Estocástico y MACD con Control Largos/Cortos
Partiendo del sistema del Tema 6, incluiremos en éste sistema un control para que sólo opere a largo o a corto en función del valor de un nuevo parámetro.

Este nuevo parámetro lo vamos a llamar CortosOLargos y según su valor se actuará del siguiente modo:
- Si vale 0. Permite operar en ambos sentidos.
- Si vale 1. Sólo opera a Largo.
- Si vale -1. Sólo opera a Corto.


La regla principal que tenemos que seguir es que la modificación del sistema no debe cambiar en absoluto los negocios que se mantienen vigentes. Para ello, debemos comprobar que en los momentos de giro de posición del sistema, el negocio previo debe cerrar en el momento adecuado, tal y como se muestra en la imagen anterior.



El desarrollo del sistema sería el siguiente:

Código PDV


Código VBA

'¡¡ Parameters
Dim Periodo_STOC As Integer '14
Dim Sk_STOC As Integer '3
Dim Sd_STOC As Integer '3
Dim UpBand_STOC As Double '80
Dim LoBand_STOC As Double '20
Dim Periodo1_MACD As Integer '12
Dim Periodo2_MACD As Integer '26
Dim PeriodoSIG_MACD As Integer '9
Dim StopPerdidas As Double '50
Dim CortoOLargo As Integer '0
'Parameters !!
Dim stkdata As DataIdentifier
Dim macddata As DataIdentifier
Dim crucestk As Integer
Option Explicit
Public APP As SysUserApp
Implements System
Public Sub System_OnInitCalculate()
With APP
    stkdata = .GetIndicatorIdentifier(Stochastic, Data, Periodo_STOC, Sk_STOC, Sd_STOC, AvgExponential, 80, 20)
    macddata = .GetIndicatorIdentifier(MACD, Data, Periodo1_MACD, Periodo2_MACD, PeriodoSIG_MACD, PriceClose, 0)
    crucestk = 0
End With
End Sub
Public Sub System_OnCalculateBar(ByVal Bar As Long)
With APP
    Dim skact As Double
    Dim skant As Double
    Dim macdact As Double
    Dim mesigact As Double
    Dim lanzae As Integer
    Dim pentrada As Double
    Dim pos_abierta As Integer
    Dim pstoploss As Double

    skact = .GetIndicatorValue(stkdata)
    skant = .GetIndicatorValue(stkdata, 1)
    macdact = .GetIndicatorValue(macddata)
    mesigact = .GetIndicatorValue(macddata, 0, 2)

    pos_abierta = .GetMarketPosition()
    pentrada = .GetEntryPrice()
    If (skact > LoBand_STOC And skant <= LoBand_STOC) Then
        crucestk = -1
    ElseIf (skact < UpBand_STOC And skant >= UpBand_STOC) Then
        crucestk = 1
    End If

    'reglas de entrada
    If (.GetMarketPosition <> 1 And crucestk = -1) Then
        If (macdact > mesigact) Then
            If (CortoOLargo = -1) Then
                If (pos_abierta = -1) Then
                    pstoploss = .High()
                End If
            Else
                .Buy AtStop, 1, .High()
                pos_abierta = 1
                pentrada = .High()
            End If
        End If
    End If
    If (.GetMarketPosition <> -1 And crucestk = 1) Then
        If (macdact < mesigact) Then
            If (CortoOLargo = 1) Then
                If (pos_abierta = 1) Then
                    pstoploss = .Low()
                End If
            Else
                .Sell AtStop, 1, .Low()
                pos_abierta = -1
                pentrada = .Low()
            End If
        End If
    End If

    'reglas de salida
    If (pos_abierta = 1) Then
        If (.GetMarketPosition() = 1 And .GetBarsSinceEntry() = 0) Then
            crucestk = 0
        End If
        If (pstoploss = 0 Or pentrada - StopPerdidas > pstoploss) Then
            pstoploss = pentrada - StopPerdidas
        End If
        .ExitLong AtStop, 1, pstoploss
    ElseIf (pos_abierta = -1) Then
        If (.GetMarketPosition() = -1 And .GetBarsSinceEntry() = 0) Then
            crucestk = 0
        End If
        If (pstoploss = 0 Or pentrada + StopPerdidas < pstoploss) Then
            pstoploss = pentrada + StopPerdidas
        End If
        .ExitShort AtStop, 1, pstoploss
    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