//+------------------------------------------------------------------+ //| TrailingLine.mq4 | //| Roger | //+------------------------------------------------------------------+ #property show_inputs extern bool TrailingLine=True; int _OrderTicket; int start() { //+------------------------------------------------------------------+ int Order_Find_Radius = 10; int Stop_Loss = 14; int Take_Profit = 30; int Slippage = 50; //+------------------------------------------------------------------+ double Open_Price_Level, Stop_Loss_Level, PriceClose, Take_Profit_Level, Take_Profit_Level_new, Stop_Loss_Level_new; int _break; double lev; double DropPrice = WindowPriceOnDropped(); bool mod; for ( int x = 0; x <= Order_Find_Radius; x ++ ) { for ( int z = 0; z < OrdersTotal(); z ++ ) { OrderSelect( z, SELECT_BY_POS, MODE_TRADES ); if ( OrderSymbol() == Symbol() ) { if ( MathAbs(( DropPrice - OrderOpenPrice() )/Point) <= x ) { _break = 1; break; } } } if ( _break == 1 ) break; } string ord; int _OrderType = OrderType(); if(_OrderType==0)ord="BUY"; else ord="SELL"; _OrderTicket = OrderTicket(); Open_Price_Level = Bid; if ( Stop_Loss > 0 ) { if(_OrderType==0)Stop_Loss_Level = Open_Price_Level - Stop_Loss*Point; if(_OrderType==1)Stop_Loss_Level = Open_Price_Level + Stop_Loss*Point; ObjectCreate( "Stop_Loss_Line"+_OrderTicket, OBJ_HLINE, 0, 0, Stop_Loss_Level, 0, 0, 0, 0 ); ObjectSet( "Stop_Loss_Line"+_OrderTicket, OBJPROP_COLOR,Red ); ObjectSetText( "Stop_Loss_Line"+_OrderTicket, "Stop_Loss_Line", 6, "Arial", Red ); } else Stop_Loss_Level =0; if ( Take_Profit >0 ) { if(_OrderType==0)Take_Profit_Level=Open_Price_Level + Take_Profit*Point; if(_OrderType==1)Take_Profit_Level=Open_Price_Level - Take_Profit*Point; ObjectCreate( "Take_Profit_Line"+_OrderTicket, OBJ_HLINE, 0, 0, Take_Profit_Level, 0, 0, 0, 0 ); ObjectSet( "Take_Profit_Line"+_OrderTicket, OBJPROP_COLOR,Lime ); ObjectSetText( "Take_Profit_Line"+_OrderTicket, "Take_Profit_Line", 6, "Arial", Lime ); } else Take_Profit_Level=0; lev=Open_Price_Level-Stop_Loss_Level; Print("OrderType - ",ord," OrderTicket - ",_OrderTicket," OpenPrice - ",Open_Price_Level," StopLoss - ",Stop_Loss_Level," TakeProfit - ",Take_Profit_Level); WindowRedraw(); int xx; while( xx<1) { RefreshRates(); if(ObjectFind("Stop_Loss_Line"+_OrderTicket)!=-1)Stop_Loss_Level_new = NormalizeDouble( ObjectGet( "Stop_Loss_Line"+_OrderTicket, OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) ); else { Stop_Loss_Level=0; Stop_Loss_Level_new =0; } if(ObjectFind("Take_Profit_Line"+_OrderTicket)!=-1)Take_Profit_Level = NormalizeDouble( ObjectGet( "Take_Profit_Line"+_OrderTicket, OBJPROP_PRICE1 ), MarketInfo( Symbol(), MODE_DIGITS ) ); else Take_Profit_Level=0; if(Stop_Loss_Level==0&&Take_Profit_Level==0)break; if(_OrderType==0)PriceClose=Bid; else PriceClose=Ask; if(Stop_Loss_Level!=Stop_Loss_Level_new)lev=Bid-Stop_Loss_Level_new; Stop_Loss_Level=Stop_Loss_Level_new; if(TrailingLine) if(MathAbs(Bid-Stop_Loss_Level)>MathAbs(lev)&&Stop_Loss_Level!=0) { Stop_Loss_Level=Bid-lev; ObjectMove( "Stop_Loss_Line"+_OrderTicket, 0, 10,Stop_Loss_Level ); WindowRedraw(); } Comment("Order ",ord," - ",_OrderTicket, " StopLoss - ",Stop_Loss_Level," TakeProfit - ",Take_Profit_Level); if((lev<0&&iHigh(0,1,0)>=Stop_Loss_Level&&Stop_Loss_Level>0)||(lev>0&&iLow(0,1,0)<=Stop_Loss_Level)|| (iHigh(0,1,1)<Take_Profit_Level&&iHigh(0,1,0)>=Take_Profit_Level)||(iLow(0,1,1)>Take_Profit_Level&&iLow(0,1,0)<=Take_Profit_Level))//óñëîâèÿ ïåðåñå÷åíèÿ ñòîïà èëè ïðîôèòà { for(int i=0;i<OrdersTotal();i++) { OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); if (_OrderTicket == OrderTicket()) { if(OrderClose(OrderTicket(),OrderLots(),PriceClose,Slippage,CLR_NONE)) { break; } } } } OrderSelect(_OrderTicket,SELECT_BY_TICKET,MODE_TRADES); if(OrderCloseTime( ) !=0)break; Sleep(2000); } return(-1); } int deinit() { ObjectDelete( "Stop_Loss_Line"+_OrderTicket ); ObjectDelete( "Take_Profit_Line"+_OrderTicket ); Comment(""); return(0); }
Sample
Analysis
Market Information Used:
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
Other Features: