Download MetaTrader 5
To add comments, please log in or register
Find and download Code Base apps via MetaTrader. It is fast!
shararath2004
26
shararath2004 2015.07.03 16:33 

 i am new to this mql4 and i am trying to modify a existing RSI based ea and i want to set previous bars difference between high and low as the take profit and stop loss level by

 doing like this

double pips= (High[1]-Low[1])/10;
double StopLoss= pips;
double TakeProfit= pips;

 but this is not working, it is placing just a takeprofit level just were the order executed and not setting any stoploss levels, If the problem is just from the above code please correct me and if not where is the problem

 

in this?

//+------------------------------------------------------------------+
//|                                                         Vita.mq4 |
//|                            Copyright © 2012, www.blank.com |
//|                                       http://www.blank.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, www.blank.com"
#property link      "http://www.blank.com"

//---- input parameters
extern double    Lots=0.1;
extern int       Slip=5;
extern string    StopSettings="Set stops below";
extern string    RSIsettings="Rsi settings follow";
extern int       period=5;
extern double    BuyPoint=30;
extern double    BuyPoint2=50;
extern double    SellPoint=70;
extern double    SellPoint2=50;
extern bool      CloseOnOpposite=true;
extern string    TimeSettings="Set the hour range the EA should trade";
extern int       StartHour=0;
extern int       EndHour=23;


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
int digits=MarketInfo("EURUSD",MODE_DIGITS);
int StopMultd=10;
int Slippage=Slip*StopMultd;

int MagicNumber1=20101,MagicNumber2=20102,i,closesell=0,closebuy=0;

double pips= (High[1]-Low[1])/10;
double StopLoss= pips;
double TakeProfit= pips;

double  TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double  SL=NormalizeDouble(StopLoss*StopMultd,Digits);



double slb=NormalizeDouble(Ask-SL*Point,Digits);
double sls=NormalizeDouble(Bid+SL*Point,Digits);


double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);

//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==MagicNumber1) {int halt1=1;}
          if(OrderMagicNumber()==MagicNumber2) {int halt2=1;}

        }
     }
}
//-------------------------------------------------------------------+
// time check
//-------------------------------------------------------------------
if((Hour()>=StartHour)&&(Hour()<=EndHour))
{
int TradeTimeOk=1;
}
else
{ TradeTimeOk=0; }
//-----------------------------------------------------------------
// Bar checks
//-----------------------------------------------------------------
 double RSInow=((((iDeMarker(NULL,0,period,0))*100)+((iWPR(NULL,0,period,0))+100)+(iRSI(NULL,0,period,PRICE_CLOSE,0))+(iMFI(NULL,0,period,0)))/4);
 double RSIlast=((((iDeMarker(NULL,0,period,1))*100)+((iWPR(NULL,0,period,1))+100)+(iRSI(NULL,0,period,PRICE_CLOSE,1))+(iMFI(NULL,0,period,1)))/4);
 double RSIprev=((((iDeMarker(NULL,0,period,2))*100)+((iWPR(NULL,0,period,2))+100)+(iRSI(NULL,0,period,PRICE_CLOSE,2))+(iMFI(NULL,0,period,2)))/4);
 
 //-------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------
// Opening criteria
//-----------------------------------------------------------------------------------------------------


// Open buy
 if((RSInow>BuyPoint2)&&(RSIlast>BuyPoint2)&&(RSIprev<BuyPoint)&&(halt1!=1)&&(TradeTimeOk==1)&&(pips>5)){
 int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Rsi trader buy order",MagicNumber1,0,Blue);
 if(CloseOnOpposite==true)closesell=1;
 }


// Open sell
 if((RSInow<SellPoint2)&&(RSIlast<SellPoint2)&&(RSIprev>SellPoint)&&(halt2!=1)&&(TradeTimeOk==1)&&(pips>5)){
 int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Rsi trader sell order",MagicNumber2,0,Green);
 if(CloseOnOpposite==true)closebuy=1;
 }


//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// Closing criteria
//-------------------------------------------------------------------------------------------------

if(closesell==1||closebuy==1||openbuy<1||opensell<1){// start

if(OrdersTotal()>0){
  for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
  
      if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
        
          if(OrderMagicNumber()==MagicNumber1&&closebuy==1) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE); }
          if(OrderMagicNumber()==MagicNumber2&&closesell==1) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE); }
          
          // set stops
          if((OrderMagicNumber()==MagicNumber1)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tpb,0,CLR_NONE); }
          if((OrderMagicNumber()==MagicNumber2)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tps,0,CLR_NONE); }
          if((OrderMagicNumber()==MagicNumber1)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,slb,OrderTakeProfit(),0,CLR_NONE); }
          if((OrderMagicNumber()==MagicNumber2)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,sls,OrderTakeProfit(),0,CLR_NONE); }

        }
     }
}


}// stop

//----
int Error=GetLastError();
  if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
  if(Error==133){Alert("Trading prohibited.");}
  if(Error==2){Alert("Common error.");}
  if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}

//----

//-------------------------------------------------------------------
   return(0);
  }
//+------------------------------------------------------------------+

 Please help me, thanks

shararath2004
26
shararath2004 2015.07.05 09:26  
please someone help me
honest_knave
947
honest_knave 2015.07.05 09:46  

I'd start by looking at the return values on your OrderModify function calls.

More information about return values 

whroeder1
13590
whroeder1 2015.07.05 11:38  
  1. Check your return codes when you have an error. Not at the end. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. OrderModify(OrderTicket(),0,OrderStopLoss(),tps,0 ...
    OrderModify(OrderTicket(),0,slb,OrderTakeProfit(),0
    You modify the TP, but since there is no RefreshRates() after the first modify, OTP will still be returning zero. So the second modify sets the SL and unsets the TP.
  3. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  4. if (OrderSelect(i-1,SELECT_BY_POS)==true)
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
     if(CloseOnOpposite==true)closesell=1;
    if(closesell==1||closebuy==1||openbuy<1||opensell<1){// start
    Don't use zero/one when you mean true/false
/
To add comments, please log in or register