// TakeProfitZero.mq4
// Ñêðèïò âûñòàâëÿþùèé TP èëè SL â áåçóáûòîê (ñðåäíèé óðîâåíü äëÿ âñåõ îðäåðîâ Sell èëè Buy).
#include <WinUser32.mqh>
void Modify(int type, double level, bool tp)
{
double stop;
double take;
for (int i=0; i<OrdersTotal(); i++)
{
if (!OrderSelect(i,SELECT_BY_POS)) break;
if (OrderSymbol()!=Symbol()) continue;
if (OrderType()==type)
{
if (tp)
{
stop=OrderStopLoss();
take=level;
}
else
{
stop=level;
take=OrderTakeProfit();
}
if (!OrderModify(OrderTicket(),OrderOpenPrice(),stop,take,0,CLR_NONE)) Print("LastError = ", GetLastError());
}
}
}
void start()
{
double BuyLots=0;
double SellLots=0;
double BuyProfit=0;
double SellProfit=0;
for (int i=0; i<OrdersTotal(); i++)
{
if (!OrderSelect(i,SELECT_BY_POS)) break;
if (OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY)
{
BuyLots=BuyLots+OrderLots();
BuyProfit=BuyProfit+OrderProfit()+OrderCommission()+OrderSwap();
}
if (OrderType()==OP_SELL)
{
SellLots=SellLots+OrderLots();
SellProfit=SellProfit+OrderProfit()+OrderCommission()+OrderSwap();
}
}
double LotSize=MarketInfo(Symbol(),MODE_LOTSIZE);
if (BuyLots>0) double BuyLevel=NormalizeDouble(Bid-(BuyProfit/(LotSize*BuyLots)),Digits); else BuyLevel=0;
if (SellLots>0) double SellLevel=NormalizeDouble(Bid+(SellProfit/(LotSize*SellLots)),Digits); else SellLevel=0;
double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
if (BuyLevel==0) MessageBox("Íåò îðäåðîâ íà ïîêóïêó ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
else
{
if (BuyLevel<(Bid+StopLevel) && BuyLevel>(Bid-StopLevel)) MessageBox("Öåíà ñëèøêîì áëèçêà ê óðîâíþ áåçóáûòêà ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
else
{
string msg="Áåçóáûòîê äëÿ îðäåðîâ íà ïîêóïêó = "+DoubleToStr(BuyLevel,Digits)+" ";
if (BuyLevel>Bid)
{
int res=MessageBox(msg,"Ìîäèôèöèðîâàòü TakeProfit â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
if (res==6) Modify(OP_BUY,BuyLevel,true);
}
else
{
res=MessageBox(msg,"Ìîäèôèöèðîâàòü StopLoss â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
if (res==6) Modify(OP_BUY,BuyLevel,false);
}
}
}
if (SellLevel==0) MessageBox("Íåò îðäåðîâ íà ïðîäàæó ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
else
{
if (SellLevel<(Bid+StopLevel) && SellLevel>(Bid-StopLevel)) MessageBox("Öåíà ñëèøêîì áëèçêà ê óðîâíþ áåçóáûòêà ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
else
{
msg="Áåçóáûòîê äëÿ îðäåðîâ íà ïðîäàæó = "+DoubleToStr(SellLevel,Digits)+" ";
if (SellLevel<Bid)
{
res=MessageBox(msg,"Ìîäèôèöèðîâàòü TakeProfit â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
if (res==6) Modify(OP_SELL,SellLevel,true);
}
else
{
res=MessageBox(msg,"Ìîäèôèöèðîâàòü StopLoss â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
if (res==6) Modify(OP_SELL,SellLevel,false);
}
}
}
}
Comments