Having trouble about getting stoploss correct.

 

This is the code i have to run EA based on an indicator which is connected by Icustom. Problem is it is fixed to put SL to object price 2 or 1 and i want to replace that with a fixed stop loss. Even though i implemented the min-max SL functions it didn't workout for me. Can someone help me it gets broken when i delete get_sl functions.


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

//  SUPPLY AND DEMAND EA                                             |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
input string comEAsettings="[EA settings]";//~
input double lots=0.;
input string SLTP_ratio="Perbandingan SL dan TP";
input int    StopLoss  = 1;
input int    TakeProfit = 3;
input int    MagicNumber =55546;
string EA_Comment  ="TheCharter";
double _point=1;
double last_sup,last_res;
double MaxSL = 1000;
double MinSL = 1000;
double MaxTP = 3000;
double MinTP = 1500;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  
  
//--- create timer
   EventSetTimer(60);
//Get Symbol digit
   string _sym=Symbol();
   double _digits=MarketInfo(_sym,MODE_DIGITS);
   if(_digits==5||_digits==3) _point=1/MathPow(10,(_digits-1));
   if(_digits==4||_digits==2) _point=1/MathPow(10,(_digits));
   if(_digits==1) _point=0.1;
   last_sup=-1;last_res=-1;
    MaxSL = MaxSL * Point();
    MinSL = MinSL * Point();
    MaxTP = MaxTP * Point();
    MinTP = MinTP * Point();
//
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(IsTesting())get_indi();
   double current_res=get_res();
   if(OrdersTotalT(OP_SELL)==0 && Ask>current_res && current_res!=last_res)
     {
     double _sl= MathAbs(current_res -get_res_sl())/_point;
      SendOrder(OP_SELL,_sl);
      last_res=current_res;
     }
   double _sup=get_sup();
   if(OrdersTotalT(OP_BUY)==0 && Bid<_sup && _sup!= last_sup)
     {
      double _sl = MathAbs(_sup -get_sup_sl())/_point;//
      SendOrder(OP_BUY,_sl);
      last_sup=_sup;
     }
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+
// function to send order
bool SendOrder(int type,double _sl)
  {
   while(IsTradeContextBusy());
   int ticket=-1;
   double SL,TP;
   if(type==OP_BUY)
     {
      if(_sl==0){SL=0;}else{SL=Ask-StopLoss*_sl*_point;}
      if(_sl==0){TP=0;}else{TP=Ask+TakeProfit*_sl*_point;}
      ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(lots,Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0);
     }
   if(type==OP_SELL)
     {
      if(_sl==0){SL=0;}else{SL=Bid+StopLoss*_sl*_point;}
      if(_sl==0){TP=0;}else{TP=Bid-TakeProfit*_sl*_point;}
      ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(lots,Symbol()),Bid,3,SL,TP,EA_Comment,MagicNumber,0);
     }
   if(ticket<0)
     {
      Print("OrderSend  failed with error #",GetLastError());
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//make lots to right format
double NormalizeLots(double _lots,string pair="")
  {
   if(pair=="") pair=Symbol();
   double  lotStep=MarketInfo(pair,MODE_LOTSTEP),
   minLot=MarketInfo(pair,MODE_MINLOT);
   _lots=MathRound(_lots/lotStep)*lotStep;
   if(_lots<MarketInfo(pair,MODE_MINLOT)) _lots=MarketInfo(pair,MODE_MINLOT);
   if(_lots>MarketInfo(pair,MODE_MAXLOT)) _lots=MarketInfo(pair,MODE_MAXLOT);
   return(_lots);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
// get res level from the chart
double get_res()
  {
   double _res=999999999999;
   double _tmp=0;
   long _chart=ChartID();
   for(int i=0;i<ObjectsTotal(_chart);i++)
     {
      string _name=ObjectName(_chart,i);
      if(StringFind(_name,"R#R",0)>0 && StringFind(_name,"Verified",0)>0) _tmp=ObjectGetDouble(_chart,_name,OBJPROP_PRICE2);
      if(_res>_tmp && _tmp!=0) _res=_tmp;
     }
   return(_res);
  }
  
double get_res_sl()
  {
   double _res=999999999999;
   double _tmp=0;
   long _chart=ChartID();
   for(int i=0;i<ObjectsTotal(_chart);i++)
     {
      string _name=ObjectName(_chart,i);
      if(StringFind(_name,"R#R",0)>0 && StringFind(_name,"Verified",0)>0) _tmp=ObjectGetDouble(_chart,_name,OBJPROP_PRICE1);
      if(_res>_tmp && _tmp!=0) _res=_tmp;
     }
   return(_res);
  }
//+------------------------------------------------------------------+

int OrdersTotalT(int _type)
  {
   int _total=0;
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==_type)
        {
         _total++;
        }
     }
   return(_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

double get_sup()
  {
   double _res=0;
   double _tmp=0;
   long _chart=ChartID();
   for(int i=0;i<ObjectsTotal(_chart);i++)
     {
      string _name=ObjectName(_chart,i);
      if(StringFind(_name,"R#S",0)>0 && StringFind(_name,"Verified",0)>0) _tmp=ObjectGetDouble(_chart,_name,OBJPROP_PRICE1);
      if(_res<_tmp && _tmp!=0) _res=_tmp;
     }
   return(_res);// return lower verified support
  }
  
double get_sup_sl()
  {
   double _res=0;
   double _tmp=0;
   long _chart=ChartID();
   for(int i=0;i<ObjectsTotal(_chart);i++)
     {
      string _name=ObjectName(_chart,i);
      if(StringFind(_name,"R#S",0)>0 && StringFind(_name,"Verified",0)>0) _tmp=ObjectGetDouble(_chart,_name,OBJPROP_PRICE2);      
      if(_res<_tmp && _tmp!=0) _res=_tmp;
     }
   return(_res);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

double get_indi()
  {
   return(iCustom(NULL,0,"shved_supply_and_demand",0,0));
  }
//+------------------------------------------------------------------+
Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
The Charter:

This is the code i have to run EA based on an indicator which is connected by Icustom. Problem is it is fixed to put SL to object price 2 or 1 and i want to replace that with a fixed stop loss. Even though i implemented the min-max SL functions it didn't workout for me. Can someone help me it gets broken when i delete get_sl functions.

You just have to change the lines highlighted in green - assign _sl with a fixed value:

   if(OrdersTotalT(OP_SELL)==0 && Ask>current_res && current_res!=last_res)
     {
     double _sl= MathAbs(current_res -get_res_sl())/_point;
      SendOrder(OP_SELL,_sl);
      last_res=current_res;
     }
   double _sup=get_sup();
   if(OrdersTotalT(OP_BUY)==0 && Bid<_sup && _sup!= last_sup)
     {
      double _sl = MathAbs(_sup -get_sup_sl())/_point;//
      SendOrder(OP_BUY,_sl);
      last_sup=_sup;
     }
 
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
 
Seng Joo Thio:

You just have to change the lines highlighted in green - assign _sl with a fixed value:

Thanks a lot for the answer will try to do that and can you show me an example please ?

I also want to change TP points and fix that as well.

 
William Roeder:
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor

Ok i changed it thanks.

 
Seng Joo Thio:

You just have to change the lines highlighted in green - assign _sl with a fixed value:

   if(IsTesting())get_indi();
   double current_res=get_res();
   if(OrdersTotalT(OP_SELL)==0 && Ask>current_res && current_res!=last_res)
     {
     double _sl= MathAbs(current_res -assign_sl())/200;
      SendOrder(OP_SELL,_sl);
      last_res=current_res;
     }
   double _sup=get_sup();
   if(OrdersTotalT(OP_BUY)==0 && Bid<_sup && _sup!= last_sup)
     {
      double _sl = MathAbs(_sup -assign_sl())/200;//
      SendOrder(OP_BUY,_sl);
      last_sup=_sup;
     }

I've edited like this but there is a function definition needed idk i that's correct.

 

Just made it thanks a lot for the help.


Seng Joo Thio

Reason: