Download MetaTrader 5

can't enter stop loss and take profit values

To add comments, please log in or register
Deposit to your account in any convenient way. There are 4 of them here!
user30456
37
user30456 2012.09.18 05:03 

i'm using the CTrade class to open positions (CTrade.Buy and CTrade.Sell) and it works well if i don't specify any stop loss or take profit values in their respective fields (meaning i type 0) , however if i specify any value in these fields the ea stops working it never open any positions

why is that and what should i do

here is my code :

#property copyright "karem talli"
#property link      "karem.talli@hotmail.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
//input parameters:
input int StopLoss=100; // Stop Loss
input int TakeProfit=240; // Take Profit
input double adx_min=25;// adx minimum
input int ma_per=8; // moving average period
input int adx_per=8; // adx period
input int dev=100; // deviation
input double vol=0.1; // lots to trade
input int k_per=5; // stoch k period
input int d_per=3; // stoch d period
input int slowing=3; // stoch slowing
input int EA_Magic=1515; // magic number
//other parameters 
int adx_hand , ma_hand , stoch_hand , AC_hand ;
double adx[] , plsdi[] , mindi[] , ma[] , AC[] , stoch[];
bool buy_opened=false , sell_opened=false;
int STP, TKP; // To be used for Stop Loss, Take Profit 
//classes
CTrade trade;
CSymbolInfo mysymbol;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    mysymbol.Name(_Symbol);
    trade.SetExpertMagicNumber(EA_Magic);
    trade.SetDeviationInPoints(dev);
    adx_hand=iADX(NULL,0,adx_per);
    ma_hand=iMA(_Symbol,0,ma_per,0,MODE_EMA,PRICE_CLOSE);
    stoch_hand=iStochastic(_Symbol,0,k_per,d_per,slowing,MODE_EMA,STO_LOWHIGH);
    AC_hand=iAC(NULL,0);
    if ( adx_hand<0 || ma_hand<0 || stoch_hand<0 || AC_hand<0 )
    {
      Alert("Error creating handles - ",GetLastError());
      ResetLastError();
      return(1);
    }
    STP = StopLoss;
    TKP = TakeProfit;
    if(_Digits==5 || _Digits==3)
    {
      STP = STP*10;
      TKP = TKP*10;
    }
    return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
    IndicatorRelease(adx_hand);
    IndicatorRelease(ma_hand);
    IndicatorRelease(stoch_hand);
    IndicatorRelease(AC_hand); 
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    MqlRates rates[];
    MqlTick latest_price;
    ArraySetAsSeries(rates,true);
    ArraySetAsSeries(adx,true);
    ArraySetAsSeries(plsdi,true);
    ArraySetAsSeries(mindi,true);
    ArraySetAsSeries(ma,true);
    ArraySetAsSeries(stoch,true);
    ArraySetAsSeries(AC,true);
    if (CopyRates(_Symbol,0,0,10,rates)<0)
    {
      Alert("Error copying rates - ",GetLastError());
      ResetLastError();
      return;
    }
    static datetime prev_time;
    datetime bar_time=rates[0].time;
    if (bar_time==prev_time)
    {
      return;
    }
    prev_time=bar_time;
    if (CopyBuffer(adx_hand,0,0,10,adx)<3 || CopyBuffer(adx_hand,1,0,10,plsdi)<3 || CopyBuffer(adx_hand,2,0,10,mindi)<3)
    {
      Alert("Error copying buffer for adx - ",GetLastError());
      ResetLastError();
      return;
    }
    if (CopyBuffer(stoch_hand,0,0,10,stoch)<3)
    {
      Alert("Error copying buffer for stochastic - ",GetLastError());
      ResetLastError();
      return;
    }
    if (CopyBuffer(ma_hand,0,0,10,ma)<3)
    {
      Alert("Error copying buffer for ma - ",GetLastError());
      ResetLastError();
      return;
    }
    if (CopyBuffer(AC_hand,0,0,10,AC)<3)
    {
      Alert("Error copying buffer for AC - ",GetLastError());
      ResetLastError();
      return;
    }
    // BUYING SELLING AND CLOSING
    if ( (/*buy conditions*/true) && (AC[1]<0 && AC[0]>0) && (buy_opened==false && sell_opened==false) )
    {
      double stloss = NormalizeDouble(mysymbol.Ask() - STP*_Point,_Digits); //--- Stop Loss
      double tprofit = NormalizeDouble(mysymbol.Ask()+ TKP*_Point,_Digits); //--- Take Profit
      if (trade.Buy(vol,NULL,0,stloss,tprofit))
      {
        Alert("Long position was opened");
        buy_opened=true;
      }
      else
      {
        Alert("long position was not opened - ",GetLastError());
        ResetLastError();
        return;
      }
    }
    if ( (/*sell conditions*/true) && (AC[1]>0 && AC[0]<0) && (buy_opened==false && sell_opened==false) )
    {
      double ssloss=NormalizeDouble(mysymbol.Bid()+STP*_Point,_Digits);   //--- Stop Loss
      double stprofit=NormalizeDouble(mysymbol.Bid()-TKP*_Point,_Digits); //--- Take Profit
      if (trade.Sell(vol,NULL,0,ssloss,stprofit))
      {
        Alert("Short position was opened");
        sell_opened=true;
      }
      else
      {
        Alert("Short position was not opened - ",GetLastError());
        ResetLastError();
        return;
      }
    }
    if ( (buy_opened) && (/*conditions to close long*/ AC[0]<AC[1] ) )
    {
      if (trade.PositionClose(_Symbol,dev))
      {
        Alert("Long was closed");
        buy_opened=false;
      }
      else
      {
        Alert("Long was not closed - ",GetLastError());
        ResetLastError();
        return;
      }
    }
    if ( (sell_opened) && (/*conditions to close short*/ AC[0]>AC[1] ) )
    {
      if (trade.PositionClose(_Symbol,dev))
      {
        Alert("Short was closed");
        sell_opened=false;
      }
      else
      {
        Alert("Short was not closed - ",GetLastError());
        ResetLastError();
        return;
      }
    }
  }
//+------------------------------------------------------------------+

 thank you.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
user30456
37
user30456 2012.09.20 01:41  

?? 

onewithzachy
2271
onewithzachy 2012.09.20 02:22  
karemtalli:

i'm using the CTrade class to open positions (CTrade.Buy and CTrade.Sell) and it works well if i don't specify any stop loss or take profit values in their respective fields (meaning i type 0) , however if i specify any value in these fields the ea stops working it never open any positions

why is that and what should i do

here is my code :

 thank you.

Hi karemtalli,

What the error from the order send say ?

 

user30456
37
user30456 2012.09.20 04:14  
what's that ? i don't i don't get any errors it just doesn't open positions it doesn't display any errors
onewithzachy
2271
onewithzachy 2012.09.20 05:51  
karemtalli:
what's that ? i don't i don't get any errors it just doesn't open positions it doesn't display any errors

Hi karemtalli,

Really ? No error ?

Your code below does not give you pop out alert error ?

      else
      {
        Alert("long position was not opened - ",GetLastError());
        ResetLastError();
        return;
      }

      else
      {
        Alert("Short position was not opened - ",GetLastError());
        ResetLastError();
        return;
      }

 

enbo lu
Moderator
1947
enbo lu 2012.09.20 15:59  
karemtalli:

i'm using the CTrade class to open positions (CTrade.Buy and CTrade.Sell) and it works well if i don't specify any stop loss or take profit values in their respective fields (meaning i type 0) , however if i specify any value in these fields the ea stops working it never open any positions

why is that and what should i do

here is my code :

 thank you.

Maybe your ea work on an ecn broker~
user30456
37
user30456 2012.09.20 20:55  

my wrong it says error 4756 (which means: Trade request sending failed )

and if you type 0 instead of the stop loss and take profit values then it workes okay

 

user30456
37
user30456 2012.09.20 20:56  
luenbo:
Maybe your ea work on an ecn broker~

what does than mean and ecn broker 

 

To add comments, please log in or register