Buy pending order problem

 

Hi all 

My EA will not activate a buy pending order, now I'm assuming the problem might be with the Open predefined variable, if you know the problem please assist.

#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>

CTrade trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
COrderInfo m_order;
CAccountInfo m_account;

input double TakeProfit=15;
input double StopLoss=5;
input double Risk=0.01;
input int PeriodIndicator=7;
input double AmountPrice=1;

int MagicNumber=567855;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
  ResetLastError();
  
  if(!m_symbol.Name(Symbol()))
  {
   return(INIT_FAILED);
  }
   
  trade.SetExpertMagicNumber(MagicNumber);
  trade.SetMarginMode();
  trade.SetTypeFillingBySymbol(m_symbol.Name());
  
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountSymbol(string Pair)
 {
  int NoSymbol=0;
  
  for(int a=PositionsTotal()-1;a>=0;a--)
  {
   if(m_position.SelectByIndex(a))
   if(m_position.Symbol()==Pair)
   NoSymbol++;
  }
  
  return(NoSymbol);
 }
 
int CountPending()
 {
  int Pending=0;
  for(int c=OrdersTotal()-1;c>=0;c--)
  {
   bool Select=OrderSelect(OrderGetTicket(c));
   string OrderSymbol=OrderGetString(ORDER_SYMBOL);
   if(OrderSymbol==Symbol())
   {
    ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
    if(Type==ORDER_TYPE_BUY_STOP||Type==ORDER_TYPE_SELL_STOP)
    Pending++;
   }
  }
  return(Pending);
 }
double Pips()
 {
  double PipPoint=0;
  int Digit=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
  if(Digit==1||Digit==3||Digit==5){PipPoint=Point()*10;}
  return(PipPoint);
 }
 
void BuyPending()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  double TPBuy=Ask+TakeProfit*Pips();
  double SLBuy=Ask-StopLoss*Pips();
  
  double Range=iHigh(Symbol(),0,0)-iLow(Symbol(),0,0);
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Filter=2;
  
  double MAClose=iMA(Symbol(),0,PeriodIndicator,0,MODE_LWMA,PRICE_CLOSE);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

  if(CountSymbol(Symbol())==0)
  {
   if(Range>Filter&&Candle[0].open>MAClose&&Bid>MAClose)
   {
    trade.BuyStop(0.01,Ask+AmountPrice*Pips(),Symbol(),SLBuy,TPBuy,ORDER_TIME_SPECIFIED,Expire,"2023");
   }
  }
  
 }
 
void SellPending()
 {
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  double TPSell=Bid-TakeProfit*Pips();
  double SLSell=Bid+StopLoss*Pips();
  
  double Range=iHigh(Symbol(),0,0)-iLow(Symbol(),0,0);
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Filter=0;
  
  double MAClose=iMA(Symbol(),0,PeriodIndicator,0,MODE_LWMA,PRICE_CLOSE);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

  if(CountSymbol(Symbol())==0)
  {
   if(Range>Filter&&Candle[0].open<MAClose&&Bid<MAClose)
   {
    trade.SellStop(0.01,Bid-AmountPrice*Pips(),Symbol(),SLSell,TPSell,ORDER_TIME_SPECIFIED,Expire,"2023");
   }
  }
  
 }    

void OnTick()
 {
  trade.SetExpertMagicNumber(MagicNumber);
 
  if(CountPending()<1){BuyPending();}
   
  if(CountPending()<1){SellPending();}
 }
 
Scalper8:

Hi all 

My EA will not activate a buy pending order, now I'm assuming the problem might be with the Open predefined variable, if you know the problem please assist.

How about checking your logs? And maybe you can add code to print out all relevant values, so you can check what's going wrong?

Also, maybe you can use the debugger on solving this problem.


 
Dominik Egert #:
How about checking your logs? And maybe you can add code to print out all relevant values, so you can check what's going wrong?

Also, maybe you can use the debugger on solving this problem.


I think the problem might be with the if statements 

if(Range>Filter&&Candle[0].open>MAClose&&Bid>MAClose)
if(Range>Filter&&Candle[0].open<MAClose&&Bid<MAClose)

I don't receive any errors when checking my logs and I'll make sure to use debugger for this problem

 

Made changes to the "iMA" from double to int still no changes 

double MAClose=iMA(Symbol(),0,PeriodIndicator,0,MODE_LWMA,PRICE_CLOSE);
int MAClose=iMA(Symbol(),0,PeriodIndicator,0,MODE_LWMA,PRICE_CLOSE);
 
Scalper8 #:

Made changes to the "iMA" from double to int still no changes 

ChatGPT....

Please learn how to code, its useless to try to fix ChatGPT-Code, its gibberish, and nobody wants to fix it...

First of all, for what language is it? MQL4 or MQL5?

Then, if you place your cursor on a function and press F1, you receive help by reading the documentation.

 
Dominik Egert #:

ChatGPT....

Please learn how to code, its useless to try to fix ChatGPT-Code, its gibberish, and nobody wants to fix it...

First of all, for what language is it? MQL4 or MQL5?

Then, if you place your cursor on a function and press F1, you receive help by reading the documentation.

It's not ChatGPT Dominik I've been coding since last year MQL4 & MQL5. The code I posted is MQL5 you can check on the first post.

 
Scalper8 #:

It's not ChatGPT Dominik I've been coding since last year MQL4 & MQL5. The code I posted is MQL5 you can check on the first post.

Really ?

Please explain us that line of code in MQL5 :

  double MAClose=iMA(Symbol(),0,PeriodIndicator,0,MODE_LWMA,PRICE_CLOSE);
 
Scalper8 #:

It's not ChatGPT Dominik I've been coding since last year MQL4 & MQL5. The code I posted is MQL5 you can check on the first post.

Could you please tell me what hint I should take to make out it's mql5 code?

I see multiple mql4 indications, but not one clear mql5 indication.

And the formatting of the code is quite typical for chatGPT, btw. The only I know who has blank lines between variable declarations, and else is formated like with a styler....

So to me it looks very much like chatGPT.

Anyways, if you are coding mql4 and mql5 as well, then you should know what warnings the code has given you, and why.

I would like to ask you to fix these, come back and show your fixed code. Then we can talk about problems you are having with it.
 
Alain Verleyen #:

Really ?

Please explain us that line of code in MQL5 :

That line is stylish, it supports multiple color modes, and also gives you cool yellow signs when compiling... And it also works in mql4, or was it mql5??? I don't remember... But in one of them it will give you a nice indication...


 
Alain Verleyen #:

Really ?

Please explain us that line of code in MQL5 :

iMA is moving average indicator, symbol means it can run on any pair you drop it on e.g. FX, Indices, Commodities & stocks etc. The 0 (current period) is the indication of the timeframe, Period Indicator is a period of the indicator which 7, the next 0 is MA shift, the next one is the mode of the MA there's 4 of them I choose the LWMA for my strategy, the last one is MA price calculations which is  price close. 

Why is so hard to believe when you can see that its not ChatGPT. 

 
 Ot complete ChatGPT, he changed the magic number
Reason: