issue with code

 

Hey so I have coded an expert advisor but I am having an issue with initiating a part of the code correctly 

   
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
#include <Trade\AccountInfo.mqh>

   // Spread out the orders of open trades by counting down the tick timer since last open position
void OnTick()
  {
  //--Defining Variables for Information for Comment
   double myAccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
   double myAccountProfit = AccountInfoDouble(ACCOUNT_PROFIT);
   double myAccountEquity = AccountInfoDouble(ACCOUNT_EQUITY);
   
 
      //--Set and Define Buy Price Variable
   double AskP= SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      //--Set and Define Sell Price Variable
   double BidP= SymbolInfoDouble(_Symbol,SYMBOL_BID);
   
   int myOrderT=PositionsTotal();
   
   //Defining Moving Average and setting the array
     double myMovingAverageDefinition;
     double myMA[];
   //--Set & Define Variable for Moving Average, period variable could be (20 or 50 for first number)
   myMovingAverageDefinition  =  iMA (_Symbol,_Period,500,0,MODE_EMA,PRICE_MEDIAN);
  // Copy informatation into an array to store information
   CopyBuffer(myMovingAverageDefinition,0,0,1,myMA);
   // Call Custom Zig Zag Indicator
   double myMovingAverageDefinitionD;
   double myMAD[];
   myMovingAverageDefinitionD   =  iMA(_Symbol,_Period,1,0,MODE_SMA,PRICE_MEDIAN);
   ArraySetAsSeries(myMAD,true);
   CopyBuffer(myMovingAverageDefinitionD,0,0,6,myMAD);
   //Defining Moving Average and setting the array
   double myMovingAverageDefinitionA;
   double myMAA[];
   //--Set & Define Variable for Moving Average, period variable could be (20 or 50 for first number)
   myMovingAverageDefinitionA  =  iMA (_Symbol,_Period,1000,0,MODE_EMA,PRICE_MEDIAN);
  // Copy informatation into an array to store information
  ArraySetAsSeries(myMAA,true);
  CopyBuffer(myMovingAverageDefinitionA,0,0,1,myMAA);
  
  double MovingAverageForSignals;
  
  double MAFS[];
  
  MovingAverageForSignals = iMA (_Symbol,_Period,10,0,MODE_EMA,PRICE_MEDIAN);
  
   double myRSI;
   double myRSIinfo[];
   myRSI = iRSI(_Symbol,_Period,50,PRICE_MEDIAN);
   CopyBuffer(myRSI,0,0,1,myRSIinfo);
   
   // ++++++++++++++++++++++++++++++++++++++++ 
   //Universal Trade Peramiters
   //+++++++++++++++++++++++++++++++++++++++++
      double universalTP=2000;
      double universalSL=100;
      double universalVolume=0.05;
      double openPositions1=1;
      double openPositions2=3;
   //-----------------------------------------   
       
       double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
       double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
  
      //--If price below moving average then buy
      //+++++++|
      // Buy   |
      //+++++++| 
   if(myRSIinfo[0]<50)
   {
   if(myMAA[0]>AskP)
   {       
      if(myMAD[5]<myMAD[3])
      {
         if(myMAD[3]<myMAD[0])
         {
      MqlTradeRequest requestB;
      MqlTradeResult resultB;
      ZeroMemory(requestB);
      //--Buy
      requestB.action    =TRADE_ACTION_DEAL;
      requestB.symbol    =_Symbol;
      requestB.volume     =universalVolume;
      requestB.type_filling     =ORDER_FILLING_FOK;
      requestB.type      =ORDER_TYPE_BUY;
      requestB.price      =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      requestB.tp     =SymbolInfoDouble(_Symbol,SYMBOL_ASK)+universalTP*_Point;
      requestB.deviation    =50;
      if(myMA[0]>myMAA[0])
      {
            if(AskP<myMA[0])
            {
               if(CountBuyPositions()<openPositions2)
               {
                  if(TimeCountBuy24hr()<1)
                  {
                  OrderSend(requestB,resultB);
                  return;
                  }
                  else
                  return;
               }
               if(CountBuyPositions()<openPositions1)
               {
               OrderSend(requestB,resultB);
               return;
               }
            }
            else 
            return;
         }
         else
         return;
         }
         }
     }
     else 
     return;
     }

    
      
      
      //++++++|
      // Sell |
      //++++++|
   if(myRSIinfo[0]>50)
   {
   if(myMAA[0]<BidP)
   {
   if(myMAD[5]>myMAD[3])
   {
      if(myMAD[3]>myMAD[0])
      {
      MqlTradeRequest requestS;
      MqlTradeResult resultS;
      ZeroMemory(requestS);
      //--Sell
      requestS.action    =TRADE_ACTION_DEAL;
      requestS.symbol    =_Symbol;
      requestS.volume     =universalVolume;
      requestS.type_filling     =ORDER_FILLING_FOK;
      requestS.type      =ORDER_TYPE_SELL;
      requestS.price      =SymbolInfoDouble(_Symbol,SYMBOL_BID);
      requestS.tp     =SymbolInfoDouble(_Symbol,SYMBOL_BID)-universalTP*_Point;
      requestS.deviation    =50;
      if(myMA[0]<myMAA[0])
      {
            if(BidP>myMA[0])
            {
               if(CountSellPositions()<openPositions2)
               {
                  if(TimeCountSell24hr()<1)
                  {
                  OrderSend(requestS,resultS);
                  return;
                  }
                  else 
                  return;
               }
               if(CountSellPositions()<openPositions1)
               {
               OrderSend(requestS,resultS);
               return;
               }
            }
      else 
      return;
    }
    else
    return;
   }
   }   
   }
   else 
   return; 
   }
      
   if(PositionSelect(_Symbol))
   {
   double PositionInfo=PositionSelect(_Symbol);
   datetime CurrentT=TimeCurrent();
   datetime  PositionChange=PositionGetInteger(POSITION_TIME);
   double PositionProfit=PositionGetDouble(POSITION_PROFIT);
   double PositionTYPE=PositionGetInteger(POSITION_TYPE);
   //-------------------------------------------------------------+
   //                Positon Change ADD times                                             |
   //                3,024,000=35days
   //                5,184,000=60days
   //                10,368,000=120days                
   //-------------------------------------------------------------+
   double TimeChange= CurrentT-(PositionChange+3024000);
   Comment(CurrentT,"\n",PositionChange,"\n",PositionProfit,"\n",TimeChange);
      if(PositionProfit>500)
      {
         if(TimeChange>0)
         {
         CloseAllOrders();
         } 
         else
         return;
      }
      else
      {
      return;
      }
   }
   }
   
void CloseAllOrders()
  {
  CTrade trade;
  int i=PositionsTotal()-1;
  while (i>=0)
   {
      if (trade.PositionClose(PositionGetSymbol(i)))  i--;
   }
   }
   
   
    //Number of Buy Positions Call
   int CountBuyPositions() //Custom Function
  {
   int numberofbuypositions=0;

   for(int i=PositionsTotal()-1; i>=0; --i)  //Look at all the positions
     {
     
     double PositionInfoB=PositionSelectByTicket(i);
     
      string CurrencyPair1=PositionGetSymbol(i); //Identify currency pair

      //get the position type
      int PositionDirection1=PositionGetInteger(POSITION_TYPE);

      // if symbol on the chart equals position symbol
      if(Symbol()==CurrencyPair1)
        {
         // if position type is a buy position
         if(PositionDirection1==POSITION_TYPE_BUY)
           {
            //if true increase count by one
            numberofbuypositions=numberofbuypositions+1;
           }
        }
     }

   return numberofbuypositions;
  }
  
  //Number of Sell Positions Call 
  int CountSellPositions() //Custom Function
  {
   int numberofsellpositions=0;

   for(int i=PositionsTotal()-1; i>=0; --i)  //Look at all the positions
     {
     
      string CurrencyPair2=PositionGetSymbol(i); //Identify currency pair

      //get the position type
      int PositionDirection2=PositionGetInteger(POSITION_TYPE);

      // if symbol on the chart equals position symbol
      if(Symbol()==CurrencyPair2)
        {
         // if position type is a buy position
         if(PositionDirection2==POSITION_TYPE_SELL)
           {
            //if true increase count by one
            numberofsellpositions=numberofsellpositions+1;
           }
        }
     }

   return numberofsellpositions;
  }
  
  int TimeCountBuy24hr()
  {
  int buyposhr=0;
  for(int i=PositionsTotal()-1; i>=0; --i)  //Look at all the positions
     {
      string CurrencyPair3=PositionGetSymbol(i); //Identify currency pair
      
      //get position open time 
      // Need to get position open time for most recent position
      datetime Psbuy1time=PositionGetInteger(POSITION_TIME);
      datetime TimeC1=TimeCurrent();
      
      int PositionDirection3=PositionGetInteger(POSITION_TYPE);
      
      double TimeChange1=TimeC1-(Psbuy1time+86400);
      
      if(Symbol()==CurrencyPair3)
      {
         if(PositionDirection3==POSITION_TYPE_BUY)
         {
      // 86,400 sec per day or per 24hrs
            if(0<TimeChange1)
            {
            buyposhr=buyposhr+1;
            }
         }
      }
      }
      return buyposhr;
  }
  
 int TimeCountSell24hr()
  {
  int sellposhr=0;
  for(int i=PositionsTotal()-1; i>=0; --i)  //Look at all the positions
     {
      string CurrencyPair4=PositionGetSymbol(i); //Identify currency pair
      
      ulong PosTicketS1=PositionGetTicket(i);
      
      //get position open time 
      //need to get time for most recent position
      datetime Pssell1time=PositionGetInteger(POSITION_TIME);
      datetime TimeC2=TimeCurrent();
      
      int PositionDirection4=PositionGetInteger(POSITION_TYPE);
      
      double TimeChange2=TimeC2-(Pssell1time+86400);
      
      if(Symbol()==CurrencyPair4)
      {
         if(PositionDirection4==POSITION_TYPE_SELL)
         {
      // 86,400 sec per day or per 24hrs
            if(0<TimeChange2)
            {
            sellposhr=sellposhr+1;
            }
         }
      }
      }
      return sellposhr;
  }

Specifically with this portion of the code. I cant tell if its spamming multiple orders as soon as it has reached true and just. Or it is just not initiating at all. The idea behind it is to count the time since and order was placed so every 24 hrs it will place a new order. Cant tell if i messed up on writing the code or its just working in a way that i did not anticipate.

int TimeCountBuy24hr()
  {
  int buyposhr=0;
  for(int i=PositionsTotal()-1; i>=0; --i)  //Look at all the positions
     {
      string CurrencyPair3=PositionGetSymbol(i); //Identify currency pair
      
      //get position open time 
      // Need to get position open time for most recent position
      datetime Psbuy1time=PositionGetInteger(POSITION_TIME);
      datetime TimeC1=TimeCurrent();
      
      int PositionDirection3=PositionGetInteger(POSITION_TYPE);
      
      double TimeChange1=TimeC1-(Psbuy1time+86400);
      
      if(Symbol()==CurrencyPair3)
      {
         if(PositionDirection3==POSITION_TYPE_BUY)
         {
      // 86,400 sec per day or per 24hrs
            if(0<TimeChange1)
            {
            buyposhr=buyposhr+1;
            }
         }
      }
      }
      return buyposhr;
  }
  
 int TimeCountSell24hr()
  {
  int sellposhr=0;
  for(int i=PositionsTotal()-1; i>=0; --i)  //Look at all the positions
     {
      string CurrencyPair4=PositionGetSymbol(i); //Identify currency pair
      
      ulong PosTicketS1=PositionGetTicket(i);
      
      //get position open time 
      //need to get time for most recent position
      datetime Pssell1time=PositionGetInteger(POSITION_TIME);
      datetime TimeC2=TimeCurrent();
      
      int PositionDirection4=PositionGetInteger(POSITION_TYPE);
      
      double TimeChange2=TimeC2-(Pssell1time+86400);
      
      if(Symbol()==CurrencyPair4)
      {
         if(PositionDirection4==POSITION_TYPE_SELL)
         {
      // 86,400 sec per day or per 24hrs
            if(0<TimeChange2)
            {
            sellposhr=sellposhr+1;
            }
         }
      }
      }
      return sellposhr;
  }

They are pretty much the same code just one is to loop through buy, and the other for sell. 

Thank you in advance.

 

You haven't posted any outcomes of your backtests, so it is difficult to judge whether the code behaves as it should.

However, there are some other mistakes in your code. You create handles to functions in the OnTick() function. For example:

myMovingAverageDefinition  =  iMA (_Symbol,_Period,500,0,MODE_EMA,PRICE_MEDIAN);

You have done this for several iMA and iRSI handles. This is incorrect! These handle definitions must be done only once, and should be placed in the OnInit() function.

 

Thank for the reply, sorry if I was unclear the issue that I am specifically confused by is that as you can see  from the log,the orders for Sell were all placed at once. The code should be cycling through the orders and checking them against a time counter as seen. Where as the code is written as current time subtracted from when the time the order was placed plus a 24 hour time period. Meaning in theory that when the counter gets past that 24hr time period it sends a signal that it is okay to place a trade. Which is where the confusion is coming from since clearly that is not happening, so I was wondering if this i a nature part of it. As in it just instantly recognized that an order could be filled and maxed it out under the given count. Or is it an issue with the code. As always, Thank you in advance


PR 0 21:55:03.577 Tester EURUSD,H1 (MetaQuotes-Demo): every tick generating

HK 0 21:55:03.577 Tester EURUSD,H1: testing of Experts\MOA Alternatiting robot 3.ex5 from 1980.07.01 00:00 to 2020.04.23 00:00 started

QI 0 21:55:12.733 Trade 1985.10.28 00:00:00   instant sell 0.05 EURUSD at 0.74080 tp: 0.72080 (0.74080 / 0.74130 / 0.74080)

EH 0 21:55:12.733 Trades 1985.10.28 00:00:00   deal #2 sell 0.05 EURUSD at 0.74080 done (based on order #2)

EG 0 21:55:12.733 Trade 1985.10.28 00:00:00   deal performed [#2 sell 0.05 EURUSD at 0.74080]

OM 0 21:55:12.733 Trade 1985.10.28 00:00:00   order performed sell 0.05 at 0.74080 [#2 sell 0.05 EURUSD at 0.74080]

MQ 0 21:55:12.739 Trade 1985.10.28 00:00:00   instant sell 0.05 EURUSD at 0.74082 tp: 0.72082 (0.74082 / 0.74132 / 0.74082)

IO 0 21:55:12.739 Trades 1985.10.28 00:00:00   deal #3 sell 0.05 EURUSD at 0.74082 done (based on order #3)

OO 0 21:55:12.739 Trade 1985.10.28 00:00:00   deal performed [#3 sell 0.05 EURUSD at 0.74082]

LE 0 21:55:12.739 Trade 1985.10.28 00:00:00   order performed sell 0.05 at 0.74082 [#3 sell 0.05 EURUSD at 0.74082]

EI 0 21:55:12.754 Trade 1985.10.28 00:00:00   instant sell 0.05 EURUSD at 0.74085 tp: 0.72085 (0.74085 / 0.74135 / 0.74085)

QG 0 21:55:12.754 Trades 1985.10.28 00:00:00   deal #4 sell 0.05 EURUSD at 0.74085 done (based on order #4)

NG 0 21:55:12.754 Trade 1985.10.28 00:00:00   deal performed [#4 sell 0.05 EURUSD at 0.74085]

IM 0 21:55:12.754 Trade 1985.10.28 00:00:00   order performed sell 0.05 at 0.74085 [#4 sell 0.05 EURUSD at 0.74085]

JR 3 21:55:16.841 Tester stopped by user

DE 0 21:55:16.848 127.0.0.1 prepare for shutdown

MG 0 21:55:16.855 test Experts\MOA Alternatiting robot 3.ex5 on EURUSD,H1 thread finished


Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by properties that allow to obtain information about it. In order to read values of properties, functions of the Identifier of a position, in the opening, modification or closing of which this deal took...
Reason: