StopLose and TakeProfit

 
//+------------------------------------------------------------------+
//|                                                       test01.mq4 |
//|                                                    helmy gabriel |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "helmy gabriel"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int fastMa = 9;
input int slowMa = 21;
input int fastMaShift = 0;
input int fastMaMode = 0;
input int fastMaPrice = 0;
input int slowMaShift = 0;
input int slowMaMode = 0;
input int slowMaPrice = 0;
input double lotSize = 0.01;
input int stopLose = 40;
input int takeProfit = 100;
double pips;
bool ordersendresult;
int ticket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   double tickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(tickSize == 0.00001 || Point == 0.001)
      pips = tickSize*10;
   else
      pips = tickSize;

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   bool rst;

   //double fastMaV0 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,0);
   double fastMaV1 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,1);

   double fastMaV2 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,2);
   //double fastMaV3 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,3);
   //double fastMaV4 = iMA(NULL,0,fastMa,fastMaShift,fastMaMode,fastMaPrice,4);
/////////////////////////////////////////////////////////////////////////////
   //double slowMaV0 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,0);
   double slowMaV1 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,1);
   double slowMaV2 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,2);
   //double slowMaV3 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,3);
   //double slowMaV4 = iMA(NULL,0,slowMa,slowMaShift,slowMaMode,slowMaPrice,4);
   
///////////////////////////////////////////////////////////////////////////// BUY
    if(fastMaV2<slowMaV2&& fastMaV1>slowMaV1&& OrdersTotal()==0  )

     {
      ticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Green);
      if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
         rst=OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-(stopLose*Point),OrderOpenPrice()+(takeProfit*Point),0,CLR_NONE);
     }

         
          

////////////////////////////////////////////////////////////////////////////////////////////BUY






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

Hello guys, I need help with SL and TP I need the code to take profit at the next cross over not like points or pips if you need more explaining tell me .

THANK YOU. 

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
VR Watch list and Linker MT5 VR Watch list and Linker is an expert advisor for synchronous changes of financial instruments in open windows of the MetaTrader terminal. Choosing a trading instrument in the market overview, you will immediately get charts in the open terminal windows. Each window can be configured individually by you. ⚡With the...
 
themasterx7:

Hello guys, I need help with SL and TP I need the code to take profit at the next cross over not like points or pips if you need more explaining tell me .

THANK YOU. 

Hello,

Just a question : why do you do ordersend then ordermodify ?

int  OrderSend( 
   string   symbol,              // symbol 
   int      cmd,                 // operation 
   double   volume,              // volume 
   double   price,               // price 
   int      slippage,            // slippage 
   double   stoploss,            // stop loss 
   double   takeprofit,          // take profit 
   string   comment=NULL,        // comment 
   int      magic=0,             // magic number 
   datetime expiration=0,        // pending order expiration 
   color    arrow_color=clrNONE  // color 
   );

There is a particular reason ?


You have an example on MQL4 help :

//--- get minimum stop level 
   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
   Print("Minimum Stop Level=",minstoplevel," points"); 
   double price=Ask; 
//--- calculated SL and TP prices must be normalized 
   double stoploss=NormalizeDouble(Bid-minstoplevel*Point,Digits); 
   double takeprofit=NormalizeDouble(Bid+minstoplevel*Point,Digits); 
//--- place market order to buy 1 lot 
   int ticket=OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen); 
   if(ticket<0) 
     { 
      Print("OrderSend failed with error #",GetLastError()); 
     } 
   else 
      Print("OrderSend placed successfully"); 
//--- 
 

hi I'm not sure I get you sir all I need to modify my code instead of take profit of 100 pips I need it to take profit when an indicator do some kind of condition lets say when the 2 ma above cross over.

I know I cant explain it well its not my first language  :) so if you need me to clarify any information send me back.

 
themasterx7:

hi I'm not sure I get you sir all I need to modify my code instead of take profit of 100 pips I need it to take profit when an indicator do some kind of condition lets say when the 2 ma above cross over.

I know I cant explain it well its not my first language  :) so if you need me to clarify any information send me back.

I think (if i've not mistaken you) you have to add OrderClose function when the condition met.

 
Irwan Adnan:

I think (if i've not mistaken you) you have to add OrderClose function when the condition met.

Hi, I think you answered my question :)

 so I replace this code (code below) with OrderClose ? can you give me an example just to fully get it 


ticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Green);
      if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
         rst=OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-(stopLose*Point),OrderOpenPrice()+(takeProfit*Point),0,CLR_NONE);
 
themasterx7:

Hi, I think you answered my question :)

 so I replace this code (code below) with OrderClose ? can you give me an example just to fully get it 


    if(fastMaV2<slowMaV2&& fastMaV1>slowMaV1&& OrdersTotal()==0  )

     {
	if(!OrderClose(ticket,lots,Ask/Bid,slippage,color)return;


     }
 
Irwan Adnan:

I think its tricky, I think I need OrderOpen also to enter the trade then I use OrderClose is there any article I can study this part .


And Thank You Very Much Sir.

 
themasterx7:

I think its tricky, I think I need OrderOpen also to enter the trade then I use OrderClose is there any article I can study this part .


And Thank You Very Much Sir.

This book might be useful to understand mql4. There have many samples of programs.
book.mql4.com

MQL4 Tutorial
MQL4 Tutorial
  • book.mql4.com
Nowadays, a personal computer became indispensable for everybody. The rapid development of Internet and performance of modern computers opened up new vistas in many fields of human activities. As early as ten years ago, the financial market trade was available only for banks and for a limited community of specialists. Today, anybody can join...
 
Irwan Adnan:

Hello,


This code can't work, because you check OrdersTotal == 0 then you can't close an order if they are not open...

maybe like this :

if(fastMaV2<slowMaV2&& fastMaV1>slowMaV1&& OrdersTotal()==0  )

     {
        //openorder
     }

if(fastMaV2<slowMaV2&& fastMaV1>slowMaV1&& OrdersTotal()>0  ) //replace fastMaV2<slowMaV2&& fastMaV1>slowMaV1 by your conditions to close order

     {
        //closeorder
     }
 
remcous:

Hello,


This code can't work, because you check OrdersTotal == 0 then you can't close an order if they are not open...

maybe like this :

Thank You Sir really helped me now all I need to do is how to openorder and closeorder you saved me a lot of time thank you again :)

 
themasterx7:

Thank You Sir really helped me now all I need to do is how to openorder and closeorder you saved me a lot of time thank you again :)

Hi again why it open the trade but never close it ?

if(fastMaV2<slowMaV2&& fastMaV1>slowMaV1&& OrdersTotal()==0  )

     {
        buyticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Green); // assuming I identify the variable buyticket and closeticket 
     }

if(fastMaV2<slowMaV2&& fastMaV1>slowMaV1&& OrdersTotal()>0  ) 
     {
        closeticket=OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
     }
Reason: