SMA for EA not working propperly

 

Hi I have been trying to code a SMA  into my EA.  I need to do this because I want to see the buy and sell signals in relation to the SMA know that it is working the way I want it to.

What is happening at the moment is a straight line is being drawn from some anchor point and travels right from left across the screen,  and points at the most recent SMA value,

so it's like a line on a hinge swinging up and down following the price action.  I have attached some pics along with the code.  Sorry I couldn't get all the code in one screen grab.

Hoping to hear from someone who might have had some luck with this, or someone who has been around long enough to know that there is no facility for this.

thanks

Screenshots of code removed by moderator.

 
Does anyone here know how to code an  sma curve or line into their ea's ?
 

If you want help with your code, then provide actual code samples or source files. Don't use screenshots.

 
Slobbergirkin56 #: Does anyone here know how to code an  sma curve or line into their ea's ?

Read it in with iMA.

Code has no eyes; they don't need to see the indicator on the chart, just the values. If you want to see it, you add it to the appropriate chart.

 

@William Roeder  Read it in with iMA.     I don't really know what that means.   How do you add a chart when it is running in debug mode?  I would like to see what is happening on a position, by position basis, and having the indicator present would allow me to see easily what is happening, rather than just hoping for the best that no mistakes have been made,  I could add the indicator to the tester,  but when I do that, what I find is that after the test has finished, and I scroll back I can only see the last few signals again leaving me pretty much blind to most of what has happened, leaving me wondering to what extent my strategy was even followed. 

There is this page here.https://www.mql5.com/en/forum/97784  I based what I was doing on this to some degree, although that code didn't compile, I don't think it was far off.  I think now that I get what he was trying to do by concatenating the 'maName' variable.  Where as my version was trying to use just the one instance of the object and alter it's stop start parameters on every new bar which would have made for a jagged output, if it had worked, it would have been  Ok for my purposes,   I'm not sure but it looks like he is creating a new object on every new tick, which would make for a much smoother curve.


@Fernando Carreiro   Thanks for the suggestion,  I will insert it here now.


bool timeInfo;
int priceMaValueDefintion;
double priceMaValue [ 5 ] ;
MqlRates  timeValue[ 5 ];
long thisChartId;
string maName = "";
bool newBar;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OnTick( ) {  

    newBar = isNewBar();
    if( newBar )
       drawMA();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Returns true if a new bar has appeared for a symbol/period pair  |

bool isNewBar() {
   //--- memorize the time of opening of the last bar in the static variable
   static datetime last_time=0;
   //--- current time
   datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
   //--- if it is the first call of the function
   if(last_time==0) {
      //--- set the time and exit
      last_time=lastbar_time;
      return(false);
   }
   //--- if the time differs
   if(last_time!=lastbar_time) {
      //--- memorize the time and return true
      last_time=lastbar_time;
      return(true);
   }
   //--- if we passed to this line, then the bar is not new; return false
   return(false);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OnInit( ) {
  
   priceMaValueDefintion = iMA( _Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE );
   thisChartId = ChartID();
   int MA_Thinkness = 3;
   ObjectSetInteger( thisChartId , maName , OBJPROP_RAY,false);
   ObjectSetInteger( thisChartId , maName , OBJPROP_COLOR,clrMagenta);
   ObjectSetInteger( thisChartId, maName , OBJPROP_WIDTH,MA_Thinkness);;
   
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void  drawMA( ) {


//     DebugBreak();
     if( ! CopyBuffer( priceMaValueDefintion, 0, 0, 5, priceMaValue )) {
          Print( " Failed to update price data " ) ;
          DebugBreak();
     }
    
     timeInfo = CopyRates(_Symbol, _Period, 0, 5, timeValue );  
     if( ! timeInfo ) {
          Print( " Failed to update time data " ) ;
          DebugBreak();
     }
     
    // if(!ObjectMove( thisChartId , maName , NULL , timeValue[ 1 ].time, priceMaValue[ 1 ] ))
     //    DebugBreak();
     
   //  ChangeTrendEmptyPoints( timeValue[ 2 ].time ,  priceMaValue[ 2 ] , timeValue[ `1 ] , priceMaValue[ 1 ] ) ;
    
     maName = "mAverage"; 
     if( ! ObjectCreate(  thisChartId ,  maName  ,  OBJ_TREND , 0 , timeValue[ 2 ].time , priceMaValue[ 2 ] , timeValue[ 1 ].time , priceMaValue[ 1 ] ) ) {
         Print(__FUNCTION__,
            ": failed to create a trend line! Error code = ",GetLastError());
         DebugBreak() ;
     } 
      
      
      
}







//    priceMaValueDefintion = iMA( _Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE );
//    newBar = false;
    // ArraySetAsSeries(priceMaValue,true) ;
    // maNameNumber = 0; 
    //  maNameNumber = "A";
    //  MA8_0 = iMA(_Symbol, timeframe, 8, 0, MODE_EMA, PRICE_CLOSE);
    //  ArraySetAsSeries(MA8,
 
Ok thanks for taking a look.
Reason: