RSI Crosses MA EA help...

 

I'm working on a EA thats suppose to buy or sell when crossing the MA line.



I also wannt the StopLoss to be 75% of the ATR value.

when i try to run the EA in "Test" i get this result




And it dosent seam right.

Here is the code...

//+------------------------------------------------------------------+
//| My_First_EA.mq4 |
//| Coders Guru |
//| https://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link "https://www.forex-tsd.com"
//---- input parameters
extern int TakeProfit=5;
extern double Lots=0.1;
extern double TrailingStop=70;
extern int RSIPeriod=8;
extern int MA_Period=8;
extern int AtrPeriod=20;

int StopLoss.1;

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_direction = 0;
if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
   int cnt, ticket, total;
   double shortEma, shortRSI;
   if(Bars<100)
      {
      Print("bars less than 100");
      return(0);
      }
   if(TakeProfit<10)
      {
      Print("TakeProfit less than 10");
      return(0); // check TakeProfit
      }
   shortEma = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
   shortRSI = iRSI(NULL,0,8,PRICE_CLOSE,0);
   int isCrossed = Crossed (shortEma,shortRSI);
   total = OrdersTotal();
   if(total < 1)
      {
      if(isCrossed == 1)
      {
   ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,
   "My EA",12345,0,Green);
   if(ticket>0)
      {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
      Print("BUY order opened : ",OrderOpenPrice());
      }     
   else Print("Error opening BUY order : ",GetLastError());
   return(0);
   }

      double d_ATR = NormalizeDouble(iATR(PERIOD_D1, AtrPeriod,1 , 0), 2); // Get the Daily range

   
      int i_ATR = d_ATR*100;  // Turn it into (full) pips
   
      StopLoss.1 = i_ATR*075;



if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,
Bid-TakeProfit*Point,"My EA",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
// close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(isCrossed == 1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
// close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,
OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}

//+------------------------------------------------------------------+

Please i need help and try to fix this one.

 

The code you provided is by the infamous Coder Guru of Forex-TSD.com try contacting him for help fixing the code.

//+------------------------------------------------------------------+
//| My_First_EA.mq4 |
//| Coders Guru |
//| https://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link "https://www.forex-tsd.com"
 

MH

Code is wrong for EURUSD ATR calculation, OTTOMH, you need something like this

extern double TP.ATR.Percent = 100.00; 
extern double TS.ATR.Percent = 32.00;
extern double SL.ATR.Percent = 75.00;


start()

{

// main code


return (0)
}

void GetATRStops(int &iTP, int &iTS, int &iSL) 
{  
   double ATRFactor, dATR, dATRPips;

   
  if (Digits == 2 || Digits == 3) ATRFactor = 100; // Yen pair

  if (Digits == 4 || Digits == 5) ATRFactor = 10000.00; // Non-yen pair
    
   
   dATR = iATR(Symbol(), PERIOD_D1, 20, 1);
   
   dATRPips = dATR*ATRFactor;
   
   iTP = NormalizeDouble((dATRPips/100)*TP.ATR.Percent, 0); 
   iTS = NormalizeDouble((dATRPips/100)*TS.ATR.Percent, 0);
   iSL = NormalizeDouble((dATRPips/100)*SL.ATR.Percent, 0);
   
}

and then the full pip values need handling to sub-pip if relevant with something like this

void HandleDigits()
{
    // Automatically handles full-pip and sub-pip accounts
    if (Digits == 4 || Digits == 2) 
     {

        Real.TrailingStop = TrailingStop;  
        Real.TakeProfit   = TakeProfit;  
        Real.StopLoss = StopLoss;
       
       Real.Slippage = Slippage;

     }
  
   if (Digits == 5 || Digits == 3) 
     {

       Real.TrailingStop =      TrailingStop*10;  
       Real.TakeProfit   =      TakeProfit*10;
       Real.StopLoss = StopLoss*10;
              
       Real.Slippage = Slippage*10;

     }

}

FWIW

-BB-

 
BarrowBoy:

MH

Code is wrong for EURUSD ATR calculation, OTTOMH, you need something like this

and then the full pip values need handling to sub-pip if relevant with something like this

FWIW

-BB-

BB, your acronyms get better by the day! I'm spending more time on Urban Dictionary than MQL4 at the moment. I'm looking forward to the day the entire post is just a series of initials :)

OMG, AFAIK it's BB FTW...fwiw.

V

 
BarrowBoy:

MH

