EA only opens buys and not sells can anyone help?

 
source file attached
Files:
 
mheathcote89: source file attached

You subtract the highest from the lowest (in the same range) then the difference will always be negative and never positive.

Also, a sell is placed at the Bid price, not the Ask price (your code is using the Ask price). A Buy opens at Ask and closes (T/P, S/L, etc.) at Bid, while a Sell opens at Bid and closes (T/P, S/L, etc.) at Ask.

int highestcandleprice= iHighest(_Symbol, PERIOD_M30,MODE_HIGH,X,0);
int lowestcandleprice= iLowest(_Symbol, PERIOD_M30,MODE_LOW,X,0);
double pricedifference;
pricedifference = (lowestcandleprice-highestcandleprice);

//---open sell trade

if  (pricedifference > 0 && TradingIsAllowed==true && OrdersTotal()== 0)
int sellticket = OrderSend
   (Symbol(),OP_SELL,0.10,Ask,3,Ask+500*_Point,Ask-500*_Point,NULL,0,0,Green);
 
int buyticket = OrderSend(Symbol(),OP_BUY,0.10,Ask,3,Ask-500* _Point,Ask+500*_Point,NULL,0,0,Green);
    

int sellticket = OrderSend(Symbol(),OP_SELL,0.10,Ask,3,Ask+500*_Point,Ask-500*_Point,NULL,0,0,Green);
    
  1. Why did you post your MT4 question in the MT5 Systems section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

 
Thankyou both, this is the first time using the forum but ill use the right one next time
 
Fernando Carreiro #:

You subtract the highest from the lowest (in the same range) then the difference will always be negative and never positive.

Also, a sell is placed at the Bid price, not the Ask price (your code is using the Ask price). A Buy opens at Ask and closes (T/P, S/L, etc.) at Bid, while a Sell opens at Bid and closes (T/P, S/L, etc.) at Ask.

yes so i've know changed  to pricedifference = (Close[6]-Close[1]); but i still get same issue. only buys and not when it should.

 
mheathcote89 #: yes so i've know changed  to pricedifference = (Close[6]-Close[1]); but i still get same issue. only buys and not when it should.

Print out the values returned by "Bars", "Close[6]", "Close[1]" and "pricedifference" and verify that they are in fact getting the values you expect or not.

EDIT: You must learn to debug your own code. Otherwise you will be returning here on almost every single line that does not meet your expectation.

 
Fernando Carreiro #:

Print out the values returned by "Bars", "Close[6]", "Close[1]" and "pricedifference" and verify that they are in fact getting the values you expect or not.

EDIT: You must learn to debug your own code. Otherwise you will be returning here on almost every single line that does not meet your expectation.

Thanks, it wasn't the calculations it seems it was the structure of the overall code, i've re written it and its all working

Reason: