last deal properties

Zoran Matosevic
23

so the problem is that my EA doesn't work even though there are no errors, if anyone knows what I did wrong and instructs or corrects me, I would be grateful.

#include <Trade/Trade.mqh>
void OnTick()
{
      MqlTradeRequest myrequest;
      MqlTradeResult myresult;
      ZeroMemory (myrequest);
      myrequest.action = TRADE_ACTION_DEAL;
      myrequest.type = ORDER_TYPE_BUY;
      myrequest.symbol = _Symbol;
      myrequest.volume = 0.1;
      myrequest.type_filling = ORDER_FILLING_IOC;
      myrequest.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      myrequest.tp = myrequest.price + 1;
      myrequest.sl = myrequest.price - 0.5;
      myrequest.deviation= 50;
      
      MqlTradeRequest myrequest1;
      MqlTradeResult myresult1;
      ZeroMemory (myrequest1);
      myrequest1.action = TRADE_ACTION_DEAL;
      myrequest1.type = ORDER_TYPE_SELL;
      myrequest1.symbol = _Symbol;
      myrequest1.volume = 0.1;
      myrequest1.type_filling = ORDER_FILLING_IOC;
      myrequest1.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      myrequest1.tp = myrequest1.price - 1;
      myrequest1.sl = myrequest1.price + 0.5;
      myrequest1.deviation= 50;
   
   //get the history data
      if(!HistorySelect(0,TimeCurrent()))
        {
         Print("__FUNCTION__ "+__FUNCTION__+", Failed to select History");
         return;
        }
   
      uint total = HistoryDealsTotal();
      ulong dticket = 0;
      datetime dtime = 0;
      string dsymbol;
      double dprice = 0;
      double dfee=0, dswap=0, dcommission=0;
      long dealentry;
      ulong dmagic = 0;
      long dtype=0;
      long reason=0;
      
   
      for(uint i=total-1; i>=0; i--) //start from the last history
        {
         dticket = HistoryDealGetTicket(i);
         if(dticket<=0)
            continue;
         //check the symbol
         dsymbol = HistoryDealGetString(dticket,DEAL_SYMBOL);
         if(dsymbol!=Symbol())
            continue;
         //check deal entry
         dealentry = HistoryDealGetInteger(dticket,DEAL_ENTRY);
         if(dealentry!=DEAL_ENTRY_OUT) //position closed
            continue;
         //deal type must be buy or sell
         dtype = HistoryDealGetInteger(dticket,DEAL_TYPE);
         if(dtype!=DEAL_TYPE_BUY && dtype!=DEAL_TYPE_SELL)
            continue;
         reason=HistoryDealGetInteger(dticket,DEAL_REASON);
         if(reason!=DEAL_REASON_TP && reason!=DEAL_REASON_SL)
            continue;
       
      
         if(PositionsTotal()<1)
            {
            if(dtype==DEAL_TYPE_BUY)
               {
               if(reason==DEAL_REASON_TP)
                  OrderSend(myrequest,myresult);
               if(reason==DEAL_REASON_SL)
                  OrderSend(myrequest1,myresult1);
               }
            if(dtype==DEAL_TYPE_SELL)
               {
               if(reason==DEAL_REASON_TP)
                  OrderSend(myrequest1,myresult1);
               if(reason==DEAL_REASON_SL)
                  OrderSend(myrequest,myresult);
               }
            }
      }       
}   

Vladimir Karputov
Moderator
305936

What's this?

   myrequest.tp = myrequest.price + 1 ;
   myrequest.sl = myrequest.price - 0.5 ;


Why are you asking for ALL history since the creation of the dinosaurs?

       if (! HistorySelect ( 0 , TimeCurrent ()))


Why are you looping through ALL trading history since the creation of the dinosaurs?

       for ( uint i=total- 1 ; i>= 0 ; i--) //start from the last history
Zoran Matosevic
23
Zoran Matosevic  
Vladimir Karputov # :

What's this?


Why are you asking for ALL history since the creation of the dinosaurs?


Why are you looping through ALL trading history since the creation of the dinosaurs?

it doesn't matter request.tp and .sl, that works, and for history I guess that's how it should be. it loops from last deal and takes last deal ticket. but last part of EA doesnt work

if(PositionsTotal()<1)
            {
            if(dtype==DEAL_TYPE_BUY)
               {
               if(reason==DEAL_REASON_TP)
                  OrderSend(myrequest,myresult);
               if(reason==DEAL_REASON_SL)
                  OrderSend(myrequest1,myresult1);
               }
            if(dtype==DEAL_TYPE_SELL)
               {
               if(reason==DEAL_REASON_TP)
                  OrderSend(myrequest1,myresult1);
               if(reason==DEAL_REASON_SL)
                  OrderSend(myrequest,myresult);
Vladimir Karputov
Moderator
305936

You should fix this:

   myrequest.tp = myrequest.price + 1 ;
   myrequest.sl = myrequest.price - 0.5 ;

It is not right.


You don't have to download the ENTIRE story! You must either download a limited story.

Zoran Matosevic
23
Zoran Matosevic  
Vladimir Karputov #:

You should fix this:

It is not right.


You don't have to download the ENTIRE story! You must either download a limited story.

sir, that part works fine, i dont have any problems with that. but that last part with lot of if statements does not work. can you instruct me there?
Vladimir Karputov
Moderator
305936
Zoran Matosevic # :
sir, that part works fine, i dont have any problems with that. but that last part with lot of if statements does not work. can you instruct me there?

You must correct the first mistake. After that, I will suggest the next step.

Zoran Matosevic
23
Zoran Matosevic  
Vladimir Karputov #:

You must correct the first mistake. After that, I will suggest the next step.

so what i should do with tp and sl?
Vladimir Karputov
Moderator
305936
Zoran Matosevic # :
so what i should do with tp and sl?

You should correct your mistake:

      myrequest1.tp = myrequest1.price - 1;
      myrequest1.sl = myrequest1.price + 0.5;
Zoran Matosevic
23
Zoran Matosevic  
Vladimir Karputov #:

You should correct your mistake:

how? what is wrong with that?
Vladimir Karputov
Moderator
305936
How to start with MQL5
How to start with MQL5
  • 2022.04.07
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Zoran Matosevic
23
Zoran Matosevic  

ok this is brilliant and just what i need. The only thing I don't understand is this:

 if ( MathRand ()< 32767 / 2 )

But I will research and I will learn. What interests me is whether I can modify it somehow and how to modify it to know if the closed position was buy or sell. If you can still explain it to me, I would be very grateful. Besides, this is brilliant. I want to learn to code like You.