MQL4 EA takes long positions but not short positions -- the code is almost the same

 

Hello, novice here. I have an EA which is supposed to take long and short positions. It is only taking long positions. The code used to take create a long or short position is almost the same, the only difference is that it is mirrored. I've already searched on Google, the MQL5 forums, Reddit, and I've referenced the official MQL4 documentation. I have rewritten this code from blank. I also showed this to another novice who could not find the bug, either.

What is supposed to happen;

- Line 1 is checking if the program should open a short position. Line 11 is checking if the program should open a long position.

-Line 2 is making sure that the stop loss distance is greater than the value of 0.05. Since this is a short position, the stop loss subtracts the entry to return the difference. Line 12 is a mirror opposite.

-The program is not getting past Line 2, even though it IS getting past Line 12.

1  if (isShort == true) {
2    if (stopLoss - entry > 0.05) {
3        bool isMarginAvailable = IsMarginAvailable();
4        if (isMarginAvailable == true) {
5            OrderSend(symbol, OP_SELL, lotSize, MarketInfo(symbol,MODE_BID), 6slippage, stopLoss, takeProfit);
7        }
8     }
9  }
10                              
11  if (isShort == false) {
12     if (entry - stopLoss > 0.05) {
13       bool isMarginAvailable = IsMarginAvailable();
14       if (isMarginAvailable == true) {
15          OrderSend(symbol, OP_BUY, lotSize, MarketInfo(symbol,MODE_ASK), 16slippage, stopLoss, takeProfit);
17       }                                    
18     }
19  }

Starting Line 11 at if(isShort==false){} and everything inside that if-statement works fine and as expected.

On Line 1, if(isShort==true){} stops working on Line 2 at the statement if(stopLoss - entry > 0.05){}. I have removed the Print statements to clean up the code, but the program won't go any further than Line 2.

All of the inputs are the same for Line 1 and Line 11. I triple checked the functions that the variables are connected to, in order to be sure that I didn't make a silly mistake. Unless I have grossly missed something, I believe that everything else is functioning as intended. If needed, please tell me if it would be beneficial to post these other functions.

Also note that the program runs in the Strategy Tester in the MT4 terminal without issue; there are no error messages in the terminal. It would appear as though the program is working normally, except the Results tab in the Strategy Tester reveals that only buy orders are being sent.

The program also compiles without error.

Thank you in advance


Progress to original post: The program is still NOT working as of updating this post.

-In another forum, it was recommended that I try to split the isShort function into an isShort/isLong function. This still did not change the results.


-I removed the function that initialized the variable isShort and placed its code where the function call had been. This did not change the results. Here is the code for what I tried;



if (closePrev > PREVUPPERBB) {
   isShort = true;
   Print("isShort = true; " + isShort);
}
if (closePrev < PREVLOWERBB) {
   isShort = false;
   Print("isShort = false;" + isShort);
}


The ironic part about the error in this code is that the above Print statements return the following;

-the first if-statement prints "isShort = true; TRUE" and the second if-statement prints "isShort = false;FALSE". It appears that the variable isShort is initializing correctly, but as can be seen in the body of the main post, the program continues to refuse to place Short orders.


To confirm, I have the Strategy Tester set to take both Long and Short positions.


ANOTHER UPDATE - I have still been trying to find the issue myself and the program is still not taking short positions. I would have preferred to save separate issues for separate posts, but I'm not sure if the other flaw in my code is related to the issue mentioned in this post -- the OrderSend() function is not returning a ticket number. As I did before, I checked the MQL4 documentation and read through the MQL4 forums about ticket numbers not being returned in the EA, and it appears that the program will not return a ticket number...


Admittedly, I'm a novice programmer and perhaps I've made a serious oversight somewhere in the code. Thank you in advance for taking your time to read over this post. I can post more code if necessary.


Thank you

 

Topics concerning MT4 and MQL4 have their own section.

You haver been told this before

In future please post in the correct section or your topic may just be deleted.

I will move your topic to the MQL4 and Metatrader 4 section.

Do not double post!

I will delete your other duplicate topic.

Post code that compiles.

OrderSend(symbol, OP_SELL, lotSize, MarketInfo(symbol,MODE_BID), 6slippage, stopLoss, takeProfit);
OrderSend(symbol, OP_BUY, lotSize, MarketInfo(symbol,MODE_ASK), 16slippage, stopLoss, takeProfit);

These are not valid variable names.