Order Execution Problems with my EA

 

Hey, guys. I'm making my first EA, but I've run into a problem. As soon as an order is placed, it is then immediately closed. Because of this, multiple orders are taken on the same bar which is also undesirable. If you try testing the advisor, you'll see what I mean.

The EA is based upon Bollinger bands, and it's supposed to work as follows:

If the upper band is touched, wait for price to cross center moving average. Once price action hits that MA, short. Exit position for profit when price touches lower band, or for loss when price touches upper band again. Signals reversed for long entries. There is also an additional stop loss parameter, just in case the market somehow runs away from your position without touching either band. I don't know how that would happen, but it's there just in case.

The retracement to the center MA before entry is to limit large losses. Often times, price can ride an upper band or lower band through a trend. If it entered without a retracement, there would be many huge losses on trends.

This EA is best run at the smallest time frame possible, since larger time frames will often touch both a band (upper or lower) as well as the center MA, causing the EA to enter and exit (for a loss) on every (or every other?) tick.

This is my first attempt at programming an Expert Advisor, and some of the code is from elsewhere, so it's pretty ugly and lacks any unifying style. Sorry about that, but I definitely appreciate any help that can be given to me. If you actually make modifications, please comment them in the source code or in the post, and tell me what I had wrong in detail.

Thanks for any help you can give me, guys!

Files:
 

I'm giving this a bump. Can someone look at the logic in this system and give me a clue as to where I've messed up?

 
mrebersv:
Hey, guys. I'm making my first EA, but I've run into a problem. As soon as an order is placed, it is then immediately closed. Because of this, multiple orders are taken on the same bar which is also undesirable. If you try testing the advisor, you'll see what I mean.

The EA is based upon Bollinger bands, and it's supposed to work as follows:

If the upper band is touched, wait for price to cross center moving average. Once price action hits that MA, short. Exit position for profit when price touches lower band, or for loss when price touches upper band again. Signals reversed for long entries. There is also an additional stop loss parameter, just in case the market somehow runs away from your position without touching either band. I don't know how that would happen, but it's there just in case.

The retracement to the center MA before entry is to limit large losses. Often times, price can ride an upper band or lower band through a trend. If it entered without a retracement, there would be many huge losses on trends.

This EA is best run at the smallest time frame possible, since larger time frames will often touch both a band (upper or lower) as well as the center MA, causing the EA to enter and exit (for a loss) on every (or every other?) tick.

This is my first attempt at programming an Expert Advisor, and some of the code is from elsewhere, so it's pretty ugly and lacks any unifying style. Sorry about that, but I definitely appreciate any help that can be given to me. If you actually make modifications, please comment them in the source code or in the post, and tell me what I had wrong in detail.

Thanks for any help you can give me, guys!

EA as posted will not place any orders due to Error 138. Your EA can not call Buy orders at the Bid price or Sell orders at the Ask. You need change both Buy & Sell OrderSend lines.

I made this change and EA placed orders, but as you stated immediately closed the orders. Your OrderClose logic is not in sync with OrderSend Logic. The span from High[0] to Low[0] on one bar can trigger both open and close due to their span lenght. Example: On same bar High[0] crosses upperband and then reverse to MA to open short, High[0] of that bar is already above upperband which is your close signal. Try using Ask/Bid in place of High[0]/Low[0] in close triggers.

Also, it seems your open order triggers are not in sync. Sell example: on same bar, Low[0] is below MA (status 2=true) and then High[0] rises and crosses upperband (status 1 = true). A sell order is triggered, but not as you intended. To make upperband cross happen first, try putting in a 1 bar delay after upperband cross or try using Bid for MA reverse cross.

I hope this helps.

Wackena

 

Thanks, Wackena. I definitely knew that any bar where it crossed both an outer band and the central MA would cause a problem. I'll switch to use Bid and Ask instead of High and Low.

I'll switch the Bid and Ask around in the Buy/Sell orders.

 

Hi guys, I post also here my problem , maybe somebody met the same already.

i coded my system into an ea which trades alone. It works fine,

But I have one problem :

I compiled the EA and let it run in, but when I try to send the ordersend() function I get 'ordersend() function not supported from EA' ?!

Anybody knows what the problem might be ?

under which format should I compile the code ?

One more thing please : anybody has good advices for reliable mt4 brokers to let an EA go 24x24 with trust ?

Any help is appreciated

Thank you

Etts

 

etts, I answered this here. Make sure the function names are correct. Use F1 on a function name to find the format, or use the Dictionary tab to verify the names of functions.

 
zuijlen:
etts, I answered this here. Make sure the function names are correct. Use F1 on a function name to find the format, or use the Dictionary tab to verify the names of functions.

Hi zuijlen,

I saw the other post, thank you. I replyed to it on the other secton