POSITION_PRICE_OPEN returns 0.0 while positions are open

 

Would like some help here, I'm unable to get a "posOpen" value (Comment(posOpen)) without an "undeclared identifier" error.

Here is a segment of the code

void OnTick()
   {
 
   double priceClose = (iClose(_Symbol,PERIOD_CURRENT,0)); // Var holds current close price of bar
   double priceOpen = (iOpen(_Symbol,PERIOD_CURRENT,0)); // Var holds current open price of bar
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); // Var holds current ask price of bar
   double bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits); // Var holds current bid price of bar
   double currentBalance = AccountInfoDouble(ACCOUNT_BALANCE); // Var holds account balance
   double currentEquity = AccountInfoDouble(ACCOUNT_EQUITY); // Var holds account equity
  
   for (int i = PositionsTotal()-1; i >= 0; i--)
      {
     
      string posSymbol = PositionGetString(POSITION_SYMBOL);
      double posOpen = PositionGetDouble(POSITION_PRICE_OPEN);
      double posProfit = PositionGetDouble(POSITION_PROFIT);
      int posSwap = (int) PositionGetDouble(POSITION_SWAP);
      double posNetProfit = posProfit + posSwap;
     
      }
  
   Comment(posOpen);
   
   double MASIGNAL = iMA(_Symbol, _Period, 5, 0, MODE_SMA, PRICE_CLOSE);
   double MA1 = iMA(_Symbol, _Period, 10, 0, MODE_SMA, PRICE_CLOSE);
   double MA2 = iMA(_Symbol, _Period, 15, 0, MODE_SMA, PRICE_CLOSE);
   double MA3 = iMA(_Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE);
   double MA4 = iMA(_Symbol, _Period, 25, 0, MODE_SMA, PRICE_CLOSE);
   double MA5 = iMA(_Symbol, _Period, 30, 0, MODE_SMA, PRICE_CLOSE);
   double MA6 = iMA(_Symbol, _Period, 35, 0, MODE_SMA, PRICE_CLOSE);
   double MA7 = iMA(_Symbol, _Period, 40, 0, MODE_SMA, PRICE_CLOSE);
   double MA8 = iMA(_Symbol, _Period, 45, 0, MODE_SMA, PRICE_CLOSE);
   double MA9 = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE);
   double MA10 = iMA(_Symbol, _Period, 55, 0, MODE_SMA, PRICE_CLOSE);
   ...
 
   double MA2 = iMA(_Symbol, _Period, 15, 0, MODE_SMA, PRICE_CLOSE);
Perhaps you should read the manual, especially the examples.

   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:
Perhaps you should read the manual, especially the examples.

   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Okay, I think I understand. So I should set it up similarly to the way I have my moving averages?


   double MASIGNAL = iMA(_Symbol, _Period, maSignalSmoother, 0, MODE_SMA, PRICE_CLOSE);
   double MA1 = iMA(_Symbol, _Period, 10, 0, MODE_SMA, PRICE_CLOSE);
   double MA2 = iMA(_Symbol, _Period, 15, 0, MODE_SMA, PRICE_CLOSE);
   double MA3 = iMA(_Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE);
   double MA4 = iMA(_Symbol, _Period, 25, 0, MODE_SMA, PRICE_CLOSE);
   double MA5 = iMA(_Symbol, _Period, 30, 0, MODE_SMA, PRICE_CLOSE);
   double MA6 = iMA(_Symbol, _Period, 35, 0, MODE_SMA, PRICE_CLOSE);
   double MA7 = iMA(_Symbol, _Period, 40, 0, MODE_SMA, PRICE_CLOSE);
   double MA8 = iMA(_Symbol, _Period, 45, 0, MODE_SMA, PRICE_CLOSE);
   double MA9 = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE);
   double MA10 = iMA(_Symbol, _Period, 55, 0, MODE_SMA, PRICE_CLOSE);
  
  
  
   double MASIGNALarr[];
   double MA1arr[];
   double MA2arr[];
   double MA3arr[];
   double MA4arr[];
   double MA5arr[];
   double MA6arr[];
   double MA7arr[];
   double MA8arr[];
   double MA9arr[];
   double MA10arr[];
  
  
  
   CopyBuffer(MASIGNAL, 0, 0, 3, MASIGNALarr);
   CopyBuffer(MA1, 0, 0, 1, MA1arr);
   CopyBuffer(MA2, 0, 0, 1, MA2arr);
   CopyBuffer(MA3, 0, 0, 1, MA3arr);
   CopyBuffer(MA4, 0, 0, 1, MA4arr);
   CopyBuffer(MA5, 0, 0, 1, MA5arr);
   CopyBuffer(MA6, 0, 0, 1, MA6arr);
   CopyBuffer(MA7, 0, 0, 1, MA7arr);
   CopyBuffer(MA8, 0, 0, 1, MA8arr);
   CopyBuffer(MA9, 0, 0, 1, MA9arr);
   CopyBuffer(MA10, 0, 0, 1, MA10arr);
 
dasani daniel:

Would like some help here, I'm unable to get a "posOpen" value (Comment(posOpen)) without an "undeclared identifier" error.

Here is a segment of the code

The obvious thing is that you forgot to select the position before call PositionGet* functions.

for (int i = PositionsTotal()-1; i >= 0; i--)
      {
      if(!PositionSelectByTicket(PositionGetTicket(i))) continue;
      string posSymbol = PositionGetString(POSITION_SYMBOL);
      double posOpen = PositionGetDouble(POSITION_PRICE_OPEN);
      double posProfit = PositionGetDouble(POSITION_PROFIT);
      int posSwap = (int) PositionGetDouble(POSITION_SWAP);
      double posNetProfit = posProfit + posSwap;
     
      }
 
Samuel Manoel De Souza #:

The obvious thing is that you forgot to select the position before call PositionGet* functions.

Okay, just added it. I'm still getting an undeclared identifier error unfortunately.

 
Add the entire code.
 
Samuel Manoel De Souza #:
Add the entire code.


It gives me this

 
dasani daniel #:


It gives me this

nothing attached. lol

 
Samuel Manoel De Souza #:

nothing attached. lol

It wont let me attach an image, but i still have a 'posNetProfit' - undeclared identifier error
 
dasani daniel #:
It wont let me attach an image, but i still have a 'posNetProfit' - undeclared identifier error

try defining the variable in global scope

double posNetProfit;
int OnInit(void)
{
        return INIT_SUCCEEDED;
}

void OnTick(void)
{
        ...
}
 
Samuel Manoel De Souza #:

try defining the variable in global scope

Now it just returns "0.0" again
Reason: