This code isn't closing my manual trades. please help.

 

This trade was to help me in scalping. but it isn't closing trades. Objective was to find alternative for sl or tp at a level closer than stop levels of a pair. please help.

Tried closing them by removing any condition for closing trades but it still didn't work. These set of statements to close trades worked in my previous programming but aren't working here on my manual trades. No concept of magic number taken into consideration so this should work.


//+------------------------------------------------------------------+
//|                                                           _5.mq5 |
//|                        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"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

input double StopLoss=0;
input double TakeProfit=0;
input bool sl_constORtrailing=0;

input double trailing_slgap=0;
double stored_sl=0;
double dummy=0;

bool openPosition=0;
long positionType=0;
ulong position_ticket=0;

string a;



int OnInit()
  {return(INIT_SUCCEEDED);}

void OnDeinit(const int reason)
  {}


void OnTick()
  {
// Trade structures
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);

// Current position information
   openPosition = PositionSelect(_Symbol);
   positionType = PositionGetInteger(POSITION_TYPE);
   position_ticket=PositionGetTicket(0);


   if(openPosition)
     {


      if(positionType==POSITION_TYPE_BUY)
        {
         a="POSITION_TYPE_BUY";
         // Modify SL/TP
         if(sl_constORtrailing==0)
            stored_sl=StopLoss;
         if(sl_constORtrailing==1)
           {
            dummy= SymbolInfoDouble(_Symbol,SYMBOL_BID)-trailing_slgap;
            if(dummy>stored_sl)
               stored_sl=dummy;
           }
         if(SymbolInfoDouble(_Symbol,SYMBOL_BID)<=stored_sl || SymbolInfoDouble(_Symbol,SYMBOL_BID)>=TakeProfit)
            m_trade.PositionClose(position_ticket);
        }



      if(positionType==POSITION_TYPE_SELL)
        {
         a="POSITION_TYPE_SELL";
         // Modify SL/TP
         if(sl_constORtrailing==0)
            stored_sl=StopLoss;
         if(sl_constORtrailing==1)
           {
            dummy= SymbolInfoDouble(_Symbol,SYMBOL_ASK)+trailing_slgap;
            if(dummy<stored_sl)
               stored_sl=dummy;
           }
         if(SymbolInfoDouble(_Symbol,SYMBOL_ASK)>=stored_sl || SymbolInfoDouble(_Symbol,SYMBOL_ASK)<=TakeProfit)
            m_trade.PositionClose(position_ticket);
        }
     }

   string
   s = "openPosition=" + (string)openPosition +"\n" + "\n";
   s = s + "positionType=" + (string)a + "\n" + "\n";
   s = s + "position_ticket=" + (string)position_ticket + "\n" + "\n";
   s = s + "TP" + (string)TakeProfit + "\n" + "\n";
   s = s + "Current_sl" + "   " + (string)stored_sl + "\n" + "\n";
   Comment(s);
  }

Thanks everyone.

 
   openPosition = PositionSelect(_Symbol);
   positionType = PositionGetInteger(POSITION_TYPE);
   position_ticket=PositionGetTicket(0);
First, you select a position with the chart's symbol. Last, you select a random order.
 
William Roeder #:
First, you select a position with the chart's symbol. Last, you select a random order.
I have assumed that the terminal has only one open trade at any given time. This should work with this condition, shouldn't it.?
 
Saurabh Suman #:
I have assumed that the terminal has only one open trade at any given time. This should work with this condition, shouldn't it.?

PositionSelect

bool  PositionSelect(
   string  symbol      // Symbol name
   );

Note

For the "netting" interpretation of positions (ACCOUNT_MARGIN_MODE_RETAIL_NETTING and ACCOUNT_MARGIN_MODE_EXCHANGE), only one position can exist for a symbol at any moment of time. This position is a result of one or more deals. Do not confuse positions with valid pending orders, which are also displayed on the Trading tab of the Toolbox window.

If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case, PositionSelect will select a position with the lowest ticket.

Function PositionSelect() copies data about a position into the program environment, and further calls of PositionGetDouble(), PositionGetInteger() and PositionGetString() return the earlier copied data. This means that the position itself may no longer exist (or its volume, direction, etc. has changed), but data of this position still can be obtained. To ensure receipt of fresh data about a position, it is recommended to call PositionSelect() right before referring to them.


The "PositionSelect" function is primarily designed for netting accounts, so I always recommend using a universal algorithm:

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
            {
             ...
            }
Documentation on MQL5: Trade Functions / PositionSelect
Documentation on MQL5: Trade Functions / PositionSelect
  • www.mql5.com
PositionSelect - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov #:

PositionSelect

Note

For the "netting" interpretation of positions (ACCOUNT_MARGIN_MODE_RETAIL_NETTING and ACCOUNT_MARGIN_MODE_EXCHANGE), only one position can exist for a symbol at any moment of time. This position is a result of one or more deals. Do not confuse positions with valid pending orders, which are also displayed on the Trading tab of the Toolbox window.

If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case, PositionSelect will select a position with the lowest ticket.

Function PositionSelect() copies data about a position into the program environment, and further calls of PositionGetDouble(), PositionGetInteger() and PositionGetString() return the earlier copied data. This means that the position itself may no longer exist (or its volume, direction, etc. has changed), but data of this position still can be obtained. To ensure receipt of fresh data about a position, it is recommended to call PositionSelect() right before referring to them.


The "PositionSelect" function is primarily designed for netting accounts, so I always recommend using a universal algorithm:

Thanks... Issue resolved. There was some logical error in modifying SL.