Integrate LR Correlation into OnTester

 

Hey guys, wondering if anyone has any clue how to integrate the LR Correlation statistic into the ontester using CTradeStatistics. Here's my code:

//+------------------------------------------------------------------+
//|                                                   macdrsiadx.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
#include <CTradeStatistics.mqh>
CTradeStatistics m_stat;

input int fastema = 12;//Fast Ema
input int slowema = 26;//Slow Ema
input int signalema = 9;//Signal Ema
input int rsiperiod = 5;//RSI Period
input int RSILow = 20;//RSI Low
input int ADXPeriod = 10;//ADX Period
input int ADXEntry = 30;//ADX Entry
input int ADXExit = 30;//ADX Exit
input int TP = 50;//Take Profit
input int Multiply = 1;//Multiply
static input double Lots = 0.10;//Lots

int RSIHigh = 100 - RSILow;
CTrade trade;
int macdhandle, rsihandle, adxhandle;
double macdarray[], signalarray[], rsiarray[], plusarray[], minusarray[];

int OnInit()
  {
ArraySetAsSeries(macdarray,true);
ArraySetAsSeries(signalarray,true);
ArraySetAsSeries(rsiarray,true);
ArraySetAsSeries(plusarray,true);
ArraySetAsSeries(minusarray,true);

macdhandle = iMACD(_Symbol,PERIOD_CURRENT,fastema,slowema,signalema,PRICE_CLOSE);
rsihandle = iRSI(_Symbol,PERIOD_CURRENT,rsiperiod,PRICE_CLOSE);
adxhandle = iADX(_Symbol,PERIOD_CURRENT,ADXPeriod);

   return(INIT_SUCCEEDED);
  }

double OnTester()
{
  double  param = 0.0;

//  Balance max + min Drawdown + Trades Number:
  double  balance = TesterStatistics(STAT_PROFIT);
  double  min_dd = TesterStatistics(STAT_BALANCE_DD);
  double sharpe = TesterStatistics(STAT_SHARPE_RATIO);
  double recovery = TesterStatistics(STAT_RECOVERY_FACTOR);
  double LR = m_stat.LRCorrelation();
  if(min_dd > 0.0)
  {
    min_dd = 1.0 / min_dd;
  }
  if(balance < 0)
    {
     balance = 0;
    }
  double  trades_number = TesterStatistics(STAT_TRADES);
  param = LR;

  return(param);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
IndicatorRelease(macdhandle);
IndicatorRelease(rsihandle);
IndicatorRelease(adxhandle);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
static datetime lastBarTime = 0;
   bool isNewBar = false;
   datetime thisBarTime = (datetime) SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE); // Retrieve current bar's open time
   if(lastBarTime != thisBarTime) // If lastBarTime is not the same as the time of the current day's open (thisBarTime), this is a new bar
     {
      lastBarTime = thisBarTime; // Assign lastDayTime as time of current bar's open
      isNewBar = true; // Current tick is the start of a new bar
     }  

double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK), bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double TKP = TP * _Point * 10;
double SLP = TKP * Multiply;

CopyBuffer(macdhandle,0,0,4,macdarray);
CopyBuffer(macdhandle,1,0,3,signalarray);
CopyBuffer(rsihandle,0,0,3,rsiarray);
CopyBuffer(adxhandle,1,0,3,plusarray);
CopyBuffer(adxhandle,2,0,3,minusarray);

//Exit Conditions//
/*
if(PositionSelect(_Symbol) == true)
  {
   if(macdarray[1] < 0 || plusarray[1] >= ADXExit)
     {
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
        {
         trade.PositionClose(_Symbol);
        }
     }
   if(macdarray[1] > 0 || minusarray[1] >= ADXExit)
   {
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
        {
         trade.PositionClose(_Symbol);
        }
     }  
  }
  */

   if(macdarray[1] > signalarray[1] && macdarray[1] > 0 && minusarray[1] > ADXEntry && minusarray[1] > plusarray[1] && macdarray[1] < macdarray[3] && isNewBar == true)
     {
      trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,Lots,ask,ask - SLP,ask + TKP,NULL);
     }
   if(macdarray[1] < signalarray[1] && macdarray[1] < 0 && plusarray[1] > ADXEntry && plusarray[1] > minusarray[1] && macdarray[1] > macdarray[3] && isNewBar == true)
     {
      trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,Lots,bid,bid + SLP,bid - TKP,NULL);
     }  
  }

//+------------------------------------------------------------------+


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.05.15
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Files:
 
So you are already doing it. What is the problem ?
 
Alain Verleyen #:
So you are already doing it. What is the problem ?

This code doesn't work for LR Correlation. It just returns a value of 0 even if it's greater than 0. 

 
Scott David Maclean #:

This code doesn't work for LR Correlation. It just returns a value of 0 even if it's greater than 0. 

Is this related to this library from the Codebase ?

Then ask to the author.

CTradeStatistics
CTradeStatistics
  • www.mql5.com
Class for the calculation of the ENUM_STATISTICS enumeration parameters
 
Scott David Maclean #:

This code doesn't work for LR Correlation. It just returns a value of 0 even if it's greater than 0. 

you need to call :

m_stat.Calculate()

before getting the LR 

But as the author states these are available in TesterStatistics() inside the tester.

Reason: