Hi, has anyone a trailing stop EA which moves the stop to the low/high of the previous bar?
Thanks!!
Yes, i have why do you wanna know that ???
Hi, has anyone a trailing stop EA which moves the stop to the low/high of the previous bar?
Thanks!!
Maybe I should be a little bit more precisely: Can anyone give me a trailing stop EA like I described before?
- Since there are no slaves here, there are only two choices: learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
- But since what you asked for is SO simple
int start(){ static datetime Time0; if (Time0 == Time[0]) return(0); Time0 = Time[0]; // New bar? for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( OrderSelect(iPos, SELECT_BY_POS) ) if( // Only my orders w/ OrderMagicNumber() == Magic.Number ) if( // my magic number OrderSymbol() == chart.symbol // and my pair. ){ double DIR = Direction( OrderType() ); if (DIR > 0) double SL = Low[1]; else SL = High[1] + Ask-Bid; if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit(), 0) Alert("OrderModify failed: ", GetLastError()); } } double Direction(int op_xxx){ return( 1. - 2. * (op_xxx%2) ); }
Not compiled, not tested
- Since there are no slaves here, there are only two choices: learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
- But since what you asked for is SO simpleNot compiled, not tested
LOL, you should see my code from 2009 . . . you would laugh your socks off . . . ;-)
The only thing I would suggest adding is a test to make sure you don't try to modify the SL to the same value, it's possible if the last candle pair is a double bottom/top . . .
Right let's take some "random code" I found somewhere and point out some short comings. Here is the code ...
int start(){ static datetime Time0; if (Time0 == Time[0]) return(0); Time0 = Time[0]; // New bar? for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( OrderSelect(iPos, SELECT_BY_POS) ) if( // Only my orders w/ OrderMagicNumber() == Magic.Number ) if( // my magic number OrderSymbol() == chart.symbol // and my pair. ){ double DIR = Direction( OrderType() ); if (DIR > 0) double SL = Low[1]; else SL = High[1] + Ask-Bid; if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit(), 0) Alert("OrderModify failed: ", GetLastError()); } } double Direction(int op_xxx){ return( 1. - 2. * (op_xxx%2) ); }
This is not a good example of how to program in MQL4. Whilst you can stick loads of statements on the same line it is not a good idea for readability. The first step is to untangle the code so it is actually readable by humans (and add the missing closing bracket to allow it to compile.)
int Magic.Number = 122345; string chart.symbol = "EURUSD"; int start(){ static datetime Time0; if( Time0 == Time[0] ) return(0); // if this is not a new bar then let's not do anything Time0 = Time[0]; for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--){ if( OrderSelect(iPos, SELECT_BY_POS) ){ if( OrderMagicNumber() == Magic.Number && OrderSymbol() == chart.symbol ){ double DIR = Direction( OrderType() ); if( DIR > 0) double SL = Low[1]; else SL = High[1] + Ask-Bid; if( !OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0) ) Alert("OrderModify failed: ", GetLastError()); } } } } double Direction(int op_xxx){ return( 1. - 2. * (op_xxx%2) ); }
Now we can actually see the structure of the program.
This chart.symbol hard-coded variable is bizarre so let's bin it and use Symbol();
Now let's turn our attention to this Direction function. My only comment is WTF? This is code obfuscation not proper code. We take a perfectly good set of OrderType constants mangle them into doubles, including pending orders, and make the code unreadable. Let's ditch that stuff and handle the pending orders too.
int Magic.Number = 122345; int start(){ static datetime Time0; if( Time0 == Time[0] ) return(0); // if this is not a new bar then let's not do anything Time0 = Time[0]; for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--){ if( OrderSelect(iPos, SELECT_BY_POS) ){ if( OrderMagicNumber() == Magic.Number && OrderSymbol() == Symbol() ){ if( OrderType()==OP_BUY ) double SL = Low[1]; else if( OrderType()==OP_SELL ) SL = High[1] + Ask-Bid; else continue; // pending orders should not be messed with. if( !OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0) ) Alert("OrderModify failed: ", GetLastError()); } } } }
It seems to me the code is getting simpler!
Now you will notice that the variable SL is only declared in one of two paths within the if statement. MQL4 figures it out and is not unhappy. Most compilers would not let you do that. Let's put it in a common path so the code is more portable and we don't learn bad practices.
int Magic.Number = 122345; int start(){ static datetime Time0; if( Time0 == Time[0] ) return(0); // if this is not a new bar then let's not do anything Time0 = Time[0]; for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--){ if( OrderSelect(iPos, SELECT_BY_POS) ){ if( OrderMagicNumber() == Magic.Number && OrderSymbol() == Symbol() ){ double SL; if( OrderType()==OP_BUY ) SL = Low[1]; else if( OrderType()==OP_SELL ) SL = High[1] + Ask-Bid; else continue; // pending orders should not be messed with. // If the stop loss is too close to the current price the OrderModify will fail // We really ought to check for that so we don't keep getting needless alerts if( !OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0) ) Alert("OrderModify failed: ", GetLastError()); } } } }
Now this code compiles but I haven't tested it and the tests may be backwards for all I know. Testing in a demo account or in the strategy tester is a major part of the job.
Right let's take some "random code" I found somewhere and point out some short comings. Here is the code ...
This is not a good example of how to program in MQL4. Whilst you can stick loads of statements on the same line it is not a good idea for readability. The first step is to untangle the code so it is actually readable by humans (and add the missing closing bracket to allow it to compile.)
Now we can actually see the structure of the program.
** not done yet **
I think you are depriving the OP of a valuable learning experience . . . and I'm 90% serious about that . . .
I am 100% serious that help, if offered, should be helpful.
I am 100% serious that help, if offered, should be helpful.
I think it was . . . I have learned a lot trying to follow WHRoeder's code . . . sometimes it wasn't obvious to me . . but that made me work harder to get to grips with it. Sometimes we need to make a little effort and not have everything handed to us on a plate . . . I appreciate the time you spend helping too. :-)
At least the OP will get to fix the one possible source of errors . . .
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, has anyone a trailing stop EA which moves the stop to the low/high of the previous bar?
Thanks!!