Could this EA structure generate error

 


I am pursuing my EA that trades between 3 trend lines drawn from a custom indicator.

With great helps from this forum, I've managed my EA running but it has a bug getting wrong trend line value. (trendline "red" in particular)

My biggest worry is that maybe the problem is the structure of the EA as I am still very new to EA programming (just about 1 month experience)

I cannot explain why the EA could get trendline values for "yellow" & "violet" correctly, but constantly gets wrong value for "red"


Could someone please help to check and advise any issues, thanks in advance.

//|---------initialization
int init()
{

 if (Digits == 5 || Digits == 3)
        {    // Adjust for five (5) digit brokers.
         pips2double = Point*10; 
                 pips2point = 10;   
                 Digits.pips = 1;
    } 
        else 
        {    
                pips2double = Point;    
                pips2point =  1;   
                Digits.pips = 0; 
        }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl

return(0); //end of init()
}


int start()
{
        
    if(TrailingProfit) MoveTrailingProfit();
          
   string symbol=Symbol();
   double spread=GetInfo(symbol);
   int err=GetLastError();
  
   int allowed_order=MoneyMM();
   int total=OrdersTotal();
                
        bool tradeonceperbar=IsNewBar();

if(tradeonceperbar)
{
        EA_cnt=EA_cnt+1; //check new bar since EA
        
        double red=NormalizeDouble(GetRed(),Digits);  //often gets wrong value??
        double yellow=NormalizeDouble(GetYellow(),Digits);
        double violet=NormalizeDouble(GetViolet(),Digits);
        int bullorbear=IsBullOrBear(red,yellow,violet);
        bool signal=GetSignal(bullorbear, red, yellow, violet);
        
        double SL, TG;
        
                
        if(bullorbear==BULL)
        {                       
                if (Ask>red&&over_red==true)
                        {SL=red; TG=NormalizeDouble(Bid+10*pips2double,Digits);}
                                                                
                if (red>=Ask&&Ask>yellow)
                        {SL=yellow; G=red;}
                        
                if (yellow>=Ask&&Ask>=violet)
                        {SL=violet; TG=yellow;}
                        
                if (Ask<violet&&under_violet==true)
                        {SL=NormalizeDouble((Bid-10*pips2double),Digits);TG=violet;
                        }
        }               
                        

        if(bullorbear==BEAR)    
        {                       
                if (Bid<red&&over_red==true)
                        {
                        SL=red; TG=NormalizeDouble(Ask-10*pips2double,Digits);}
                        
                if (red<=Bid&&Bid<yellow)
                        {SL=yellow; TG=red;}
                        
                if (yellow<=Bid&&Bid<=violet)
                        {SL=violet; TG=yellow;}
                        
                if (Bid>violet&&under_violet==true)
                        {SL=NormalizeDouble((Bid+10*pips2double),Digits);TG=violet;     }
        } 


        if(TradeIsBusy() < 0) 
        return(-1); 
        RefreshRates(); // refresh the market info
        
        //OPEN Trade
        if(signal==true&&total<allowed_order)
        { 
                if(TG_dist<(spread+2)/*Min_target*/) Print(symbol, " target too close to order");
                if(TG_dist>(spread+2)/*Min_target*/) int ticket=MoveOpenOrder(Magic, SL, bullorbear, spread, TG);
        }

        TradeIsNotBusy();

}
return(0);

}       //end of start

//trade once per bar
bool IsNewBar()
{  
   bool temp;
   if(previous<Time[0]){previous=Time[0];temp=true;}
   else temp=false;
      
   return(temp);
}
/////////////////////////////////////////////
//Current_red (often gets wrong value???)
double GetRed()
{
 double red_y1=ObjectGet("red",OBJPROP_PRICE1);
 double red_y2=ObjectGet("red",OBJPROP_PRICE2);
 double red_x1=ObjectGet("red",OBJPROP_TIME1);
 double red_x2=ObjectGet("red",OBJPROP_TIME2);
 double red_slope = (red_y2-red_y1)/(red_x2-red_x1);
 double red_offset = red_y1-red_slope*red_x1;
 
 double current_red=red_offset+red_slope*Time[0];
 return(current_red);
}

//Current_yellow 
double GetYellow()
{
double yellow_y1=ObjectGet("yellow",OBJPROP_PRICE1);
double yellow_y2=ObjectGet("yellow",OBJPROP_PRICE2);
double yellow_x1=ObjectGet("yellow",OBJPROP_TIME1);
double yellow_x2=ObjectGet("yellow",OBJPROP_TIME2);
double yellow_slope = (yellow_y2-yellow_y1)/(yellow_x2-yellow_x1);
double yellow_offset = yellow_y1-yellow_slope*yellow_x1;

double current_yellow=yellow_offset+yellow_slope*Time[0];
return(current_yellow);
}

//Current_violet 
double GetViolet()
{
 double violet_y1=ObjectGet("violet",OBJPROP_PRICE1);    
 double violet_y2=ObjectGet("violet",OBJPROP_PRICE2); 
 double violet_x1=ObjectGet("violet",OBJPROP_TIME1); 
 double violet_x2=ObjectGet("violet",OBJPROP_TIME2); 
 double violet_slope = (violet_y2-violet_y1)/(violet_x2-violet_x1);
 double violet_offset = violet_y1-violet_slope*violet_x1;

 double current_violet=violet_offset+violet_slope*Time[0];
 return(current_violet);
}
 
holdenmcgorin:

Could someone please help to check and advise any issues, thanks in advance.

What are the following variables meant to be: temp, red_y1, red_offset, etc ? perhaps your variable names could be more meaningful ?

Why haven't you added more comments to you code to explain what is going on ?

How do you know "red" is wrong ? I don't see any Print statements which will show you it's value and the value of the variables used to calculate it . . .

I can't see any fundamental differences between the red, yellow and violet calculations so the error is probably in the way you are drawing the lines . . . add some Print statements, check the variable values, it will probably help you.

Reason: