Position Close Help! I'm losing my mind on this!

 

Hey all newb here again

Using a hedging account, I want to open an order when at an EMA cross with stop loss and take profit. I also however, want the order to close if the opposite cross happens before a stop loss or take profit has been triggered. About 8 hours into this problem and I'm lost as to why the close orders are not executing. My buys and sells seem to be happening in the proper place using the total=0 but closes seem to be completely ignored. Please help.

  if(total==0)
     {
      //--- no opened positions identified    
        
        
    

      if(buyCondition_1 && buyCondition_2)
        {
               
          if(!trade.Buy(Lot, symbol.Name(), symbol.Ask(),(symbol.Bid()-StopLoss*Point()),(symbol.Bid()+TakeProfit*Point()),NULL)) //Request is completed or order placed
           {
            Print("Buy Order Failed",trade.ResultRetcode(),". Error:",trade.ResultRetcodeDescription());
           }
          else
           {
            Print("Buy Order Successful")   ;     
            return;
       }
     
    } 

      if(sellCondition_1 && sellCondition_2)
        {
         if(!trade.Sell(Lot, symbol.Name(), symbol.Ask(),(symbol.Bid()+StopLoss*Point()),(symbol.Bid()-TakeProfit*Point()),NULL)) //Request is completed or order placed
           {
            Print("Sell Order Failed ",trade.ResultRetcode(),". Error:",trade.ResultRetcodeDescription());
             }
          else
           {
            Print("Sell Order Successful");     
               return;
             }
          } 
       return;
       }
    

   for(int i=PositionsTotal()-1;i>=0;i--)
      if(position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(position.Symbol()==Symbol() && position.Magic()==Magic_Number)
           {
            if(!RefreshRates())
              {
                return;
              }
            //---
            if(position.PositionType()==POSITION_TYPE_BUY)
              {
               if(sEMA[0]>fEMA[0])
                 {
                  trade.PositionClose(position.Ticket()); // close position
                  continue;
                 }
              }
            //---
            if(position.PositionType()==POSITION_TYPE_SELL)
              {
               if(fEMA[0]>sEMA[0])
                 {
                  trade.PositionClose(position.Ticket()); // close position
                  continue;
                 }
               }
             }
 

Probably the problem is here:

      if(sellCondition_1 && sellCondition_2)
        {
         if(!trade.Sell(Lot, symbol.Name(), symbol.Ask(),(symbol.Bid()+StopLoss*Point()),(symbol.Bid()-TakeProfit*Point()),NULL)) //Request is completed or order placed
           {
            Print("Sell Order Failed ",trade.ResultRetcode(),". Error:",trade.ResultRetcodeDescription());
             }
          else
           {
            Print("Sell Order Successful");     
               return;
             }
          } 
       return;
       }
Styler - Working with Source Code - Creating Programs - MetaEditor Help
Styler - Working with Source Code - Creating Programs - MetaEditor Help
  • www.metatrader5.com
This function is intended for formatting the source code in accordance with the recommended standard. This allows making the source code readable...
 
Vladimir Karputov:

Probably the problem is here:

Is there a way to customize the styler? I know GNU indent style is default but something about seeing the braces invented two spaces makes me cringe. I'd like to line up the braces with the beginning of the line tab such as in the allman indent convention
 
nicholishen:
Is there a way to customize the styler? I know GNU indent style is default but something about seeing the braces invented two spaces makes me cringe. I'd like to line up the braces with the beginning of the line tab such as in the allman indent convention
Not possible. (and there was big discussion about this on Russian forum, as far as I remember Metaquotes CEO (Renat) said "Never").
 
nicholishen:
Is there a way to customize the styler? I know GNU indent style is default but something about seeing the braces invented two spaces makes me cringe. I'd like to line up the braces with the beginning of the line tab such as in the allman indent convention

Link to the "styler" I inserted accidentally. This does not apply to the question: "Why positions are not closed."

 
Vladimir Karputov:

Link to the "styler" I inserted accidentally. This does not apply to the question: "Why positions are not closed."

Removing the return code did not allow the close position command to initiate.
 
AnarchyInvest:
Removing the return code did not allow the close position command to initiate.
Wow! I figured it out. So for whatever reason, switching the code so that the close position happens first, then putting a return function after the end of the close statement fixed the problem. Thanks Vladimir for all of your help! Hopefully, I can return the assistance to other new traders!
Reason: