int TrailingStopOrders = 20; // Фиксированный размер трала StopOrders
int TrailingStepStop = 3; // Шаг трала StopOrders
int TrailingLimitOrders = 30; // Фиксированный размер трала LimitOrders
int TrailingStepLimit = 3; // Шаг трала LimitOrders
color clModifyBuyOrder = clrNONE; // Цвет значка модификации покупки
color clModifySellOrder = clrNONE; // Цвет значка модификации продажи
//===============================================================================================
//------------------------- Сопровождение Stop ордеров простым тралом --------------------------+
//===============================================================================================
void TrailingStopOrder(string symb="0",int type=-1,int mg=-1) {
double di=0, pa=0, pb=0, p=0, sl=0, tp=0;
if(symb=="0") { symb=Symbol();}
p=MarketInfo(symb,MODE_POINT);
int msl=(int)MarketInfo(symb, MODE_STOPLEVEL);
if(TrailingStopOrders<msl) {
Alert("TrailingStopOrder(): Значение меньше допустимого=",TrailingStopOrders/Sign,", Минимум=",msl/Sign);
TrailingStopOrders=msl;} else {TrailingStopOrders=TrailingStopOrders;}
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if((OrderSymbol()==symb||symb=="")&&(type<0||OrderType()==type)&&(mg<0||OrderMagicNumber()==mg)) {
if(OrderType()==OP_BUYSTOP) {
pa=MarketInfo(symb,MODE_ASK);
if(OrderOpenPrice()-pa>(TrailingStopOrders+TrailingStepStop-1)*p) {
di=OrderOpenPrice()-pa-(TrailingStopOrders*p);
if(OrderStopLoss()>0) sl=OrderStopLoss()-di; else sl=0;
if(OrderTakeProfit()>0) tp=OrderTakeProfit()-di; else tp=0;
ModifyOrder(symb,pa+(TrailingStopOrders*p), sl, tp, clModifyBuyOrder);
}}
if(OrderType()==OP_SELLSTOP) {
pb=MarketInfo(symb,MODE_BID);
if(pb-OrderOpenPrice()>(TrailingStopOrders+TrailingStepStop-1)*p) {
di=pb-OrderOpenPrice()-(TrailingStopOrders*p);
if(OrderStopLoss()>0) sl=OrderStopLoss()+di; else sl=0;
if(OrderTakeProfit()>0) tp=OrderTakeProfit()+di; else tp=0;
ModifyOrder(symb,pb-(TrailingStopOrders*p), sl, tp, clModifySellOrder);
}}
}}}}
//===============================================================================================
//------------------------- Сопровождение Limit ордеров простым тралом -------------------------+
//===============================================================================================
void TrailingLimitOrder(string symb="0",int type=-1,int mg=-1) {
double di=0, pa=0, pb=0, p=0, sl=0, tp=0;
if(symb=="0") { symb=Symbol();}
p=MarketInfo(symb,MODE_POINT);
int msl=(int)MarketInfo(symb, MODE_STOPLEVEL);
if(TrailingLimitOrders<msl) {
Alert("TrailingLimitOrder(): Значение меньше допустимого=",TrailingLimitOrders/Sign,", Минимум=",msl/Sign);
TrailingLimitOrders=msl; } else { TrailingLimitOrders=TrailingLimitOrders; }
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if((OrderSymbol()==symb||symb=="")&&(type<0||OrderType()==type)&&(mg<0||OrderMagicNumber()==mg)) {
if(OrderType()==OP_BUYLIMIT) {
pa=MarketInfo(symb,MODE_ASK);
if(pa-OrderOpenPrice()>(TrailingLimitOrders+TrailingStepLimit-1)*p) {
di=pa-OrderOpenPrice()-(TrailingLimitOrders*p);
if(OrderStopLoss()>0) sl=OrderStopLoss()+di; else sl=0;
if(OrderTakeProfit()>0) tp=OrderTakeProfit()+di; else tp=0;
ModifyOrder(symb,pa-(TrailingLimitOrders*p), sl, tp, clModifyBuyOrder);
}}
if(OrderType()==OP_SELLLIMIT) {
pb=MarketInfo(symb,MODE_BID);
if(OrderOpenPrice()-pb>(TrailingLimitOrders+TrailingStepLimit-1)*p) {
di=OrderOpenPrice()-pb-(TrailingLimitOrders*p);
if(OrderStopLoss()>0) sl=OrderStopLoss()-di; else sl=0;
if(OrderTakeProfit()>0) tp=OrderTakeProfit()-di; else tp=0;
ModifyOrder(symb,pb+(TrailingLimitOrders*p), sl, tp, clModifySellOrder);
}}
}}}}