calc/tests/UnitTestSMA.mq5
2026-02-13 14:27:21 -03:00

129 lignes
4,4 Kio
MQL5

//+------------------------------------------------------------------+
//| UnitTests.mq5 |
//| Douglas Rechia |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#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 "../knitpkg/include/douglasrechia/calc/Calc.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TestBar1()
{
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
if(!bar.Refresh(15))
return false;
MqlRates m_rates[];
ArraySetAsSeries(m_rates, true);
if(CopyRates(_Symbol, PERIOD_CURRENT, 0, 10, m_rates) <= 0)
return false;
return bar.Close(1) == m_rates[1].close &&
bar.Close(2) == m_rates[2].close &&
bar.Close(3) == m_rates[3].close &&
bar.Close(4) == m_rates[4].close &&
bar.Size() == 15;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TestSMA1()
{
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
if(!bar.Refresh(15))
return false;
double sma = douglasrechia::SMA(bar.CloseSeries(), 5, 0);
double smaExpected = (bar.Close(0) + bar.Close(1) + bar.Close(2) + bar.Close(3) + bar.Close(4))/5.0;
return sma == smaExpected;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TestSMA2()
{
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
if(!bar.Refresh(15))
return false;
double sma = douglasrechia::SMA(bar.OpenSeries(), 5, 0);
double smaExpected = (bar.Open(0) + bar.Open(1) + bar.Open(2) + bar.Open(3) + bar.Open(4))/5.0;
return sma == smaExpected;
}
//+------------------------------------------------------------------+
//| DoTests |
//+------------------------------------------------------------------+
void DoTests(int &tests_performed,int &tests_passed)
{
string test_name="";
//--- Test1
tests_performed++;
test_name="TestBar1";
if(TestBar1())
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
//--- Test2
tests_performed++;
test_name="TestSMA1";
if(TestSMA1())
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
//--- Test3
tests_performed++;
test_name="TestSMA2";
if(TestSMA2())
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
}
//+------------------------------------------------------------------+
//| UnitTests() |
//+------------------------------------------------------------------+
void UnitTests(const string package_name)
{
PrintFormat("Unit tests for Package %s\n",package_name);
//--- initial values
int tests_performed=0;
int tests_passed=0;
//--- test distributions
DoTests(tests_performed,tests_passed);
//--- print statistics
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
UnitTests("Calc");
}
//+------------------------------------------------------------------+