Cannot use the position time

 
Hello, here's my file, it does work if i remove the position total and position get interger funtions but when i try to input them, it doesn't, can someone help me?
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {


   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
  
double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
int   totalpos = PositionsTotal();
int   opentime = PositionGetInteger(POSITION_TIME);

double Max5j = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 120, 0);

double Max3j = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 72, 0);

double Max1j = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 24, 0);

double Max4h = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 4, 0);


if (ask<=((Max4h)*0.99) && totalpos==0 || opentime>=24)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   }
if (ask<=((Max1j)*0.98) && totalpos==0 || opentime>=24)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   }
if (ask<=((Max3j)*0.97) && totalpos==0 || opentime>=24)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   }
if (ask<=((Max5j)*0.96) && totalpos==0 || opentime>=24)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   }
   
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Merlinou: Hello, here's my file, it does work if i remove the position total and position get interger funtions but when i try to input them, it doesn't, can someone help me?
  • You should strive to format or style your code properly, to make it easier to read and detect possible issues.
  • PositionGetInteger() should only be used AFTER you have selected a position to process. Please see the example code in the documentation.
  • The property POSITION_TIME is a datetime property, so it needs to be typecast as such.
  • The datetime is expressed in elapsed seconds and should be treated as such.
 
Fernando Carreiro #:
  • You should strive to format or style your code properly, to make it easier to read and detect possible issues.
  • PositionGetInteger() should only be used AFTER you have selected a position to process. Please see the example code in the documentation.
  • The property POSITION_TIME is a datetime property, so it needs to be typecast as such.
  • The datetime is expressed in elapsed seconds and should be treated as such.
//+------------------------------------------------------------------+
//|                                                   Investtest.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

static int ordersPlacedToday = 0;
int maxOrdersPerDay = 1;

void OnTick()
  {
  
static datetime Old_Time;
datetime New_Time[1];
bool IsNewBar = false;

int copied = CopyTime(_Symbol, PERIOD_D1, 0, 1, New_Time);
if(copied>0)
   {
      if(Old_Time != New_Time[0])
      {
         IsNewBar = true;
         Old_Time = New_Time[0];
         ordersPlacedToday = 0;
      }
   }




double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

double Max5j = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 120, 0);

double Max3j = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 72, 0);

double Max1j = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 24, 0);

double Max4h = iHighest(_Symbol, PERIOD_H1, MODE_HIGH, 4, 0);


if (ask<=((Max4h)*0.99) && ordersPlacedToday < maxOrdersPerDay)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   ordersPlacedToday++;
   }
else if (ask<=((Max1j)*0.99) && ordersPlacedToday < maxOrdersPerDay)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   ordersPlacedToday++;
   }
else if (ask<=((Max3j)*0.99) && ordersPlacedToday < maxOrdersPerDay)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   ordersPlacedToday++;
   }
else if (ask<=((Max5j)*0.99) && ordersPlacedToday < maxOrdersPerDay)
   {
   trade.Buy( 1, NULL, 0.0, 0,ask*1.05, "order succeded");
   ordersPlacedToday++;
   } 
  }

Here is my new code, it does work on forex but when i try to make it work on Shares CFD, it works for a few month and then stops buying without explanation even if margin is available.

Any help or explanation?

Thanks.