if(OrderType()==OP_BUY) { if(tstop>0&&(Bid-tstop*trade_point)>OrderStopLoss()) { int ticket_modify= OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(tstop*trade_point), OrderTakeProfit(),0,clrNONE); } if(OrderType()==OP_SELL) { if(tstop>0&&(Ask+tstop*trade_point)<OrderStopLoss()) { int ticket_modify= OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(tstop*trade_point), OrderTakeProfit(),0,clrNONE); }
I didn't test it but I believe the following code ...
if(enableTStop) { for(int cnt=0; cnt<OrdersTotal(); cnt++) { RefreshRates(); int ticket_select=OrderSelect(cnt,SELECT_BY_POS); if(OrderStopLoss()!=NULL && OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==buy_magic_number) { BuyTrailingStop(); SellTrailingStop(); } } }
Must be like this ...
if(enableTStop)
{
BuyTrailingStop();
SellTrailingStop();
}
input string infoTStop="___TRAILING STOP SETTINGS___"; //. input bool enableTStop=true; //Enable Trailing Stop input int MagicNumber=198202; input double TrailingStart = 30; // Trailing Start input double TrailingStop = 25; // Trailing Stop input double TrailingStep = 5; // Trailing Step //--- double trade_point = Point; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if(Digits==3 || Digits==5) trade_point=Point*10; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(OrdersTotal()==0) { int result=OrderSend(Symbol(),OP_BUY,1,Ask,5,Ask-100*trade_point,NULL,NULL,MagicNumber,0,clrNONE); if(result<0)Alert(ErrorDescription(GetLastError())); } //--- if(enableTStop) TrailStop(); } //-------------------------------------------------------------------+ // Trailing stop Function | //-------------------------------------------------------------------+ void TrailStop() { int Res = 0; double SLPrice = 0; double NewSL = 0; double Range = 0; int digit = 0; for ( int cnt = 0; cnt < OrdersTotal(); cnt++ ) if( OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) { while ( IsTradeContextBusy()) Sleep(100); if ( OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber ) { Range = MarketInfo(OrderSymbol(),MODE_BID) - OrderOpenPrice(); NewSL = MarketInfo(OrderSymbol(),MODE_BID) - TrailingStop*trade_point; NewSL = NormalizeDouble(NewSL,Digits); if ( Range >= TrailingStart*trade_point ) if ( NewSL - OrderStopLoss() >= TrailingStep*trade_point ) { Res = OrderModify ( OrderTicket(), OrderOpenPrice(), NewSL, OrderTakeProfit(), 0, clrNONE); } } if ( OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber ) { Range = OrderOpenPrice() - MarketInfo(OrderSymbol(),MODE_ASK); NewSL = MarketInfo(OrderSymbol(),MODE_ASK) + TrailingStop*trade_point; NewSL = NormalizeDouble(NewSL,Digits ); if ( Range >= TrailingStart*trade_point ) if ( OrderStopLoss() - NewSL >= TrailingStep*trade_point || OrderStopLoss() == 0 ) { Res = OrderModify ( OrderTicket(), OrderOpenPrice(), NewSL, OrderTakeProfit(), 0, clrNONE); } } } }
one of my friends gave me the above code
it seems it is better than my code
thanksLorentzos &Usama
:)

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I need help in the trailing stop code above
can any one please modify it
it is working continuously with any tick
!