stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Close Orders From Shell Auto
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4146
Page 1 of 1
Author:  DigitalCrypto [ Tue Mar 03, 2015 10:40 am ]
Post subject:  Close Orders From Shell Auto

Hey guys,

First of all I've not been coding in MQL for more than 2 weeks. So I am pretty sure I qualify as a newbie. Secondly I've been looking at example code all over the net and seeing great things but it wasn't until I google "Best EA template" that I came upon this fantastic website.

I've been a long time lurker but this is my first time posting because I wanted to be a little smarter before looking dumb or asking a dumb question, which this may very well be, but I am going ahead and asking it anyway since I've been searching the forum and trying for hours without success to close this stupid order.

I'm using the basic Shell Auto from Steve and all you great coders here (fantastic job!) And I've managed to create long positions based on my criteria to create a long entry with candle stops etc.

But for the life of me I cannot figure out how to close an open order where the trade is going against me.

The situation is simply this "If indicator indicates a long position take it. If indicator turns sour, close the open order"

I've been back and forth through the code and I recall seeing if a Long or Short was taken that it won't look for trade opportunities because it is in one. And naturally that wouldn't make any logical sense.

Also I tried following the CloseOrder on page 1 of the forum posting but I get an incorrect parameter count for CloseOrder();

On other EAs this is done like

Code: Select all

CloseOrder(OP_BUY);
And that works for them but Houston, we've got a problem and I can't wrap my head around it without a little bit of direction.

So this is what I basically have done:

Code: Select all

bool IsBuyingOp()
      {
      //Open Buy Trade Order
         if(((MACDM5>MACD_SIGM5) && (MACDM5>0)) && 
            ((rsi_m5>50) && (stoc_M5>40) && (cci_MM5>0)) && 
            (EMA_M5>ema_m5))
           {
            return(true);
           }
      
      } // End IsBuyingOp()
And in LookForTradeOpportunities()

Code: Select all

//Trading decision.
   bool SendLong=false,SendShort=false;

//Long trade

   if(IsBuyingOp()){SendLong=true;}

//End Long Trade
This works perfectly.

Now I have this condition:

Code: Select all

bool  IsCloseTrade()
      {
         //Rules for closing trades go here
         if(MACDM5<0)
            {
                (?) CloseOrder(OrderTicket());
            }
     return;
      }//End IsCloseTrade();
I know where I want to stick it but it doesn't compile in that location. Also it doesn't seem to throw any errors except that it can't find a ticket.

Could someone be kind enough to explain how to close an order without entering a new long or short position? I would be very greatful!

Best regards,
David
Author:  milanese [ Tue Mar 03, 2015 10:53 am ]
Post subject:  Close Orders From Shell Auto

read there---> http://docs.mql4.com/trading/orderclose , how to use the OrderClose function
a short code example from one of my EA's how to use the OrderClose:

Code: Select all

// to call the function is used:

CloseFriday(Symbol(),MagicNumber);

void CloseFriday(string strSymbol,int nMagic) 
  {
   int ticket=-1;
   if((DayOfWeek()==5) && (TimeHour(TimeCurrent())>=FridayCloseHour) && (TimeMinute(TimeCurrent())>=FridayCloseMinute)) 
     {

      for(int i=OrdersTotal()-1; i>=0; i--) 
        {
         if(!OrderSelect(i,SELECT_BY_POS)) continue;
         if(OrderMagicNumber()!=nMagic) continue;
         if(OrderSymbol()!=strSymbol) continue;
         if(OrderType()==OP_BUY) 
           {
            ticket=OrderTicket();

            if(AllowedSpread(strSymbol)==true)

              {
               while(IsTradeContextBusy()) Sleep(100);
               Alert("CloseFriday:Attempting to close ",strSymbol);
               Print("CloseFriday:Attempting to close ",strSymbol);
               if(OrderClose(ticket,OrderLots(),Bid,slippage,Red)) 
                 {
                  Alert("CloseFriday:Close ",strSymbol," Succeeded !");
                  Print("CloseFriday:Close ",strSymbol," Succeeded !");
                    } else {
                  Alert("CloseFriday:Close ",strSymbol," Failed !");
                  Print("CloseFriday:Close ",strSymbol," Failed !");
                 }
              }

           }
         if(OrderType()==OP_SELL) 
           {
            ticket=OrderTicket();

            if(AllowedSpread(strSymbol)==true)

              {
               while(IsTradeContextBusy()) Sleep(100);
               Alert("CloseFriday:Attempting to close ",strSymbol);
               Print("CloseFriday:Attempting to close ",strSymbol);
               if(OrderClose(ticket,OrderLots(),Ask,slippage,Red)) 
                 {
                  Alert("CloseFriday:Close ",strSymbol," Succeeded !");
                  Print("CloseFriday:Close ",strSymbol," Succeeded !");
                    } else {
                  Alert("CloseFriday:Close ",strSymbol," Failed !");
                  Print("CloseFriday:Close ",strSymbol," Failed !");
                 }
              }

           }
        }
     }

  }
Cheers :)

Tommaso
Author:  DigitalCrypto [ Tue Mar 03, 2015 11:17 am ]
Post subject:  Close Orders From Shell Auto

Hello Tommaso!

Thank you for the expert reply! I was able to close the order by RTFMing the official docs. I don't know why I wasn't getting it right there.

Code: Select all

OrderClose(OrderTicket(),Lot,Ask,MaxSlippagePips);
This is the close answer I was looking for on it. Would you also happen to know where to place the conditional test to see if it needs to be closed in the Shell Auto Trading Robot codebase?

bool LookForTradeClosure(int ticket)

Is the only thing I see but I don't know if I want to be monkeying around in code that already works. If it ain't broken I don't need to fix it. :)


Best regards,
David
Author:  milanese [ Tue Mar 03, 2015 11:33 am ]
Post subject:  Close Orders From Shell Auto

DigitalCrypto ยป Tue Mar 03, 2015 11:17 am wrote:Hello Tommaso!

Thank you for the expert reply! I was able to close the order by RTFMing the official docs. I don't know why I wasn't getting it right there.

Code: Select all

OrderClose(OrderTicket(),Lot,Ask,MaxSlippagePips);
This is the close answer I was looking for on it. Would you also happen to know where to place the conditional test to see if it needs to be closed in the Shell Auto Trading Robot codebase?

bool LookForTradeClosure(int ticket)

Is the only thing I see but I don't know if I want to be monkeying around in code that already works. If it ain't broken I don't need to fix it.


Best regards,
David
This you should call each tick so you embed in OnTick(), something like CheckOpenPositions(Symbol(),Magic):

Code: Select all

void CheckOpenPositions(string strSymbol,int nMagic)
 for(int i=OrdersTotal()-1; i>=0; i--) 
        {
         if(!OrderSelect(i,SELECT_BY_POS)) continue;
         if(OrderMagicNumber()!=nMagic) continue;
         if(OrderSymbol()!=strSymbol) continue;
         if(OrderType()==OP_BUY) 
           {
            ticket=OrderTicket();
// here your coditions to check for BUY 
if(tradeToClose==true){
        OrderClose(ticket,Lot,Bid,MaxSlippagePips);
}
}
   if(OrderType()==OP_SELL) 
           {
            ticket=OrderTicket();
// here your coditions to check for SELL
if(tradeToClose==true){
        OrderClose(ticket,Lot,Ask,MaxSlippagePips);
}
}


Cheers :)

Tommaso
Author:  DigitalCrypto [ Tue Mar 03, 2015 12:08 pm ]
Post subject:  Close Orders From Shell Auto

Tommaso,

I don't know how to thank you. I knew there was something simple I was missing. OnTick() was it.

I am still learning the MQL or CrapQL as Steve puts it. I have to smile at his frustration because I find myself frustrated sometimes too.

I will give it a go and see how I make out.

Again my deepest thanks!

Best regards,
David
All times are UTC Page 1 of 1