calc/tests/UnitTestSMA.mq5

129 lines
4.4 KiB
MQL5
Raw Permalink Normal View History

2025-12-17 15:11:55 -03:00
//+------------------------------------------------------------------+
//| UnitTests.mq5 |
//| Douglas Rechia |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
2026-02-13 14:27:21 -03:00
#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"
2025-12-17 15:11:55 -03:00
2026-02-02 15:37:53 -03:00
#include "../knitpkg/include/douglasrechia/calc/Calc.mqh"
2025-12-17 15:11:55 -03:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-02-13 14:27:21 -03:00
bool TestBar1()
2025-12-17 15:11:55 -03:00
{
2026-02-02 15:37:53 -03:00
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
2025-12-17 15:11:55 -03:00
if(!bar.Refresh(15))
return false;
2025-12-17 15:34:26 -03:00
2025-12-17 15:11:55 -03:00
MqlRates m_rates[];
ArraySetAsSeries(m_rates, true);
if(CopyRates(_Symbol, PERIOD_CURRENT, 0, 10, m_rates) <= 0)
return false;
2025-12-18 09:09:05 -03:00
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;
2025-12-17 15:11:55 -03:00
}
2025-12-17 15:34:26 -03:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-02-13 14:27:21 -03:00
bool TestSMA1()
2025-12-17 15:34:26 -03:00
{
2026-02-02 15:37:53 -03:00
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
2025-12-17 15:34:26 -03:00
if(!bar.Refresh(15))
return false;
2025-12-17 15:11:55 -03:00
2026-02-02 15:37:53 -03:00
double sma = douglasrechia::SMA(bar.CloseSeries(), 5, 0);
2025-12-17 15:34:26 -03:00
2025-12-18 09:09:05 -03:00
double smaExpected = (bar.Close(0) + bar.Close(1) + bar.Close(2) + bar.Close(3) + bar.Close(4))/5.0;
return sma == smaExpected;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-02-13 14:27:21 -03:00
bool TestSMA2()
2025-12-18 09:09:05 -03:00
{
2026-02-02 15:37:53 -03:00
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
2025-12-18 09:09:05 -03:00
if(!bar.Refresh(15))
return false;
2026-02-02 15:37:53 -03:00
double sma = douglasrechia::SMA(bar.OpenSeries(), 5, 0);
2025-12-18 09:09:05 -03:00
double smaExpected = (bar.Open(0) + bar.Open(1) + bar.Open(2) + bar.Open(3) + bar.Open(4))/5.0;
2025-12-17 15:34:26 -03:00
return sma == smaExpected;
}
2025-12-17 15:11:55 -03:00
//+------------------------------------------------------------------+
//| DoTests |
//+------------------------------------------------------------------+
void DoTests(int &tests_performed,int &tests_passed)
{
string test_name="";
//--- Test1
tests_performed++;
2026-02-13 14:27:21 -03:00
test_name="TestBar1";
if(TestBar1())
2025-12-17 15:11:55 -03:00
{
tests_passed++;
2025-12-17 15:34:26 -03:00
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
//--- Test2
tests_performed++;
2026-02-13 14:27:21 -03:00
test_name="TestSMA1";
if(TestSMA1())
2025-12-17 15:34:26 -03:00
{
tests_passed++;
2025-12-18 09:09:05 -03:00
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
//--- Test3
tests_performed++;
2026-02-13 14:27:21 -03:00
test_name="TestSMA2";
if(TestSMA2())
2025-12-18 09:09:05 -03:00
{
tests_passed++;
2025-12-17 15:11:55 -03:00
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()
{
2026-01-01 19:20:30 -03:00
UnitTests("Calc");
2025-12-17 15:11:55 -03:00
}
//+------------------------------------------------------------------+