- Why did you post your MT4 question in the Root / MT5 EA
section instead of the MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. -
if(OrderSelect(ord2,SELECT_BY_TICKET,MODE_HISTORY)==true ){ //check if order 2 is closed by tkprofit, because doesn't have stoploss
Mode is irrevalent when you select by ticket. You need to select and check OrderCloseTime. -
int ord1 = OrderSend(_Symbol,OP_BUY,0.1,Ask,3,0,0,NULL,0,0,Green); int ord2 = OrderSend(_Symbol,OP_SELL,0.2,Bid,3,0,Bid-20*_Point,NULL,0,0,Red);
You've lost the ticket numbers once you return from your function.
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required. -
Never risk more than a small percentage of your
account, certainly less than 2% per trade, 6% total to the account. Risk depends on your initial stop loss, lot size, and the value of the
pair. It does not depend on margin and leverage. No SL means you
have infinite risk.
- You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
- Do NOT use TickValue by itself - DeltaPerLot
and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or
whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19 - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
if(OrderSelect(ord2,SELECT_BY_TICKET,MODE_HISTORY)==true ){ //check if order 2 is closed by tkprofit, because doesn't have stoploss
This will not check if an order is closed.
Please read the documentation for OrderSelect()
Note
The pool parameter is ignored if the order is selected by the ticket number. The ticket number is a unique order identifier.
You should check OrderCloseTime()
Why do you want to check if an order is closed immediately after opening it?
This will not check if an order is closed.
Please read the documentation for OrderSelect()
Note
The pool parameter is ignored if the order is selected by the ticket number. The ticket number is a unique order identifier.
You should check OrderCloseTime()
Why do you want to check if an order is closed immediately after opening it?
So, i want to check not immediately, but check when take profit and if has a takeprofit, close all. I was using this code to check. Do you have any sugestion?
Thanks bro! I tryed to use global variable with flush, but without success. could you give me a little example of how to use?
- Why did you post your MT4 question in the Root / MT5 EA
section instead of the MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - Mode is irrevalent when you select by ticket. You need to select and check OrderCloseTime.
- You've lost the ticket numbers once you return from your function.
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required. - Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account. Risk depends on your
initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No
SL means you have infinite risk.
- You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
- Do NOT use TickValue by itself - DeltaPerLot
and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or
whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19 - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
I have deleted your other topic and banned your other user name.
Why open another topic to discuss the same code. Please continue here.
This is the code that you posted.
void OnTick() int ord1,ord2 ord1 = OrderSend(_Symbol,OP_BUY,0.1,Ask,3,0,0,NULL,001,0,Green); //abre ordem de compra ord2 = OrderSend(_Symbol,OP_SELL,0.2,Bid,3,0,Bid-20*_Point,NULL,0,0,Red); //abre ordem de venda datetime ctm=OrderCloseTime(); if(OrderSelect(ord2,SELECT_BY_POS,MODE_HISTORY)==true ){ // if(ctm>0){ if(OrderSelect(ord1,SELECT_BY_POS,MODE_TRADES)==true){ OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Pink); } } } }
Why do you want to check if an order is closed immediately after opening it?
datetime ctm=OrderCloseTime();
Where have you selected an order before calling OrderCloseTime()?
if(OrderSelect(ord2,SELECT_BY_POS,MODE_HISTORY)==true )
ord2 is not a position.
I have deleted your other topic and banned your other user name.
Why open another topic to discuss the same code. Please continue here.
This is the code that you posted.
Why are you still doing this?Where have you selected an order before calling OrderCloseTime()?
ord2 is not a position.
Hey! I don't have another user. Probaly was my friend "mumuazevedo" which posted before talked with me.
So, im seeing your point and thanks for the help and tip. I'm PHP programmer hehe. New on mql4 language(which i think is based on C).
To make easier to understand, lets say an example. On rsi between 72 an 76 and total orders =0, mql4 will open a sell order with TP and without SL.
If rsi keeps growing, above 76 and orders=1, will open a buy order with takeprofit.
All i want is when buy order closes when reach TP, close the buy order. The profit i will ajusts based on points.
But that code i can't do. I think my problem is on select. The perfect will be:
if ord2(buy order) is closed by TP(only way to close it){
close ord1(sell order);
}
Any suggest to a select code to run this? For eg. Check if ord2 has tp or closetime >0, and then close all open orders (which will have only one, the sell order)
Hi. Im developing a EA that close order when another is closed.
But, im using this code below. When open the first 2 orders, the code work(so, when 1 close, close another). But when mql4 open again this two orders on another moment, doesn't close them. Here is the code:
Hello,
why make a butter from history and current orders (closed and opened)?
To close orders need to count from last to first.
See the code....
bool CloseOrdr=false; for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) { CloseOrdr=OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL); } }
Do NOT start a new topic when the subject is obviously related to this topic. Next time your new topic will be deleted with no warning.
I will delete the other topic and move the content here,
Hi. Im developing a EA that close order when another is closed.
But, im using this code below. When open the first 2 orders, the code work(so, when 1 close, close another). But when mql4 open again this two orders on another moment, doesn't close them. Here is the code:
for(int i=1;i<=OrdersHistoryTotal();i++){ OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true) {}else { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){ OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL); } } }
.
Guys, did not work this way.
So, lets go. Fist of all, i need to create 2 voids, one to close all sell orders and other to close buy orders. Here''s the code of one (other just change)
void fecharcompras(){ for(int i=OrdersTotal()-1;i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderType() == OP_BUY){ bool result = OrderClose(OrderTicket(),OrderLots(),Bid,3,Red); condicao=0; if(result !=true){ int err = GetLastError(); Print("Erro",err); }else{ condicao=0; } } } else Print("Erro ao selecionar o trade",GetLastError()); } }
But, this code if i run, start to close all buy positions even when buy is positive and closes negative.
Then, i need a code to check if the last sell order is closed (the only way to close it is buy takeprofit). But need to be the last sell close order. And if check that is closed, run the void. Here's the code:
for(int i=OrdersTotal()-1;i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){ if(OrderType() == OP_SELL){ fecharcompras(); }else if(OrderType() == OP_BUY){ fecharvendas(); } } else Print("Erro ao selecionar o trade",GetLastError()); }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, im developing a EA which closes orders when take profit another. I dont know why isn't working. Here is the code
There is some wrong with my code? When run, if the market keeps growing up, mt4 closes the ord1 when reach the spread and keep ord2 opened, instead keep both opened.
Considerations: