EA not making any sell trades

To add comments, please log in or register
Anthony
36
Anthony  
I have an EA that waits for a MACD to cross over, then it see's if the price action is above the top bollinger for a sell or below the bottom bollinger to buy, in the hope that as it reaches that level and bounces back. From here i check whether the next bar, within a user defined range has broken the level of the setup bar to take the trade. All works well except i'm not getting any sell signals and sell orders. Logically i see nothing wrong with the code, as it's based off the MACD sample and Moving Average examples given with MetaTrader and i can see where i should be getting signals, but the orders never appear, it only wants to take long (buy) signals. If anyone could help me with this problem it would be greatly appreciated. The code i'm using is attached below.
Files:
tryqthis.mq4 5 kb
fbj
1047
fbj  

Hello AK

.

Looking at your code... no idea what it is doing simply due to no documentation and [for me at least] not understood presets and use of constants.

#define is a great item to use: gives meaningful id to constant and allows single point of change no matter how large source is...

You will easily solve your no sell issue by simply: documenting your code.

Having to write meaningful comments with the why's of a code block and/or variable usage will instantly clarify [to you] what is going on.

.

Additionally, I see no relevant Print() statements placed at areas of interest for informing you what's going on...

Print() used as a trace tool is VIP medicine and if so desired can preface each trace statement/block with an extern flag so can toggle on/off...

.

Anyway, nice to see readable code - is obvious that you have made effort regards source format - many do not...

.

Regards

Anthony
36
Anthony  

Sorry about that, i had print statements at every level but i removed them before posting the code, as it printed several lines at every tick, thought it would just irritate people, as it was irritating me.

Essentially i work with ESignal, so have had to convert everything over, mainly in the way that ESignal lets u choose the option to only calculate on the close of bars, not every tick, which is why i've had to add extra functions to ensure certain variables are only calculated once per bar.

Here's a full run down of the code:

I wait for the MACD to cross, ie, the previous bar MACD to be above 0 and the bar before that has a MACD below 0, or vice versa. We use the NewBar function to make sure it's the beginning of the bar and then change the variable to 1 if this occurs. We only check trade conditions if the MACD has crossed. We check whether the bars have opened above/below the bollingers, depending on whether it's a buy or sell signal. We ensure that we only get one sell at a time, that is, as soon asthe conditions are checked, whether met or not, we change the alert variable so we can't get the same signal. This ensures that if we take a trade and close straightaway, we won't get another buy on the same bar, next tick, as essentially the condtions will still be met. If the conditions are met we change the crossing variable to 0, to ensure we're not looking for signals anymore, then set a buy or sell variable 0, to say we have a signal and to start looking for a trade. We check that the bar is within the setuplength, defined by the user, then check whether price action has broken, if so, we take a trade. If we don't meet the criteria, but within the range, we increment the bar counter by one per bar, again, using the NewBar function to ensure it only happens once a bar, not each tick. If the bar count is outside the range, it's changed to 100 to make sure it's way over the range. When a trade is closed, we make the crossing variable 0 to ensure that if the MACD crossed during a trade it doesn't open a new order as soon as we close a trade.

Lines of code are printed at each tick, stating the crossing variable, alert variable, sell and buy counters. Essentially if nothing is happening it should say that both variables are 0 and counts are 100. When the MACD crosses crossing should be 1 and last alert should be 0. If we meet the criteria for a trade, that count should be 0, last alert equal to 1 or 2 depending on the trade and crossing is 0. Sell/Buy signal should also be printed. Then the count should change if the criteria isn't met, if it is, the count will be set back to 100, "enter trade" will be printed, trade will be executed, then "trade entered" is printed. Until the order is closed, the counts will remain 100, the crossing variable and alert can change as the MACD crosses, but will have no effect. Order closed will be printed when an order is closed, with the counts put to 100 and the variables set to 0.

The new code with all the comments and print statements is attached now. It seems to give buy and sell signals as the chart runs, but will not give sell signals in the backtester, which is a problem as i can't logically find a reason why it shouldn't take any. It seems to print enter trade and trade entered, meaning it's completing that chunk of code, but doesn;t open a trade, as if it's skipping that line of code for no apparent reason.

Thanks in advance for any insight you can provide,

AK

Files:
tryithis.mq4 8 kb
To add comments, please log in or register