[Resolvido] Erro na lógica para plotar uma HLine - página 2

 
prsc:

Eu estava só testando individualmente o ChartRedraw(), não deixava todos habilitados.

Sobre a hora, não tem o que faça, sempre o resultado é o mesmo, time[pos] também não funcionou e outros que tentei.

No indicador de pintar candle que tenho, tem as condições para pintar, e o else no final caso termine sem validar as anteriores, vai para um valor padrão

Nesse aqui não sei fazer um else onde buscaria a posição anterior como o valor padrão.

Por data/time não está fazendo efeito nenhum.

Eu gostaria que funciona-se rápido o indicador, mas também não me queixo de um pouco de dificuldade, afinal estou aprendendo ainda, só tenha paciência com o novato :)

Buscar a posição anterior é um problema de lógica... Você tem que exercitar isso.

Reveja seu código porque o 2o. ObjectMove() tem uma data zerada...


Coloque Print()'s em tudo até vc entender onde vc está errando...


;)

 
Flavio Jarabeck:

Buscar a posição anterior é um problema de lógica... Você tem que exercitar isso.

Reveja seu código porque o 2o. ObjectMove() tem uma data zerada...


Coloque Print()'s em tudo até vc entender onde vc está errando...


;)

O 2º ObjectMove() deixei zero só naquele momento, era TimeCurrent() também.

De fato preciso treinar a lógica, mas encavalou na 1ª marcha, ta osso.

Tentei da seguinte forma.

Criei um bufferAuxUp[]
Fiz o mapeamento.
Inverti a posição ArraySetAsSeries true, e também tentei com false.

No código tentei guardar a posição nesse novo buffer.

     

 if(Period() == PERIOD_M1){
    if(wBuffer[i] >= 91500){
        wColor[i] = 2;
        ObjectMove(0,"HighLineM1",0,TimeCurrent(),high[i]);
         bufferAuxUp[i] = high[i]; //Estando aqui dentro, guarde a posição de high atual.
     }
      else{ ObjectMove(0,"HighLineM1",0,TimeCurrent(),bufferAuxUp[i-1]); } //Caso não valide, devolva a posição anterior


Deixei apenas uma linha, é a que estou testando.

O que acontece, como o histograma faz uma falsa validação, acaba poluindo as variáveis interna da condição.

Tentei várias maneiras, encadeando um dentro do outro. Colocando uma variável para receber o valor do wBuffer antes de entrar  na condição do PERIOD_M1 e não foi.

Não tem maneira da linha plotar fora do while, não consegui mesmo.

Tentei travar uma condição em um loop para não afetar as variáveis depois, e a única coisa que travei foi o metatrader, várias vezes :)

Diga se pelo menos cheguei perto de acertar guardar o valor anterior.
            

 

Conseguiiiiiiiiiiii, pelo menos vou saber somente no pregão se não aparece outro bug.

Criei um método e fiz as condições lá dentro, e inclusive arrumou aquela plotagem no candle errado.

Vou deixar o modelito aqui para quem precisar de algo parecido.


#property copyright "Neto"
#property link      ""
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| PROPRIEDADES DO INDICADOR (DIRETIVAS)                            |
//+------------------------------------------------------------------+
#property indicator_buffers 4
#property indicator_plots 1 
//---
#property indicator_label1 "HistogramLines" 
#property indicator_type1 DRAW_COLOR_HISTOGRAM 
#property indicator_style1 STYLE_SOLID 
#property indicator_color1 clrDodgerBlue,clrRed,clrWhite //histogram colors
#property indicator_width1 2 

//###############
double waveBuffer[]; 
double waveColor[]; 
double cl[];
double bufferAux[];

int widthLineUp = 1;
int widthLineDown = 1;


int OnInit()
  {
//---
   SetIndexBuffer(0,waveBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,waveColor,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,cl,INDICATOR_DATA);
   SetIndexBuffer(3,bufferAux,INDICATOR_DATA);

   IndicatorSetString( INDICATOR_SHORTNAME, "Wave Lines" );

   ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
   
   CreateHline(0,"HighLineM1",clrWhite,STYLE_DASH,widthLineUp,"  Line UP");
   CreateHline(0,"LowLineM1",clrWhite,STYLE_DASH,widthLineDown,"  Line DN");
   CreateHline(0,"HighLineM2",clrWhite,STYLE_DASH,widthLineUp,"  Line UP");
   CreateHline(0,"LowLineM2",clrWhite,STYLE_DASH,widthLineDown,"  Line DN");
   CreateHline(0,"HighLineM5",clrWhite,STYLE_DASH,widthLineUp,"  Line UP");
   CreateHline(0,"LowLineM5",clrWhite,STYLE_DASH,widthLineDown,"  Line DN");
   
   return(INIT_SUCCEEDED);
  }
  
  void OnDeinit(const int reason){
      ObjectDelete(0, "LowLineM1");
      ObjectDelete(0, "HighLineM1");
      ObjectDelete(0, "LowLineM2");
      ObjectDelete(0, "HighLineM2");
      ObjectDelete(0, "LowLineM5");
      ObjectDelete(0, "HighLineM5");
      ChartRedraw();
  }

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[])
  {

int i;
if(prev_calculated == 0) i = 2;
else i = prev_calculated-1;

int pos;

while(i < rates_total){

   double value = 0;
   cl[i] = close[i];
   double vol = (double)volume[i];
   
   // ***** UP 
   if (cl[i] >= cl[i-1]) { 
      if (cl[i-1] >= cl[i-2]) {
         waveBuffer[i] = waveBuffer[i-1] + vol;
         waveColor[i] = 0; //Color DodgerBlue
      } 
      else{
         waveBuffer[i] = vol;
         waveColor[i] = 0;
      }
   } 
   // ***** DOWN   
   else{                          
      if (cl[i-1] < cl[i-2]) {
         waveBuffer[i] = waveBuffer[i-1] - vol;
         waveColor[i] = 1; //Color Red
      }
      else{
         waveBuffer[i] = -vol;
         waveColor[i] = 1;
      }
   }
         
    pos = i;
    i++;

    if(Period() == PERIOD_M1){
      value = 91500;  
      bufferAux[pos] = buff(pos,waveBuffer,value); 
      if(bufferAux[pos-1] >= value) {waveColor[pos-1] = 2; ObjectMove(0,"HighLineM1",0,TimeCurrent(),high[pos-1]);} //If condition, color white, move line
      else if(bufferAux[pos-1] <= -value) {waveColor[pos-1] = 2; ObjectMove(0,"LowLineM1",0,TimeCurrent(),low[pos-1]);}
    }
    if(Period() == PERIOD_M2){
       value = 250000;
       bufferAux[pos] = buff(pos,waveBuffer,value);
       if(bufferAux[pos-1] >= value) {waveColor[pos-1] = 2; ObjectMove(0,"HighLineM2",0,TimeCurrent(),high[pos-1]);}
       else if(bufferAux[pos-1] <= -value) {waveColor[pos-1] = 2; ObjectMove(0,"LowLineM2",0,TimeCurrent(),low[pos-1]);}
    }
    if(Period() == PERIOD_M5){
       value = 720000;
       bufferAux[pos] = buff(pos,waveBuffer,value);
       if(bufferAux[pos-1] >= value) {waveColor[pos-1] = 2; ObjectMove(0,"HighLineM5",0,TimeCurrent(),high[pos-1]);}
       else if(bufferAux[pos-1] <= -value) {waveColor[pos-1] = 2; ObjectMove(0,"LowLineM5",0,TimeCurrent(),low[pos-1]);}
    }
 }

   ChartRedraw();
   return(rates_total);
  }
//+------------------------------------------------------------------+
// Function to define line plotting through TimeFrame
double buff(int pos, double &buf[], double value){
   double res = 0;
   int h = 0;
   int l = 0;
      for(int j = 0; j < 1; j++){
         if(buf[pos] >= value){h = 1;}
         else{h = 0;}
         
         if(buf[pos] <= -value){l = 2;}
         else{l = 0;}
      }
      if((h == 1) || l == 2) res = buf[pos];
      else{res = 0;}
 
   return res;
}

void CreateHline(long   chart_id,      // chart ID
                 string name,          // object name
                 color  Color,         // line color
                 int    style,         // line style
                 int    width,         // line width
                 string text           // text
                 )
  {
//----
   ObjectCreate(chart_id,name,OBJ_HLINE,0,0,0);
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
   //ObjectSetInteger(chart_id,name,OBJPROP_BACK,true); //Sets the object as background or foreground
//----
  }


Obrigado por enquanto.