Close order question

 

can someone please answer a rookie question... I want to close my open order at the close of an hourly candle, but am having a problem doing it.


eg : I open a trade and then when the hourly candle closes then close the trade no matter if in profit or loss...


I tried this : for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol // This didnt work for obvious reasons so i tried the code below which also didnt work.
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(TimeCurrent()-OrderOpenTime()>60)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}


and I tried this : for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(Volume[0]==1) // *** I though this would close the trade when the new hourly candle opens because Im running on a 1Hr chart --but this doesnt work***
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}


If any one can please tell me how to close open orders on a close of an hourly candle I will really appreciate it.

Thank you

 
23510 wrote >>

can someone please answer a rookie question... I want to close my open order at the close of an hourly candle, but am having a problem doing it.

eg : I open a trade and then when the hourly candle closes then close the trade no matter if in profit or loss...

You need to get the close of the candle, the only way that I know how to do that in MQL4 is to note the start of the next candle.

This routine will work:

int init()
  {

    Double PreviousBarTime = Time[0]; //sets the time for the first bar
   
//----
   return(0);
  }
                      ...................................................................
int start()
  {



if(NewBar()!= true){return(0);}           //returns to start if this is not the start of a new bar 
else CloseOrder(.................................)

   return(0);
   }

// user function defined-------------------------------------------------------------------------------------

bool NewBar()           // Function determines if this is a new bar, returns true it it is.
      {
      if(PreviousBarTime<Time[0])
      {PreviousBarTime = Time[0];          //resets time marker for current bar;
      return(true);
      }
      else
      return(false);
      }

The other effect of this routine is that you only process data on the close of each bar, so if you wanted to do something with ticks in the middle of the hour you would need some work around.

HTH Keith

 
kminler:

You need to get the close of the candle, the only way that I know how to do that in MQL4 is to note the start of the next candle.

This routine will work:

The other effect of this routine is that you only process data on the close of each bar, so if you wanted to do something with ticks in the middle of the hour you would need some work around.

HTH Keith

Thank you I will try it.

 
23510:

Thank you I will try it.

now I will show how rookie I am...I do not know what to put into the CloseOrder(........) that you used.


eg :


if(NewBar()!= true){return(0);}           //returns to start if this is not the start of a new bar 
else CloseOrder(.................................)

   return(0);
   }


I use a different style to close order like in my example of my 1st post...

so to make your style of Close Order work must I do something like this???


double CloseOrder()
{
if(OrderType()==OP_BUY)
{
CloseOrder = OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close buy position

else
{
if(OrderType()==OP_SELL)
{
CloseOrder = OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close sell position
return(0); // exit
}


even so I dont know how to use the CloseOrder.


I am sorry for stupid question but I want to understand.

thank you

 
23510 wrote >>

now I will show how rookie I am...I do not know what to put into the CloseOrder(........) that you used.

eg :

I use a different style to close order like in my example of my 1st post...

so to make your style of Close Order work must I do something like this???

double CloseOrder()
{
if(OrderType()==OP_BUY)
{
CloseOrder = OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close buy position

else
{
if(OrderType()==OP_SELL)
{
CloseOrder = OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close sell position
return(0); // exit
}

even so I dont know how to use the CloseOrder.

I am sorry for stupid question but I want to understand.

thank you

Actually your logic is sound, MQL4 is very convoluted and I'm just getting it sorted out myself

In order for your close routine to work you will need to select the order, to do that it is likely easiest to use the ticket number

eg when you enter the trade get ticket number

  int Ticket = OrderSend(Symbol(),OP_SELLSTOP,Lots,Entry,3,Stop,Takep); //depending on how you entered
then before your close routine add this line

OrderSelect(Ticket,SELECT_BY_TICKET);   // order now selected so your close routine should work

You should probably work through some of the examples in the documentation or code base understanding comes best from doing...I know

I'm still struggling

good luck

keith

 
kminler:

Actually your logic is sound, MQL4 is very convoluted and I'm just getting it sorted out myself

In order for your close routine to work you will need to select the order, to do that it is likely easiest to use the ticket number

eg when you enter the trade get ticket number

You should probably work through some of the examples in the documentation or code base understanding comes best from doing...I know

I'm still struggling

good luck

keith

Thanx Keith! I will definitely try what you have suggested...I am Glad my code is not altogether nonsensical and it does make some sense...

Some times I am amazed at what does not work in MQL4 for example to close the order I also tried this :


string Closetrade ="false";
if(Volume[0]==1)
{
Closetrade ="true";
return(0);
}

for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(Closetrade=="true")
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}


else // go to short position
{
if(OrderType()==OP_SELL)
{
// should it be closed?
if(Closetrade=="true")
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
}
}
}
}
}


I think it should work and can not see why it does not respond at all...it baffles me as I thought the logic was good.

I will have to keep trying maybe Ill figure it out one day and I will be as good a coder as the coder on this forum called "phy"...I WISH!

Cheers mate.


 

Hi can some please help me with the closeoeder function i cant get it to work 

 All im trying to do is to close the current open order using RSI


if(iRSI(NULL,0,14,PRICE_CLOSE,0)>75)
{
OrderClose(order_id,1,Ask,3,Red);
return(0);
}

 

my send function looks like this at the moment

 

//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
} else {
Print("Error opening SELL order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}

if (!EachTickMode) BarCount = Bars;

return(0);
 

Reason: