Inability to close trade using EA

 

Good day all. I am fairly new to MQL5. I attempted writing an EA code based on the stochastic indicator but the trades are not closing even when the conditions are met. kindly help me debug.  the trade is supposed to close when there is a reverse cross of the indicator opposite to the one that created the signal. this is the code:

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade/Trade.mqh>

ulong posTicket;

int handle;
int totalBars;
input double Lots = 0.1;

input double StochUpperBound = 80;
input double StochLowerBound = 20;

CTrade trade;

int OnInit()
  {totalBars = iBars(_Symbol,PERIOD_CURRENT);

 handle = iStochastic(_Symbol,PERIOD_CURRENT,24,7,7,MODE_SMA,STO_LOWHIGH);

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {int bars = iBars(_Symbol,PERIOD_CURRENT);
if (totalBars != bars){
   totalBars = bars;
   
   double stoch[];
   double signal[];
   CopyBuffer(handle,0,1,2,stoch);
   CopyBuffer(handle,1,1,2,signal);  
   
   if(stoch[1]>signal[1] && stoch[0]<signal[0]){
   Print("Buy crossover");
   if(stoch[1]<= StochLowerBound || signal[1]<= StochLowerBound){
      Print("Buy signal");
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      ask = NormalizeDouble(ask,_Digits);

     trade.Buy(Lots,_Symbol,ask);

      if(stoch[1]<signal[1] && stoch[0]>signal[0]){
      if (posTicket > 0 && PositionSelectByTicket (posTicket)){
       
         trade.PositionClose(posTicket);
         posTicket = 0;
         
         }
         }
 }}}   
  }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.05.25
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
When you post code please use the Code button (Alt+S) !

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

...

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Sergey Golubev #:
When you post code please use the Code button (Alt+S) !

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Hover your mouse over your post and select "edit" ... 

...

Thanks for the correction. I have done that.
 
copia #: Thanks for the correction. I have done that.

No you did not! You created a new post instead of editing the first one.

So, I've corrected that now, but please do it properly as instructed next time.

Also, please don't create topics randomly in any section. It has been moved to the section: Expert Advisors and Automated Trading

 
Fernando Carreiro #:

No you did not! You created a new post instead of editing the first one.

So, I've corrected that now, but please do it properly as instructed next time.

Also, please don't create topics randomly in any section. It has been moved to the section: Expert Advisors and Automated Trading


ok. Thanks.

 

Is the logic here is :

always open a new pos when  "Buy crossover" and "Buy signal" happens

and ,close pos when both happens also " stoch[1]<signal[1] && stoch[0]>signal[0] "   ?

 
Mage He #:

Is the logic here is :

always open a new pos when  "Buy crossover" and "Buy signal" happens

and ,close pos when both happens also " stoch[1]<signal[1] && stoch[0]>signal[0] "   ?


The logic is to buy when the signal line crosses above the stochastic line below the 20 level. The position is to be closed when signal line crosses below the stochastic line.

Please help me. The trade opens as is supposed to but the trades do not close. What do I do?

 
copia #:

The logic is to buy when the signal line crosses above the stochastic line below the 20 level. The position is to be closed when signal line crosses below the stochastic line.

Please help me. The trade opens as is supposed to but the trades do not close. What do I do?


Hi I've never used Ctrade as I like to make my own functions & classes instead of importing them. This way you can have complete control over what your code is doing.

In mql5, trades are opened and closed using the OrderSend() function. This function takes a while to get your head around. Opening and closing trades is not as simple as you might think. There is a lot of stuff going on. 

However! In the documentation there is lots of example code. If you copy and paste the entire examples, they will work, and that is usually a good starting point.

Have a good study of this:
https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
J Sky #:


Hi I've never used Ctrade as I like to make my own functions & classes instead of importing them. This way you can have complete control over what your code is doing.

In mql5, trades are opened and closed using the OrderSend() function. This function takes a while to get your head around. Opening and closing trades is not as simple as you might think. There is a lot of stuff going on. 

However! In the documentation there is lots of example code. If you copy and paste the entire examples, they will work, and that is usually a good starting point.

Have a good study of this:
https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Thanks. I will go right through them.
Reason: