# 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:

```//+------------------------------------------------------------------+
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"

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 TP = 50;//Take Profit
input int Multiply = 1;//Multiply
static input double Lots = 0.10;//Lots

int RSIHigh = 100 - RSILow;
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);

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;
}
param = LR;

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

//+------------------------------------------------------------------+
//| 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 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);

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

if(macdarray[1] > signalarray[1] && macdarray[1] > 0 && minusarray[1] > ADXEntry && minusarray[1] > plusarray[1] && macdarray[1] < macdarray[3] && isNewBar == true)
{
}
if(macdarray[1] < signalarray[1] && macdarray[1] < 0 && plusarray[1] > ADXEntry && plusarray[1] > minusarray[1] && macdarray[1] > macdarray[3] && isNewBar == true)
{
}
}

//+------------------------------------------------------------------+```

Discover new MetaTrader 5 opportunities with MQL5 community and services
• 2023.05.15
• www.mql5.com
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 ?

• 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.