Stop loss e Take Profit não estão ativando

 
#include <Trade/Trade.mqh>

CTrade trade;

input int stoploss = 5;
input int takeprofit = 3;

int candleG;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
      candleG = 1;
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      if(isNewDay()){
         candleG = 1;
      }
      
      if(isNewBar()){
         candleG++;      
         
         if(candleG == 5){
            
            double price = normalizePrice(iOpen(NULL, PERIOD_CURRENT, 0));
            double tp = price + takeprofit;
            double sl = price - stoploss;
            
            bool ticket = trade.Buy(1, _Symbol, price, sl, tp, "[BUY]");           
            
            if(ticket > 0){
               Print("Success! Ticket: ", trade.ResultDeal());
            } else {
               Print("Error code: ", GetLastError());
            }           
         }
      }             

  }
//+------------------------------------------------------------------+

bool isNewDay(){
   datetime currentTime = iTime(Symbol(), PERIOD_CURRENT, 0);
   datetime previousTime = iTime(Symbol(), PERIOD_CURRENT, 1);
   
   MqlDateTime mqlCurrentTime;
   MqlDateTime mqlPreviousTime;
   TimeToStruct(currentTime, mqlCurrentTime);
   TimeToStruct(previousTime, mqlPreviousTime);
   
   if(mqlCurrentTime.day == mqlPreviousTime.day){
      return false;
   } else {
      return true;
   }
}

bool isNewBar(){
   static datetime last_time=0;
   datetime lastbar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
   if(last_time==0){
      last_time=lastbar_time;
      return(false);
   }
   if(last_time!=lastbar_time){
      last_time=lastbar_time;
      return(true);
   }
   return(false);
}

double normalizePrice(double price){
   double tick_size = SymbolInfoDouble ( _Symbol , SYMBOL_TRADE_TICK_SIZE );
   
   if(tick_size == 0.0){
   return(NormalizeDouble(price, _Digits));
   }
   
   return(NormalizeDouble(MathRound(price / tick_size) * tick_size, _Digits));
}


Eu tenho esse código simples acima.

Por algum motivo o stop loss e take profit definidos em

bool ticket = trade.Buy(1, _Symbol, price, sl, tp, "[BUY]");
não estão ativando, conforme a imagem em anexo mostra.

Alguém poderia me indicar o que fiz de errado?

Agradeço desde já!

----------------------- UPDATE ---------------------------------

Eu tentei com o código abaixo mas ainda continuo na mesma situação.

if(candleG == 5){
            double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
            double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
            
            double tp = Bid + takeprofit;
            double sl = Bid - stoploss;
            
            bool ticket = trade.Buy(1, _Symbol, Ask, sl, tp, "[BUY]");           
            
            if(ticket > 0){
               Print("Success! Ticket: ", trade.ResultDeal());
            } else {
               Print("Error code: ", GetLastError());
            }           
         }
Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
Arquivos anexados:
file.png  102 kb
 
Matheus Freire:


Eu tenho esse código simples acima.

Por algum motivo o stop loss e take profit definidos em

não estão ativando, conforme a imagem em anexo mostra.

Alguém poderia me indicar o que fiz de errado?

Agradeço desde já!

----------------------- UPDATE ---------------------------------

Eu tentei com o código abaixo mas ainda continuo na mesma situação.

Escreva na moeda/currency do testador BRL, nao vai ter opcao, so escreve e dai manda executar novamente que tudo deve dar certo. Entretanto a linha do sl/tp esta sem normalizar e alguns provavelmente devem estar falhando e seria bom checar as mensagens de log por "invalid stop".

EDIT: Deixa que o item que ta em vermelho que nem o da imagem abaixo e deve dar certo a questao dos stops.


 
Ricardo Rodrigues Lucca #:

Escreva na moeda/currency do testador BRL, nao vai ter opcao, so escreve e dai manda executar novamente que tudo deve dar certo. Entretanto a linha do sl/tp esta sem normalizar e alguns provavelmente devem estar falhando e seria bom checar as mensagens de log por "invalid stop".

EDIT: Deixa que o item que ta em vermelho que nem o da imagem abaixo e deve dar certo a questao dos stops.


Perfeito, meu amigo!

Funcionou lindamente!

Aqui na minha tela toda essa parte abaixo do delay estava escondido e eu não percebi que havia um scroll kkkkk

Agora tô eu aqui com cara de palhaço kkk

Valeu, irmãozão!
 
Matheus Freire #:
Perfeito, meu amigo!

Funcionou lindamente!

Aqui na minha tela toda essa parte abaixo do delay estava escondido e eu não percebi que havia um scroll kkkkk

Agora tô eu aqui com cara de palhaço kkk

Valeu, irmãozão!
Eu fiz isso tambem de nao perceber o scroll, nao me ocorreu que poderia ser isso nao. hahaha :)