An MQL4 function

 
Hello Developers

I'm stuck here, Can anyone help me with a function that works this Way?

"I want it to open just one position. If that position hits TP, then EA must open another trade in the same direction. But if trade hits SL, EA must open a trade in the other direction"
 
Fadhili Kwaka Kombo: Hello Developers. I'm stuck here, Can anyone help me with a function that works this Way? "I want it to open just one position. If that position hits TP, then EA must open another trade in the same direction. But if trade hits SL, EA must open a trade in the other direction"

That is not a function. That is a program. Each step needs to be considered and coded to to that.

If you want to improve your programming skill, then take some time to study CodeBase examples while referencing the MQL4 documentation, so that you can learn to define your steps and then code them to achieve your goal.

 
Fadhili Kwaka Kombo:
Hello Developers

I'm stuck here, Can anyone help me with a function that works this Way?

"I want it to open just one position. If that position hits TP, then EA must open another trade in the same direction. But if trade hits SL, EA must open a trade in the other direction"

Hello , consult this example . 

I am using an existing library for trading , but , you can tailor the system around it to your own trading functions with ease.

Everything is commented out .

If you hit an error check the location of the mqh 

Cheers  ☕️

#property copyright "Read the discussion"
#property link      "https://www.mql5.com/en/forum/440019"
#property version   "1.00"
#property strict
input double myLot=0.01;//the lot size 
input int myTp=300;//tp in points
input int mySl=300;//sl in points 
input int myAttempts=12;//attempts to open 
input uint myDelay=300;//delay between attempts in ms
input int myMagic=426;//magic number
input string myComment="";//comment 
input int mySlippage=1000;//max slippage in points

/* these are the basic inputs 
   what you need now is to remember the trade 
   and its direction.
   If the connection is lost and the ea restarts , to not lose track of it
   If mt4 crashes and reopens , to not lose track of it 
   And monitor what it hit .
   That is it .
   I'm assuming you have your own trading functions so i will import mine 
   as that is not the sticking point of your request
*/
#include <PlugAndPlay\trades\safeMarketTrade.mqh>
//now lets declare an ea folder a systen bane and an extention 
  string system_folder="TPSLSignal";
  string system_name="";//the system name is empty because it forms live
  string extention=".txt";
//we would create a class here or a structure but lets keep it simple 
  int myTicket=-1;//a ticket
  char myDirection=0;//a direction , -1 is SELL +1 is BUY 0 is nothing
//and you need 3 things 
  //1. to save these 2 things in a file 
    void saveState(string folder,string filename,string extention_with_dot){
         //you compose the location of the file 
           string location=folder+"\\"+filename+extention_with_dot;
         //you delete the file if it exists 
           if(FileIsExist(location)){FileDelete(location);}
         //you create the new file 
           int f=FileOpen(location,FILE_WRITE|FILE_BIN);
           //if the file can be created 
             if(f!=INVALID_HANDLE){
             //you write the ticket 
               FileWriteInteger(f,myTicket,INT_VALUE);//essentially write myTicket , which is an int so write an INT_VALUE
             //you write the direction
               FileWriteInteger(f,myDirection,CHAR_VALUE);//
             //and you close the file 
               FileClose(f);
             }
         }
   //1 out of 3 down 
   //2. you need to load these 2 things from a file 
     void loadState(string folder,string filename,string extention_with_dot){
          //same as above 
            string location=folder+"\\"+filename+extention_with_dot;
          //but now if the file exists you don't delete it but you open it 
            //first you reset your values though 
              myTicket=-1;
              myDirection=0;
            //if file exists - load it read values  
              if(FileIsExist(location)){
              int f=FileOpen(location,FILE_READ|FILE_BIN);
              if(f!=INVALID_HANDLE){
              //we want an integer , read an INT_VALUE , we know what we stored so we know what to expect
                myTicket=(int)FileReadInteger(f,INT_VALUE);
                myDirection=(char)FileReadInteger(f,CHAR_VALUE);
              FileClose(f);
              }
              }
          }
    //2 out of 3 and ...
    //3. monitor the open trade 
      void monitorState(){
             bool needs_save=false;//if true we save at the end 
             //first question , do we have a ticket ?
             if(myTicket!=-1){
             //we have a ticket lets check it , select it 
               if(OrderSelect(myTicket,SELECT_BY_TICKET)){
               //if the trade has closed 
                 if(OrderCloseTime()!=0){
                 //in tp
                   if(OrderProfit()>0.0){
                   //open same direction 
                     ENUM_ORDER_TYPE direction=OP_BUY;//this is the direction to send to the order
                     color order_color=clrAliceBlue;
                     //if the trade was a sell , change to sell 
                       if(myDirection==-1){direction=OP_SELL;order_color=clrPink;} 
                     //this structure is in the include you can ignore it 
                       trade_result trade=SafeMarketTrade(direction,myLot,mySl,false,myTp,false,false,1.5,myAttempts,myDelay,mySlippage,myMagic,myComment,order_color);                 
                     /*
                     but as stated below what you must do 
                     is upon opening of the new trade 
                     save the ticket 
                     and the new direction
                     you will adapt this to your own trading function
                     */
                     if(trade.success){
                     //so if the trade opened in other words 
                       myTicket=trade.ticket;
                       //and the myDirection does not change because its the same 
                       //and we light up the save indication
                       needs_save=true;
                     }
                     
                   }
                 //in sl
                   else{
                   //open opposite direction 
                     ENUM_ORDER_TYPE direction=OP_BUY;//this is the direction to send to the order
                     color order_color=clrAliceBlue;
                     //if the trade was a buy , change to sell because opposite 
                       if(myDirection==1){direction=OP_SELL;order_color=clrPink;} 
                     //this structure is in the include you can ignore it 
                       trade_result trade=SafeMarketTrade(direction,myLot,mySl,false,myTp,false,false,1.5,myAttempts,myDelay,mySlippage,myMagic,myComment,order_color);                 
                     if(trade.success){
                     //so if the trade opened
                       myTicket=trade.ticket;
                       //and the myDirection flips 
                       if(myDirection==-1){myDirection=1;}
                       else if(myDirection==1){myDirection=-1;}
                       //and we light up the save indication
                       needs_save=true;
                     } 
                   }
                 }
               }
             }
             //if we don't have a ticket in the first place 
             else{
             //random trade , i assume 
               int r=MathRand();
               //if its heads 
                 if(r>16000){
                 trade_result trade=SafeMarketTrade(OP_BUY,myLot,mySl,false,myTp,false,false,1.5,myAttempts,myDelay,mySlippage,myMagic,myComment,clrAliceBlue);                 
                 /* the trade_result structure is from the include you can 
                    ignore it if you have your own functions to trade but 
                    what is important here , which you will need to plug in to your system
                    is that if the trade succeeds you :
                    -store the ticket
                    -store the direction
                    -save the state 
                 */
                 if(trade.success){
                   myTicket=trade.ticket;
                   myDirection=1;//1 for buy
                   needs_save=true;//light up the save indication for these to be stored
                   }
                 }else{
                 trade_result trade=SafeMarketTrade(OP_SELL,myLot,mySl,false,myTp,false,false,1.5,myAttempts,myDelay,mySlippage,myMagic,myComment,clrPink);
                 if(trade.success){
                   myTicket=trade.ticket;
                   myDirection=-1;//-1 for sell
                   needs_save=true;//light up the save indication for these to be stored
                   }                 
                 }
             }//if we dont have a ticket in the first place ends here
           //if the save indication is on 
             if(needs_save){
             saveState(system_folder,system_name,extention);
             }
           }
//these are our functions , now lets connect the system to the meta trader
int OnInit()
  {
  /* on load what do we do ?
     first we compose the name of the system 
  */
  system_name=_Symbol+IntegerToString(_Period);
  //which is the name of the traded symbol and the timeframe 
  //then , we try to load .
  //if nothing exists the system resets anyway 
  //if something exists the system continues from where it left
    loadState(system_folder,system_name,extention);
  //done , on to the tick function

  return(INIT_SUCCEEDED);
  }
  
  void OnTick()
  {
  //what should we do here ? 
    //we call the monitor 
      monitorState();
    //that is it .
  }

void OnDeinit(const int reason)
  {  
  }
Files:
 
Fadhili Kwaka Kombo:
Hello Developers

I'm stuck here, Can anyone help me with a function that works this Way?

"I want it to open just one position. If that position hits TP, then EA must open another trade in the same direction. But if trade hits SL, EA must open a trade in the other direction"

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford #:

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Thank you Keith.
 
Lorentzos Roussos #:

Hello , consult this example . 

I am using an existing library for trading , but , you can tailor the system around it to your own trading functions with ease.

Everything is commented out .

If you hit an error check the location of the mqh 

Cheers  ☕️

Thank you Lorentzos.
 

You can find someone who will code this for you – since it’s like a normal strategy.

Or you can try do code it yourself – one idea is to place a trade with SL and TP and add pending orders (buylimit and selllimit) on its SL/TP levels. So, when TP is hit on Buy, buylimit will be automatically triggered.

Of course, you need to later remove remained pending order and open new pending orders on this new trades’ sl/tp. And so on