I am struggling to code an OrderClose to my EA can anyone help?

 
Hi, i'm trying to create an EA that enters a trade depending on whether the signal line is above or below the main line on the stochastic indicator. I would like it to close the current order when the lines cross but I do not know how to do it. I am using an optimal lot size calcultor so each trade has a different lot size depending on the stop loss but when I close the order I would like it to close the whole order.
#include <CustomFunctions02.mqh>


int magicNB = 2020000;

input int bbPeriod = 50;

input int bandStdLossExit = 6;

input double riskPerTrade = 0.02;

input int kPeriod = 21;

input int dPeriod = 7;

input int slowing = 7;

int openOrderID;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert("");
   Alert("Starting Stoch strategy");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Alert("The Stoch strategy Just closed");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
   double bbLossUpper = iBands(NULL,0,bbPeriod,bandStdLossExit,0,PRICE_CLOSE,MODE_UPPER,0);
   double bbLossLower = iBands(NULL,0,bbPeriod,bandStdLossExit,0,PRICE_CLOSE,MODE_LOWER,0);
   
   double StochSignal = iStochastic(NULL,0,kPeriod,dPeriod,slowing,MODE_SMA,1,MODE_SIGNAL,0);
   double StochMain = iStochastic(NULL,0,kPeriod,dPeriod,slowing,MODE_SMA,1,MODE_MAIN,0);
   
   
   if(!CheckIfOpenOrdersByMagicNB(magicNB))//if no open orders, we try to enter a new position
   {
   if(StochMain > StochSignal)//buying

   {
      Print("");
      Print("Signal price above main line, Sending buy order");

      double stopLossPrice = NormalizeDouble(bbLossLower,Digits);

      Print("Entry Price = " + Ask);

      Print("Stop Loss Price = " + stopLossPrice);
      
      double lotSize = OptimalLotSize(riskPerTrade,Ask,stopLossPrice);
      
      Print("Optimal Lot Size: " + lotSize);

      

      openOrderID = OrderSend(NULL,OP_BUYLIMIT,lotSize,Ask,10,stopLossPrice,NULL,NULL,magicNB);
      
     if(StochSignal > StochMain) //I dont know how to do this
          {
          OrderClose(magicNB,1,Bid,10);
          }

      if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());

   }

   else if(StochMain < StochSignal)//shorting

   {
      
      Print("");
      Print("Main line above signal price, Sending short order");

      double stopLossPrice = NormalizeDouble(bbLossUpper,Digits);

      Print("Entry Price = " + Bid);

      Print("Stop Loss Price = " + stopLossPrice);

      double lotSize = OptimalLotSize(riskPerTrade,Bid,stopLossPrice);
      
      Print("Optimal Lot Size: " + lotSize);
          

          openOrderID = OrderSend(NULL,OP_SELLLIMIT,lotSize,Bid,10,stopLossPrice,NULL,NULL,magicNB);
          
          if(StochSignal > StochMain) //I dont know how to do this
          {
          OrderClose(magicNB,1,Ask,10);
          }

          if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());

   }

   }


 
 
   
   
  }
 
  1.    double bbLossUpper = iBands(NULL,0,bbPeriod,bandStdLossExit,0,PRICE_CLOSE,MODE_UPPER,0);

    Why did you post your MT4 question in the MT5 EA 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. Francesco Pareti I would like it to close the current order when the lines cross but I do not know how to do it.

    Look for a cross.

    double aPrev = …(i+1),  aCurr = …(i),
           bPrev = …(i+1), bCurr = …(i);
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
           isCross = isUp != wasUp;
    Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help (2017)

Reason: