need help for my first autotrailingstop funciton, i checked codes again and again, it just work wrong way. thanks
Please allow me to offer you some constructive criticism...
It looks like you might not fully understand the concept of scope. You are using curly braces in a way that is designed to limit scope when this is not the desired outcome. For example:
bool fenpi() { double lot=OrderLots(); { //<-- this should not be here! for(int cnt=0;cnt<OrdersTotal();cnt++) {
Another thing, you are trying to do way too much in your functions. Break the problem down into more manageable functions with less nesting (flatten your code). Also, stop using global variables unless you absolutely have to, and when you do it's good practice to prefix the variable name with "g_". Here is an example of how you might refactor your code to make it easier to read and debug.
#property strict input bool inpAutoStoploss = true; input int inpStopLossPips = 1; int g_point_modifier = 1; //+------------------------------------------------------------------+ int OnInit() { if(_Digits == 3 || _Digits == 5) g_point_modifier = 10; return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnTick() { if(OrdersTotal() == 0) return; if(inpAutoStoploss) my_trailing_stops(); } //+------------------------------------------------------------------+ #include <stdlib.mqh> #include <Double.mqh> /* https://www.mql5.com/en/code/19727 */ bool trailing_stop_by_ticket(int ticket, int trailing_points) { if(!OrderSelect(ticket, SELECT_BY_TICKET) || OrderType() > 1) return false; string symbol = OrderSymbol(); int stops_level = (int)SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL); if(trailing_points < stops_level) { Print("TrailingStopError: Invalid Stops"); return false; } MqlTick tick; if(!SymbolInfoTick(symbol, tick)) return false; double delta_points = trailing_points * SymbolInfoDouble(symbol, SYMBOL_POINT); double stop_curr = OrderStopLoss(); int type = OrderType(); double stop_new; if(type == OP_BUY) stop_new = CDouble::RoundToTick(tick.bid - delta_points, symbol); else stop_new = CDouble::RoundToTick(tick.ask + delta_points, symbol); if(stop_curr == 0.0 || (type == OP_BUY && stop_new > stop_curr) || (type == OP_SELL && stop_new < stop_curr) ){ return OrderModify(ticket, OrderOpenPrice(), stop_new, OrderTakeProfit(), 0, clrRed); } return true; } //+------------------------------------------------------------------+ void my_trailing_stops() { for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == _Symbol && OrderMagicNumber() <= 0 ){ bool is_trailed = trailing_stop_by_ticket( OrderTicket(), inpStopLossPips * g_point_modifier ); if(!is_trailed) Print("TrailingError: ", ErrorDescription(_LastError)); } } }
Please allow me to offer you some constructive criticism...
It looks like you might not fully understand the concept of scope. You are using curly braces in a way that is designed to limit scope when this is not the desired outcome. For example:
Another thing, you are trying to do way too much in your functions. Break the problem down into more manageable functions with less nesting (flatten your code). Also, stop using global variables unless you absolutely have to, and when you do it's good practice to prefix the variable name with "g_". Here is an example of how you might refactor your code to make it easier to read and debug.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
please figure the problem out. thanks a lot.