QUICK partial closing programming question-----

 

int start()
{
RefreshRates();
if (OrdersTotal()>0) {OrderSelect(5555, SELECT_BY_TICKET);
if (Ask >= (OrderOpenPrice()+ (5 * Point)) && OrderLots() == 2 && (OrderType() == OP_BUY)) {
OrderClose(5555,1,Ask,10,Red); }
if (Bid <= (OrderOpenPrice()- (5 * Point)) && OrderLots() == 2 && (OrderType() == OP_SELL)) {
OrderClose(5555,1,Bid,10,Blue); }}

if (*******************) {
RefreshRates();
OrderSelect(5555,SELECT_BY_TICKET);
if (OrdersTotal() > 0 && OrderType()==OP_BUY) {OrderClose(5555, OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");}
Sleep(1500);
RefreshRates();
if (OrdersTotal() ==0) Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 10, 0, 0, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket<0)Print("OrderSend failed with error #",GetLastError());
}

I'm making VERY BASIC coding right now to get it 100% functional.

Still trying to learn obviously..

would this work for basically closing half (2 lots down to 1) of a position when the price hits 5 pips away from open price?

And what is wrong with the code for closing BUY and immediately opening a SELL?

Really doesn't seem to be working correctly.

THANKS SOOOOO MUCH for any information

 

Do you really have ticket #5555 ?

 
yeah i use that number for every EA order in this program..i'm over simplifying until this works correctly
 
spacechimp wrote >>
yeah i use that number for every EA order in this program..i'm over simplifying until this works correctly

With due respect to you, phy was asking you to think outside your current knowledge boundaries.

You do not understand about order tickets.

https://book.mql4.com/trading/orderclose
https://docs.mql4.com/trading/OrderSend

One keyword tied to trade orders is unique


"Still trying to learn obviously.." = sure.. understood, we all are - but you must read

Absolutely anyone can play about with code.

To understand requires some good old fashioned book work!

good luck...

 

ANY information is good info...

I'll go check out those links and try to get somewhere with my krappy kode.

THANKS!

 
spacechimp wrote >>

ANY information is good info...

I'll go check out those links and try to get somewhere with my krappy kode.

THANKS!

cool... again, I not being funny about it. Is just that one see many questions which could so easily be answered by 'hitting the books', know what I mean?

Is win win for the reader, yes? Learn about environment working in and enable self to code btr and... maybe not blow account so quick when "the beast" is unleashed :o)

"krappy kode" - no... not at all! You code and that is good. It is those that not even bother doing that but spend pages of posts using English when far better to "publish and be damned", cuz no body gonna make fun of posted code.


There are those on this site that are very talented bods and freely give of their time - a marvelous thing to behold imho.

 

ok, read those links and it still seems to me that this code should be closing 1 lot of a 2 lot order with magic number 5555.

RefreshRates();
if (OrdersTotal()>0) {OrderSelect(5555, SELECT_BY_TICKET);
if (Ask >= (OrderOpenPrice()+ (5 * Point)) && OrderLots() == 2 && (OrderType() == OP_BUY)) {
OrderClose(5555,1,Ask,10,Red); }
if (Bid <= (OrderOpenPrice()- (5 * Point)) && OrderLots() == 2 && (OrderType() == OP_SELL)) {
OrderClose(5555,1,Bid,10,Blue); }}

and then the second section of code should close a buy with magic number 5555 and immediately open a sell if the conditions are true.

if (*******************) {
RefreshRates();
OrderSelect(5555,SELECT_BY_TICKET);
if (OrdersTotal() > 0 && OrderType()==OP_BUY) {OrderClose(5555, OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");}
Sleep(1500);
RefreshRates();
if (OrdersTotal() ==0) Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 10, 0, 0, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket<0)Print("OrderSend failed with error #",GetLastError());
}

I'm still baffled but remember, I'm not a programmer. I generally try to take samples from other EA's

 

Ok...

Generally speaking, people search through the open orders...

for(int i = OrdersTotal()-1; i >= 0; i--){

... and select each one in turn (if there are more than one)...

... you select an order then you can collect information about it...

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

... and look at it for some property they are interested in...

if(OrderSymbol() == Symbol()) .... the selected order pair is the same as the charted pair, or....

if(OrderStoploss() == 0) ... the selected order has no stop loss! might want to fix that...

.

Rarely do people keep track of specific order numbers, because a partial close will change the order number, a new ticket is generated at the server...

And they NEVER use a constant like 5555 for the ticket number because you can't know in advance what an order number will be.

.

There are plenty of examples floating around. None that work look like yours.

 

OH Krap...i was blinded.

Actually i was making it WAYY over simplified just for testing. NO other trades are being taken on this demo account. So i figured I could skip the orderselect with the Loop....

BUT I was mixing TICKET number up with MAGIC number?

I can add the order select with the for i loop later but just wanted to get the core stripped down code working and build on it.

I think if I fix that, I should be on my way

THANKS a ton