No trades are placed, but the code and journal are perfect.

 

Here is the code, I don't know why it doesn't want to place any trades at all.


I want to make an EA that is going to place Buy Stops only after the Sell Order hits SL or TP. Same for Sell Stops.


Thanks in advance.


Edit: I'm using MQL4.

   //inputs
   int MagicNumber = 123;
   int Slippage = 2;
   double LotSize = 0.10;

   //vars
   int BuyTicket;
   int SellTicket;

   //Condition for Short
   bool conditionS = High[2] > High[1] &&
                     High[2] > High[3];
   //Condition for Long
   bool conditionL = Low[2] < Low[1] &&
                     Low[2] < Low[3];
                     
   //Buying
   if(conditionL == true && BuyTicket == 0)
     {
      OrderSelect(SellTicket, SELECT_BY_TICKET);
      
      //Buy Order if there is no Sell Order
      if(OrderCloseTime() != 0 && SellTicket == 0)
        {
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = High[1];
         double StopLoss = Low[1];
         double TakeProfit = PendingPrice + (PendingPrice - StopLoss);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Buy Order
         BuyTicket = OrderSend(Symbol(), OP_BUYSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
        }
     }
   
   //Selling
   if(conditionS == true && SellTicket == 0)
     {
      OrderSelect(BuyTicket, SELECT_BY_TICKET);
      
      //Sell Order if there is no Buy Order
      if(OrderCloseTime() != 0 && BuyTicket == 0)
        {
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = Low[1];
         double StopLoss = High[1];
         double TakeProfit = PendingPrice - (StopLoss - PendingPrice);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Sell Order
         SellTicket = OrderSend(Symbol(), OP_SELLSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
        }
     }
  return;
  }
 
Shele:

Here is the code, I don't know why it doesn't want to place any trades at all.


I want to make an EA that is going to place Buy Stops only after the Sell Order hits SL or TP. Same for Sell Stops.


Thanks in advance.


Edit: I'm using MQL4.

Bit there is no function there OnTick() or something … 
 
  1. You posted a bunch of code, but without context. Is that code in a function?
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  2.       OrderSelect(SellTicket, SELECT_BY_TICKET);
          
          //Buy Order if there is no Sell Order
          if(OrderCloseTime() != 0 && SellTicket == 0)
    If SellTicket is zero, the select fails and your OrderCloseTime is bogus. Otherwise, your condition will never be true.

    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 (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

 

Sorry, felt like i haven't needed to put the whole code in to understand the issue... here is the whole code now.


By the way William - can you explain me a little better your 2. answer? I can't really understand it.


//+------------------------------------------------------------------+
//|                                             3BarPatternStrat.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Custom Functions.mqh>

//inputs
input int MagicNumber = 123;
input int Slippage = 2;
input double LotSize = 0.10;

//vars
int BuyTicket;
int SellTicket;

//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
void OnTick()
  {
   //Condition for Short
   bool conditionS = High[2] > High[1] &&
                     High[2] > High[3];
   //Condition for Long
   bool conditionL = Low[2] < Low[1] &&
                     Low[2] < Low[3];
                     
   //Buying
   if(conditionL == true && BuyTicket == 0)
     {
      OrderSelect(SellTicket, SELECT_BY_TICKET);
      
      //Buy Order if there is no Sell Order
      if(OrderCloseTime() != 0 && SellTicket == 0)
        {
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = High[1];
         double StopLoss = Low[1];
         double TakeProfit = PendingPrice + (PendingPrice - StopLoss);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Buy Order
         BuyTicket = OrderSend(Symbol(), OP_BUYSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
         
         SellTicket = 0;
        }
     }
   
   //Selling
   if(conditionS == true && SellTicket == 0)
     {
      OrderSelect(BuyTicket, SELECT_BY_TICKET);
      
      //Sell Order if there is no Buy Order
      if(OrderCloseTime() != 0 && BuyTicket == 0)
        {
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = Low[1];
         double StopLoss = High[1];
         double TakeProfit = PendingPrice - (StopLoss - PendingPrice);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Sell Order
         SellTicket = OrderSend(Symbol(), OP_SELLSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
         
         BuyTicket = 0;
        }
     }
  return;
  }
 
Shele #: can you explain me a little better your 2. answer? I can't really understand it.
Ticket
OrderSelect(SellTicket, SELECT_BY_TICKET);
if(OrderCloseTime() != 0 && SellTicket == 0)
0
fail
if bogus number != 0 && true is likely false
1 success if OCP != 0 && false is always false
 

I dont understand the idea of this :

OrderSelect(SellTicket, SELECT_BY_TICKET);
      
      //Buy Order if there is no Sell Order
      if(OrderCloseTime() != 0 && SellTicket == 0)

why do you need that selection...is not enough  this ?

if(conditionL == true && BuyTicket == 0 && SellTicket == 0)
     {
     
        
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = High[1];
         double StopLoss = Low[1];
         double TakeProfit = PendingPrice + (PendingPrice - StopLoss);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Buy Order
         BuyTicket = OrderSend(Symbol(), OP_BUYSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
         
         SellTicket = 0;
        }
     

and similar to Sell side...


Also ... why would you need to place a pending order with EA? Cause is not like EA has to sleep ... he can watch the market continuously... 😂

 

Thanks guys! Had a bunch of unnecessary code basically... that was the issue.


The code below is how my code looks rn.


Btw thanks Daniel Cioca for a tip for pending order, but can you explain me a little better how can I use market orders just?

Sorry I'm new to the programming...

My idea is that - when ask price gets above high[1] then place a buy market order at ask? Or something like that?


//+------------------------------------------------------------------+
//|                                             3BarPatternStrat.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Custom Functions.mqh>

//inputs
input int MagicNumber = 123;
input int Slippage = 2;
input double LotSize = 0.10;

//vars
int BuyTicket;
int SellTicket;

//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
void OnTick()
  {
   //Condition for Short
   bool conditionS = High[2] > High[1] &&
                     High[2] > High[3];
   //Condition for Long
   bool conditionL = Low[2] < Low[1] &&
                     Low[2] < Low[3];
                     
   //Buying
   if(conditionL == true && BuyTicket == 0)
     {
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = High[1];
         double StopLoss = Low[1];
         double TakeProfit = PendingPrice + (PendingPrice - StopLoss);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Buy Order
         BuyTicket = OrderSend(Symbol(), OP_BUYSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
         
         SellTicket = 0;
     }
   
   //Selling
   if(conditionS == true && SellTicket == 0)
     {
         //Pending Price, Stop Loss and Take Profit
         double PendingPrice = Low[1];
         double StopLoss = High[1];
         double TakeProfit = PendingPrice - (StopLoss - PendingPrice);
         datetime Expiration = iTime(Symbol(), Period(), 0) + PeriodSeconds() - 1;
         
         //Sell Order
         SellTicket = OrderSend(Symbol(), OP_SELLSTOP, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, Expiration);
         
         BuyTicket = 0;
     }
  return;
  }
 
Shele #:


My idea is that - when ask price gets above high[1] then place a buy market order at ask? Or something like that?


Yes...you set your conditions and when they are met you either buy or sell 

BuyTicket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, 0);

SellTicket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, StopLoss, TakeProfit, NULL, MagicNumber, 0);I would go for Close

I would go for Close[] though rather then High[]... High[] / Low[] might be a price spike and you don't want to open trades like that... but I think this what you did is for learning how code a EA which is operating some some basic conditions...which you will improve in time

 
I understand, thanks a lot for helping!
 
Shele #: My idea is that - when ask price gets above high[1] then place a buy market order at ask?
  1. There is no need to create pending orders in code.
    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
  2. So wait until the market exceeds the High[1] and open.
 
Yes, but pending orders do have an expiration date that I can set...
Reason: