Need help in coding the rest of my ea

 

I am new to mql5 and i am watching a lot of videos on how to create a ea.

I started to build a trix rsi ea but i am struggling to get the ea to place a order when the trix goes above the 0 and is below the rsi 70 and above the rsi 30 levels.

The ea also needs to close the trade in 50 seconds after it was opened.

Any help would greatly be appreciated.

I want the ea to open a trade when the trix crosses the 0 line and is between the rsi 70 and 30 lines.

Then if the order was opened it must stay open for 50 seconds then close automatically.

I was watching a few videos on how to build a ea and this is how far i got with the script, i am learning how to code and this is my first ea.







//+------------------------------------------------------------------+
//| include                                                          |
//+------------------------------------------------------------------+
#include<Trade\trade.mqh>
MqlTick currentTick,previouseTime,currentTime;
CTrade trade;

//+------------------------------------------------------------------+
//| inputs                                                           |
//+------------------------------------------------------------------+

input int                           InpPeriodRSI      = 14;          // Period for RSI
input int                           InpPeriodTriX     =  5;          // Period for TriX
static input double                 InpLots           = 0.20;        // lots
input int                           InpRSILevelUp     = 70;          // RSI Level (Upper)
input int                           InpRSILevelDown   = 30;          // RSI Level (lower)
input int                           InpStopLoss       = 200;         // Stop loss in points
input                               ENUM_APPLIED_PRICE
                                    enumInpPriceRSI   = PRICE_CLOSE, // Applied Price for RSI
                                    enumInpPriceTriX  = PRICE_CLOSE; // Applied Price for TriX

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
int                                HandleRSI;     // Handle for RSI  Indicator
int                                HandleTriX;    // Handle for TriX Indicator
double                             ArrayRSI[];    // Dynamic array for RSI  data
double                             ArrayTriX[];   // Dynamic array for Trix data
datetime                           openTimeBuy = 0;
         

int OnInit()
{
   
       // Set Magic Number to Trade Object
           trade.SetExpertMagicNumber(20230714);
   
       // Create Handle for RSI Indicator
           HandleRSI = iRSI( _Symbol, _Period, InpPeriodRSI, enumInpPriceRSI );
           if( HandleRSI == INVALID_HANDLE )
         {
            Print("Error creating iRSI handle: ", _LastError );
            return( INIT_FAILED );
         }
   
        // Create Handle for Trix Indicator
           HandleTriX = iTriX( _Symbol, _Period, InpPeriodTriX, enumInpPriceTriX );
           if( HandleTriX == INVALID_HANDLE )
         {
            Print("Error creating iTriX handle: ", _LastError );
            return( INIT_FAILED );
         }
   
        // Initialisation Succeeded
           return( INIT_SUCCEEDED );
         }
   
        //normalize price
          bool NormalizePrice (double &price){
 
          double tickSize=0;
          if(!SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE,tickSize)){
          Print("Failed to get tick size");
          return false;
        }
          price = NormalizeDouble(MathRound(price/tickSize)*tickSize,_Digits);
          return true;
 }
     

void OnTick()
{
   
       // get current tick
         if(!SymbolInfoTick(_Symbol,currentTick)){Print("Failed to get current tick");return;}
         
       
         
       //check for new bar open ticks
         if(!IsNewBar()){return;} 
             
      
       // Get RSI value of current bar/candle
         if( CopyBuffer( HandleRSI,  0, 0, 2, ArrayRSI  ) > 0 )
            Print( "RSI  value at current bar: ", DoubleToString( ArrayRSI[0], 2 ) );  
   
       // Get & Print TriX value of current bar/candle
         if( CopyBuffer( HandleTriX, 0, 0, 2, ArrayTriX ) > 0 )
            Print( "TriX value at current bar: ", DoubleToString( ArrayTriX[0], _Digits ) );
            
       
       // count open positions
         int cntBuy,cntSell;
         if(!CountOpenPositions(cntBuy,cntSell)){return;}
         
       //Check for buy position
         if(cntBuy==0 && ArrayTriX[0]<=(InpRSILevelDown) && ArrayTriX[0]>(InpRSILevelUp) && openTimeBuy!=iTime(_Symbol,PERIOD_CURRENT,0)){
         
        //calculate stop loss 
         double sl = InpStopLoss==0 ? 0 :currentTick.bid - InpStopLoss * _Point;
         if(!NormalizePrice(sl)){return;}
         openTimeBuy=iTime(_Symbol,PERIOD_CURRENT,0);
         //double ask  = SymbolInfoDouble(_Symbol,SYMBOL_ASK);      
   
         trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,InpLots,currentTick.ask,sl,0,"test");
   
   
         }
   
        }
//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+
   
         //check if we have a bar open tick
         bool IsNewBar(){
 
         static datetime previouseTime = 0;
         datetime currentTime = iTime(_Symbol,PERIOD_CURRENT,0);
         if(previouseTime!=currentTime){
         previouseTime=currentTime;
         return true;
         }
         return false;
 
 
         }
        
         //count open positions
         bool CountOpenPositions(int &cntBuy, int &cntSell){
 
         cntBuy = 0;
         //cntSell = 0;
         int total = PositionsTotal();
         for(int i=total-1; i>=0; i--){
         ulong ticket = PositionGetTicket(i);
         if(ticket<=0){Print("Failed to get position ticket"); return false;}
         if(!PositionSelectByTicket(ticket)){Print("Failed to select position"); return false;}
         long magic;
         if(!PositionGetInteger(POSITION_MAGIC,magic)){Print("Failed to get position magic number"); return false;}
         if(magic==20230714){
         long type;
         if(!PositionGetInteger(POSITION_TYPE,type)){Print("Failed to get position type"); return false;}
         if(type==POSITION_TYPE_BUY){cntBuy++;}
         //if(POSITION_TYPE_SELL){cntSell++;}
         }
   
      }
  
  
         return true;
  
   }
Reason: