Specification
The attached code segment below, takes a specific symbol and side (BUY/SELL) and calculates the average price for the group of trades and applies a breakeven so hat the sum of the trades closes at breakeven (closes at the same price).
I need a version of this BEP2 code segment I need a dollar value offset added into the price, for example if a $50 loss is specified, the applied TP to the group of open trades on the sell side, the total loss for the group of trades would be $50.
Code:
//________________________________________________________________________
void BEP2(string symb1, int orderType)
{
double avgbuy=0,avgsell=0;
double totpricesell=0,totpricebuy=0;
int OrderBuy=0,OrderSell=0;
double lotbuy=0,lotsell=0;
int ticket=0;
double val=0;
double poin = MarketInfo(symb1,MODE_POINT);
int digit= (int)MarketInfo(symb1,MODE_DIGITS);
for(int cnt = 0; cnt < OrdersTotal(); cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol()==symb1 && (OrderMagicNumber()>= magic))
{
if(OrderType()==OP_BUY)
{
OrderBuy++;
lotbuy+=OrderLots();
totpricebuy += (OrderOpenPrice()) * OrderLots();
}
if(OrderType()==OP_SELL)
{
OrderSell++;
lotsell+=OrderLots();
totpricesell += (OrderOpenPrice()) * OrderLots();
}
}
}
}
if(OrderSell > 0 && totpricesell!=0)
avgsell = NormalizeDouble(totpricesell / lotsell, digit);
if(OrderBuy > 0 && totpricebuy!=0)
avgbuy = NormalizeDouble(totpricebuy / lotbuy, digit);
for(int cnt = 0; cnt < OrdersTotal(); cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol()==symb1 && (OrderMagicNumber()>= magic) && (StringSubstr(OrderComment(),0,4)=="to #" || StringSubstr(OrderComment(),0,6)=="from #"))
{
if(OrderType()==OP_BUY)
{
ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(avgbuy+CommisionPoint*poin,digit),0,clrYellow);
}
if(OrderType()==OP_SELL)
{
ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(avgsell-CommisionPoint*poin,digit),0,clrYellow);
}
}
}
}
if(ticket>0)
ok++;
}