#property show_inputs
extern double ForOrderLot=0.01;
extern bool UseAverage = true;
extern int OrderTP=100;
extern int OrderSL=0;
extern double OrderBuyTakeProfitPrice=0;
extern double OrderBuyStopLossPrice=0;
extern double OrderSellTakeProfitPrice=0;
extern double OrderSellStopLossPrice=0;
int start()
{
int x;
string OrdSymbol;
int OrdTotal;
double OrdBuyTotal;
double OrdBuyCount = 0;
double OrdBuyAvareg;
double OrdBuyTP = 0;
double OrdBuySL = 0;
double OrdSellTotal;
double OrdSellCount = 0;
double OrdSellAvareg;
double OrdSellTP = 0;
double OrdSellSL = 0;
OrdTotal = OrdersTotal();
if(UseAverage == true)
{
for(x=OrdTotal-1 ; x>=0 ; x--)
{
OrderSelect(x,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
{
OrdBuyTotal = OrdBuyTotal + (OrderOpenPrice()*OrderLots());
OrdBuyCount = OrdBuyCount + OrderLots();
}
else if(OrderType() == OP_SELL)
{
OrdSellTotal = OrdSellTotal + (OrderOpenPrice()*OrderLots());
OrdSellCount = OrdSellCount + OrderLots();
}
}
}
if(OrdBuyCount > 0)
{
OrdBuyAvareg = (OrdBuyTotal/OrdBuyCount);
if(OrderTP!=0)
OrdBuyTP = OrdBuyAvareg+(OrderTP*Point);
else
OrdBuyTP = 0;
if(OrderSL!=0)
OrdBuySL = OrdBuyAvareg-(OrderSL*Point);
else
OrdBuySL = 0;
}
if(OrdSellCount > 0)
{
OrdSellAvareg = (OrdSellTotal/OrdSellCount);
if(OrderTP!=0)
OrdSellTP = OrdSellAvareg-(OrderTP*Point);
else
OrdSellTP = 0;
if(OrderSL!=0)
OrdSellSL = OrdSellAvareg+(OrderSL*Point);
else
OrdSellSL = 0;
}
}
else
{
OrdBuyTP = OrderBuyTakeProfitPrice;
OrdBuySL = OrderBuyStopLossPrice;
OrdSellTP = OrderSellTakeProfitPrice;
OrdSellSL = OrderSellStopLossPrice;
}
// Modify Orders
for(x=OrdTotal-1 ; x>=0 ; x--)
{
OrderSelect(x,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol() == Symbol())
{
if(OrderType()==OP_BUY&&OrderLots()==ForOrderLot)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrdBuySL,OrdBuyTP,0,CLR_NONE);
}
if(OrderType()==OP_SELL&&OrderLots()==ForOrderLot)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrdSellSL,OrdSellTP,0,CLR_NONE);
}
}
}
return(0);
}