Forex Factory (https://www.forexfactory.com/)
-   Platform Tech (https://www.forexfactory.com/forum/69-platform-tech)
-   -   How to close order on BAR close? (https://www.forexfactory.com/thread/208733-how-to-close-order-on-bar-close)

LuboLabo Nov 28, 2009 5:18am | Post# 1

How to close order on BAR close?
 
I need help, I would like to close the order at close of the same bar of open position.
There's a way to do this?

Thanks.

Lubo

LuboLabo Nov 28, 2009 7:15am | Post# 2

Anyone?
 
Anyone?

Horrigan Nov 28, 2009 8:51am | Post# 3

Try do this on open next bar, for example, you opened position when new candle has lit, or when progresses, and you want to close at close of THE same bar or on open of new one. Use this part of code (the variables, their initalize and reset value you may solve, I hope. This is for 1hr charts, the daily has the similar logic


if ( pos_open==true && Hour() == OpenHour+1)

{
OrderClose(ticket,....,....,....,.... blah blah)
}

The bool var pos_open you may manage by OrderSelect and OrderCloseTime ==0, as true, else false
The var OpenHour you initialize at the IF body where you open the position
(Example:

if(pos_open==false && ......&&......&&..... ) //your entry rules
{
OrderSend(NULL, OP_BUY, Lots, Ask, 0, Bid-SL*Point, Bid+PT*Point);
OpenHour=Hour();
}

It's clear and understandable?

P.S. for Daily, or other TF you can use the similar logic, replace only Hour() Minute() or Day() - as new candle lights

LuboLabo Nov 28, 2009 10:47am | Post# 4

thanks
 
thanks for the code, and I think it's better >= instead ==
I have tried also this.
if(TimeCurrent() >= Time[0] + (Period() * 60 - 1)) ......Close Trade

But I still get orderclose error 138.....requote, I don't understand why..

Any ideas?

Lubo.

Horrigan Nov 28, 2009 11:39am | Post# 5

>= maybe better than ==, but the order you close only once, so this is irrelevant, what you wrote it... you may get orderclose error when you realize close by elseway (SL, PT hit, for example), or the EA is trying close position with wrong ticket number. Try re-check the orderSelect passage, try recompile without it, simply try attempt/error. I now work on another PC, where I have not some functionable EA, from what I can get you a code.

I had here only right OrderSelect, you may edit it for your needs


if(OrderSelect(position1,SELECT_BY_TICKET)==true)
{
OrderSelect(position1, SELECT_BY_TICKET);
if(OrderCloseTime() ==0)
{
total1=1;
}

else
{
total1=0;
}
}
else total1=0; //nikdy pozice 1 nebyla
if(OrderSelect(position2,SELECT_BY_TICKET)==true)
{
OrderSelect(position2,SELECT_BY_TICKET);
if(OrderCloseTime() ==0)
{
total2=1;
}
else
{
total2=0;
}
}
else total2=0;

EDIT: the re-quote you try slove by the "slippage" argument

LuboLabo Nov 28, 2009 2:43pm | Post# 6

>=
 
>= maybe better than ==, but the order you close only once, so this is irrelevant, what you wrote it... .....
I think is rilevant coz if the next bar is missed the position is not closed....

Horrigan Nov 29, 2009 4:18am | Post# 7

I think is rilevant coz if the next bar is missed the position is not closed....
Yes, I agree, this i relevanton M1 or similar charts, on higher TF (for example 1hr) you have usualy more than 500 tick, so the EA calls the start() function more than 500 times. We were disunderstood about TF that you play But, you're right, the operator more than or equal is more secure wiriting

Were you solve the problem with closing position??

Another (maybe stupid) idea: I had once upon a time some problem with similar signs as yours (OrderSend Error : (i think) 128) - I tried all what is possible - nothing, errors still occurs. I was get angry (very, very angry) the code, what and where it stayed I erased and entrie EA re-wrote. The code looks identically, but in old was some hidden error or problem. To current days I don't know where, but by this (drastic) procedure I solved it.

If you don't see some way, try return on start you way and procced step by step

Sorry, my English is horrible, but I hope you can understood it. (And I must do something with it )


© Forex Factory