mql4-calc/tests/UnitTestSMA.mq4

123 lines
4 KiB
MQL4

2026-01-05 17:01:03 -03:00
//+------------------------------------------------------------------+
//| UnitTests.mq5 |
//| Douglas Rechia |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Douglas Rechia"
#property link "https://www.mql5.com"
#property version "1.00"
2026-02-03 10:39:39 -03:00
#include "../knitpkg/include/douglasrechia/calc/Calc.mqh"
2026-01-05 17:01:03 -03:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Test1()
{
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 Test2()
{
BarMqlRates bar(_Symbol, PERIOD_CURRENT);
if(!bar.Refresh(15))
return false;
double sma = 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 Test3()
{
BarMqlRates bar(_Symbol, PERIOD_CURRENT);
if(!bar.Refresh(15))
return false;
double sma = 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="Test1";
if(Test1())
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
//--- Test2
tests_performed++;
test_name="Test2";
if(Test2())
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
//--- Test3
tests_performed++;
test_name="Test3";
if(Test3())
{
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");
}
//+------------------------------------------------------------------+