My EA for mt5 only opens orders it doesnt close orders

 
#property copyright "objemmanuel"
#property link      "https://www.mql5.com"
#property version   "1.00"

input int TrendPeriod=5;
input int SmoothingCoefficient=2;
input int HistoryBars=5000;

input int stopLoss=100;
input int takeProfit=100;
input double volume=0.01;

enum orderType{
   orderBuy,
   orderSell
};

datetime candleTimes[],lastCandleTime;

MqlTradeRequest request;
MqlTradeResult result;
MqlTradeCheckResult checkResult;

bool checkNewCandle(datetime &candles[],datetime &last){
        bool newCandle=false;

        CopyTime(_Symbol,_Period,0,3,candles);

        if(last!=0){
                if(candles[0]>last){
                        newCandle=true;
                        last=candles[0];
                }
        }else{
                last=candles[0];
        }

        return newCandle;
}

bool closePosition(){
        double vol=0;
        long type=WRONG_VALUE;
        long posID=0;

        ZeroMemory(request);

        if(PositionSelect(_Symbol)){
                vol=PositionGetDouble(POSITION_VOLUME);
                type=PositionGetInteger(POSITION_TYPE);
                posID=PositionGetInteger(POSITION_IDENTIFIER);

                request.sl=PositionGetDouble(POSITION_SL);
                request.tp=PositionGetDouble(POSITION_TP);     
        }else{
                return false;
        }

        request.symbol=_Symbol;
        request.volume=vol;
        request.action=TRADE_ACTION_DEAL;
        request.type_filling=ORDER_FILLING_FOK;
        request.deviation=10;
        double price=0;


        if(type==POSITION_TYPE_BUY){
                //Buy
                request.type=ORDER_TYPE_BUY;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
        }else if(POSITION_TYPE_SELL){
                //Sell
                request.type=ORDER_TYPE_SELL;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
        }

        request.price=price;

        if(OrderCheck(request,checkResult)){
                Print("Checked!");
        }else{
                Print("Not correct! ERROR :"+IntegerToString(checkResult.retcode));
                return false;
        }

        if(OrderSend(request,result)){
                Print("Successful send!");
        }else{
                Print("Error order not send!");
                return false;
        }

        if(result.retcode==TRADE_RETCODE_DONE || result.retcode==TRADE_RETCODE_PLACED){
                Print("Trade Placed!");
                return true;
        }else{
                return false;
        }   
   
}

bool makePosition(orderType type){
        ZeroMemory(request);
        request.symbol=_Symbol;
        request.volume=volume;
        request.action=TRADE_ACTION_DEAL;
        request.type_filling=ORDER_FILLING_FOK;
        double price=0;

        if(type==orderBuy){
                //Buy
                request.type=ORDER_TYPE_BUY;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
                request.sl=NormalizeDouble(price-stopLoss,_Digits);
                request.tp=NormalizeDouble(price+takeProfit,_Digits);
                
        }else if(type==orderSell){
                //Sell
                request.type=ORDER_TYPE_SELL;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
                request.sl=NormalizeDouble(price+stopLoss,_Digits);
                request.tp=NormalizeDouble(price-takeProfit,_Digits);

        }
        request.deviation=10;
        request.price=price;


        if(OrderCheck(request,checkResult)){
                Print("Checked!");
        }else{
                Print("Not Checked! ERROR :"+IntegerToString(checkResult.retcode));
                return false;
        }

        if(OrderSend(request,result)){
                Print("Ordem enviada com sucesso!");
        }else{
                Print("Ordem não enviada!");
                return false;
        }

        if(result.retcode==TRADE_RETCODE_DONE || result.retcode==TRADE_RETCODE_PLACED){
                Print("Trade Placed!");
                return true;
        }else{
                return false;
        }
}

int OnInit(){
        ArraySetAsSeries(candleTimes,true);
        return(0);
}



void OnTick(){
        
        if(checkNewCandle(candleTimes,lastCandleTime)){
                double buy_signal[];
                double sell_signal[];
                ArraySetAsSeries(sell_signal,true);
                ArraySetAsSeries(buy_signal,true);
                double candleClose[];
                ArraySetAsSeries(candleClose,true);
                //----- define indicator handle
      int MagicTrend_handle = iCustom(Symbol(),0,"KT-Trend-Magic-Indicator");
      
      //----- Copying buffer into arrays
      CopyBuffer(MagicTrend_handle,0,1,1,buy_signal);
      CopyBuffer(MagicTrend_handle,1,1,1,sell_signal);
      
                CopyClose(_Symbol,_Period,0,3,candleClose);

                if(buy_signal[0]!=0){
                        //cross up
                        Print("Cross above!");
                        
                        makePosition(orderBuy);

                }else if(sell_signal[0]!=0){
                        //cross down
                        Print("Cross under!");
                        
                        makePosition(orderSell);     

                }
        }
  
}
bool closePosition(){
        
        if(checkNewCandle(candleTimes,lastCandleTime)){
                double buy_signal[];
                double sell_signal[];
                ArraySetAsSeries(sell_signal,true);
                ArraySetAsSeries(buy_signal,true);
                double candleClose[];
                ArraySetAsSeries(candleClose,true);
                //----- define indicator handle
      int MagicTrend_handle = iCustom(Symbol(),0,"KT-Trend-Magic-Indicator");
      
      //----- Copying buffer into arrays
      CopyBuffer(MagicTrend_handle,0,1,1,buy_signal);
      CopyBuffer(MagicTrend_handle,1,1,1,sell_signal);
      
                CopyClose(_Symbol,_Period,0,3,candleClose);

                if(buy_signal=0){
                        //cross up
                        Print("Cross above!");
                        
                        closePosition();

                }else if(sell_signal=0){
                        //cross down
                        Print("Cross under!");
                        
                        closePosition();     

                }
        }
  
}

Please guys i need your help on this
 
objemmanuel1997:

Do not double post.

I have deleted your duplicate topic.

 
objemmanuel1997:

I think, you have 2 options

a ) should have conditions to close order and call  closePosition()

b) or should call closePosition()

within Ontick() part of your program 


since I did not see any call for closePosition() in main process ( only there is call for open order),

wish you healthy days..

 
nail sertoglu:

I think, you have 2 options

a ) should have conditions to close order and call  closePosition()

b) or should call closePosition()

within Ontick() part of your program 


since I did not see any call for closePosition() in main process ( only there is call for open order),

wish you healthy days..

still not working sir

 
objemmanuel1997:

still not working sir

I do not have your indicator; found something similar and revised your code and when i compiled saw that

a ) ur code has 2 closePosition() routine but cannot do same operations, 

b)  in testing i look at the last page of "Strategy Tester Visualization"  and see " 2020.03.09 01:15:00   Not Checked! ERROR :10016 " ; so "NO BUY or SELL order could not opened" as you mentioned you have orders opened but EA did not close any.

Please check your code again .. 

 
nail sertoglu:

I do not have your indicator; found something similar and revised your code and when i compiled saw that

a ) ur code has 2 closePosition() routine but cannot do same operations, 

b)  in testing i look at the last page of "Strategy Tester Visualization"  and see " 2020.03.09 01:15:00   Not Checked! ERROR :10016 " ; so "NO BUY or SELL order could not opened" as you mentioned you have orders opened but EA did not close any.

Please check your code again .. 

#property copyright "objemmanuel"
#property link      "https://www.mql5.com"
#property version   "1.00"

input int TrendPeriod=5;
input int SmoothingCoefficient=2;
input int HistoryBars=5000;

input int stopLoss=100;
input int takeProfit=100;
input double volume=0.01;


enum orderType{
   orderBuy,
   orderSell
};

datetime candleTimes[],lastCandleTime;

MqlTradeRequest request;
MqlTradeResult result;
MqlTradeCheckResult checkResult;

bool checkNewCandle(datetime &candles[],datetime &last){
        bool newCandle=false;

        CopyTime(_Symbol,_Period,0,3,candles);

        if(last!=0){
                if(candles[0]>last){
                        newCandle=true;
                        last=candles[0];
                }
        }else{
                last=candles[0];
        }

        return newCandle;
}

