Dear RickD
if I close half of Lot of open order, how to get the rest of the Lot from the Order which is still open?
Trims
if I close half of Lot of open order, how to get the rest of the Lot from the Order which is still open?
Trims
MQL4 code help 12 replies
Learn MQL4 Programming - Video Guide 5 replies
MQL4 Language Most Recent Version is it updated beyond the tutorial on the mql4 websi 6 replies
MT4 Indicators Usage Tutorial/MQL4 Reference Documents 36 replies
DislikedIf you close half of lot then the rest is another 50% of closed lot.
Please specify your question.Ignored
DislikedIf you close half of lot then the rest is another 50% of closed lot.
Please specify your question.Ignored
bool PartialClose() { int cnt,total=OrdersTotal(); for(cnt=total-1;cnt>=0;cnt--) { if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != magic) continue; if (OrderProfit() < 0) continue; //no further checking double lot_close = NormalizeDouble(OrderLots() * 0.6, lotsdigit); if (lot_close < MarketInfo(Symbol(), MODE_MINLOT)) continue; // unable to close if(OrderType() == OP_BUY) { if((Bid-OrderOpenPrice()) > TP_1*pips2dbl) { OrderCloseReliableMKT(OrderTicket(),lot_close, dBid, slippage * pips2points, Blue); } } else if(OrderType() == OP_SELL) { if((OrderOpenPrice()-Ask) > TP_1*pips2dbl) { OrderCloseReliableMKT(OrderTicket(),lot_close, dAsk, slippage * pips2points, Red); } } } }
DislikedYour question is still not clear as can be seen in the replies above which seemed to have missed the point, but I will give it a try. If I miss the point, well that makes 3 of us.
For simplicity I am going to assume you want to close the trade in 2 parts. 3 or more parts is possible, but I don't want to complicate my answer. The key to partial closing is knowing what the original size of the trade position was. You need to keep track of this in your program. There are several ways to do this, you can decide how you would like to do this. If you...Ignored
double lastlot() { double lot=0; int i; for(i=0; i<OrdersHistoryTotal(); i++) { OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue; lot=OrderLots(); } return(lot); }
Disliked1) You can compare initial lot with actual lot size of orders.
If actual lot size is less than initial lot then partial close was applied so you need to skip PartialClose function.
2) It would be better for you to change the conception.
Just open 2,3,4,... orders with different targets.Ignored
bool Allow_Buy, Allow_Sell;
datetime GetLastCloseTime(int type) { datetime dt = 0; int cnt = OrdersHistoryTotal(); for (int i=0; i < cnt; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue; //if (OrderSymbol() != Symbol()) continue; //if (OrderMagicNumber() != Magic) continue; if (OrderType() != type) continue; if (OrderCloseTime() > dt) { dt = OrderCloseTime(); } } return (dt); }
void Get_EOW_Stop() { // moved outside loop datetime LastBuy = GetLastCloseTime(OP_BUY); datetime LastSell = GetLastCloseTime(OP_SELL); int cnt = OrdersHistoryTotal(); for (int i=0; i < cnt; i++) { if (!OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != magic) continue; if(OrderType() == OP_BUY) { // Close order outside trade time if(LastBuy != IsTradeTime()) { //If Last BuyOrder was Profit, then Stop Order if (OrderProfit() <= 0) {Allow_Buy = true;} else {Allow_Buy = false;} } } if(OrderType() == OP_SELL) { // Close order outside trade time if(LastSell != IsTradeTime()) { //If Last SellOrder was Profit, then Stop Order if (OrderProfit() <= 0) {Allow_Sell = true;} else {Allow_Sell = false;} } } } }
bool trade_time = IsTradeTime(); if (trade_time) { ea_action = "Trading Time"; } else { ea_action = "Out of Trading Time"; //Only run this function in the end of week Get_EOW_Stop(); }
if ((trade_time || Allow_Buy) && (i_countB == 0)) { OrderSend(Symbol(),....,.....);}