calc/tests/UnitTestATR.mq5

106 lines
3.8 KiB
MQL5
Raw Permalink Normal View History

2026-02-13 14:27:21 -03:00
//+------------------------------------------------------------------+
//| UnitTestATR.mq5 |
//| Unit Tests for Package calc |
//| Organization: douglasrechia |
//+------------------------------------------------------------------+
#property copyright "Douglas Rechia"
#property link "https://knitpkg.dev"
#property description "Description: Unit tests for package calc"
#property description "Organization: douglasrechia"
#property description "Author: Doug"
#property description "License: MIT"
#property description ""
#property description "Powered by KnitPkg for MetaTrader"
#property description "https://knitpkg.dev"
// Include the headers under test
#include "../knitpkg/include/douglasrechia/calc/Calc.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TestATR1()
{
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
if(!bar.Refresh(15))
return false;
double atr0 = douglasrechia::ATR(bar, 5, 0);
double atr1 = douglasrechia::ATR(bar, 5, 1);
double atr2 = douglasrechia::ATR(bar, 5, 2);
double atr0Expected = 0.0;
for(int i = 0; i < 5; i++)
{
double tr = MathMax(bar.High(i) - bar.Low(i),
MathMax(MathAbs(bar.High(i) - bar.Close(i+1)),
MathAbs(bar.Low(i) - bar.Close(i+1))));
atr0Expected += tr;
}
atr0Expected /= 5.0;
double atr1Expected = 0.0;
for(int i = 1; i < 6; i++)
{
double tr = MathMax(bar.High(i) - bar.Low(i),
MathMax(MathAbs(bar.High(i) - bar.Close(i+1)),
MathAbs(bar.Low(i) - bar.Close(i+1))));
atr1Expected += tr;
}
atr1Expected /= 5.0;
double atr2Expected = 0.0;
for(int i = 2; i < 7; i++)
{
double tr = MathMax(bar.High(i) - bar.Low(i),
MathMax(MathAbs(bar.High(i) - bar.Close(i+1)),
MathAbs(bar.Low(i) - bar.Close(i+1))));
atr2Expected += tr;
}
atr2Expected /= 5.0;
return atr0 == atr0Expected && atr1 == atr1Expected && atr2 == atr2Expected;
}
//+------------------------------------------------------------------+
//| DoTests |
//+------------------------------------------------------------------+
void DoTests(int &testsPerformed,int &testsPassed)
{
string testName="";
//--- TestATR1
testsPerformed++;
testName="TestATR1";
if(TestATR1())
{
testsPassed++;
PrintFormat("%s pass",testName);
}
else
PrintFormat("%s failed",testName);
//---
// Add more tests here as needed
}
//+------------------------------------------------------------------+
//| UnitTests() |
//+------------------------------------------------------------------+
void UnitTests(const string packageName)
{
PrintFormat("Unit tests for Package %s\n",packageName);
//--- initial values
int testsPerformed=0;
int testsPassed=0;
//--- test distributions
DoTests(testsPerformed,testsPassed);
//--- print statistics
PrintFormat("\n%d of %d passed",testsPassed,testsPerformed);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
UnitTests("autoctest");
}
//+------------------------------------------------------------------+