How to Close Profit without calculating the lowest open position (for buy) & highest open position (for Sell)?
How to Close Profit without calculating the lowest open position (for buy) & highest open position (for Sell)?
Loading saved threads...
Roller · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Roller
Original post: https://stackoverflow.com/questions/66999069
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I manage to get the highest,closeall and profit..cant get the lowest..
for lowest can we use the MathMin(lowest, OrderOpenPrice())?
still stuck how to close by remaining the highest(sell) and lowest(buy) open position
double HighestOpenPrice(int m)
{
double highest=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
continue;
if(OrderType()==m && m==0 && OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
highest=MathMax(highest, OrderOpenPrice());
if(OrderType()==m && m==1 && OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
highest=MathMax(highest, OrderOpenPrice());
}
return(highest);
}
double TotalOpenProfit(int direction)
{
double result = 0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if((direction < 0 && OrderType() == OP_BUY) || (direction > 0 && OrderType() == OP_SELL)) continue;
result += OrderProfit()+OrderSwap()+OrderCommission();
}
return(result);
}
bool CustomCloseAll(int m)
{
int ticket=0;
for(int i = OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue;
if(OrderType()==m && m==0 && OrderType()==OP_BUY && OrderSymbol()==Symbol())
ticket= OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, CLR_NONE);
if(OrderType()==m && m==1 && OrderType()==OP_SELL && OrderSymbol()==Symbol())
ticket= OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, CLR_NONE);
}
if(!OrdersTotal())break;
}
Quote
Report
tomgny · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: tomgny
Original post: https://stackoverflow.com/a/67003104
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You can do it without iterating over all orders. If in your strategy, the
latest
order has the highest price in case of
buy
or the lowest in case of
sell
, you can simply assign the ticket number to the global variable when sending a new order. For example:
//Global variables
int lastSellTicket = 0;
int lastBuyTicket = 0;
and on order send case (e.g.
sell
)
int ticket = OrderSend(Symbol(), OP_SELL, lot, Bid, 0, 0, 0, "Sell", magic);
if(ticket > 0) {
lastSellTicket = ticket;
}
else{
Print("Error send order. Error code: ", GetLastError());
}
If it's not the latest order (but the highest/lowest price), you can do the same but additionally before order send case, use comparison between the highest/lowest
OrderOpenPrice()
and the current open price.
Example (
buy
):
double lastBuyOpenPrice = -1;
if(OrderSelect(lastBuyTicket, SELECT_BY_TICKET)){
lastBuyOpenPrice = OrderOpenPrice();
}
int ticket = OrderSend(Symbol(), OP_BUY, lot, Ask, 0, 0, 0, "Buy", magic);
if(ticket > 0 ) {
if(Ask > lastBuyOpenPrice ){ //If current open price is greater than the latest one
lastBuyTicket = ticket;
}
}
else{
Print("Error send order. Error code: ", GetLastError());
}
And then, in your
TotalOpenProfit
and
CustomCloseAll
function add logic for avoid calculating/closing those latest orders:
if(OrderTicket() != lastSellTicket || OrderTicket() != lastBuyTicket){
//your logic...
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Roller Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/66999069 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I manage to get the highest,closeall and profit..cant get the lowest.. for lowest can we use the MathMin(lowest, OrderOpenPrice())? still stuck how to close by remaining the highest(sell) and lowest(buy) open position double HighestOpenPrice(int m) { double highest=0; for(int i=OrdersTotal()-1; i>=0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if(OrderType()==m && m==0 && OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber) highest=MathMax(highest, OrderOpenPrice()); if(OrderType()==m && m==1 && OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber) highest=MathMax(highest, OrderOpenPrice()); } return(highest); } double TotalOpenProfit(int direction) { double result = 0; for(int i=OrdersTotal()-1; i>=0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if((direction < 0 && OrderType() == OP_BUY) || (direction > 0 && OrderType() == OP_SELL)) continue; result += OrderProfit()+OrderSwap()+OrderCommission(); } return(result); } bool CustomCloseAll(int m) { int ticket=0; for(int i = OrdersTotal()-1; i>=0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue; if(OrderType()==m && m==0 && OrderType()==OP_BUY && OrderSymbol()==Symbol()) ticket= OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, CLR_NONE); if(OrderType()==m && m==1 && OrderType()==OP_SELL && OrderSymbol()==Symbol()) ticket= OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, CLR_NONE); } if(!OrdersTotal())break; }
Checking account access…