EA Open position only once

 

Hello,

im new to MQL5 programming. I wanna to create EA thats open one buy trade once per day. My problem is that first day position open normal and then never again.

I think that problem is "!isTradeOpen" part, because if i remove it, positions will open every day at set time, but then EA open a lot of trades at set time. I look

a lot of other people codes and cant find out.

Thanks for help. Have a nice day.

#include <Trade\Trade.mqh>

//+------------------------------------------------------------------+
//| Variables                                |
//+------------------------------------------------------------------+
input int openHour = 6;
input double InpLots = 0.01;
input int InpTakeProfit = 300;
input int InpStopLoss = 100;
bool isTradeOpen = false;
CTrade trade;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {

   // get currnet time
   MqlDateTime timeNow;
   TimeToStruct(TimeCurrent(), timeNow);
   
   // check for trade open
   
   if(PositionsTotal() < 1)
   
   if(openHour == timeNow.hour && !isTradeOpen){
   
      // position open
      double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      
      trade.PositionOpen(_Symbol,ORDER_TYPE_BUY, InpLots, SymbolInfoDouble(_Symbol,SYMBOL_ASK), (Ask - InpStopLoss * _Point), (Ask + InpTakeProfit * _Point));
      
      //set flag
      isTradeOpen = true;
      
      }
   
    }