EMA Channel Breakout

Hello to everybody!
i’m new in the forum and in the forex markets,i need help with this EA ,that i try to code.
The system is based on the breakout of the hi-lo 21ema channel.
You put a buystop order on the high of a candle that open below and close above the upper line of the ema,
and you put a sellstop order at the low of a candle that open above and close below the lower line of the ema.
If a new entry condition appear the system delete the previous pending order and generate a new one.
For close a position i use the break of the opposite line(example…if you go long you’ll close the position on the brakout of the lower ema line)and a take profit/stop loss method.

I’m not an expert coder as you’ll see in the EA,i had make a copy/paste from other EA and wrote few lines by myself

i have some problems with the EA ,first of all the famous “error 130” and then with the trailing stop loss.
it doesn’t work with the short position,and in the long position does not work as i want(it contiously change the stoploss level ,while i want that once the price goes up for xx pips the sl move up xx pips and if the price go back i dont want that sl to change again)

pleas help me to make the right corrections in the code and give me some input/advice to improve it.
thx to all!

P.S. sorry for my english!

Hi blgi

I have had a look at your EA and it is nothing like the way I normally code mine I use an excellent book by Andrew R Young called “Expert Advisor Programming”.

Ok, that said I think your first problem is that you are trying to just place your SellStop or BuyStop pending orders directly in the market based on the variables, candle_l and candle_h.

Taken from the above book page 46 the broker stop level is the minimum number of pips away from the current Ask or Bid price that all stops and pending orders must be placed.
This can be 3-4 pips above the Ask and 3-4 pips below the Bid. Alpari have wider stop levels as big as 8 pips.

I think your values are often to close to the current market value and explain why you get error 130.

The answer is something like this.

Double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point; This gives you the stop level size of your broker.

So your minimum upper stop level (entry level) will be the Ask+StopLevel and the lower stop level Bid-StopLevel.

So you will need to add some code like before the res = OderSend(OP_SELLSTOP and OP_BUYSTOP) order entries.

if(candle_h < UpperStopLevel) candle_l = UpperStopLevel + a few extra pips for safety. And for shorts if(candle_l > LowerStopLevel) candle_l = LowerStopLevel - a few extra pips for safety.

As for your trailing stop as far as I can tell the code looks like it will work, but you are using a TrailingStop = 55 pips and a StopLoss = 233 pips. So what I think is happening is the code is checking to see if the current price is > 55 pips in profit.

if(Bid-OrderOpenPrice()>TrailingStop*Point)

Then you check to see if(OrderStopLoss()<Bid-TrailingStop*Point) which it always will be as you place the stop at Bid-sl which is 233 pips away. I would have thought for this to work both TrailingStop (TS) and StopLoss (SL) should be the same value. So once the market moves > TS in your favour the SL starts trailing at the same value.

Regards, Trader9.

i’m sorry but i dont understand what i’have to do for the trailstop!

it seems that “error 130” is solved,but i still have probelm with the sell operation!

it works good for the long positon(no Takeprofit,only trailstop),but not for the short positions
what is wrong?

what i have to add?

this is the new version of the EA…
take a look please!

Hi blgi,

As I said before I believe your trailing stop code to be alright.

This trailing stop type is designed not to start moving the stop until a certain value of profit has been reached i.e. TS=55 pips in your case.

I had to use 550 pips as I have a five digit broker. Not sure if using SL=55*Point will work as it gave me 0.0006 pips. Which should have been 0.0055.

So, if you set TS=55 and SL=55 when the market moves more than 55 pips in your favour the stop is first moved to break even and then trailed from there at the SL value.

However in your case you had the SL set to 233 pips, so it was being set to the Bid value minus 233 pips, so would have appeared not to work or weird.

The way I believe it works is that it first tests to see if the market has moved in your direction buy more than TS.

if(Bid-OrderOpenPrice()>Point*TrailingStop)

Second, you then check to see where the current stop loss is placed and if it is greater than TS

if(OrderStopLoss()<Bid-Point*TrailingStop)

You then move your stop to to its new position Bid-sl. Which I believe was better than the (OrderStopLoss()+TS) you have changed it too.

if(!OrderModify(OrderTicket(),OrderOpenPrice(),(OrderStopLoss()+TS),0,0,Green))

Changing the code as you have won’t work, as you have also got the + and - reversed. Go back to Bid-sl and Ask+sl and set SL = 55 same as the TS = 55.

Note:- you could change Point*TrailingStop to TS as you already have this as a variable.

[B]Found your Sell trailing stop problem. There was a bug in your Sell trail code layout change it to this:-[/B]

if(OrderType()==OP_SELL)//close sell order
{
if(Close[0]>ma_buy)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
return;
}
//— check for sell trailing stop
if(TrailingStop>0 && UseTrailing==true)
{
if((OrderOpenPrice()-Ask)>TS)
{
if(OrderStopLoss()>(Ask+TS))
{
//— modify order and exit
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+sl,0,0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}

Thats it regards, Trader9.

Hi Trader9!!
Thank you so much!
it was only a problem of ( and )…i make these changes:
//— check for sell trailing stop

        if(TrailingStop&gt;0 && UseTrailing==true)
          {
           if((OrderOpenPrice()-Ask)&gt;Point*TrailingStop)
             {
              if(OrderStopLoss()&gt;(Ask+Point*TrailingStop)) 
                {
                 //--- modify order and exit
                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),(OrderStopLoss()-TS),0,0,Green))
                    Print("OrderModify error ",GetLastError());
                 return;
                }
             }
          }

i used (OrderStopLoss()-TS) instead of Ask-sl ,because in that way the stoploss will move in the direction of the profit and doesn’t make step back if the price start to goes up!

it seems to work now!
please take a look on the new version!

Hi trader9,just a question…
i use the EA on eur/jpy on H4,when i use the tester on metatrader4,if a "big candle"appear(ex…more than 200 pips) the trailstop moves only by the amount set in the EA(ex…sloss set at 200 from the open price and trailstop at 50…if the candle makes a move up of 150 pips the sl moves to 150 from the open price,only a 50 pips movement…
why not a 150 pips movement…it should change every 50 pips…
it’s a problem in the backtest routine,in the code or something else?

Hi blgi,

Good to here it is working.

Please read the beginning of my last post again.

As I said there, this type of trailing stop loss is confusing in operation, as it will only move the amount of the lowest of the two values.

So if you have a TS = 50 and SL =200 it will only move 50. So once your trade is up buy more than the TS = 50 pips it will start trailing your SL = 200, 200 pips below the Bid price. Your stop can never be closer than SL = 200.

For this type of stop to work as you think it should, it will only work that way if both values SL and TS are the same.

Example:- TS=50 and SL=50.

Nothing will happen until you are up 50 pips and then the stop will move to break even and then trail at 50 pips below the Bid price.

If you want anything else then you will need to alter the code to suit.

Regards, Trader9.

Hi trader9

i had make some modification to the code,but i have always some problem with the trail.
i had change some lines of the previous EA but the concept is the same,actually i try to move the stop loss to the break even plus a minimum gain.
i know that it’s the same error of the previous ea…:33::33:
the problem is that if i set usetrail stop to false everything is ok,but if i set it true the tester print “modify error 130”…
can you help me one more time!?MA_hi_lo_channel.zip (26 KB)

Hi Blgi,

You have made this way to complicated with so many calculations based on other calculations that it is almost impossible to find any bugs. I will have a look, but my first thought is that you have to many decimal places in the new stop loss value. Also using Point all the time can be a problem depending on your broker.

While you are waiting for my reply go to Amazon and download the Kindle book below.

[B]Easy Automated Trading: Simplified coding for metatrader 4 [Kindle Edition][/B]

This book is less than £7.00 and has a channel breakout EA that does all you want. The book also has lots of other EA’s and indicators written similar to your style. All the code in the book is available for free from the authors website and you can log in and get help from the author, as well.

The EA you want is the Donc channel breakout with extras.

The book is very good and there is a lot of useful information there.

Regards for now, Trader9.

Or, why don’t you just automate the coding and focus 100% on the logic and money management - with a graphical builder… There are lots of free builders out there. IMO coding eas by hand is a bit old-school and useless work :wink:

Hi blgi,

First it is not a good idea to use SL= StopLoss * Point, as dependant on your broker 4 or 5 digits it could give you a wrong value.

Read the Kindle book I told you about and you will see it is better to use the following.

Int digits = MarketInfo(Symbol(),MODE_DIGITS);
if(digits == 2 || digits == 3) double PipDigits = 0.1;
else if (digits == 4 || digits == 5) PipDigits = 0.0001;

Now SL=StopLoss*PipDigits.

This should be made into a function and run once when the EA is first loaded and PipDigits used instead of Point.

I looked at your EA 3, for the buy trailing stop you seem to want it to also move to breakeven.

If(TrailingStop >0 && UseTrailing==true). First you don’t need UseTrailing==true, as if TrailingStop = 0 nothing happens and if it is >0 then it runs.

When you have done your test you seem to want to place your new buy stop above the last bar high. This will be to high as your calculation is this.

newslbuy = Open_Buy + StopLevel. Where Open_Buy = candle_h+S_Pips.

It then gets a bit crazy with if(candle_h < UP_slevel) candle_h = UP_slevel ; Where UP_slevel = candle_h+StopLevel. To this you then add min_gain = 20pips*Point.

I cant see my way through this, but by being above the last high + safety pips + StopLevel + gain you must be above the Bid price which is a strange place for a buy stop.

However, try this NormalizeDouble(newsslbuy+Plus,PipDigits), as it will ensure that after all your calculations you at least have the correct number of decimal places.

Please read that book I mentioned in my last post before going any further.

Regards, Trader9.

Hi 5astelija,

I don’t agree with that, there is nothing more rewarding than writing your own EA and watching it work in the market.

If you are involved with EA’s in any way, then you are always going to want to make the odd change or two. Also it is important to understand how the EA is working, as it is your money on the line.

Regards, Trader9.

Yeah, its only my opinion, I just think the ea’s performance is most important thing, no matter how its made :slight_smile: With graphical interface I get better overview of the whole system, also saves a lot of time…

Hi 5astelija,would you suggest me something about MM system?

@trader9 i’ll try to make the changes in the code…and i’ll buy the book as soon as i can…thx!