OrderClose activated by Stochastic

 
Hi, I've got this EA here that uses the MA and stochastic to enter trades and has a set TP in pips but I'd like to add an orderclose function that would close the sell trades when the stochastic goes below 20 and close buy trades when it goes above 80. Unfortunately when I tried to do this the order is immediately closed as soon as it's opened, can anyone advise? cheers in advance!
//+------------------------------------------------------------------+
//|                                                       Daily1.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int TakeProfit = 100;
extern int StopLoss = 20;
extern double LotSize = 1.0;
extern int MagicNumber = 1234;
double Pips;






//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize == 0.00001 || ticksize == 0.001)
   Pips = ticksize*10;
   else Pips = ticksize;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double movingAverage = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
   double K0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double D0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   double K1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,1);
   double D1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   
   if(Close[0]<movingAverage)
   if(K0 < 20)
   if(OrdersTotal()==0)
   OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Pips),Ask+(TakeProfit*Pips),NULL,MagicNumber,0,Green);  
   


   if(Close[0]>movingAverage)
   if(K0 > 80)  
   if(OrdersTotal()==0) 
   OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*Pips),Bid-(TakeProfit*Pips),NULL,MagicNumber,0,Red);




   OrderSelect(SELECT_BY_POS, MODE_TRADES);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber);
   if(OrderType()==OP_BUY);
   if(K0 < 80) ;
   OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);


   OrderSelect(SELECT_BY_POS, MODE_TRADES);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber);
   if(OrderType()==OP_SELL);
   if(K0 > 20);
   OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

}
 
henrygeer:
Hi, I've got this EA here that uses the MA and stochastic to enter trades and has a set TP in pips but I'd like to add an orderclose function that would close the sell trades when the stochastic goes below 20 and close buy trades when it goes above 80. Unfortunately when I tried to do this the order is immediately closed as soon as it's opened, can anyone advise? cheers in advance!
  1. As a native English speaker, you should know that you don't "got this EA here", you have an EA. Treat your language with better respect.
  2. Stochastic is the difference between two moving averages. They don't go below 20 or above 80. #3.1
 
William Roeder:
  1. As a native English speaker, you should know that you don't "got this EA here", you have an EA. Treat your language with better respect.
  2. Stochastic is the difference between two moving averages. They don't go below 20 or above 80.

1. What's happening when the blue or red line goes above 80? (or below 20)

2. This part works so it clearly does go above 80 other wise it wouldn't open trades.

if(K0 > 80)  
   if(OrdersTotal()==0) 
   OrderSend

3. Why comment if you're not going to help.

4. Thank you so kindly for the English lesson that I didn't ask for.

 
  1. I apologize. Pre-coffee, I switched Stochastic and MACD.
  2. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

  3. Your welcome.
 
William Roeder:
  1. I apologize. Pre-coffee, I switched Stochastic and MACD.
  2. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

  3. Your welcome.

How have I not stated a problem?? " I'd like to add an orderclose function that would close the sell trades when the stochastic goes below 20 and close buy trades when it goes above 80. Unfortunately when I tried to do this the order is immediately closed as soon as it's opened "

As for my attempt, are you blind? can you not see that whole page of code I posted using the code button???

 
henrygeer: … but I'd like … Unfortunately when I tried … I posted using the code button???
  1. "I'd like" is a want not a problem.

  2. Tried is not a problem. Where have you debugged it? What are the variable values.

  3. Immediately close is a symptom, not a problem. You posted code, you have done anything else. Do you really expect us to debug your code for you? Debug your broken code. Find the problem.

  4.    OrderSelect(SELECT_BY_POS, MODE_TRADES);
    This shouldn't even compile. It certainly will not work.

  5.    if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber);
       if(OrderType()==OP_BUY);
       if(K0 < 80) ;
    
    
       OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
    You test, do nothing. You test, do nothing. You test, do nothing. Then close. Why do you think it should not immediately close every time?

  6. Check your return codes, and report your errors. If you had done this simple thing, you would have found have of your problems.

    Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  7. OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*Pips),Ask+(TakeProfit*Pips),NULL,MagicNumber,0,Green); 

    You buy at the Ask and sell at the Bid. You do not close buy orders 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 the 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 at 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 spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

Reason: