EA dosnt work at real time - page 2

 
thiezo:

Hi Nati2009,

I had the same issue with one of my accounts only to find out that my Broker blocked my account from using Expert Advisors.

Use the following code to see if you are allowed to use an expert advisor on your account.

hi bro

thank you

i did the check and it was true :\

any other idea ?

maybe its somthing with "allow dll imports"? its disable 
 
nati2009:

hi bro

thank you

i did the check and it was true :\

any other idea ?

maybe its somthing with "allow dll imports"? its disable 

That was my best stab in the dark with my eyes closed. You need to attach/post most of your code so that it can be further analysed.

 

This is the code


its work at the back tasting but not at live account 


//+------------------------------------------------------------------+
//|                                               RSI div.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include  <Assistence fix.mqh>


int percentloss=10; 
              
bool uselong=true;                      //define if to use LONG or not (true or false)
bool useshort=true;                    //define if to use SHORT or not (true or false)

bool usesecondtrade=true;


bool usetralingstop=true;                //define if to use traling stop or not (true or false)  


int pipslosspivot=140;                       //4 hourdefine the stoploss x pips from the lower or higher point
int pipsprofitentry=10;                    //4 hourdefine the profit x pips from the entry point

bool tradebyatr=false;                     //define if to use trade by ATR or by PIPS (true or false) 

double ATRmulifordef=2;                   //define the stoploss x ATR from the lower or higher point
double profitvsloss=1;                    //define take profit (pips to stoploss multiply by x )z


double maxpipsfrompivot1hour=40;            //1 hour chart  - limit pips allowed from price to lower or high point when trading 1 hour chatr 
double maxpipsfrompivot4hour=80;           //4 hour chart  " " 

double rsiminval=0;                         // Min val for SHORT first pivot rsi val
double rsiminvallong=100;                     // Max val for LONG first pivot rsi val


 int period1=20;                             // How many candle to lookback
 int period2=50;                             // How many candle to lookback


 int maxdistancefromdiv=10;           //limit the distance from divergence that allowed to send order
 int distancefrompivot=10;           //minimum distance from prev hh to new hh ( or prev ll to new ll)

 int rsidifferent1hour=13;           // 1 hour chart min val bettwen rsi divergence 
 int rsidifferent4hour=0;            // 4 hour chart min val bettwen rsi divergence 

double secondtadestatr;

static int prevbar;
static int currentbar;

static double prevRSIhighperiod1;
static double prevRSIhighperiod2;

static double prevhighperiod1;
static double prevhighperiod2;

static int prevbarsincehighperiod1;
static int prevbarsincehighperiod2;

static datetime BearDivergenceperiod1;
static datetime BearDivergenceperiod2;


static int barfrombearDivergenceperiod1=distancefrompivot+1;
static int barfrombearDivergenceperiod2=distancefrompivot+1;


static double bearRSIpivotval;
static double highrsifrombeardiv;

static double stoplossshort;
static double takeprofit;
static double takeprofitshort;

static double enterylong;
static double enteryshort;

static int orderlong;
static int ordershort;

datetime orderlongtime;
datetime ordershorttime;

static int rsidifferent=0;
static int maxpipsfrompivot=40;

double rsical[10];


int magicNMlong=3333;
int magicNMshort=2222;

string currency[5]= {"USDJPY","USDCAD"};

bool currencyallowed;

bool BarPeriod;

static bool tradeallowed=false;


static bool tradelong=false;
static bool tradeshort=false;

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

      
   Alert(" time" + TimeCurrent());
   Alert("spread time" + spreadtime());

   
//chack if currency allowed ---------------------------------------------  

   for(int i=0;i<5;i++)
   {
   
   if (currency[i]==Symbol()) 
   {
   currencyallowed=true;
   break;
   }
   else currencyallowed=false;
   }
   
   if(!currencyallowed) Alert("the EA dosent allowed to trade this currency");
   
//chack if timeframe allowed ---------------------------------------------    
   
   if (ChartPeriod()=="240") BarPeriod=true;
   else BarPeriod=false;
   if(!BarPeriod) Alert("the EA dosent allowed to trade this timeframe");

//chack if auto trading on ---------------------------------------------   

   if( !IsTradeAllowed())
   {
      Alert("EA does not have premition to work, check auto trading");
   }


//chack if trade allowed on ---------------------------------------------   

   //&& currencyallowed && BarPeriod  &&
   if( currencyallowed && BarPeriod  && IsTradeAllowed())
   {
      tradeallowed=true;
      Alert("The EA start to work, Good Luck Bro :)");
   }




//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  Alert("");
  Alert("The EA as finish for today, go rest :) "); 
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---


//check for open trade   ---------------------------------------------    

   
   bool opentradelong=checkforopentrade(magicNMlong);
   bool opentradeshort=checkforopentrade(magicNMshort);

   double rsival=iRSI(_Symbol,_Period,14,PRICE_CLOSE,0);
   double RSIvalcurr1D=iRSI(_Symbol,PERIOD_D1,14,PRICE_CLOSE,0);  
   double RSIvalprev1D=iRSI(_Symbol,PERIOD_D1,14,PRICE_CLOSE,1);  
   
   
   double ATRval=iATR(_Symbol,_Period,14,0);
   
//pips from pivot ---------------------------------------------   
   
   if(ChartPeriod()==60)
   {
   maxpipsfrompivot=maxpipsfrompivot1hour ;
   rsidifferent=rsidifferent1hour;
   }
   if(ChartPeriod()==240)
   {
   maxpipsfrompivot=maxpipsfrompivot4hour ;
   rsidifferent=rsidifferent4hour;
   }
   
//Get the   higest price period 1---------------------------------------------
   

   int highestperiod1=iHighest(_Symbol,_Period,MODE_HIGH,period1,0);
   //ObjectDelete("Period 1 highest price");
   //ObjectCreate("Period 1 highest price",OBJ_HLINE,0,Time[0],High[highestperiod1]);

//Get the   higest price period 2---------------------------------------------

   int highestperiod2=iHighest(_Symbol,_Period,MODE_HIGH,period2,0);
   //ObjectDelete("Period 2 highest price");
  // ObjectCreate("Period 2 highest price",OBJ_HLINE,0,Time[0],High[highestperiod2]);
   
//Get the rsi value of the price level---------------------------------------------


   double rsihighperiod1=iRSI(_Symbol,_Period,14,PRICE_CLOSE,highestperiod1);

   double rsihighperiod2=iRSI(_Symbol,_Period,14,PRICE_CLOSE,highestperiod2);
   
//Get the Price vlaue of HH and LL---------------------------------------------


   double highpriceperiod1=High[highestperiod1];

   double highpriceperiod2=High[highestperiod2];

// record last RSI and Price and Barsisnce at new variable ---------------------------------------------
   
   currentbar=Bars;
   if((currentbar-prevbar)==1)
   {

      prevRSIhighperiod1=rsihighperiod1;

      prevRSIhighperiod2=rsihighperiod2;
      
      prevhighperiod1=highpriceperiod1;

      prevhighperiod2=highpriceperiod2;
      
      prevbarsincehighperiod1=highestperiod1;

      prevbarsincehighperiod2=highestperiod2;
      
      
   }
   prevbar=Bars ;



//------------------------------short---------------------------------------------------------------------------------------------------------------//


//check for bear divergance period 1 ---------------------------------------------

   bool rsimindifferentshortperiod1=MathAbs(prevRSIhighperiod1-rsival)>rsidifferent;
   
   if(prevRSIhighperiod1  > rsiminval  &&rsimindifferentshortperiod1 && prevbarsincehighperiod1>distancefrompivot&& Bid>prevhighperiod1+2*pipval() && rsival<prevRSIhighperiod1 && barfrombearDivergenceperiod1>distancefrompivot&& !spreadtime())
   {
     
      BearDivergenceperiod1=TimeCurrent();
      bearRSIpivotval=prevRSIhighperiod1;
      highrsifrombeardiv=rsival;
   }

   int barfrombearDivergenceperiod1=iBarShift(_Symbol,_Period,BearDivergenceperiod1,false);
   bool beardivperiod1=barfrombearDivergenceperiod1>0 && barfrombearDivergenceperiod1<maxdistancefromdiv;

//check for bear divergance period 2 ---------------------------------------------

   bool rsimindifferentshortperiod2=MathAbs(prevRSIhighperiod2-rsival)>rsidifferent;
   
   if(prevRSIhighperiod2  > rsiminval && rsimindifferentshortperiod2 && prevbarsincehighperiod2>distancefrompivot&& Bid>prevhighperiod2+2*pipval() && rsival<prevRSIhighperiod2 && barfrombearDivergenceperiod2>distancefrompivot&& !spreadtime())
   {
      BearDivergenceperiod2=TimeCurrent();
      bearRSIpivotval=prevRSIhighperiod2;
      highrsifrombeardiv=rsival;
   } 

   int barfrombearDivergenceperiod2=iBarShift(_Symbol,_Period,BearDivergenceperiod2,false);
   bool beardivperiod2=barfrombearDivergenceperiod2>0 && barfrombearDivergenceperiod2<maxdistancefromdiv;

//how meny pips to low point for bear div---------------------------------------------
   
   double pipsfromhigh;
   if(beardivperiod1 && !beardivperiod2 )
   {

      pipsfromhigh=(highpriceperiod1-Bid)/pipval();
   }
   else if(beardivperiod2)
   {

      pipsfromhigh=(highpriceperiod2-Bid)/pipval();
   }

 // still bear divergance? ---------------------------------------------   
 

   for(int i=0;i<maxdistancefromdiv;i++)
   {

      
      rsical[i]=iRSI(_Symbol,_Period,14,PRICE_CLOSE,i);
      if(rsical[i]>highrsifrombeardiv) highrsifrombeardiv=rsical[i];
   }
   
     
//send order for bear divergance ---------------------------------------------
   

   int barfromshort=iBarShift(_Symbol,_Period,ordershorttime,FALSE);
  
   if( barfromshort > 10 &&  highrsifrombeardiv<bearRSIpivotval  && pipsfromhigh<maxpipsfrompivot && (beardivperiod1 ||beardivperiod2 )&& Ask<Low[1]-2*pipval()&& !opentradeshort &&!spreadtime() &&useshort && tradeallowed )
   {
        if(beardivperiod1 && !beardivperiod2 )
           {
               stoplossshort=highpriceperiod1+(pipslosspivot*pipval());
           }
        else if(beardivperiod2)
           {
            stoplossshort=highpriceperiod2+(pipslosspivot*pipval());
           }
         
         takeprofitshort=Bid-pipsprofitentry*pipval();
    }
      
      enteryshort=Bid;
      ordershorttime=TimeCurrent();
      
      ordershort=OrderSend(_Symbol,OP_SELL,optimallotsize(percentloss,Bid,stoplossshort),Bid,20,stoplossshort,takeprofitshort,_Symbol,magicNMshort);

      if(ordershort<0)
      {
         Alert("order Short bear divergence faild error = " + GetLastError());
      }
         else
      {
         Alert("order Short bear divergence was sent");
      }
   
 
 


  }
//+------------------------------------------------------------------+




//get pipval
double pipval()
{
   if (Digits >= 4 )
   {
      return 0.0001;
   }
   else
   {
      return 0.01;
   }

}   


//get account trade allowed

double optimallotsize(double PercentLossFortrade, double MaxLossPIPS)
{
   double tickvalue=MarketInfo(NULL,MODE_TICKVALUE);
   if (_Digits <= 3) tickvalue=tickvalue /100 ;
   double lotsize=MarketInfo(NULL,MODE_LOTSIZE);
   double accountequity=AccountEquity();
   double maxlossbasecur=accountequity * PercentLossFortrade / 100;
   double maxlossquotecur=maxlossbasecur / tickvalue;
   double optimallotsize=NormalizeDouble(maxlossquotecur/(MaxLossPIPS*pipval())/lotsize,2);
   Alert("Your account equity is = " + accountequity);
   Alert("Allowed order size to fill is= " + optimallotsize);
   return optimallotsize;
}

double optimallotsize(double PercentLossFortrade, double EntryPrice, double StopLossPrice)
{
   int MaxLossPIPS=MathAbs((EntryPrice-StopLossPrice)/pipval());
   return optimallotsize(PercentLossFortrade,MaxLossPIPS);
}


//checkforopentrade
bool checkforopentrade(int magicNM)
{
   int openorder=OrdersTotal();
   
   for(int i=0 ; i < openorder; i++)
   {
      if (OrderSelect(i,SELECT_BY_POS)==TRUE)
      {
         if((OrderMagicNumber()==magicNM)&& OrderSymbol()==_Symbol)
         {
         return True;
         }
      }
    }
    return False;
}


/*
//get error
     
   if(OrderID<0)
   {
      Alert("order faild error = " + GetLastError());
   }
   else
   {
     Alert("order was sent");
   }
  
   return ordererror;
   }

*/

double optimallotsizebyequity(double PercentOfequity)
{
   double tickvalue=MarketInfo(NULL,MODE_TICKVALUE);
   if (_Digits <= 3) tickvalue=tickvalue /100 ;
   double lotsize=MarketInfo(NULL,MODE_LOTSIZE);
   double accountequity=AccountEquity();
   double optimallotsizebyequity=accountequity * (PercentOfequity / 100) /lotsize;
   double optimallotsizebyequityNormalize=NormalizeDouble(optimallotsizebyequity,2);
   Alert("Your account equity is = " + accountequity);
   Alert("Allowed order size to fill is= " + optimallotsizebyequity);
   return optimallotsizebyequity;
}


bool spreadtime()
{
   datetime time=TimeCurrent();
   datetime timestartrest="22:55:00";
   datetime timefinishrest="00:10:00";
   if (time>timestartrest && time < timefinishrest )
   {
      return true;
   }
   else
   {
      return false;
   }

}