Encerrar posição - página 4

 
Na Onda #acho que comemorei cedo kkkkkkkk ele rodou legal, porem apos algumas operações começou a dar esse erro nao entende pq funcionou e depois parou tentei normalizar ask e bid mas nao conseguir ta erro

😁😁😁 Beleza... É porque você utiliza Stop Loss = 0.0. Faça as pequenas alterações abaixo na função Check_SL_TP() que eu acredito que corrija:

//+--------------------------------------------------------------------------------------------------------------------+
//| Verifica se distância entre o TakeProfit/StopLoss e o preço de fechamento é maior do que SYMBOL_TRADE_STOPS_LEVEL  |
//+--------------------------------------------------------------------------------------------------------------------+
bool Check_SL_TP(ENUM_POSITION_TYPE type, double SL, double TP)
  {
//--- Local variables
   bool SL_check = false, TP_check = false;

//--- Gets the last price for current symbol
   double BID = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double ASK = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

//--- get the SYMBOL_TRADE_STOPS_LEVEL level
   int stops_level = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
   if(stops_level != 0)
     {
      PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must not be nearer than %d points from the closing price", stops_level, stops_level);
     }

//--- check only two order types
   switch(type)
     {
      //--- Buy operation
      case  POSITION_TYPE_BUY:
        {
         //--- check the StopLoss
         SL_check = (BID - SL > stops_level * _Point);
         if(!SL_check)
           {
            PrintFormat("For order %s StopLoss=%.5f must be less than %.5f (Bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type), SL, BID - stops_level * _Point, BID, stops_level);
           }
         //--- check the TakeProfit
         TP_check = (TP - BID > stops_level * _Point) || TP == 0.0;
         if(!TP_check)
           {
            PrintFormat("For order %s TakeProfit=%.5f must be greater than %.5f (Bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type), TP, BID + stops_level * _Point, BID, stops_level);
           }
         //--- return the result of checking
         return(SL_check && TP_check);
        }
      //--- Sell operation
      case  POSITION_TYPE_SELL:
        {
         //--- check the StopLoss
         SL_check = (SL - ASK > stops_level * _Point) || SL == 0.0;
         if(!SL_check)
           {
            PrintFormat("For order %s StopLoss=%.5f must be greater than %.5f (Ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type), SL, ASK + stops_level * _Point, ASK, stops_level);
           }
         //--- check the TakeProfit
         TP_check = (ASK - TP > stops_level * _Point);
         if(!TP_check)
           {
            PrintFormat("For order %s TakeProfit=%.5f must be less than %.5f (Ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type), TP, ASK - stops_level * _Point, ASK, stops_level);
           }
         //--- return the result of checking
         return(TP_check && SL_check);
        }
      break;
     }

//--- Verification succeeded
   return(false);
  }

Se você considerar essa função/verificação desnecessária no seu caso (B3, stops zerados), pode também removê-la, não sei...

 
Vinicius de Oliveira #:

😁😁😁 Beleza... É porque você utiliza Stop Loss = 0.0. Faça as pequenas alterações abaixo na função Check_SL_TP() que eu acredito que corrija:

Se você considerar essa função/verificação desnecessária no seu caso (B3, stops zerados), pode também removê-la, não sei...

funcionou perfeitamente, obrigado

 

uma duvida, se eu quiser que os indicadores externos icustom nao sejam plotados na tela,comno eu faço, se puder me indicar um artigo pra mim ver, nao achei nada especifico


desde ja agradeço

 
Na Onda #uma duvida, se eu quiser que os indicadores externos icustom nao sejam plotados na tela,comno eu faço, se puder me indicar um artigo pra mim ver, nao achei nada especifico desde ja agradeço

A função abaixo, chamada antes de criar os indicadores, oculta os mesmos durante os testes:

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

// . . .


//--- Hide indicators during testing
   TesterHideIndicators(true);

//--- Creates indicators

// . . .

  }
 
Na Onda #funcionou perfeitamente, obrigado

De nada. 👍

 
Vinicius de Oliveira #:

A função abaixo, chamada antes de criar os indicadores, oculta os mesmos durante os testes:

existi um pra conta real?

 
Na Onda #existi um pra conta real?

Se você tiver o código do indicador personalizado, utilize:

PlotIndexSetInteger(plot_index, PLOT_SHOW_DATA, false);

Se não tiver o código do indicador, TESTE ChartIndicatorDelete()...


EDIT.1: Retificação da primeira informação acima...

 
Vinicius de Oliveira #:
PlotIndexSetInteger(plot_index, PLOT_SHOW_DATA, false);

tenho o codigo mas nao funcionou, ou coloquei no lugar errado

//+------------------------------------------------------------------+
//|                                         Customizable Keltner.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://mql5.com/"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   3
//--- plot Upper
#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Middle
#property indicator_label2  "Middle"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot Lower
#property indicator_label3  "Lower"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrGreen
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- enums
enum ENUM_METHOD_VARIATION
  {
   METHOD_HL,                 // Smoothed H-L
   METHOD_ATR                 // ATR as source
  };
//--- input parameters
input int                     InpSmoothCenter      =  50;            // Number of the periods to smooth the center line
input int                     InpSmoothDeviation   =  50;            // Number of periods to smooth deviation
input double                  InpF                 =  1.0;           // Factor which is used to apply the deviation
input ENUM_APPLIED_PRICE      InpAppliedPrice      =  PRICE_CLOSE;   // The center line applied price:
input ENUM_MA_METHOD          InpMethodSmoothing   =  MODE_SMA;      // The center line smoothing method
input ENUM_METHOD_VARIATION   InpMethodVariation   =  METHOD_HL;     // Variation Method
//--- indicator buffers
double         BufferUpper[];
double         BufferMiddle[];
double         BufferLower[];
double         BufferVariation[];
//--- global variables
int            handle_ma;
int            handle_atr;
int            period_center;
int            period_dev;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferUpper,INDICATOR_DATA);
   SetIndexBuffer(1,BufferMiddle,INDICATOR_DATA);
   SetIndexBuffer(2,BufferLower,INDICATOR_DATA);
   SetIndexBuffer(3,BufferVariation,INDICATOR_CALCULATIONS);
//--- settings indicators parameters
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
   IndicatorSetString(INDICATOR_SHORTNAME,"Keltner with color zones");
//--- settings variables
   period_center=(InpSmoothCenter<1 ? 1 : InpSmoothCenter);
   period_dev=(InpSmoothDeviation<1 ? 1 : InpSmoothDeviation);
//--- MA and ATR handles
   ResetLastError();
   handle_ma=iMA(Symbol(),PERIOD_CURRENT,period_center,0,(ENUM_MA_METHOD)InpMethodSmoothing,InpAppliedPrice);
   if(handle_ma==INVALID_HANDLE)
     {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
     }
   if(InpMethodVariation==METHOD_ATR)
     {
      ResetLastError();
      handle_atr=iATR(Symbol(),PERIOD_CURRENT,period_dev);
      if(handle_atr==INVALID_HANDLE)
        {
         Print("The iATR object was not created: Error ",GetLastError());
         return INIT_FAILED;
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- Check for the minimum number of bars for calculation
   if(rates_total<4) return 0;
//--- Set the indexing order in arrays as in timeseries
   ArraySetAsSeries(BufferLower,true);
   ArraySetAsSeries(BufferMiddle,true);
   ArraySetAsSeries(BufferUpper,true);
   ArraySetAsSeries(BufferVariation,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
//--- Checking and calculating the number of calculated bars
   int copied_ma=0,copied_atr=0,limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-1;
      ArrayInitialize(BufferLower,EMPTY_VALUE);
      ArrayInitialize(BufferMiddle,EMPTY_VALUE);
      ArrayInitialize(BufferUpper,EMPTY_VALUE);
      ArrayInitialize(BufferVariation,EMPTY_VALUE);
      copied_ma=CopyBuffer(handle_ma,0,0,rates_total,BufferMiddle);
      if(copied_ma!=rates_total) return 0;
      if(InpMethodVariation==METHOD_ATR)
        {
         copied_atr=CopyBuffer(handle_atr,0,0,rates_total,BufferVariation);
         if(copied_atr!=rates_total) return 0;
        }
      else for(int i=limit; i>=0; i--) BufferVariation[i]=high[i]-low[i];
     }
//--- Preparing data
   int copied=(limit>1 ? rates_total : 1);
   copied_ma=CopyBuffer(handle_ma,0,0,copied,BufferMiddle);
   if(copied_ma!=copied) return 0;
   if(InpMethodVariation==METHOD_ATR)
     {
      copied_atr=CopyBuffer(handle_atr,0,0,copied,BufferVariation);
      if(copied_atr!=copied) return 0;
     }
   else for(int i=limit; i>=0; i--)
      BufferVariation[i]=high[i]-low[i];
//--- Indicator calculation loop
   double v=0.0;
   for(int i=limit; i>=0; i--)
     {
      v=(InpMethodVariation==METHOD_ATR ? BufferVariation[i] : iMAOnArray(BufferVariation,0,period_dev,0,InpMethodVariation,i));
      BufferUpper[i]=BufferMiddle[i]+v*InpF;
      BufferLower[i]=BufferMiddle[i]-v*InpF;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| iMAOnArray() https://www.mql5.com/ru/articles/81&nbsp;                |
//+------------------------------------------------------------------+
double iMAOnArray(double &array[],int total,int period,int ma_shift,int ma_method,int shift)
  {
   double buf[],arr[];
   if(total==0) total=ArraySize(array);
   if(total>0 && total<=period) return(0);
   if(shift>total-period-ma_shift) return(0);
//---
   switch(ma_method)
     {
      case MODE_SMA :
        {
         total=ArrayCopy(arr,array,0,shift+ma_shift,period);
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0;
         int    i,pos=total-1;
         for(i=1;i<period;i++,pos--)
            sum+=arr[pos];
         while(pos>=0)
           {
            sum+=arr[pos];
            buf[pos]=sum/period;
            sum-=arr[pos+period-1];
            pos--;
           }
         return(buf[0]);
        }
      case MODE_EMA :
        {
         if(ArrayResize(buf,total)<0) return(0);
         double pr=2.0/(period+1);
         int    pos=total-2;
         while(pos>=0)
           {
            if(pos==total-2) buf[pos+1]=array[pos+1];
            buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
            pos--;
           }
         return(buf[shift+ma_shift]);
        }
      case MODE_SMMA :
        {
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0;
         int    i,k,pos;
         pos=total-period;
         while(pos>=0)
           {
            if(pos==total-period)
              {
               for(i=0,k=pos;i<period;i++,k++)
                 {
                  sum+=array[k];
                  buf[k]=0;
                 }
              }
            else sum=buf[pos+1]*(period-1)+array[pos];
            buf[pos]=sum/period;
            pos--;
           }
         return(buf[shift+ma_shift]);
        }
      case MODE_LWMA :
        {
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0.0,lsum=0.0;
         double price;
         int    i,weight=0,pos=total-1;
         for(i=1;i<=period;i++,pos--)
           {
            price=array[pos];
            sum+=price*i;
            lsum+=price;
            weight+=i;
           }
         pos++;
         i=pos+period;
         while(pos>=0)
           {
            buf[pos]=sum/weight;
            if(pos==0) break;
            pos--;
            i--;
            price=array[pos];
            sum=sum-lsum+price*period;
            lsum-=array[i];
            lsum+=price;
           }
         return(buf[shift+ma_shift]);
        }
      default: return(0);
     }
   return(0);
  }
//+------------------------------------------------------------------+
 
Na Onda #tenho o codigo mas nao funcionou, ou coloquei no lugar errado

Um exemplo deixando "Plotar?" como parâmetro [esse parâmetro deverá ser informado ao chamar iCustom()]:

//+------------------------------------------------------------------+
//|                                         Customizable Keltner.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://mql5.com/"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   3

//--- plot Upper
#property indicator_label1  "Upper"
//#property indicator_type1   DRAW_LINE
//#property indicator_color1  clrRed
//#property indicator_style1  STYLE_SOLID
//#property indicator_width1  1

//--- plot Middle
#property indicator_label2  "Middle"
//#property indicator_type2   DRAW_LINE
//#property indicator_color2  clrBlue
//#property indicator_style2  STYLE_SOLID
//#property indicator_width2  1

//--- plot Lower
#property indicator_label3  "Lower"
//#property indicator_type3   DRAW_LINE
//#property indicator_color3  clrGreen
//#property indicator_style3  STYLE_SOLID
//#property indicator_width3  1

//--- enums
enum ENUM_METHOD_VARIATION
  {
   METHOD_HL,                 // Smoothed H-L
   METHOD_ATR                 // ATR as source
  };
//--- input parameters
input int                     InpSmoothCenter      =  50;            // Number of the periods to smooth the center line
input int                     InpSmoothDeviation   =  50;            // Number of periods to smooth deviation
input double                  InpF                 =  1.0;           // Factor which is used to apply the deviation
input ENUM_APPLIED_PRICE      InpAppliedPrice      =  PRICE_CLOSE;   // The center line applied price:
input ENUM_MA_METHOD          InpMethodSmoothing   =  MODE_SMA;      // The center line smoothing method
input ENUM_METHOD_VARIATION   InpMethodVariation   =  METHOD_HL;     // Variation Method
input bool                    InpShowBuffer        =  true;          // Show Buffer
//--- indicator buffers
double         BufferUpper[];
double         BufferMiddle[];
double         BufferLower[];
double         BufferVariation[];
//--- global variables
int            handle_ma;
int            handle_atr;
int            period_center;
int            period_dev;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferUpper,INDICATOR_DATA);
   SetIndexBuffer(1,BufferMiddle,INDICATOR_DATA);
   SetIndexBuffer(2,BufferLower,INDICATOR_DATA);
   SetIndexBuffer(3,BufferVariation,INDICATOR_CALCULATIONS);
//--- settings indicators parameters
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
   IndicatorSetString(INDICATOR_SHORTNAME,"Keltner with color zones");
//--- settings variables
   period_center=(InpSmoothCenter<1 ? 1 : InpSmoothCenter);
   period_dev=(InpSmoothDeviation<1 ? 1 : InpSmoothDeviation);
//--- MA and ATR handles
   ResetLastError();
   handle_ma=iMA(Symbol(),PERIOD_CURRENT,period_center,0,(ENUM_MA_METHOD)InpMethodSmoothing,InpAppliedPrice);
   if(handle_ma==INVALID_HANDLE)
     {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
     }
   if(InpMethodVariation==METHOD_ATR)
     {
      ResetLastError();
      handle_atr=iATR(Symbol(),PERIOD_CURRENT,period_dev);
      if(handle_atr==INVALID_HANDLE)
        {
         Print("The iATR object was not created: Error ",GetLastError());
         return INIT_FAILED;
        }
     }
//---
   if(InpShowBuffer)
     {
      PlotIndexSetInteger(0, PLOT_SHOW_DATA,  true);
      PlotIndexSetInteger(0, PLOT_DRAW_TYPE,  DRAW_LINE);
      PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrRed);
      PlotIndexSetInteger(0, PLOT_LINE_STYLE, STYLE_SOLID);
      PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 1);

      PlotIndexSetInteger(1, PLOT_SHOW_DATA,  true);
      PlotIndexSetInteger(1, PLOT_DRAW_TYPE,  DRAW_LINE);
      PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrBlue);
      PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID);
      PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1);

      PlotIndexSetInteger(2, PLOT_SHOW_DATA,  true);
      PlotIndexSetInteger(2, PLOT_DRAW_TYPE,  DRAW_LINE);
      PlotIndexSetInteger(2, PLOT_LINE_COLOR, clrGreen);
      PlotIndexSetInteger(2, PLOT_LINE_STYLE, STYLE_SOLID);
      PlotIndexSetInteger(2, PLOT_LINE_WIDTH, 1);
     }
   else
     {
      PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
      PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_NONE);

      PlotIndexSetInteger(1, PLOT_SHOW_DATA, false);
      PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_NONE);

      PlotIndexSetInteger(2, PLOT_SHOW_DATA, false);
      PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_NONE);
     }

//---
   return(INIT_SUCCEEDED);
  }
 
Na Onda #:

tenho o codigo mas nao funcionou, ou coloquei no lugar errado

Acredito que não fez errado, o PLOT_SHOW_DATA é pra ocultar dados da janela de dados. Eu não entendi o que voce quer ocultar no indicador que tu mandou tu poderia ocultar todas as linhas que são plotadas via aba de cores (o que me parece mais facil e simples pro usuário final) e não faz muito sentido ocultar isso em indicador. Se tu quiser ocultar isso num expert que faz uso do indicador, tu so precisa ocultar no testador. Quando o EXPERT é colocado na conta real, os indicadores para aparecerem tem que ser adicionados manualmente com ChartIndicatorAdd.