Replacing Pending Order and Closing all Position at the end of the day

 

I want my EA to place a pending order at the prices (Hoch- und Tiefpreis as prices) and to replace the pending order, when on of them was filled and closed by the tp or sl. Unfortunately my EA  wont to this. Furthermore I want to close all open postions at the end of the day, but it wont close the position, causing trades to go on for several days. Thank you for your help :D


input color           InpColor=clrRed;     // Linienfarbe
input ENUM_LINE_STYLE InpStyle=STYLE_DASH; // Linienstil
input int             InpWidth=2;          // Linienbreite
input int             Periode=PERIOD_M5;
input datetime        anfangszeit = D'10:00';
input datetime        endzeit     = D'11:00';
input datetime        tagesende   = D'21:58';
input int             Lot=2;


#include <ChartObjects\ChartObjectsLines.mqh>
#include <Trade\Trade.mqh>
CChartObjectHLine high_line, low_line;

CTrade trade;

void CloseAllPositions()
{
      for (int i=PositionsTotal()-1; i>=0; i--)
          {int ticket=PositionGetTicket(i);
         trade.PositionClose(i);}
}


int OnInit()
{

 
   high_line.Create(0, "high", 0, 0.0);   
   high_line.Color(InpColor);
   high_line.Style(InpStyle);
   high_line.Width(InpWidth);
   low_line.Create(0, "low", 0, 0.0);   
   low_line.Color(InpColor);
   low_line.Style(InpStyle);
   low_line.Width(InpWidth);

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+

void OnTick()
{

MqlTradeRequest request;
MqlTradeResult  result;
MqlTradeRequest request1;
MqlTradeResult  result1;
MqlTradeCheckResult checkresult;



  
   
   string Time1= TimeToString(anfangszeit,TIME_MINUTES);
   string Time2= TimeToString(endzeit,TIME_MINUTES);
   string Time3= TimeToString(TimeLocal(),TIME_MINUTES);
   string Time4= TimeToString(tagesende, TIME_MINUTES);

if (Time1<Time3 && Time3<Time2 ) 
{
   int BarIndex1=iBarShift(_Symbol,PERIOD_H1,anfangszeit,false);
   int BarIndex2=iBarShift(_Symbol,PERIOD_H1,endzeit,false);
   double Hoch=iHigh(_Symbol,PERIOD_H1,BarIndex2);
   double Tief=iLow(_Symbol,PERIOD_H1,BarIndex2);
   high_line.Price(0,Hoch);
   low_line.Price(0,Tief);
   double HochPreis=high_line.Price(0,Hoch);
   double TiefPreis=low_line.Price(0,Tief);
   
 };

if(Time3>Time2 && OrdersTotal()==0 && PositionsTotal()==0)

{    
         double HochPreis =ObjectGetDouble(0,"high",OBJPROP_PRICE,0);
         Print("HochPreis" , HochPreis);
         double TiefPreis =ObjectGetDouble(0,"low",OBJPROP_PRICE,0);
         Print("TiefPreis" , TiefPreis);         
         ZeroMemory(request);
         ZeroMemory(request1);
         Print("HochPreis" ,HochPreis);
         Print("TiefPreis" , TiefPreis);
         double Range=HochPreis-TiefPreis;
         request.action = TRADE_ACTION_PENDING;
         request.type = ORDER_TYPE_BUY_STOP;
         request.symbol = _Symbol;
         request.volume = Lot;
         request.type_filling = ORDER_FILLING_FOK;
         request.price = HochPreis;
         request.sl = TiefPreis;
         request.tp = HochPreis+Range;
         request.type_time = ORDER_TIME_DAY;
         request.deviation = 50;
         request.stoplimit = 0;

      if (!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());
      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
      
      if (SymbolInfoDouble(_Symbol,SYMBOL_BID)>HochPreis && result.retcode == 4756)
      request.action=TRADE_ACTION_DEAL;    
      request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); 
   

        
         
         
         request1.action = TRADE_ACTION_PENDING;
         request1.type = ORDER_TYPE_SELL_STOP;
         request1.symbol = _Symbol;
         request1.volume = Lot;
         request1.type_filling = ORDER_FILLING_FOK;
         request1.price = TiefPreis;
         request1.sl = HochPreis;
         request1.tp = TiefPreis-Range;
         request1.type_time = ORDER_TIME_DAY;
         request1.deviation = 50;
         request1.stoplimit = 0;   
 
      if (!OrderSend(request1,result1))
      PrintFormat("OrderSend error %d",GetLastError());
      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result1.retcode,result1.deal,result1.order);
      
      if (SymbolInfoDouble(_Symbol,SYMBOL_BID)<TiefPreis && result1.retcode == 4756)
      request1.action=TRADE_ACTION_DEAL; 
      request1.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);    


}
if(Time3>Time4)
{
CloseAllPositions();
};
};
 

 

About closing positions :

void CloseAllPositions()
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      ulong ticket=PositionGetTicket(i);
      trade.PositionClose(ticket);
     }
  }

No idea what you are trying to do beside that.

 
//+------------------------------------------------------------------+
//|                                          FirstTrade_MQL5_ABG.mq5 |
//|                                  Copyright © 2018, Anwar Goulouh |
//|                                         compstuffcrazy@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Anwar Goulouh"
#property link      "compstuffcrazy@gmail.com"
#property version   "1.000"
//---
#include <Trade\Trade.mqh>

void OnTick()
  {

   double   AccBal   =  AccountInfoDouble(ACCOUNT_BALANCE);
   double   AccProf  =  AccountInfoDouble(ACCOUNT_PROFIT);
   double   AccEq    =  AccountInfoDouble(ACCOUNT_EQUITY);
   
   Comment("Account Balance: ", AccBal, "/n", "Account Profit: " ,AccProf, "/n", "Account Equity: ", AccEq, "/n");
   
   MqlTradeRequest   MyReq;
   MqlTradeResult    MyRes;
   ZeroMemory        (MyReq);
   MyReq.action         =  TRADE_ACTION_DEAL;
   MyReq.type           =  ORDER_TYPE_BUY;
   MyReq.symbol         =  _Symbol;
   MyReq.volume         =  0.01;
   MyReq.type_filling   =  ORDER_FILLING_FOK;
   MyReq.price          =  SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   MyReq.tp             =  0;
   MyReq.deviation      =  50;
   
   Comment("First Auto Trade");

   if(!PositionSelect(_Symbol)) // If Open position (!) Does not exist.
     {
      OrderSend(MyReq,MyRes);
     }
   
   
   if(AccEq >= AccBal+2)
     {
      CloseAllOrders();
     }
   
  }
   
CTrade trade;

void CloseAllOrders()
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      ulong ticket=PositionGetTicket(i);
      trade.PositionClose(ticket);
     }
  }
//
//void CloseAllOrders()
//{
//  
//  int i=PositionsTotal()-1;
//  while (i>=0)
//      {
//      if(trade.PositionClose(PositionGetSymbol())) i--;
//      }
//  
//}



   


HI,


Having similar issues attempting to follow tutorial...

https://www.youtube.com/watch?v=JClTPth8aXA&list=PLV8YK-9p3TcM1gb106qDgzvwPExB5Fxra&index=8


Compiled Fine except for close all orders function..

Followed your suggestion @Alain Verleyen 

A bit better. Now only get warning.

return value of 'OrderSend' should be checked |   FirstTrade_MQL5_ABG.mq5 37 7

Any help would be appreciated honestly I am finding getting into MQL5 horrendous especially debugging and the tutorials? Do I have to pay for correct/professional training manuals or something?


Thanks Mate Peace From NZ

 
Yarly Yarls:

HI,


Having similar issues attempting to follow tutorial...

https://www.youtube.com/watch?v=JClTPth8aXA&list=PLV8YK-9p3TcM1gb106qDgzvwPExB5Fxra&index=8


Compiled Fine except for close all orders function..

Followed your suggestion @Alain Verleyen 

A bit better. Now only get warning.

return value of 'OrderSend' should be checked |   FirstTrade_MQL5_ABG.mq5 37 7

Any help would be appreciated honestly I am finding getting into MQL5 horrendous especially debugging and the tutorials? Do I have to pay for correct/professional training manuals or something?


Thanks Mate Peace From NZ

that warning simply means you should check whether the SendOrder() has done the supposed operations successfully or not.
it should return true, and returned code from server should be 10009, which means it's done successfully.
you can read about all these functions in documentation : Trade Functions section.
 

Although the tutorial does not ever make a profit in the strategy tester? and because the tutorial was not correctly explained, im not sure where, how and why it is supposed to be making money? Any links to better tuts?

\\\thnx

 
Code2219 or probably 2319:
that warning simply means you should check whether the SendOrder() has done the supposed operations successfully or not.
it should return true, and returned code from server should be 10009, which means it's done successfully.
you can read about all these functions in documentation : Trade Functions section.

thnx reading now

 

Like this for the checking just me trying...

This is edited with two errors

'oChkRes' - undeclared identifier | FirstTrade_MQL5_ABG.mq5 44 8

'retcode' - struct or class type expected | FirstTrade_MQL5_ABG.mq5 44 16



//---Added This   MqlTradeCheckResult  MyChkRes;
//---Below  These MqlTradeRequest      MyReq;
//---             MqlTradeResult       MyRes;

if(!PositionSelect(_Symbol))
     {
     
     bool oChkRes = (OrderCheck(MyReq,MyChkRes));
     }
   
   
   if (oChkRes.retcode != 10009)
   {
   
      if(!PositionSelect(_Symbol)) // If Open position (!) Does not exist.
        {
         OrderSend(MyReq,MyRes);
        }
   }
   
 
Sorry should have tried to compile before commenting
 
read any code you can find related to trading and try to learn from them. (doc examples, source code of other EAs)
AND again read the docs to learn what is the reason/meaning of each error code and warnings during compilation.

you can fix the issues , or if not, ask someone to do so (pay them)