Problemas de fechamento, por favor, ajude - página 4

 
Ais wrote >>

É bom trabalhar sem SL e TP.

Mas ainda precisamos da condição de fechar a ordem em caso de lucro.
Por favor, dê uma olhada na função renovada "iSignalClose",
https://www.mql5.com/en/forum/124521/page2.
Agora é, naturalmente, condição de SL virtual. https://www.mql5.com/en/forum/124521/page2 Mas ainda precisamos da condição de TP virtual.
Aguardando sua resposta.
:)


Olá, Ais
Uau, isso é um grande trabalho. Obrigado.
Desculpe-me por ter que sair. Era domingo, e hora da família no lago e depois do jantar.
Acabei de abrir o programa há alguns minutos para ver todo o trabalho que você tem realizado.
Como posso agradecer o suficiente? Pode levar um pouco de tempo para eu absorver.
A idéia de que o TP não tinha sido discutido era que eu estava montando isto
declaração ou função de cada vez. Apenas compreendendo lentamente como construir um programa.
Sua tomada de decisão encurta o programa e é profissional. Obrigado por compartilhar .
O TP e o SL serão um pouco mais intrincados. Como eu havia declarado, estou aprendendo isso aos poucos.
Não tenho certeza de qual função deve ser feita em seguida. Se me permitem, vou informá-los sobre o quadro geral.

1. Com este programa, eu estava procurando acrescentar mais posições. Tal como a posição inicial.
A que já está no programa, e depois as posições 2, 3, 4 e 5.
Nota: Não fiz o backtest com mais de 5 posições.

2. Com novas posições, o SL precisará ser ajustado com cada uma. Quando o SL for atingido, todas as posições
são liquidados.

3. Cada posição adicional será 1/2 ATR da posição anterior. Uma compra será 1/2 ATR maior,
onde como Venda será 1/2 ATR mais baixo.

4. O SL será ajustado em 1/2 ATR, em passo a passo com cada posição adicional.
Nota: Chega um momento em que você está em uma tendência decente, e o preço passa de 1/2 ATR.
E não deseje mais posições. Há um tempo dentro desta tendência, que o preço também atingirá um ponto em que o mais baixo das últimas dez barras, será mais alto que o último SL. Neste caso, o SL é ajustado pelo mais baixo das últimas 10 barras. E é ajustado de acordo depois que uma baixa for deslocada para fora do gráfico. Como você pode ver, o SL não é um trailing stop. Mas é ajustado pelo ATR*2, depois pelo valor mais baixo dos últimos dez dias. (A situação mencionada foi uma condição de compra. Uma condição de Venda é exatamente o oposto).

Este é o sistema que eu tinha testado à mão.
Eu não tive o luxo de fazer backtesting com códigos, ou com um TP. Talvez, num futuro próximo, eu possa analisar
padrões de preços. Mas, por enquanto, eu deixei os lucros correr, e sofri muitas pequenas perdas.

Disseram-me que eu não deveria tentar ser sofisticado. Que eu deveria aprender em pequenos programas
e construir a partir deles. Por isso, estou tentando fazer exatamente isso. Portanto, suponho que o próximo passo seria aprender
como girar em posições adicionais. Em seguida, aprenda como ajustar o SL.

A administração do dinheiro poderia ser adiada até o último. Mas tão importante. Se você acha que eu estou indo na direção errada, eu aprendo
qualquer entrada.

Espero que o trabalho que foi feito não seja prejudicado por etapas adicionais.
Só não sabia qual passo deveria ser feito primeiro, ou o que deveria seguir.

Obrigado por tudo.
Estou admirando suas melhorias. Estarei de volta, tenho certeza de que com mais perguntas também. Há muito a aprender com o que você deu.
Saúde
 

Olá Huckleberry,

Os melhores agradecimentos:
"
Olá Ais
Uau que é um ótimo trabalho. Obrigado.
"
Realmente e de verdade.

Agora vou melhorar os cálculos.
Exatamente, quero computar o ATR somente em uma nova barra.
Ele permite testar e otimizar mais rapidamente.

E, o principal, minha intenção era que você possa mudar facilmente o programa.
Mesmo que você amplie o programa e acrescente novas funcionalidades.
A estrutura feita especialmente para reduzir a complexidade.
Usarei a mesma estrutura no futuro próximo para grandes programas.

Quanto à estratégia comercial, eu a estudei apenas de forma fugaz.
Tenho certeza de que um grande número de estratégias pode e deve funcionar.

Gostaria de ajudá-los a estudar a estrutura para melhor compreensão.

Saudações e adeus por enquanto

P.S. No futuro, vou trabalhar somente com tomadas e paradas virtuais.

 
////////////////////////////////////////////////////////////////////<         1>
// < Program : Property >                                         //<          >
//   #define   1    " "                                           //<          >
//   #define   2    " "                                           //<          >
// </Program : Property >                                         //<          >
//                                                                //<          >
// < Program : Content >                                          //<          >
//                                                                //<          >
// < Structure             16 elements  in      4 domains       > //<          >
// < 1. Data                7 elements  in      2 domains      /> //<          >
// < 2. Code                9 elements  in      2 domains      /> //<          >
// </Structure             16 elements  in      4 domains       > //<          >
//                                                                //<          >
// < 1. Data                7 =       4 i       3 d       - s   > //<          >
// < 1.1. Input             7 =       4 i       3 d       - s  /> //<          >
// < 1.2. Buffer            - =       - i       - d       - s  /> //<          >
// </1. Data                7 =       4 i       3 d       - s   > //<          >
//                                                                //<          >
// < 2. Code                9 /       - i      82 l       4 o   > //<          >
// < 2.1. Interface         6 /       - i      71 l       4 o  /> //<          >
// < 2.2. Special           3 /       - i      11 l       - o  /> //<          >
// </2. Code                9 /       - i      82 l       4 o   > //<          >
//                                                                //<          >
// </Program : Content >                                          //<          >
     

////////////////////////////////////////////////////////////////////<         2>
// < 1.1. Data : Input >                                          //<          >
//                                                                //<          >
// < 1.1. Input             7 =       4 i       3 d       - s   > //<          >
// <      1. Strategy       4 =       2 i       2 d       - s  /> //<          >
// <      2. Trading        3 =       2 i       1 d       - s  /> //<          >
// </1.1. Input             7 =       4 i       3 d       - s   > //<          >
//                                                                //<          >
// <      1. Strategy 4 >=========================================//<          >
                    int       iBaseLag          = 20            ; //<          >
                    int       iBaseBar          = 1             ; //<          >
                    double    dFactorTP         = 2.0           ; //<          >
                    double    dFactorSL         = 2.0           ; //<          >
// </     1. Strategy 4 >=========================================//<          >
//                                                                //<          >
// <      2. Trading 3 >==========================================//<          >
                    int       iSlippage         = 1             ; //<          >
                    int       iMagic            = 1             ; //<          >
                    double    dLots             = 0.1           ; //<          >
// </     2. Trading 3 >==========================================//<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </1.1. Data : Input >                                          //<          >
     
 
////////////////////////////////////////////////////////////////////<         3>
// < 1.2. Data : Buffer >                                         //<          >
//                                                                //<          >
// < 1.2. Buffer            - =       - i       - d       - s   > //<          >
// </1.2. Buffer            - =       - i       - d       - s   > //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </1.2. Data : Buffer >                                         //<          >
     

////////////////////////////////////////////////////////////////////<         4>
// < 2.1. Code : Interface >                                      //<          >
//                                                                //<          >
// < 2.1. Interface         6 /       - i      71 l       4 o   > //<          >
// <      1. iNewBar                  - i       4 l       1 o  /> //<          >
// <      2. iSignalOpen              - i      15 l       1 o  /> //<          >
// <      3. iSignalClose             - i      15 l       1 o  /> //<          >
// <      4. iGetTicket               - i       7 l       1 o  /> //<          >
// <      5. iTryOpen                 - i      15 l       - o  /> //<          >
// <      6. iTryClose                - i      15 l       - o  /> //<          >
// </2.1. Interface         6 /       - i      71 l       4 o   > //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </2.1. Code : Interface >                                      //<          >
     
 
////////////////////////////////////////////////////////////////////<         5>
// < 2.1.1. Code : Interface : iNewBar >                          //<          >
int       iNewBar ()         //       - i       4 l       1 o     //<          >
{                                                                 //<          >
static    int       iTime_0 = 0                                 ; //<          >
//                                                                //<          >
if      ( iTime_0 < iTime   ( 0 , 0 , 0 ) )                       //<          >
        { iTime_0 = iTime   ( 0 , 0 , 0 ) ;  return ( TRUE  ) ; } //<          >
else    {                                    return ( EMPTY ) ; } //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.1.1. Code : Interface : iNewBar >                          //<          >
     

////////////////////////////////////////////////////////////////////<         6>
// < 2.1.2. Code : Interface : iSignalOpen >                      //<          >
int       iSignalOpen ()     //       - i      15 l       1 o     //<          >
{                                                                 //<          >
static    int                           iFirstRun   = 1         ; //<          >
if    ( ( iNewBar () == TRUE )   ||   ( iFirstRun  == 1 ) )       //<          >
        {                               iFirstRun   = 0         ; //<          >
          int iIndexH = iHighest      ( 0 , 0    , MODE_HIGH  ,   //<          >
                                        iBaseLag , iBaseBar   ) ; //<          >
          int iIndexL = iLowest       ( 0 , 0    , MODE_LOW   ,   //<          >
                                        iBaseLag , iBaseBar   ) ; //<          >
          static double dHigh ; dHigh = High     [ iIndexH    ] ; //<          >
          static double dLow  ; dLow  = Low      [ iIndexL    ] ; //<          >
        } // if                                                   //<          >
//                                                                //<          >
double    dAsk    = MarketInfo        ( Symbol () , MODE_ASK  ) ; //<          >
if      ( dAsk    > dHigh )             return    ( OP_BUY    ) ; //<          >
//                                                                //<          >
double    dBid    = MarketInfo        ( Symbol () , MODE_BID  ) ; //<          >
if      ( dBid    < dLow  )             return    ( OP_SELL   ) ; //<          >
//                                                                //<          >
                                        return    ( EMPTY     ) ; //<          >
}                                                                 //<          >
// </2.1.2. Code : Interface : iSignalOpen >                      //<          >
     
 
////////////////////////////////////////////////////////////////////<         7>
// < 2.1.3. Code : Interface : iSignalClose >                     //<          >
int       iSignalClose ()    //       - i      15 l       1 o     //<          >
{                                                                 //<          >
static    int                           iFirstRun   = 1         ; //<          >
if    ( ( iNewBar () == TRUE )   ||   ( iFirstRun  == 1 ) )       //<          >
        {                               iFirstRun   = 0         ; //<          >
          static double dATR          , dProfit  , dLoss        ; //<          >
          dATR        = iATR ( 0 , 0  , iBaseLag , iBaseBar   ) ; //<          >
        } // if                                                   //<          >
//                                                                //<          >
double    dDelta  = OrderOpenPrice () - OrderClosePrice ()      ; //<          >
//                                                                //<          >
if      ( OrderType ()     == OP_BUY  )                           //<          >
        { dProfit = -dDelta ; dLoss  =  dDelta                ; } //<          >
else if ( OrderType ()     == OP_SELL )                           //<          >
        { dProfit =  dDelta ; dLoss  = -dDelta                ; } //<          >
else                                    return    ( EMPTY     ) ; //<          >
//                                                                //<          >
if      ( dProfit > dATR * dFactorTP )  return    ( TRUE      ) ; //<          >
if      ( dLoss   > dATR * dFactorSL )  return    ( TRUE      ) ; //<          >
                                        return    ( EMPTY     ) ; //<          >
}                                                                 //<          >
// </2.1.3. Code : Interface : iSignalClose >                     //<          >
     

////////////////////////////////////////////////////////////////////<         8>
// < 2.1.4. Code : Interface : iGetTicket >                       //<          >
int       iGetTicket ()      //       - i       7 l       1 o     //<          >
{                                                                 //<          >
for     ( int i   = OrdersTotal () - 1 ; i >= 0 ; i -- )          //<          >
        {                                                         //<          >
          if      ( OrderSelect ( i , SELECT_BY_POS ) == TRUE   ) //<          >
          if      ( OrderMagicNumber ()               == iMagic ) //<          >
                                      return ( OrderTicket () ) ; //<          >
        } // for                                                  //<          >
                                      return ( EMPTY          ) ; //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.1.4. Code : Interface : iGetTicket >                       //<          >
     
 
////////////////////////////////////////////////////////////////////<         9>
// < 2.1.5. Code : Interface : iTryOpen >                         //<          >
int       iTryOpen ()        //       - i      15 l       - o     //<          >
{                                                                 //<          >
int       iCommand          = iSignalOpen ()                    ; //<          >
if      ( iCommand         == EMPTY  )                return    ; //<          >
if      ( iCommand         == OP_BUY )                            //<          >
        { string    sType   = "Buy"  ;  int  iColor = Blue    ; } //<          >
else    {           sType   = "Sell" ;       iColor = Red     ; } //<          >
//                                                                //<          >
if      ( iCommand         == OP_BUY )  int  iMode  = MODE_ASK  ; //<          >
                                        else iMode  = MODE_BID  ; //<          >
double    dPrice  = MarketInfo ( Symbol () , iMode            ) ; //<          >
//                                                                //<          >
OrderSend  ( Symbol ()       , iCommand             , dLots   ,   //<          >
             NormalizeDouble ( dPrice , Digits )              ,   //<          >
             iSlippage  , 0  , 0 , "" , iMagic , 0  , iColor  ) ; //<          >
//                                                                //<          >
int       iTrap   = GetLastError ()                             ; //<          >
if      ( iTrap  == 0 )                                           //<          >
          Alert   ( sType , " Was a Big Success"              ) ; //<          >
else      Alert   ( sType , " open exception "      , iTrap   ) ; //<          >
}                                                                 //<          >
// </2.1.5. Code : Interface : iTryOpen >                         //<          >
     

////////////////////////////////////////////////////////////////////<        10>
// < 2.1.6. Code : Interface : iTryClose >                        //<          >
int       iTryClose ()       //       - i      15 l       - o     //<          >
{                                                                 //<          >
int       iCommand          = iSignalClose ()                   ; //<          >
if      ( iCommand         == EMPTY  )                return    ; //<          >
if      ( OrderType ()     == OP_BUY )                            //<          >
        { string    sType   = "Buy"  ;  int  iColor = Red     ; } //<          >
else    {           sType   = "Sell" ;       iColor = Blue    ; } //<          >
//                                                                //<          >
if      ( OrderProfit ()    > 0      )  string sAct = "Take"    ; //<          >
                                        else   sAct = "Stop"    ; //<          >
double    dPrice  = OrderClosePrice ()                          ; //<          >
//                                                                //<          >
OrderClose ( OrderTicket ()  , OrderLots ()                   ,   //<          >
             NormalizeDouble ( dPrice , Digits )              ,   //<          >
             iSlippage                              , iColor  ) ; //<          >
//                                                                //<          >
int       iTrap   = GetLastError ()                             ; //<          >
if      ( iTrap  == 0 )                                           //<          >
          Alert   ( sType , " closed with Hard "    , sAct    ) ; //<          >
else      Alert   ( sType , " close exception "     , iTrap   ) ; //<          >
}                                                                 //<          >
// </2.1.6. Code : Interface : iTryClose >                        //<          >
     
 
////////////////////////////////////////////////////////////////////<        11>
// < 2.2. Code : Special >                                        //<          >
//                                                                //<          >
// < 2.2. Special           3 /       - i      11 l       - o   > //<          >
// <      1. init                     - i       1 l       - o  /> //<          >
// <      2. deinit                   - i       1 l       - o  /> //<          >
// <      3. start                    - i       9 l       - o  /> //<          >
// </2.2. Special           3 /       - i      11 l       - o   > //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </2.2. Code : Special >                                        //<          >
     

////////////////////////////////////////////////////////////////////<        12>
// < 2.2.1. Code : Special : Init >                               //<          >
int       init    ()         //       - i       1 l       - o     //<          >
{                                                                 //<          >
Alert   ( ""               , "Start " , UninitializeReason () ) ; //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.2.1. Code : Special : Init >                               //<          >
     
 
////////////////////////////////////////////////////////////////////<        13>
// < 2.2.2. Code : Special : Deinit >                             //<          >
int       deinit  ()         //       - i       1 l       - o     //<          >
{                                                                 //<          >
Alert   ( ""               , "Stop "  , UninitializeReason () ) ; //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.2.2. Code : Special : Deinit >                             //<          >
     

////////////////////////////////////////////////////////////////////<        14>
// < 2.2.3. Code : Special : Start >                              //<          >
int       start   ()         //       - i       9 l       - o     //<          >
{                                                                 //<          >
// < 2.2.3.1. History data inspection 4 >`````````````````````````//<          >
static    int       iTrigger   = 0       ; if ( iTrigger == 0 ) { //<          >
  if  ( ( iTime ( 0 , 0 , 0 ) == 0                          )     //<          >
  ||    ( iBars ( 0 , 0     )  < iBaseLag     + iBaseBar    ) )   //<          >
          return                         ; else iTrigger  = 1 ; } //<          >
// </2.2.3.1. History data inspection 4 >`````````````````````````//<          >
//                                                                //<          >
// < 2.2.3.2. Main routine 3 >````````````````````````````````````//<          >
int       iTicket           = iGetTicket ()                     ; //<          >
//                                                                //<          >
if      ( iTicket < 0 )       iTryOpen   ()                     ; //<          >
else                          iTryClose  ()                     ; //<          >
// </2.2.3.2. Main routine 3 >````````````````````````````````````//<          >
//                                                                //<          >
// < 2.2.3.3. Exception handler 2 >```````````````````````````````//<          >
int       iTrap   =           GetLastError ()                   ; //<          >
if      ( iTrap   > 0 )       Alert  ( "Exception " , iTrap   ) ; //<          >
// </2.2.3.3. Exception handler 2 >```````````````````````````````//<          >
}                                                                 //<          >
// </2.2.3. Code : Special : Start >                              //<          >
////////////////////////////////////////////////////////////////////<         0>
     
 
Adjust MetaEditor window to see 25 lines x 80 columns.
Use <Page Up> and <Page Down> to scroll the content.

Last edit: 2010.03.16 18:44
State: Ready
     

Arquivos anexados:
1_24.mq4  27 kb