how to close a position - page 3

 
irinacfd :

Hi Vladimir,

I am having the same problem, my code doesnt work.

I tried every variation, including part of your code. The RSI signal is working.  I am suspecting now it is never called, but dont know how to test that. Any tips please??  I would also hire someone to help me.

Error # 1

Wrong cycle.

for(int i=0; i<PositionsTotal(); i++)

You need this:

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      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)
            if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());


Mistake # 2 and # 3: first you need to check the signals and if there is a signal, then call the close function. Like that:

***
if(CloseBySignal && RSI_tpLong())
   ClosePositions();
 
Vladimir Karputov:

Error # 1

Wrong cycle.

You need this:



Thank you very much for the reply.  I got this from a mql5 article:

// in this loop we're checking all opened positions
   for(i=0;i<PositionsTotal();i++)

and it does work. I use it also in other parts of my EA and it works.

What does not work is the PositionClosePartial. It keeps partial closing of the position until it is closed completely. What I want however is to partial close the actual position only once.

Update, I found a simple solution:

CTrade Pool;
----
lt=lot/2

if(PositionGetDouble(POSITION_VOLUME)==lot) // lot = volume in settings

if (RSI_tppShort() && closeP)  
                   
Pool.PositionClosePartial(PositionGetTicket(i),lt,200);
 
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 
irinacfd :

Thank you very much for the reply.  I got this from a mql5 article:

and it does work. I use it also in other parts of my EA and it works.

What does not work is the PositionClosePartial. It keeps partial closing of the position until it is closed completely. What I want however is to partial close the actual position only once.

Update, I found a simple solution:

 

Be careful when you are told.

When closing a position, use the loop:

   for ( int i= PositionsTotal ()- 1 ; i>= 0 ; i--) // returns the number of current positions 
 
Vladimir Karputov:

Be careful when you are told.

When closing a position, use the loop:

Thanks again Vladimir, but that code doesnt work in my Hedge system. Would you  have a code base reference for your return open positions loop please ?
 
irinacfd :
Thanks again Vladimir, but that code doesnt work in my Hedge system. Would you  have a code base reference for your return open positions loop please ?

This code works in both Hedge and Netting

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      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)
            if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());


nuance: you need to connect trade classes and declare objects - 

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
 
Vladimir Karputov:

This code works in both Hedge and Netting


nuance: you need to connect trade classes and declare objects - 

Hi Vladimir,

Your code works now also in my EA in the Strategy Tester, but not in real time. It closes the position but not at the correct RSI value.  I suspect it might be my RSI signal. I looked into your RSI EA and see that you are using two values. Can you please explain why. This is your code in case you dont remember:

if(CloseBySignal)
     {
      if(count_buys>0 && rsi_0<RsiSellLevel && rsi_1>RsiSellLevel)
         ClosePositions(POSITION_TYPE_BUY);

Thank you!

 
irinacfd :

Hi Vladimir,

Your code works now also in my EA in the Strategy Tester , but not in real time. It closes the position but not at the correct RSI value.  I suspect it might be my RSI signal. I looked into your RSI EA and see that you are using two values. Can you please explain why. This is your code in case you dont remember:

Thank you!

If you misused my code, it is not my fault. My code works both in the tester and online.

I said for a long time: if you want to get the correct code, let's start from the very beginning. Let's create an Expert Advisor step by step. But you prefer torment :)

 
irinacfd:

Hi Vladimir,

Your code works now also in my EA in the Strategy Tester, but not in real time. It closes the position but not at the correct RSI value.  I suspect it might be my RSI signal. I looked into your RSI EA and see that you are using two values. Can you please explain why. This is your code in case you dont remember:

Thank you!

That code is right. So what's the issue you have with two values rsi_0/1, it's just the way it has to be done.
 
lippmaje:
That code is right. So what's the issue you have with two values rsi_0/1, it's just the way it has to be done.

Well, I am one of those guys who want to understand the code  I am using and why I am using it. Other programmers used RSI differently, i.e. using only one value.d

 
Vladimir Karputov:


If you misused my code, it is not my fault. My code works both in the tester and online.

I said for a long time: if you want to get the correct code, let's start from the very beginning. Let's create an Expert Advisor step by step. But you prefer torment :)

No,I dont.  I am just a 73 year old "do it my way" hardware engineer, who spent most of his life in financial markets and btw is married to a Russian. I try to improve the EA I got delivered from a professional developer without one word of comment. I developed an iPhone app 10 years ago. Before that a Fortran program 50 years ago. So I am a bit rusty. As I said already I would appreciate to get some help with the PCSv9 I am using and which sometimes does not open a position and I cant figure out why. So my chart paints that signal dot, but apparently I am missing the signal. That is also why I was asking for your RSI code.

This is how I get the values:

bool GetIndValue()
  {
   return(CopyBuffer(InpInd_Handle1,0,0,2,wpr1)<=0  ||
          CopyBuffer(InpInd_Handle2,0,0,2,wpr2)<=0  || 
          CopyBuffer(adxHandle,0,0,2,adx1)<=0       ||
          CopyBuffer(InpInd_Handle4,0,0,2,adx2)<=0  || 
          CopyBuffer(PCS,1,0,3,UpSignal)<=0         ||  // 1 = PSCv9 UPsignal
          CopyBuffer(PCS,4,0,4,DnSignal)<=0         ||  // 4 = Dn Signal 
          CopyBuffer(mfi_Handle,0,0,1,mfiVAL)<=0    ||
          CopyBuffer(brainTrend2Stop,0,0,2,BT2_bStop)<=0  ||
          CopyBuffer(brainTrend2Stop,1,0,2,BT2_sStop)<=0  ||  // 1 is working
          CopyBuffer(rsi_Handle,0,0,1,rsiVAL)<0     ||             // RSI for ClosePos
          CopyBuffer(rsi_Handle,0,0,1,rsiPVAL)<0                   // RSI for partial TP   
          )?false:true;
  } 

bool PCS_BuySignal()
   {
   return(UpSignal[0] && UpSignal[0] !=EMPTY_VALUE)  ? true:false;  // PCSv9 signal
   } 


So what all is wrong with above code?

Thank you!