How to get opening and current price information according to position interpretation?

 

Hi,

I have two open positions that say "aaa" and "bbb" in the position commentary.

How can I get the opening and current price information of the "aaa" position?

 

this did not work.



#include <Trade\Trade.mqh>
CTrade trade;

void OnTick()
  {


//AL/SAT FİYATLARI-----------------------------------------------------------
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
//----------------------------------------------------------------------------

//RSI------------------------------------------------------------------------
   double RsiArray[];
   int Rsi = iRSI(_Symbol,PERIOD_H12,14,PRICE_CLOSE);
   ArraySetAsSeries(RsiArray,true);
   CopyBuffer(Rsi,0,0,3,RsiArray);
   double RsiRound = NormalizeDouble(RsiArray[0],2);
//---------------------------------------------------------------------------

//RSI OPEN-----------------------------------------------------------------------------------------------------
if(PositionsTotal()<10)
if(RSI_POZISYON_SAYISI()<1)
if(RsiRound<=30)
{trade.Buy(0.01,NULL,ask,0,0,"RSI OPEN");}

string RSI_PRICE_CURRENT_SIGNAL="";

for(int i=PositionsTotal()-1; i>0; i--)
{int ticket=PositionGetTicket(i); PositionSelectByTicket(ticket);
double pricecurrent = PositionGetDouble(POSITION_PRICE_CURRENT);
double priceopen = PositionGetDouble(POSITION_PRICE_OPEN);
if(PositionGetString(POSITION_COMMENT)=="RSI OPEN") 
if(pricecurrent<priceopen)
{RSI_PRICE_CURRENT_SIGNAL="SELL";}}


if(RSI_PRICE_CURRENT_SIGNAL=="SELL")
if(RsiRound<=70)
{RSI_OPEN_CLOSE();}



}


int RSI_POZISYON_SAYISI()
{
int i=0;int toplam=0;for(i;i<PositionsTotal();i++)
{int ticket=PositionGetTicket(i);PositionSelectByTicket(ticket);
if(PositionGetString(POSITION_COMMENT)=="RSI OPEN")
{toplam=toplam+1;}}return(toplam);}


void RSI_OPEN_CLOSE()
{
for(int i=PositionsTotal()-1; i>0; i--)
{int ticket=PositionGetTicket(i);
string PositionComment = PositionGetString(POSITION_COMMENT);
if (PositionComment=="RSI OPEN"){trade.PositionClose(ticket);}
}
}















 

Remember the rule: the indicator handle MUST be received ONCE! This is done in OnInit () !!!

Example: Receiving data from an indicator in an MQL5.

How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Vladimir Karputov:

Remember the rule: the indicator handle MUST be received ONCE! This is done in OnInit () !!!

Example: Receiving data from an indicator in an MQL5.


