Hello guys, Total MQL4 noob 16 year old here.
Having a go at writing my first EA, and I've been trying to implement trailing stops by creating a user-defined function, but I can't seem to get it to work. I'm sure I've just done something stupid, but some help would be much appreciated.
I am getting the error "Unbalanced Right Parenthesis" As well as "Function Definition Unexpected"
Sort your indenting out and you will probably see the braces issue for yourself . . . or just count up the right and left braces, the number of each has to match . . .
The function has to be declared outside of all other functions . . .
Sort your indenting out and you will probably see the braces issue for yourself . . . or just count up the right and left braces, the number of each has to match . . .
The function has to be declared outside of all other functions . . .
if(OrderType == OP_BUY)//forgotten ()
also
else if(OrderType == OP_SELL)
and don't modify every point difference
also
and don't modify every point difference
Fixed it I think, Thanks for the help.
OrderSymbol == Symbol())
I went through and got them all after you posted the first one, thanks tho. I think the problem I was having was that I had it inside the main EA start function. If it was outside the compiler would have picked up basic stuff like that. I've got it to compile, but it doesn't seem to be implementing the stops on the strategy tester. My revised code is as follows:
int cnt,total; void MoveTrailingStop() { for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderType()<=OP_SELL && OrderSymbol() == Symbol()) { if(OrderType() == OP_BUY) { if(TrailingStop>0) { if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-TrailingStop*Point,Digits)) || (OrderStopLoss()== 0)) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Red); } } } else if(OrderType() == OP_SELL) { if(TrailingStop>0) { if((NormalizeDouble(OrderStopLoss(),Digits)>NormalizeDouble(Ask+TrailingStop*Point,Digits))|| (OrderStopLoss() == 0)) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*Point,Digits),OrderTakeProfit(),0,Green); } } } } } }
I then have this up earlier in the codeif(TrailingStop>0) MoveTrailingStop();
Not sure why it's not putting in the stops, if you could offer any insight would much appreciate it.
I went through and got them all after you posted the first one, thanks tho. I think the problem I was having was that I had it inside the main EA start function. If it was outside the compiler would have picked up basic stuff like that. I've got it to compile, but it doesn't seem to be implementing the stops on the strategy tester. My revised code is as follows:
Not sure why it's not putting in the stops, if you could offer any insight would much appreciate it.
Some suggestions to work on
- your total looks still to be zero total has to be made value OrdersTotal() when you start the loop
- make use of magicnumber inside your code
- check if modify succeed and if it fails why it fails
- move first breakeven instead of directly trailing orderstoploss
- make it work for 4 as 5 digitbrokers as well
- use trailingstep so that you not trail every point
- checking trades count down like for(int i= OrdersTotal()-1; i>=0 ; i--)
- don't use normalizeDouble (example : if(OrderStopLoss()<Bid-((TrailingStop+TrailingStep)*pips2dbl)||(OrderStopLoss()== 0)) )
- use one loop if you count your trades you also can do trailingstop
I often see code posted with lots of if conditions following each Other, makes it difficult to find unmatched { } even in one's own code.
Can somebody experienced please confirm that this code does exactly the same as Doomanx's.
void MoveTrailingStop() { for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderType()<=OP_SELL && OrderSymbol()== Symbol() ) { if(OrderType() == OP_BUY && TrailingStop>0 && (NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-TrailingStop*Point,Digits) || OrderStopLoss()==0) ) OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Red); if(OrderType() == OP_SELL && TrailingStop>0 && (NormalizeDouble(OrderStopLoss(),Digits)>NormalizeDouble(Ask+TrailingStop*Point,Digits)|| OrderStopLoss() == 0)) OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*Point,Digits),OrderTakeProfit(),0,Green); } } }
I often see code posted with lots of if conditions following each Other, makes it difficult to find unmatched { } even in one's own code.
Can somebody experienced please confirm that this code does exactly the same as Doomanx's.
According to me it is
although having it more readable if { } is correct
to my opinion it makes it hard to check ( ) if it is over more lines then one
normally i have one loop checking open trades in start....
void MoveTrailingStop() //lot of ea's separate functions count trades count orderprofit count.... { int error; double pBid = Bid; double pAsk = Ask; EATrades = 0; //if you have to do a loop through open trades make it do all actions in one time BUYS = 0; //so it is not needed to do this several times every tick makes ea faster in backtesting/working SELLS = 0; //you can make it close trades also inside for(int i= OrdersTotal()-1; i>=0 ; i--) //count down checking trades { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break; if(OrderSymbol()!=Symbol()||OrderMagicNumber()!= magic.number) continue; //make use of magicnumber error = 0; EATrades ++; double SL = OrderStopLoss(); double TP = OrderTakeProfit(); if(OrderType() == OP_BUY) { BUYS++; if(TrailingStop>0) { if(OrderOpenPrice()<pBid-(TrailingStop*pips2dbl))//starts at breakeven this way { if(OrderStopLoss()<pBid-((TrailingStop+TrailingStep)*pips2dbl)) //use trailingstep {SL = pBid-(TrailingStop*pips2dbl);} //pips2dbl make it work 4 / 5 digit accounts } } } if(OrderType() == OP_SELL) { SELLS++; if(TrailingStop>0) { if(OrderOpenPrice()>pAsk+(TrailingStop*pips2dbl)) { if(OrderStopLoss()>pAsk+(TrailingStop+TrailingStep)*pips2dbl||(OrderStopLoss()== 0)) {SL = pAsk+(TrailingStop*pips2dbl);} } } } if(SL != OrderStopLoss()||TP != OrderTakeProfit()) { bool ordermodify = OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White); if ( ordermodify > 0 ) { if(OrderType()==OP_SELL)Print("Modify Sell ",OrderTicket(), " done!"); if(OrderType()==OP_BUY)Print("Modify Buy ",OrderTicket(), " done!"); OrderPrint(); continue; } else { error = GetLastError(); if(OrderType()==OP_SELL)Print("Modification sell ",OrderTicket()," failed! GetLastError = ", error, ", ErrorDescription = \"", ErrorDescription( error ), "\"" ); if(OrderType()==OP_BUY)Print("Modification buy ",OrderTicket()," failed! GetLastError = ", error, ", ErrorDescription = \"", ErrorDescription( error ), "\"" ); } //modify checked if it fails you find why } } }
this is with making inside the most suggestions i did
- 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 guys, Total MQL4 noob 16 year old here.
Having a go at writing my first EA, and I've been trying to implement trailing stops by creating a user-defined function, but I can't seem to get it to work. I'm sure I've just done something stupid, but some help would be much appreciated.
I am getting the error "Unbalanced Right Parenthesis" As well as "Function Definition Unexpected"
Code is here:
Thanks a lot, Sorry for wasting anyone's time
Dooman