I'm not sure why you are cycling through each bar once you've selected the order. Once you have selected the order, get the current stop loss associated with the order. Set your new SL as the low of the last full bar and then check how this compares to the current stop. if new stop is better than current, then modify it... along the lines of..
if (OrderType()==OP_BUY) { SL=OrderStopLoss(); NewSL=Low[1]; if (NewSL>SL+1*Point) { OrderModify(OrderTicket(), OrderOpenPrice(), NewSL,OrderTakeProfit(),0,0);} } }
And please use the SRC button for code.
Also, you may also want to check that the NewSL is outside the minimum allowable distance. It will just fail if it's too close. https://docs.mql4.com/constants/marketinfo
V
I'm not sure why you are cycling through each bar once you've selected the order. Once you have selected the order, get the current stop loss associated with the order. Set your new SL as the low of the last full bar and then check how this compares to the current stop. if new stop is better than current, then modify it... along the lines of..
And please use the SRC button for code.
Also, you may also want to check that the NewSL is outside the minimum allowable distance. It will just fail if it's too close. https://docs.mql4.com/constants/marketinfo
V
Hello Viffer,
Thanks for your answer. I have change my code with your example, but the result is not the as in my brain. Sorry my Englisch is not good, an i am a newbee.
I´ll code an Breakout strategy. It is always one Trade open. The current candle´s high is higher then the previous -> buy-Order; SL low of the previous candle. Now Current Candel closed; TP not closed; low of this candle higher then the previous candle-> change SL on this low. Now SL closed -> open Sellorder; SL on the high of the previous candle.
What is the SRC Button????
Can you help me once more.
Thanks Andy
TradesInThisSymbol=openPositions();
// Only allow 1 trade per Symbol
if(TradesInThisSymbol > 0)
{
return(0);
}
if(Bid >High[1])
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Low[1]-0.0001,Ask + TakeProfit,0,MagicNumber,0,Green);
if(Bid<Low[1])
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,High[1]+0.0001,Bid - TakeProfit,0,MagicNumber,0,Red);
return(0);}
//+------------------------------------------------------------------------+
//| counts the number of open positions |
//+------------------------------------------------------------------------+
int openPositions( )
{ int op =0;
for(int i=OrdersTotal()-1;i>=0;i--) // scan all orders and positions...
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber()!=MagicNumber) continue;
if(OrderSymbol()==Symbol() )
{
if(OrderType()==OP_BUY)op++;
if(OrderType()==OP_SELL)op++;
}
}return(op);
}
int HandleOpenPositions()
{int cnt; int NewSL; double SL;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol() )continue;
if (OrderMagicNumber()!=MagicNumber) continue;
if(OrderType()==OP_BUY)
{ SL=OrderStopLoss();
NewSL=Low[1];
if (NewSL>SL+1*Point)
{OrderModify(OrderTicket(), OrderOpenPrice(), NewSL,OrderTakeProfit(),0,0);}}
if(OrderType()==OP_SELL)
{ SL=OrderStopLoss();
NewSL=High[1];
if (NewSL>SL+1*Point)
{ OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,0);}}} }
It's difficult to tell but I think a main problem is
if(TradesInThisSymbol > 0) { return(0); }As soon as you open your order, the code won't go any further than this return. You would be better to put something like
if(TradesInThisSymbol == 0) { // do Trading }
on your sell modify, you need
if (NewSL<SL-1*Point).
You've set your trailing stop as a function which is fine, but you don't call that function (not in this code anyway). At a minimum, you probably want want
if(TradesInThisSymbol > 0) { HandleOpenPositions(); }
It's difficult to tell but I think a main problem is
on your sell modify, you need
if (NewSL<SL-1*Point).
You've set your trailing stop as a function which is fine, but you don't call that function (not in this code anyway). At a minimum, you probably want wa
#include <stdlib.mqh> int MagicNumber=291177; extern double TakeProfit=0.0008; extern double Lots=0.01; extern double BreakOutLevel=0.0001; extern bool UseTimeLimit=true; extern int StartHour=0; extern int StopHour=23; extern int FridayCloseHour=20; int TradesInThisSymbol; bool YesStop; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() {int ticket; if (UseTimeLimit) { YesStop=True; if(DayOfWeek()==0 || DayOfWeek()==6) return(0); // kein Trading am Samstag und Sonntag if(DayOfWeek()==5 && Hour()>= FridayCloseHour) return(true); // kein Trading Freitags nach 20:00 Uhr if(Hour()>=StartHour && Hour()<StopHour) YesStop=false; // Trading von 0:00 bis 24:00 Uhr if (YesStop) return(0); } TradesInThisSymbol=openPositions(); // Only allow 1 trade per Symbol if(TradesInThisSymbol == 0) { if(Bid >High[1]) ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Low[1]-0.0001,Ask + TakeProfit,0,MagicNumber,0,Green); if(Bid<Low[1]) ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,High[1]+0.0001,Bid - TakeProfit,0,MagicNumber,0,Red); HandleOpenPositions();} } //+------------------------------------------------------------------------+ //| counts the number of open positions | //+------------------------------------------------------------------------+ int openPositions( ) { int op =0; for(int i=OrdersTotal()-1;i>=0;i--) // scan all orders and positions... { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderMagicNumber()!=MagicNumber) continue; if(OrderSymbol()==Symbol() ) { if(OrderType()==OP_BUY)op++; if(OrderType()==OP_SELL)op++; } }return(op); } int HandleOpenPositions() {int cnt; int NewSL; double SL; for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol()!=Symbol() )continue; if (OrderMagicNumber()!=MagicNumber) continue; if(OrderType()==OP_BUY) { SL=OrderStopLoss(); NewSL=Low[1]; if (NewSL>SL+1*Point) {OrderModify(OrderTicket(), OrderOpenPrice(), NewSL,OrderTakeProfit(),0,0);}} if(OrderType()==OP_SELL) { SL=OrderStopLoss(); NewSL=High[1]; if (NewSL<SL-1*Point) { OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,0);}}} }
Sorry, but the result is not correct. The SL is not on the Previous bar.
Look where your HandleOpenPositions() is. It's inside the if (TradesInThisSymbol == 0) so when you open an order, the call to trail the stop is never reached. Almost there I think :).
V
Look where your HandleOpenPositions() is. It's inside the if (TradesInThisSymbol == 0) so when you open an order, the call to trail the stop is never reached. Almost there I think :).
V
I have tested it (The HandleOpen.....) in different Positions in the int start() Funktion. The result ist always the same, but not in my expression. Have you got more Ideas.
Missed it first time, but you have set NewSL as an int. It needs to be double. I would also recomend you put something in to check that your new stoploss is allowable.
if (NewSL>Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point) { NewSL=Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; }
The function needs to be inside start but outside the if statement. You should end start with return(0); also.
hth
V
EDIT:
The function call needs to be inside start but outside the if statement.
Missed it first time, but you have set NewSL as an int. It needs to be double. I would also recomend you put something in to check that your new stoploss is allowable.
The function needs to be inside start but outside the if statement. You should end start with return(0); also.
hth
V
EDIT:
The function call needs to be inside start but outside the if statement.
Hello,
i have tested the code with your statement and the result is the same as ever. Sorry, but this is the problem since a week.
int start() {int ticket; if (UseTimeLimit) { YesStop=True; if(DayOfWeek()==0 || DayOfWeek()==6) return(0); // kein Trading am Samstag und Sonntag if(DayOfWeek()==5 && Hour()>= FridayCloseHour) return(true); // kein Trading Freitags nach 20:00 Uhr if(Hour()>=StartHour && Hour()<StopHour) YesStop=false; // Trading von 0:00 bis 24:00 Uhr if (YesStop) return(0); } HandleOpenPositions(); TradesInThisSymbol=openPositions(); // Only allow 1 trade per Symbol if(TradesInThisSymbol == 0) { if(Bid >High[1]) ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Low[1]-0.0001,Ask + TakeProfit,0,MagicNumber,0,Green); if(Bid<Low[1]) ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,High[1]+0.0001,Bid - TakeProfit,0,MagicNumber,0,Red); } return(0); } //+------------------------------------------------------------------------+ //| counts the number of open positions | //+------------------------------------------------------------------------+ int openPositions( ) { int op =0; for(int i=OrdersTotal()-1;i>=0;i--) // scan all orders and positions... { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderMagicNumber()!=MagicNumber) continue; if(OrderSymbol()==Symbol() ) { if(OrderType()==OP_BUY)op++; if(OrderType()==OP_SELL)op++; } }return(op); } //+------------------------------------------------------------------------+ //| counts the number of open positions | //+------------------------------------------------------------------------+ int HandleOpenPositions() { int cnt; double NewSL; double SL; for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol()!=Symbol() )continue; if (OrderMagicNumber()!=MagicNumber) continue; if(OrderType()==OP_BUY) { if (NewSL>Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point) { NewSL=Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; } } if(OrderType()==OP_SELL) { if (NewSL<Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point) { NewSL=Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; } } } }
Right, but you are doing more than just the changes I have suggested. This latest version, you have now deleted a load of stuff we put in earlier. Focus on your handleopenpositins function as the errors are in the changes you made there. Start at the begining and try and understand what the code is doing. In particular what happens to NewSL in this code. Think about the values of each variable. COmpare it to the code you posted previously and see if you can work it out. If you can't see it straight away, put in a Print to see the values.
V
Right, but you are doing more than just the changes I have suggested. This latest version, you have now deleted a load of stuff we put in earlier. Focus on your handleopenpositins function as the errors are in the changes you made there. Start at the begining and try and understand what the code is doing. In particular what happens to NewSL in this code. Think about the values of each variable. COmpare it to the code you posted previously and see if you can work it out. If you can't see it straight away, put in a Print to see the values.
V
Hello Viffer,
Sorry, can you explain me your last code,
if (NewSL>Bid-MarketInfo.....
with an example. I have read in mq4, but my englisch not so good.
I have tested the code with your last statement, and now on a few trades the EA modifed the SL, but not on all.
Thanks
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello together,
my problem is that the EA didn´t change the Stoploss. Example low[1] 1.0000 low[0 current candle] 1.0009, so now current candle is closed and
low[2] 1.0000 low[1] 1.0009. the new stoploss must be changed to 1.0009 ......
Can anybody help me.
Andy
int HandleOpenPositions()
{int cnt; int NewSL; int j;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol() )continue;
if (OrderMagicNumber()!=MagicNumber) continue;
if(OrderType()==OP_BUY)
{ for( j=0;j>=Bars;j--)
if(Low[1+j]>Low[2+j])
NewSL=Low[1+j];
OrderModify(OrderTicket(), OrderOpenPrice(), NewSL,OrderTakeProfit(),0,0);}
if(OrderType()==OP_SELL)
{ for( j=0;j>=Bars;j--)
if(High[j+1]<High[j+2])
NewSL=High[j+1];
OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,0);}}}