//--------------------------------------------------------------------
int start()
{
int STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);
double OSL,OTP,OOP,StLo,SL,TP;
double ProfitS,ProfitB,LB,LS,NLb,NLs,price_b,price_s,OL;
int b,s,OT,OMN;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
OMN = OrderMagicNumber();
if (OrderSymbol() == Symbol())
{
OOP = NormalizeDouble(OrderOpenPrice(),Digits);
OT = OrderType();
OL = OrderLots();
if (OT==OP_BUY)
{
price_b = price_b+OOP*OL;
b++; LB+= OL;
ProfitB+=OrderProfit()+OrderSwap()+OrderCommission();
}
if (OT==OP_SELL)
{
price_s = price_s+OOP*OL;
s++;LS+= OL;
ProfitS+=OrderProfit()+OrderSwap()+OrderCommission();
}
}
}
}
ObjectDelete("SLb");
ObjectDelete("SLs");
if (b>0)
{
NLb = NormalizeDouble(price_b/LB,Digits);
ObjectCreate("SLb",OBJ_ARROW,0,Time[0],NLb,0,0,0,0);
ObjectSet ("SLb",OBJPROP_ARROWCODE,6);
ObjectSet ("SLb",OBJPROP_COLOR, Blue);
}
if (s>0)
{
NLs = NormalizeDouble(price_s/LS,Digits);
ObjectCreate("SLs",OBJ_ARROW,0,Time[0],NLs,0,0,0,0);
ObjectSet ("SLs",OBJPROP_ARROWCODE,6);
ObjectSet ("SLs",OBJPROP_COLOR, Red);
}
int OTicket;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol())
{
OT = OrderType();
OSL = NormalizeDouble(OrderStopLoss(),Digits);
OTP = NormalizeDouble(OrderTakeProfit(),Digits);
OOP = NormalizeDouble(OrderOpenPrice(),Digits);
SL=OSL;TP=OTP;
if (OT==OP_BUY)
{
b++;
if (OSL==0 && Stoploss>=STOPLEVEL && Stoploss!=0)
{
SL = NormalizeDouble(Bid - Stoploss * Point,Digits);
}
else SL=OSL;
if (OTP==0 && Takeprofit>=STOPLEVEL && Takeprofit!=0)
{
TP = NormalizeDouble(Ask + Takeprofit * Point,Digits);
}
else TP=OTP;
if (NoLoss>=STOPLEVEL && OSL<NLb && NoLoss!=0)
{
if (OOP<=NLb && NLb!=0 && NLb <= NormalizeDouble(Bid-NoLoss*Point,Digits))
SL = NormalizeDouble(NLb+MinProfitNoLoss*Point,Digits);
}
if (TrailingStop>=STOPLEVEL && TrailingStop!=0)
{
StLo = NormalizeDouble(Bid - TrailingStop*Point,Digits);
if (StLo>=NLb && NLb!=0) if (StLo > OSL) SL = StLo;
}
if (SL != OSL || TP != OTP)
{
OTicket=OrderTicket();
if (!OrderModify(OTicket,OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
}
}
if (OT==OP_SELL)
{
s++;
if (OSL==0 && Stoploss>=STOPLEVEL && Stoploss!=0)
{
SL = NormalizeDouble(Ask + Stoploss * Point,Digits);
}
else SL=OSL;
if (OTP==0 && Takeprofit>=STOPLEVEL && Takeprofit!=0)
{
TP = NormalizeDouble(Bid - Takeprofit * Point,Digits);
}
else TP=OTP;
if (NoLoss>=STOPLEVEL && (OSL>NLs || OSL==0) && NoLoss!=0)
{
if (OOP>=NLs && NLs!=0 && NLs >= NormalizeDouble(Ask+NoLoss*Point,Digits))
SL = NormalizeDouble(NLs-MinProfitNoLoss*Point,Digits);
}
if (TrailingStop>=STOPLEVEL && TrailingStop!=0)
{
StLo = NormalizeDouble(Ask + TrailingStop*Point,Digits);
if (StLo<=NLs && NLs!=0) if (StLo < OSL || OSL==0) SL = StLo;
}
if ((SL != OSL || OSL==0) || TP != OTP)
{
OTicket=OrderTicket();
if (!OrderModify(OTicket,OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
}
}
}
}
}
if (IsTesting())
{
if (OrdersTotal()==0)
{
if (OrderSend(Symbol(),OP_BUY,0.1,NormalizeDouble(Ask,Digits),3,0,0,"тест",0,0,Blue)==-1) Print("Error OrderSend ",GetLastError());
if (OrderSend(Symbol(),OP_SELL,0.1,NormalizeDouble(Bid,Digits),3,0,0,"тест",0,0,Red)==-1) Print("Error OrderSend ",GetLastError());
}
}
return(0);
}
ssg