Code is wrong for EURUSD ATR calculation, OTTOMH, you need something like this

and then the full pip values need handling to sub-pip if relevant with something like this

FWIW

-BB-

I'm not to sure of where to put the code you gave me.
I think i placed the first one right but i have no idea where to but the second one with the I put it at the bottom. Can you tell me where to put the other one and i would be really great full.

void HandleDigits() Here is where i put the first one.
//+------------------------------------------------------------------+
//| My_First_EA.mq4 |
//|  |
//|  |
//+------------------------------------------------------------------+
#property copyright "XXX"
#property link "XXX"
//---- input parameters
extern int TakeProfit=250.0;
extern double Lots=0.1;
extern double TrailingStop=35.0;
extern int RSIPeriod=8;
extern int MA_Period=8;
extern int AtrPeriod=20;
extern double TP.ATR.Percent = 100.00; 
extern double TS.ATR.Percent = 32.00;
extern double SL.ATR.Percent = 75.00;

int StopLoss.1;

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
   {
   //----
   //----
   return(0);
   }
int Crossed (double line1 , double line2)
   {
   static int last_direction = 0;
   static int current_direction = 0;
   if(line1>line2)current_direction = 1; //up
   if(line1<line2)current_direction = 2; //down
   if(current_direction != last_direction) //changed
      {
      last_direction = current_direction;
      return (last_direction);
      }
   else
      {
      return (0);
      }
   }
   
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
   {
   //----
   int cnt, ticket, total;
   double shortEma, shortRSI;
   
    
   if(Bars<100)
      {
      Print("bars less than 100");
      return(0);
      }
   if(TakeProfit<10)
      {
      Print("TakeProfit less than 10");
      return(0); // check TakeProfit
      }
   shortEma = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
   shortRSI = iRSI(NULL,0,8,PRICE_CLOSE,0);
   int isCrossed = Crossed (shortEma,shortRSI);
   total = OrdersTotal();
   if(total < 1)
      {
      if(isCrossed == 1)
      {
   ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,
   "My EA",12345,0,Green);
   if(ticket>0)
      {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
      Print("BUY order opened : ",OrderOpenPrice());
      }     
   else Print("Error opening BUY order : ",GetLastError());
   return(0);
   }

 if(isCrossed == 2)
   {
   ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,
   Bid-TakeProfit*Point,"My EA",12345,0,Red);
   if(ticket>0)
      {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
      Print("SELL order opened : ",OrderOpenPrice());
      }
   else Print("Error opening SELL order : ",GetLastError());
   return(0);
   }
return(0);
}
for(cnt=0;cnt<total;cnt++)
   {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
      {
      if(OrderType()==OP_BUY) // long position is opened
         {
         // should it be closed?
         if(isCrossed == 2)
            {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
            // close position
            return(0); // exit
            }
         // check for trailing stop
         if(TrailingStop>0)
            {
            if(Bid-OrderOpenPrice()>Point*TrailingStop)
               {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                     {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
                     Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                     }
               }
             }
         }
         else // go to short position
            {
            // should it be closed?
            if(isCrossed == 1)
               {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
               // close position
               return(0); // exit
               }
            // check for trailing stop
            if(TrailingStop>0)
            {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                  {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
                  (OrderStopLoss()==0))
                     {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,
                     OrderTakeProfit(),0,Red);
                     return(0);
                     }
                  }
            }
         }
      }
   }

return(0);
}
void GetATRStops(int &iTP, int &iTS, int &iSL) 
{  
   double ATRFactor, dATR, dATRPips;

   
  if (Digits == 2 || Digits == 3) ATRFactor = 100; // Yen pair

  if (Digits == 4 || Digits == 5) ATRFactor = 10000.00; // Non-yen pair
    
   
   dATR = iATR(Symbol(), PERIOD_D1, 20, 1);
   
   dATRPips = dATR*ATRFactor;
   
   iTP = NormalizeDouble((dATRPips/100)*TP.ATR.Percent, 0); 
   iTS = NormalizeDouble((dATRPips/100)*TS.ATR.Percent, 0);
   iSL = NormalizeDouble((dATRPips/100)*SL.ATR.Percent, 0);
   
}

//+------------------------------------------------------------------+

// Mathias

 
mattehalen:

I'm not to sure of where to put the code you gave me.
I think i placed the first one right but i have no idea where to but the second one with the I put it at the bottom. Can you tell me where to put the other one and i would be really great full.

void HandleDigits() Here is where i put the first one.

