StopLose and TakeProfit - page 2

remcous
543
remcous  
themasterx7:

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

bool  OrderClose( 
   int        ticket,      // ticket 
   double     lots,        // volume 
   double     price,       // close price 
   int        slippage,    // slippage 
   color      arrow_color  // color 
   );

OrderClose(buyticket,lotSize,Ask,3,Red); 


Just a suggestion : if your conditions to close order are the same than opening, order will be close immediatly after open in your code. It's not the opposite ?

like this :

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(buyticket,lotSize,Ask,3,Red);
     }


A last thing : when you open order, check the return of function to be sure your order is executed.

themasterx7
150
themasterx7  
remcous:


Just a suggestion : if your conditions to close order are the same than opening, order will be close immediatly after open in your code. It's not the opposite ?

like this :


A last thing : when you open order, check the return of function to be sure your order is executed.

Hi, yes sir I agree its just an example now its clear thank you .

themasterx7
150
themasterx7  
themasterx7:

Hi, yes sir I agree its just an example now its clear thank you .

HI again now it opens an order but doesn't close it I tried to return the value and same issue I don't know what's wrong with my OrderClose here is the code:


//+------------------------------------------------------------------+
//|                                                       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 = MODE_EMA;
input int fastMaPrice = PRICE_CLOSE;

input int slowMaShift = 0;
input int slowMaMode = MODE_EMA;
input int slowMaPrice = PRICE_CLOSE;

input double lotSize = 0.01;
input int stopLose = 100;
input int takeProfit = 500;
double pips;
bool ordersendresult;
int buyticket;//BUY
int sellticket;//SELL
int closeticket;//close
//+------------------------------------------------------------------+
//| 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;
////////////////////////////////////////////////////////////////////////////////////fast ma
  // 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);
///////////////////////////////////////////////////////////////////////////////////slow ma
   //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(OrdersTotal()==0)
    if(fastMaV2<slowMaV2&&fastMaV1>slowMaV1)

     {
      buyticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Green);
      
     }
     if(OrdersTotal()>0&&fastMaV2>slowMaV2&&fastMaV1<slowMaV1)//close
     {
     closeticket=OrderClose(buyticket,OrderLots(),Ask,3,Red);
     
     }
     
///////////////////////////////////////////////////////////////////////////SELL
    if(OrdersTotal()==0  )
    if(fastMaV2>slowMaV2&&fastMaV1<slowMaV1)
     {
      sellticket= OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,NULL,0,0,Red);      
     }
     
     if(OrdersTotal()>0&&fastMaV2<slowMaV2&&fastMaV1>slowMaV1)//close
     
     {
     closeticket=OrderClose(sellticket,OrderLots(),Ask,3,Red);
     
     }

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






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


   

William Roeder
23727
William Roeder  
    closeticket=OrderClose(buyticket,OrderLots(),Ask,3,Red);
  1. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. You can not use any Trade Functions until you first select an order.

themasterx7
150
themasterx7  
William Roeder:
  1. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. You can not use any Trade Functions until you first select an order.

hello, Its a lot of information thank you, can you give me a code example contains OrderClose function so I can fully get it and I'll work the rest of your steps after this one (I mean OrderClose) .

Keith Watford
Moderator
26609
Keith Watford  
themasterx7:

I have deleted your new topic concerning the same code.

Continue here, do not open new topics unnecessarily.

themasterx7
150
themasterx7  
Keith Watford:

I have deleted your new topic concerning the same code.

Continue here, do not open new topics unnecessarily.

It was a different code but ok :) 

themasterx7
150
themasterx7  

hello guys I need a fix to this code please, I researched a lot and couldn't find it . it cant close the trade don't know why? I need an example (I mean Code) or fix to this one .

Thank You.


//+------------------------------------------------------------------+
//|                                                       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 = MODE_EMA;
input int fastMaPrice = PRICE_CLOSE;

input int slowMaShift = 0;
input int slowMaMode = MODE_EMA;
input int slowMaPrice = PRICE_CLOSE;

input double lotSize = 0.01;
input int stopLose = 100;
input int takeProfit = 500;
double pips;
bool ordersendresult;
int buyticket;//BUY
int sellticket;//SELL
int closeticket;//close
int select;//select
int i;
//+------------------------------------------------------------------+
//| 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;
////////////////////////////////////////////////////////////////////////////////////fast ma
  // 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);
///////////////////////////////////////////////////////////////////////////////////slow ma
   //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(OrdersTotal()==0)
    if(fastMaV2<slowMaV2&&fastMaV1>slowMaV1)

     {
      buyticket= OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0,NULL,0,0,Green);
      
     }
     
     for(i=0;i<OrdersTotal();i++)
      { 
      select= OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      
       if(OrderType()==OP_BUY &&OrderSymbol()==Symbol())
       if(fastMaV2>slowMaV2&&fastMaV1<slowMaV1)
       {
            closeticket=OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
            
     
       }
       
       
      }
      
///////////////////////////////////////////////////////////////////////////SELL
   // if(OrdersTotal()==0  )
    //if(fastMaV2>slowMaV2&&fastMaV1<slowMaV1)
     //{
     // sellticket= OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,NULL,0,0,Red);      
     //}
     
     //if(OrdersTotal()>0&&fastMaV2<slowMaV2&&fastMaV1>slowMaV1)//close
     
     //{
     //closeticket=OrderClose(sellticket,OrderLots(),Ask,3,Red);
     
    // }

////////////////////////////////////////////////////////////////////////////////////////////BUY
}




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