How can i speed up my backtest? ( Open code expert)

 
I have created a Supertrend expert but so i can optimize that and use it. I built it because i was trying to do the same thing on Tradingview but it was a manual process.
The problem is that it takes 12 hours to do the same test that i took about 40 ~ 60 minutes on TradingView. 

Here is the code: 

#include <Trade/Trade.mqh>


double nominalValuePerrUnitPerLot = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE) / SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double nominalValue = nominalValuePerrUnitPerLot * iClose(_Symbol,PERIOD_CURRENT, 0);
double CalculatedLots = ACCOUNT_BALANCE / nominalValue;


input double Lots = ACCOUNT_EQUITY;
//double Lots = (NormalizeDouble(CalculatedLots, 1));

input int TpFactor = 100;

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;
input int Periods = 10;
input double Multiplier = 3.0;

int stHandle;
int totalBars;

CTrade trade;
ulong posTicket;

int OnInit(){

   Print(" > Calculated Lots = ",CalculatedLots," NormalizedDouble: ", Lots);
   
   totalBars = iBars(_Symbol,Timeframe);
   stHandle = iCustom(_Symbol,Timeframe,"Supertrend.ex5",Periods,Multiplier);
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){
 
}

void OnTick(){
   int bars = iBars(_Symbol,Timeframe);
   if(totalBars != bars){
      totalBars = bars;
      
      
      double st[];
      CopyBuffer(stHandle,0,0,3,st);
   
      double close1 = iClose(_Symbol,Timeframe,1);
      double close2 = iClose(_Symbol,Timeframe,2);
      
      if(close1 > st[1] && close2 < st[0]){
         Print(__FUNCTION__," > Buy Signal...");
         
         if(posTicket > 0 ) {
            if(PositionSelectByTicket(posTicket)){
               if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){
                  if(trade.PositionClose(posTicket)){
                     Print(__FUNCTION__," > Pos #",posTicket," was closed...");
                  }
               }
            }
         }
         
         if(trade.Buy(Lots,_Symbol)){
            if(trade.ResultRetcode() == TRADE_RETCODE_DONE){
               posTicket = trade.ResultOrder();
            }
         }
      } else if(close1 < st[1] && close2 > st[0]){
         Print(__FUNCTION__," > Sell Signal...");
         
          if(posTicket > 0 ) {
            if(PositionSelectByTicket(posTicket)){
               if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){
                  if(trade.PositionClose(posTicket)){
                     Print(__FUNCTION__," > Pos #",posTicket," was closed...");
                  }
               }
            }
         }
         
         if(trade.Sell(Lots,_Symbol)){
            if(trade.ResultRetcode() == TRADE_RETCODE_DONE){
               posTicket = trade.ResultOrder();
            }
         }
       }
       
     Comment("ST Value [0]: ",st[0],
    "\nST Value [1]: ",st[1],
    "\nST Value [2]: ",st[2]);  
    
   }

      
    
    

 }

Can anyone help me to make a better code for backtesting?
 
  1.    int bars = iBars(_Symbol,Timeframe);
       if(totalBars != bars){
          totalBars = bars;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)


  2. double nominalValuePerrUnitPerLot = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE) / SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
    double nominalValue = nominalValuePerrUnitPerLot * iClose(_Symbol,PERIOD_CURRENT, 0);
    

    Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  3. input double Lots = ACCOUNT_EQUITY;

    That is the same as Lots = 3.

    Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.