// Mathias

It's another function so just put it below GetATRStops. Basically, so long as it is not inside a special function like start() it can go anywhere ...

V

 
Viffer:

It's another function so just put it below GetATRStops. Basically, so long as it is not inside a special function like start() it can go anywhere ...

V


I get this at the and in MetaEditor

Function "GetATRStops" is not referenced and will be removed from exp-file

Function "HandleDigits" is not referenced and will be removed from exp-file

how can i fix that?


 

It means your not using the function inside your code so it being there is pointless. I don't have time to read all this code at the moment but basically they will need to be called inside either start() or init() to calculate your pips into points and whatever the ATR one was. You will need to figure out where that is. Maybe BB can offer more help as he had obviuosly read it to determin the solution.

V

 
Viffer:

It means your not using the function inside your code so it being there is pointless. I don't have time to read all this code at the moment but basically they will need to be called inside either start() or init() to calculate your pips into points and whatever the ATR one was. You will need to figure out where that is. Maybe BB can offer more help as he had obviuosly read it to determin the solution.

V


Tank you for the help
 
BarrowBoy:

MH

Code is wrong for EURUSD ATR calculation, OTTOMH, you need something like this

and then the full pip values need handling to sub-pip if relevant with something like this

FWIW

-BB-

I get this at the and in MetaEditor

Function "GetATRStops" is not referenced and will be removed from exp-file

Function "HandleDigits" is not referenced and will be removed from exp-file

how can i fix that?

I'm not sure where to put the code you gave me.

 

Please can somebody help me to make this code work

I'm trying to code MA of OsMA but does not trade.

I attached the code.

please help!

Thanks.

#property copyright "Copyright xxxxx"
#property link      xxxxxxxxxxxxxx
// Trailing stop
extern int MA50 = 50;
extern int MA3 = 3;
extern int MA8 = 8;
extern int FastEMA = 12;
extern int SlowEMA = 26;
extern int SignalSMA = 9;
extern double Lots=0.05;
extern int Slippage=3;
extern double  TakeProfit=300; // take profit
extern double  StopLoss=200; // stop loss
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int arraysize=600;
   double OsMA[];
   double EMAArray[];
   double SMAArray[];
   ArrayResize(OsMA,arraysize);
   ArrayResize(EMAArray,arraysize);
   ArrayResize(SMAArray,arraysize);
   ArraySetAsSeries(OsMA,true);
 
   for(int i3=arraysize-1;i3>=0;i3--)
      OsMA[i3]=iOsMA(NULL,0,12,26,9,PRICE_CLOSE,i3);
   for(i3=arraysize-1;i3>=0;i3--)
      EMAArray[i3]=iMAOnArray(OsMA,0,1,0,OsMA[i3],i3);
   double EMA =EMAArray[i3];
   
   for(i3=arraysize-1;i3>=0;i3--)
      OsMA[i3]=iOsMA(NULL,0,12,26,9,PRICE_CLOSE,i3);
   for(i3=arraysize-1;i3>=0;i3--)
        SMAArray[i3]=iMAOnArray(OsMA,0,8,0,OsMA[i3],i3);
   double SMA =SMAArray[i3];
   double MA50=iMA(NULL,0, 50 ,0,MODE_SMA,PRICE_CLOSE,0);
   double MA3 =iMA(NULL,0, 3 ,0,MODE_LWMA,PRICE_OPEN,0);
   double MA8 =iMA(NULL,0, 8 ,0,MODE_SMA,PRICE_OPEN,0);
//----
   bool Long=false;
   bool Short=false;
   bool Sideways=false;
   if(MA3>MA8 && MA8>MA50 && EMA>SMA) Long=true;
   if(MA3<MA8 && MA8<MA50 && EMA<SMA) Short=true;
   if(Long==true  && OrdersTotal()==0)
     {
      OrderSend(Symbol(),OP_BUY ,Lots,Ask,Slippage,Ask/3,Ask*3,0,0,Blue);
     }
   if(Short==true && OrdersTotal()==0)
     {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid*3,Bid/3,0,0,Red);
     }
   if(Sideways==true  && OrdersTotal()!=0)
     {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
      Comment("Sideways detected");
      if(OrderType()==OP_BUY) OrderClose(OrderTicket(),1,Ask,3,Red);
      if(OrderType()==OP_SELL) OrderClose(OrderTicket(),1,Bid,3,Red);
     }
  }
//+------------------------------------------------------------------+
Reason: