How to modify the multiple target profit by excluding the last order?
How to modify the multiple target profit by excluding the last order?
Loading saved threads...
Roller · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Roller
Original post: https://stackoverflow.com/questions/68326163
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 created this Modify Take Profit function but this will calculate all the total orders.
How to remove the last order? want it to calculate the average price without the last order
void ModifyTakeProfit()
{
double point=MarketInfo(OrderSymbol(),MODE_POINT);
double tp1,tp2,bbep,bsize; tp1=0;tp2=0;bbep=0;bsize=0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderMagicNumber())
{
if(OrderType()==OP_BUY)
{
tp1=OrderTakeProfit();
bbep += OrderOpenPrice()*OrderLots();
bsize+= OrderLots();
}
}
}
if (bbep>0)
{
bbep/=bsize;
tp1=bbep + TakeProfit*point;
if(TakeProfit!=0)
tp1=NormalizeDouble(tp1,(int)MarketInfo(OrderSymbol(),MODE_DIGITS));
else
tp1=0;
}
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderMagicNumber())
{
if(OrderType()==OP_BUY)
{
tp2=OrderTakeProfit();
if(tp1!=tp2)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp1,0,Blue))
Print ("Error: ", GetLastError());
}
}
}
}
}
Quote
Report
DDV · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: DDV
Original post: https://stackoverflow.com/a/68403980
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Your last order will be in the first position (FIFO) - so just adjust your for loops to loop to lower bound of 1 instead of 0.
void ModifyTakeProfit()
{
double point=MarketInfo(OrderSymbol(),MODE_POINT);
double tp1,tp2,bbep,bsize; tp1=0;tp2=0;bbep=0;bsize=0;
for (int i=OrdersTotal()-1; i>=1; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderMagicNumber())
{
if(OrderType()==OP_BUY)
{
tp1=OrderTakeProfit();
bbep += OrderOpenPrice()*OrderLots();
bsize+= OrderLots();
}
}
}
if (bbep>0)
{
bbep/=bsize;
tp1=bbep + TakeProfit*point;
if(TakeProfit!=0)
tp1=NormalizeDouble(tp1,(int)MarketInfo(OrderSymbol(),MODE_DIGITS));
else
tp1=0;
}
for (int i=OrdersTotal()-1; i>=1; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderMagicNumber())
{
if(OrderType()==OP_BUY)
{
tp2=OrderTakeProfit();
if(tp1!=tp2)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp1,0,Blue))
Print ("Error: ", GetLastError());
}
}
}
}
}
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/68326163 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 created this Modify Take Profit function but this will calculate all the total orders. How to remove the last order? want it to calculate the average price without the last order void ModifyTakeProfit() { double point=MarketInfo(OrderSymbol(),MODE_POINT); double tp1,tp2,bbep,bsize; tp1=0;tp2=0;bbep=0;bsize=0; for (int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderMagicNumber()) { if(OrderType()==OP_BUY) { tp1=OrderTakeProfit(); bbep += OrderOpenPrice()*OrderLots(); bsize+= OrderLots(); } } } if (bbep>0) { bbep/=bsize; tp1=bbep + TakeProfit*point; if(TakeProfit!=0) tp1=NormalizeDouble(tp1,(int)MarketInfo(OrderSymbol(),MODE_DIGITS)); else tp1=0; } for (int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==OrderMagicNumber()) { if(OrderType()==OP_BUY) { tp2=OrderTakeProfit(); if(tp1!=tp2) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp1,0,Blue)) Print ("Error: ", GetLastError()); } } } } }
Checking account access…