Very useful. Thanks.
Steven
The trailing example fails with an error.
Programmers, help with mql5, please!!!
Is it possible to use just an ordinary trailing function as in mql4 without any classes? Just like, for example, the TradeSizeOptimised function is implemented in Moving Averages.mq5.
I have already searched everything, in examples, articles, forum - I have not found anything. I've already got completely lost.... Maybe someone has a ready variant, I will be very grateful!!! - I would like to participate in the championship.
Programmers, help with mql5, please!!!
Is it possible to use just an ordinary trailing function as in mql4 without any classes? Just like, for example, the TradeSizeOptimised function is implemented in Moving Averages.mq5.
I have already searched everything, in examples, articles, forum - I have not found anything. I've already got completely lost.... Maybe someone has a ready variant, I will be very grateful!!! - I would like to participate in the championship.
here you go
int TrailingStop() { if(PositionSelect(Symbol())) // select position { //MqlTradeRequest m_request;// declare the structure of the request to the server //MqlTradeResult m_result;// declare the server response structure double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID); // write the bid price to the variable double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK); // write to the variable ask price double OpenPrice=PositionGetDouble(POSITION_PRICE_OPEN); // write the position opening price into the variable double PositionSL=PositionGetDouble(POSITION_SL); // write the stop loss level to the variable double PositionTP=PositionGetDouble(POSITION_TP); // write take profit level into the variable if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) // define position type { if(TrailWhileMinus==true || Bid-OpenPrice>_Point*Trail) // don't trawl until we can reach breakeven with the first stop transfer { if(Bid-PositionSL>Trail*_Point) //basic condition for the necessity to move the stop loss { //--- write data to the structure request.action = TRADE_ACTION_SLTP; request.symbol = Symbol(); request.sl = NormalizeDouble(Bid-Trail*_Point,_Digits); request.tp = NormalizeDouble(PositionTP,_Digits); //--- return(OrderSend(request,result)); // send the request to the server } } } else { if(TrailWhileMinus==true || OpenPrice-Ask>_Point*Trail) // don't trawl until we can reach breakeven with the first stop transfer { if(PositionSL-Ask>Trail*_Point) //basic condition for the necessity to move the stop loss { //--- write data to the structure request.action = TRADE_ACTION_SLTP; request.symbol = Symbol(); request.sl = NormalizeDouble(Ask+Trail*_Point,_Digits); request.tp = NormalizeDouble(PositionTP,_Digits); //--- return(OrderSend(request,result)); // send the request to the server } } } } return(0); }
here you go
I assume (probably not without reason) that result is better passed as a parameter :)
Otherwise there is no way to analyse it. It's not good somehow...
PS
I would also create two functions - TrailingStopBuy and TrailingStopSell
I assume (probably not without reason) that result is better passed as a parameter :)
Otherwise there is no way to analyse it. It's not good somehow...
PS
I would also create two functions - TrailingStopBuy and TrailingStopSell.
Well, I gave the man an example, because he has already made his brain how to write an ordinary trailing, and then let him think a little, that would work without errors, in the tester in principle, and this design works fine. in real life did not check
Here you go.
sergey1294,
Thanks again, everything works!!!
There is one more small question, how to add a check by magic number to the function? I'm trying to insert such a check:
if (OrderGetInteger(ORDER_MAGIC)==EA_Magic) { ..... }..... but something it breaks the whole trade.....

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article How to Create Your Own Trailing Stop is published:
The basic rule of trader - let profit to grow, cut off losses! This article considers one of the basic techniques, allowing to follow this rule - moving the protective stop level (Stop loss level) after increasing position profit, i.e. - Trailing Stop level. You'll find the step by step procedure to create a class for trailing stop on SAR and NRTR indicators. Everyone will be able to insert this trailing stop into their experts or use it independently to control positions in their accounts.
Author: Дмитрий