bool closePosition(){
        double vol=0;
        long type=WRONG_VALUE;
        long posID=0;

        ZeroMemory(request);

        if(PositionSelect(_Symbol)){
                vol=PositionGetDouble(POSITION_VOLUME);
                type=PositionGetInteger(POSITION_TYPE);
                posID=PositionGetInteger(POSITION_IDENTIFIER);

                request.sl=PositionGetDouble(POSITION_SL);
                request.tp=PositionGetDouble(POSITION_TP);     
        }else{
                return false;
        }

        request.symbol=_Symbol;
        request.volume=vol;
        request.action=TRADE_ACTION_DEAL;
        request.type_filling=ORDER_FILLING_FOK;
        request.deviation=10;
        double price=0;


        if(type==POSITION_TYPE_BUY){
                //Buy
                request.type=ORDER_TYPE_BUY;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
        }else if(POSITION_TYPE_SELL){
                //Sell
                request.type=ORDER_TYPE_SELL;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
        }

        request.price=price;

        if(OrderCheck(request,checkResult)){
                Print("Checked!");
        }else{
                Print("Not correct! ERROR :"+IntegerToString(checkResult.retcode));
                return false;
        }

        if(OrderSend(request,result)){
                Print("Successful send!");
        }else{
                Print("Error order not send!");
                return false;
        }

        if(result.retcode==TRADE_RETCODE_DONE || result.retcode==TRADE_RETCODE_PLACED){
                Print("Trade Placed!");
                return true;
        }else{
                return false;
        }   
   
}

bool makePosition(orderType type){
        ZeroMemory(request);
        request.symbol=_Symbol;
        request.volume=volume;
        request.action=TRADE_ACTION_DEAL;
        request.type_filling=ORDER_FILLING_FOK;
        double price=0;

        if(type==orderBuy){
                //Buy
                request.type=ORDER_TYPE_BUY;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
                request.sl=NormalizeDouble(price-stopLoss,_Digits);
                request.tp=NormalizeDouble(price+takeProfit,_Digits);
                
        }else if(type==orderSell){
                //Sell
                request.type=ORDER_TYPE_SELL;
                price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
                request.sl=NormalizeDouble(price+stopLoss,_Digits);
                request.tp=NormalizeDouble(price-takeProfit,_Digits);

        }
        request.deviation=10;
        request.price=price;


        if(OrderCheck(request,checkResult)){
                Print("Checked!");
        }else{
                Print("Not Checked! ERROR :"+IntegerToString(checkResult.retcode));
                return false;
        }

        if(OrderSend(request,result)){
                Print("Ordem enviada com sucesso!");
        }else{
                Print("Ordem não enviada!");
                return false;
        }

        if(result.retcode==TRADE_RETCODE_DONE || result.retcode==TRADE_RETCODE_PLACED){
                Print("Trade Placed!");
                return true;
        }else{
                return false;
        }
}

int OnInit(){
        ArraySetAsSeries(candleTimes,true);
        return(0);
}



void OnTick(){
        
        if(checkNewCandle(candleTimes,lastCandleTime)){
                double buy_signal[];
                double sell_signal[];
                ArraySetAsSeries(sell_signal,true);
                ArraySetAsSeries(buy_signal,true);
                double candleClose[];
                ArraySetAsSeries(candleClose,true);
                
                //----- define indicator handle
      int MagicTrend_handle = iCustom(Symbol(),0,"KT-Trend-Magic-Indicator");
      
      //----- Copying buffer into arrays
      CopyBuffer(MagicTrend_handle,0,1,1,buy_signal);
      CopyBuffer(MagicTrend_handle,1,1,1,sell_signal);
      
                CopyClose(_Symbol,_Period,0,3,candleClose);

                if(buy_signal[0]!=0){
                        //cross up
                        Print("Cross above!");
                        
                        makePosition(orderBuy);

                }else if(sell_signal[0]!=0){
                        //cross down
                        Print("Cross under!");
                        
                        makePosition(orderSell);     

                }
        }
  
}
  see the code this only opens order but does not close i have attached the indicator too thanks a lot
<ex5 file removed>
 
nail sertoglu: a ) ur code has 2 closePosition() routine
Please don't write ur - it's "you are" or "your" - MQL4 programming forum 2014.03.04
 
objemmanuel1997:
<ex5 file removed>

YOUR EA is working now ; but in my short test EA open only BUY orders, you should check all your conditions / specifications for order open / close .. 

Files:
 
nail sertoglu:

YOUR EA is working now ; but in my short test EA open only BUY orders, you should check all your conditions / specifications for order open / close .. 

So it doesnt open SELL orders? does it open and close?

 
nail sertoglu:

YOUR EA is working now ; but in my short test EA open only BUY orders, you should check all your conditions / specifications for order open / close .. 

It gave me over 20 warnings when i compiled it

Files:
warningB.PNG  50 kb
warningc.PNG  48 kb
 
objemmanuel1997:

It gave me over 20 warnings when i compiled it

I have "22 warnings" and did not cause to stop ,

please go on/over the codes to find your mistakes and test your EA then revise as you wish :) :)