Comprar en otro marco temporal para no esperar a finalizar la vela

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #236461 quote
    mamees
    Participant
    New

    El backtest practicamente lo tengo finalizado y cuando lo paso en el marco temporal diario, segun veo, la compra la efectua en la vela siguiente.

    Se puede de alguna forma, añadir alguna condicion en marco temporal inferior, para que efectue antes la compra? ya que si ves el ejemplo de la imagen, la compra se efectua un 2% ya despues de cuando quiero.

    En la imagen te pongo diario y a la derecha horario (el sombreado horario equivale a la vela diaria).

    Este es el codigo:

     

    DEFPARAM CumulateOrders = true // Acumulación de posiciones desactivada
    compra=0
    maximosdecrecientes=(high[4]>high[3] or close[4]>close[3]) and (high[3]>high[2] or close[3]>close[2]) and (high[2]>high[1] or close[2]>close[1])
    cMaxDec=0
    minimosdecrecientes=low[4]>low[3] and low[3]>low[2] and low[2]>low[1]
    cMinDec=0
    velasrojas=open[3]>close[3] and open[2]>close[2] and open[1]>close[1]
    cVelRoj=0
    m20=average[20](close)
    cm20=0
    c3VelasDebajoM20=0
    cPC1=0
    cM20Alcista=0

    if maximosdecrecientes then
    cMaxDec=1
    endif

    if minimosdecrecientes and vminimosdecrecientes then
    cMinDec=1
    elsif vminimosdecrecientes=0 then
    cMinDec=1
    endif

    if vvelasrojas and velasrojas then
    cVelRoj=1
    elsif vvelasrojas=0 then
    cVelRoj=1
    endif

    for i=3 to 251
    minRel = (low[i] < low[i-3]) AND (low[i] < low[i-2]) AND (low[i] < low[i-1]) AND (low[i] < low[i+1]) AND (low[i] < low[i+2]) AND (low[i] < low[i+3])
    if minRel then
    posicionminrel=i
    break
    endif
    next

    if close[posicionminrel]>m20[posicionminrel] then
    cm20=1
    endif

    if close[1]<m20[1] and close[2]<m20[2] and close[3]<m20[3] then
    c3VelasDebajoM20=1
    endif

    if high>high[1] then
    dist=((high[1]-m20[1])/m20[1])*100
    if dist<4 then
    cPC1=1
    endif
    endif

    if m20>m20[1] and m20[1]>m20[2] then
    cM20Alcista=1
    endif

    if cMaxDec and cMinDec and cVelRoj and cm20 and c3VelasDebajoM20=0 and cPC1 and cM20Alcista then
    if low<low[1] then
    stoploss=low-low*0.01
    else
    stoploss=low[1]-low[1]*0.01
    endif

    // Calcula la cantidad de acciones a comprar basado en el riesgo y el stoploss
    capital = 10000 // Capital disponible // Riesgo por operación
    precioEntrada = close[0] // Precio actual
    cantidadAcciones = floor(riesgo / (precioEntrada – stoploss))

    if cantidadAcciones > 0 then
    BUY cantidadAcciones SHARES AT MARKET
    endif

    endif

    // Variables para el cálculo de salida
    maxAnterior = high[1]
    minAnterior = low[1]
    maxActual = high
    minActual = low

    // Condición 1: Vela actual tiene mayor máximo y mayor mínimo que la anterior
    condicion1 = (maxActual > maxAnterior and minActual > minAnterior)

    // Condición 2: El cierre de la vela actual es superior al cierre de la vela anterior
    condicion2 = (close > close[1])

    // Si ambas condiciones se cumplen
    if condicion1 and condicion2 then
    // Establecer el stop loss en el mínimo más bajo de las dos velas anteriores
    nuevostoploss=0
    if low[1]<low[2] then
    nuevostoploss = low[1]-low[1]*0.01
    else
    nuevostoploss = low[2]-low[2]*0.01
    endif
    if nuevostoploss>stopLoss then
    stopLoss=nuevostoploss
    endif
    endif

    if longonmarket and low<=stopLoss then
    SELL AT MARKET
    endif

     

     

    Muchas gracias.

    compra-pc1.png compra-pc1.png
    #236470 quote
    robertogozzi
    Moderator
    Master

    Faltan algunas variables, publique o adjunte el archivo ITF.

    #236488 quote
    mamees
    Participant
    New

    Adjunto el fichero.

    2 variables de momento no las uso, es para filtrar mas.

    la otra es para la gestion del riesgo.

    Aqui tienes, muchas gracias.

    PC1.itf
    #236499 quote
    robertogozzi
    Moderator
    Master

    Prueba esta versión:

    DEFPARAM CumulateOrders = true // Acumulación de posiciones desactivada
    Timeframe(Daily)
    IF OnMarket THEN
    Senal = 0
    ENDIF
    
    //vminimosdecrecientes=0
    //vvelasrojas=0
    //riesgo=100
    compra=0
    //maximosdecrecientes=(high[4]>high[3] or close[4]>close[3]) and (high[3]>high[2] or close[3]>close[2]) and (high[2]>high[1] or close[2]>close[1])
    maximosdecrecientes=high[4]>high[3] and high[3]>high[2] and high[2]>high[1] and low[4]<high[3] and low[3]<high[2] and low[2]<high[1]
    cMaxDec=0
    minimosdecrecientes=low[4]>low[3] and low[3]>low[2] and low[2]>low[1]
    cMinDec=0
    velasrojas=open[3]>close[3] and open[2]>close[2] and open[1]>close[1]
    cVelRoj=0
    c3VelasDebajoM20=0
    cPC1=0
    m20=average[20](close)
    m40=average[40](close)
    cM2040Alcista=0
    cHPBajista=0
    
    if maximosdecrecientes  then
    cMaxDec=1
    endif
    
    if minimosdecrecientes and vminimosdecrecientes then
    cMinDec=1
    elsif vminimosdecrecientes=0 then
    cMinDec=1
    endif
    
    if vvelasrojas and velasrojas then
    cVelRoj=1
    elsif vvelasrojas=0 then
    cVelRoj=1
    endif
    
    if close[1]<m20[1] and close[2]<m20[2] and close[3]<m20[3] then
    c3VelasDebajoM20=1
    endif
    
    if high>high[1]+high[1]*0.003 then
    dist=((high[1]-m20[1])/m20[1])*100
    if dist<4 then
    cPC1=1
    endif
    endif
    
    if m20>m20[1] and m20[1]>m20[2] and m40>m40[1] and m40[1]>m40[2] and m40[2]>m40[3] then
    cM2040Alcista=1
    endif
    
    if cMaxDec and cMinDec and cVelRoj and c3VelasDebajoM20=0 and cPC1 and cM2040Alcista then
    for j=1 to 4
    if close[j]>open[j] then
    cuerpoAlcista = close[j] - open[j]
    puntoMedio = open[j] + (cuerpoAlcista * 0.5)
    if open[j-1]<puntoMedio and open[j-1]>close[j-1] then
    if j>2 and high[1]<open[j-1] then
    cHPBajista=1
    endif
    if j<=2 and high[1]<open[2] then
    cHPBajista=1
    endif
    endif
    endif
    next
    
    if cHPBajista=0 then
    if low<low[1] then
    stoploss=low-low*0.01
    else
    stoploss=low[1]-low[1]*0.01
    endif
    
    // Calcula la cantidad de acciones a comprar basado en el riesgo y el stoploss
    capital = 10000 // Capital disponible    // Riesgo por operación
    precioEntrada = close[0] // Precio actual
    cantidadAcciones = floor(riesgo / (precioEntrada - stoploss))
    Senal = 0
    if cantidadAcciones > 0 then
    Senal = 1
    //BUY cantidadAcciones SHARES AT MARKET
    endif
    endif
    endif
    
    // Variables para el cálculo de salida
    maxAnterior = high[1]
    minAnterior = low[1]
    maxActual = high
    minActual = low
    
    // Condición 1: Vela actual tiene mayor máximo y mayor mínimo que la anterior
    condicion1 = (maxActual > maxAnterior and minActual > minAnterior)
    
    // Condición 2: El cierre de la vela actual es superior al cierre de la vela anterior
    condicion2 = (close > close[1])
    
    // Si ambas condiciones se cumplen
    if condicion1 and condicion2 then
    // Establecer el stop loss en el mínimo más bajo de las dos velas anteriores
    nuevostoploss=0
    if low[1]<low[2] then
    nuevostoploss = low[1]-low[1]*0.01
    else
    nuevostoploss = low[2]-low[2]*0.01
    endif
    if nuevostoploss>stopLoss then
    stopLoss=nuevostoploss
    endif
    endif
    
    if longonmarket then
    if low <= stopLoss then
    SELL AT MARKET
    //elsif high >= takeProfit then
    //SELL AT MARKET
    endif
    endif
    
    Timeframe(default)
    IF Senal THEN
    BUY cantidadAcciones SHARES AT MARKET
    ENDIF
    
    TIMEFRAME(WEEKLY)
    for i=3 to 251
    maxRel = (high[i] > high[i-3]) AND (high[i] > high[i-2]) AND (high[i] > high[i-1]) AND (high[i] > high[i+1]) AND (high[i] > high[i+2]) AND (high[i] > high[i+3])
    if maxRel and high[i]>close then
    takeProfit=high[i]
    break
    endif
    next
    
    if longonmarket then
    if high >= takeProfit then
    SELL AT MARKET
    endif
    endif
    PC1-v2.itf
    #236519 quote
    mamees
    Participant
    New

    Sigue sin funcionar.

     

    Lo que estoy pensando es, hago las condiciones diarias y las lanzo en marco temporal inferior.

    Por tanto, para mi lo importante es:

     

    if high>high[1]+high[1]*0.003 then
    dist=((high[1]m20[1])/m20[1])*100
    if dist<4 then
    cPC1=1
    endif
    endif
    El maximo que tengo en la vela de ayer, en horario es la zona sombreada, como puedo saber el maximo en horario que equivaldria a la vela de ayer?
    asi pongo que se ejecute en horario una vez supere el maximo diario (en horario te lo marco).
    Muchas gracias.
    compra-pc1-1.png compra-pc1-1.png
    #236522 quote
    mamees
    Participant
    New

    He mirado con el DHigh(1) por ejemplo, para saber el maximo de ayer.

    Pero veo que el problema reside en que en diario debe de cumplir unas condiciones.

    Y si no se cumplen no quiero que se ejecute, por lo que la variable que usaria como condicion diaria no la puedo usar en horario o 15min.

    hay alguna forma de poder poner condiciones en un marco temporal y en el inferior ver si ha cumplido y ejecutar?

    #236523 quote
    mamees
    Participant
    New

    El nuevo codigo modificado con lo que te comento:

    DEFPARAM CumulateOrders = true // Acumulación de posiciones desactivada
    compra=0
    //maximosdecrecientes=(high[4]>high[3] or close[4]>close[3]) and (high[3]>high[2] or close[3]>close[2]) and (high[2]>high[1] or close[2]>close[1])
    TIMEFRAME(DAILY)
    maximosdecrecientes=high[4]>high[3] and high[3]>high[2] and high[2]>high[1] and low[4]<high[3] and low[3]<high[2] and low[2]<high[1]
    cMaxDec=0
    minimosdecrecientes=low[4]>low[3] and low[3]>low[2] and low[2]>low[1]
    cMinDec=0
    velasrojas=open[3]>close[3] and open[2]>close[2] and open[1]>close[1]
    cVelRoj=0
    c3VelasDebajoM20=0
    m20=average[20](close)
    m40=average[40](close)
    cM2040Alcista=0
    cHPBajista=0
    cDistM20=0

    if maximosdecrecientes then
    cMaxDec=1
    endif

    if minimosdecrecientes and vminimosdecrecientes then
    cMinDec=1
    elsif vminimosdecrecientes=0 then
    cMinDec=1
    endif

    if vvelasrojas and velasrojas then
    cVelRoj=1
    elsif vvelasrojas=0 then
    cVelRoj=1
    endif

    if close[1]<m20[1] and close[2]<m20[2] and close[3]<m20[3] then
    c3VelasDebajoM20=1
    endif

    if m20>m20[1] and m20[1]>m20[2] and m40>m40[1] and m40[1]>m40[2] and m40[2]>m40[3] then
    cM2040Alcista=1
    endif

    if ((high[1]-m20[1]/m20[1])*100)<5 then
    cDistM20=1
    endif

    for j=1 to 4
    if close[j]>open[j] then
    cuerpoAlcista = close[j] – open[j]
    puntoMedio = open[j] + (cuerpoAlcista * 0.5)
    if open[j-1]<puntoMedio and open[j-1]>close[j-1] then
    if j>2 and high[1]<open[j-1] then
    cHPBajista=1
    endif
    if j<=2 and high[1]<open[2] then
    cHPBajista=1
    endif
    endif
    endif
    next

    if cMaxDec and cMinDec and cVelRoj and c3VelasDebajoM20=0 and cM2040Alcista and cDistM20 and cHPBajista=0 then
    compra=1
    endif

    TIMEFRAME(DEFAULT)
    if compra then
    maximoAyer=DHigh(1)
    minimoAyer=DLow(1)
    if high>maximoAyer+maximoAyer*0.003 then
    if low<low[1] then
    stoplss=low-low*0.01
    else
    stoplss=low[1]-low[1]*0.01
    endif
    // Calcula la cantidad de acciones a comprar basado en el riesgo y el stoploss
    capital = 10000 // Capital disponible // Riesgo por operación
    precioEntrada = close[0] // Precio actual
    cantidadAcciones = floor(riesgo / (precioEntrada – stoplss))
    if cantidadAcciones > 0 then
    BUY cantidadAcciones SHARES AT MARKET
    endif
    endif
    endif

    TIMEFRAME(DAILY)
    // Variables para el cálculo de salida
    maxAnterior = high[1]
    minAnterior = low[1]
    maxActual = high
    minActual = low

    // Condición 1: Vela actual tiene mayor máximo y mayor mínimo que la anterior
    condicion1 = (maxActual > maxAnterior and minActual > minAnterior)

    // Condición 2: El cierre de la vela actual es superior al cierre de la vela anterior
    condicion2 = (close > close[1])

    // Si ambas condiciones se cumplen
    if condicion1 and condicion2 then
    // Establecer el stop loss en el mínimo más bajo de las dos velas anteriores
    nuevostoploss=0
    if low[1]<low[2] then
    nuevostoploss = low[1]-low[1]*0.01
    else
    nuevostoploss = low[2]-low[2]*0.01
    endif
    if nuevostoploss>stopLoss then
    stopLoss=nuevostoploss
    endif
    endif

    if longonmarket then
    if low <= stopLoss then
    SELL AT MARKET
    //elsif high >= takeProfit then
    //SELL AT MARKET
    endif
    endif

    TIMEFRAME(WEEKLY)
    for i=3 to 251
    maxRel = (high[i] > high[i-3]) AND (high[i] > high[i-2]) AND (high[i] > high[i-1]) AND (high[i] > high[i+1]) AND (high[i] > high[i+2]) AND (high[i] > high[i+3])
    if maxRel and high[i]>close then
    takeProfit=high[i]
    break
    endif
    next

    if longonmarket then
    if high >= takeProfit then
    SELL AT MARKET
    endif
    endif

    #236580 quote
    robertogozzi
    Moderator
    Master

    El código está incompleto. Si intento agregar las variables que faltan, aparece un error debido al período de tiempo. Publique el archivo ITF e intente explicarme nuevamente lo que quiere lograr.

    #236943 quote
    mamees
    Participant
    New

    Borre el archivo.

    Lo he dejado por el momento ese caso.

     

    Estoy haciendo uno que si me gusta, y es este: Añado el archivo ITF que es mas sencillo.

     

    Mi duda es, una vez salta el stop, puedo saber cuando sucedio? Para que si sucedio hace menos de 5 velas, entonces en vez de comprar 1, compre 2.

    En el caso de la imagen, seria, se hace 1 compra, me salta el stop, como la nueva compra sucede en menos de 5 velas desde que salto el stop, quiero comprar 2, no 1.

    Si hubiese pasado mas de 5 velas, entonces la compra que solo sea de 1, no 2.

    Para el caso de la imagen, donde esta la flecha negra debería comprar 2 y donde la azul solo 1.

     

    Adjunto imagen para explicar.

     

    Muchas gracias!

    Medias-Juntas-Superacion-M200.itf compras.jpg compras.jpg
    #237004 quote
    robertogozzi
    Moderator
    Master

    Prueba esta versión:

    // Definición de los parámetros del código
    DEFPARAM CumulateOrders = true // Acumulación de posiciones desactivada
    velasBajoM200=0
    m200 = average[200](close)
    
    TIMEFRAME(WEEKLY)
    m20s = average[20](close)
    m40s = average[40](close)
    
    cM20MayorM40 = m20s>m40s and m20s[1]>m40s[1] and m20s[2]>m40s[2] and m20s[3]>m40s[3]
    cM20JuntandoseM40 = m20s<m20s[1] and m20s[1]<m20s[2] and m40s>m40s[1] and m40s[1]>m40s[2] and m40s[2]>m40s[3]
    
    TIMEFRAME(DEFAULT)
    ONCE Cerrada = 0
    
    // Condiciones para entrada de posiciones largas
    c1 = (close >= m200 and open <m200 and cM20MayorM40 and cM20JuntandoseM40)
    
    IF c1 THEN
       stoploss=open
       esperar=0
       numerodevelas = BarIndex - Cerrada
       IF numerodevelas > 5 THEN
          BUY 1 SHARES AT MARKET
       ELSE
          BUY 2 SHARES AT MARKET
       ENDIF
    ENDIF
    
    if longonmarket then
       if esperar>=0 then
          esperar=esperar+1
       endif
    
       if esperar>2 then
          // Variables para el cálculo de salida
          maxAnterior = high[1]
          minAnterior = low[1]
          maxActual = high
          minActual = low
    
          // Condición 1: Vela actual tiene mayor máximo y mayor mínimo que la anterior
          condicion1 = (maxActual >= maxAnterior and minActual >= minAnterior)
          // Condición 2: El cierre de la vela actual es superior al cierre de la vela anterior
          condicion2 = (close >= close[1])
    
          // Si ambas condiciones se cumplen
          if condicion1 and condicion2 then
             // Establecer el stop loss en el mínimo más bajo de las dos velas anteriores
             if low[1]<low[2] then
                nuevostoploss=low[1]-low[1]*0.01
             else
                nuevostoploss=low[2]-low[2]*0.01
             endif
             if nuevostoploss>stopLoss then
                stopLoss=nuevostoploss
             endif
          endif
       endif
    
       if close <= stopLoss then
          SELL AT MARKET
          Cerrada = BarIndex
       endif
    endif
    Medias-Juntas-Superacion-M200.itf
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

Comprar en otro marco temporal para no esperar a finalizar la vela


ProOrder: Trading Automático y Backtesting

New Reply
Author
author-avatar
mamees @mamees Participant
Summary

This topic contains 9 replies,
has 2 voices, and was last updated by robertogozzi
1 year, 6 months ago.

Topic Details
Forum: ProOrder: Trading Automático y Backtesting
Language: Spanish
Started: 08/18/2024
Status: Active
Attachments: 7 files
Logo Logo
Loading...