i get some errors :(

sorry im new


'RsiRound' - undeclared identifier deneme2.mq5 31 4

'ask' - undeclared identifier deneme2.mq5 32 22
'RsiRound' - undeclared identifier deneme2.mq5 46 4


#include <Trade\Trade.mqh>
CTrade trade;


int OnInit()
  {


//AL/SAT FİYATLARI-----------------------------------------------------------
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
//----------------------------------------------------------------------------

//RSI------------------------------------------------------------------------
   double RsiArray[];
   int Rsi = iRSI(_Symbol,PERIOD_H12,14,PRICE_CLOSE);
   ArraySetAsSeries(RsiArray,true);
   CopyBuffer(Rsi,0,0,3,RsiArray);
   double RsiRound = NormalizeDouble(RsiArray[0],2);
//---------------------------------------------------------------------------

   return(INIT_SUCCEEDED);
  }

void OnTick()
  {

//RSI OPEN-----------------------------------------------------------------------------------------------------
if(PositionsTotal()<10)
if(RSI_POZISYON_SAYISI()<1)
if(RsiRound<=30)
{trade.Buy(0.01,NULL,ask,0,0,"RSI OPEN");}

string RSI_PRICE_CURRENT_SIGNAL="";

for(int i=PositionsTotal()-1; i>0; i--)
{int ticket=PositionGetTicket(i); PositionSelectByTicket(ticket);
double pricecurrent = PositionGetDouble(POSITION_PRICE_CURRENT);
double priceopen = PositionGetDouble(POSITION_PRICE_OPEN);
if(PositionGetString(POSITION_COMMENT)=="RSI OPEN") 
if(pricecurrent<priceopen)
{RSI_PRICE_CURRENT_SIGNAL="SELL";}}


if(RSI_PRICE_CURRENT_SIGNAL=="SELL")
if(RsiRound<=70)
{RSI_OPEN_CLOSE();}
   
  }
int RSI_POZISYON_SAYISI()
{
int i=0;int toplam=0;for(i;i<PositionsTotal();i++)
{int ticket=PositionGetTicket(i);PositionSelectByTicket(ticket);
if(PositionGetString(POSITION_COMMENT)=="RSI OPEN")
{toplam=toplam+1;}}return(toplam);}


void RSI_OPEN_CLOSE()
{
for(int i=PositionsTotal()-1; i>0; i--)
{int ticket=PositionGetTicket(i);
string PositionComment = PositionGetString(POSITION_COMMENT);
if (PositionComment=="RSI OPEN"){trade.PositionClose(ticket);}
}
}


 
Gökhan Erdoğdu :


i get some errors :(

sorry im new


'RsiRound' - undeclared identifier deneme2.mq5 31 4

'ask' - undeclared identifier deneme2.mq5 32 22
'RsiRound' - undeclared identifier deneme2.mq5 46 4


What is 'RsiRound'?

 
Vladimir Karputov:

What is 'RsiRound'?

double RsiRound = NormalizeDouble(RsiArray[0],2);


I created a variable for rounding.

 
Gökhan Erdoğdu :


I created a variable for rounding.

Once again, I strongly recommend reading the example: Receiving data from an indicator in an MQL5. - we get the handle in OnInit, and we get the data from the indicator in OnTick.

Think carefully WHERE you place your variables: always think about the visibility of the variables.

How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Vladimir Karputov:

Once again, I strongly recommend reading the example: Receiving data from an indicator in an MQL5. - we get the handle in OnInit, and we get the data from the indicator in OnTick.

Think carefully WHERE you place your variables: always think about the visibility of the variables.

I ran the codes in the link you sent, but it gave an error.

//+------------------------------------------------------------------+
//|                                        iMA Values on a Chart.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property version   "1.00"
//--- input parameters
input int                  Inp_MA_ma_period     = 12;          // MA: averaging period
input int                  Inp_MA_ma_shift      = 5;           // MA: horizontal shift
input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_SMA;    // MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price
//---
int    handle_iMA;                           // variable for storing the handle of the iMA indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMA
   handle_iMA=iMA(Symbol(),Period(),Inp_MA_ma_period,Inp_MA_ma_shift,
                  Inp_MA_ma_method,Inp_MA_applied_price);
//--- if the handle is not created
   if(handle_iMA==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
  
  
  //+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double array_ma[];
   ArraySetAsSeries(array_ma,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iMA,0,start_pos,count,array_ma))
      return;

   string text="";
   for(int i=0; i<count; i++)
      text=text+IntegerToString(i)+": "+DoubleToString(array_ma[i],Digits()+1)+"\n";
//---
   Comment(text);
  }

'iGetArray' - undeclared identifier deneme2.mq5 50 8

',' - unexpected token deneme2.mq5 50 28
'handle_iMA' - some operator expected deneme2.mq5 50 18
'(' - unbalanced left parenthesis deneme2.mq5 50 6
',' - unexpected token deneme2.mq5 50 30
',' - unexpected token deneme2.mq5 50 46
expression has no effect deneme2.mq5 50 41
')' - unexpected token deneme2.mq5 50 55
expression has no effect deneme2.mq5 50 47
')' - unexpected token deneme2.mq5 50 56
9 errors, 4 warnings 10 5

 
Gökhan Erdoğdu :

I ran the codes in the link you sent, but it gave an error.

'iGetArray' - undeclared identifier deneme2.mq5 50 8

',' - unexpected token deneme2.mq5 50 28
'handle_iMA' - some operator expected deneme2.mq5 50 18
'(' - unbalanced left parenthesis deneme2.mq5 50 6
',' - unexpected token deneme2.mq5 50 30
',' - unexpected token deneme2.mq5 50 46
expression has no effect deneme2.mq5 50 41
')' - unexpected token deneme2.mq5 50 55
expression has no effect deneme2.mq5 50 47
')' - unexpected token deneme2.mq5 50 56
9 errors, 4 warnings 10 5

Learn to read, not blindly copy. All functions are in the file attached to the message.

 
Vladimir Karputov:

Learn to read, not blindly copy. All functions are in the file attached to the message.

First of all, thank you for your help.

I adapted the code you wrote for RSI, I did not get any errors. 


I have a code that counts transactions that say "RSI BUY" in the position comment.

Like this:

int RSI_POZISYON_SAYISI()
{
int i=0;int toplam=0;for(i;i<PositionsTotal();i++)
{int ticket=PositionGetTicket(i);PositionSelectByTicket(ticket);
if(PositionGetString(POSITION_COMMENT)=="RSI BUY")
{toplam=toplam+1;}}return(toplam);}

Where would I add this?


//+------------------------------------------------------------------+
//|                                        iMA Values on a Chart.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property version   "1.001"

#include <Trade\Trade.mqh>
CTrade trade;


//--- input parameters
input int                  Inp_RSI_period     = 14;          // MA: averaging period
input ENUM_APPLIED_PRICE   Inp_RSI_applied_price = PRICE_CLOSE; // MA: type of price
//---
int    handle_iRSI;                           // variable for storing the handle of the iMA indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMA
   handle_iRSI=iRSI(Symbol(),Period(),Inp_RSI_period,Inp_RSI_applied_price);               
//--- if the handle is not created
   if(handle_iRSI==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);



   double array_RSI[];
   ArraySetAsSeries(array_RSI,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iRSI,0,start_pos,count,array_RSI))
      return;
      
   if(!array_RSI[0]<=30)
   {trade.Buy(0.01,NULL,ask,0,(ask+0.10 * _Point),"RSI BUY");}   

   string text="";
   for(int i=0; i<count; i++)
      text=text+IntegerToString(i)+": "+DoubleToString(array_RSI[i],Digits()+1)+"\n";
//---
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      //if(InpPrintLog)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      //if(InpPrintLog)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                     __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+