bar/tests/UnitTestsBarMqlRates.mq5

180 lines
6.8 KiB
MQL5
Raw Permalink Normal View History

2025-12-17 11:30:54 -03:00
//+------------------------------------------------------------------+
//| UnitTests.mq5 |
//| Douglas Rechia |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Douglas Rechia"
#property link "https://www.mql5.com"
2025-12-18 06:54:34 -03:00
#property version "2.00"
2025-12-17 11:30:54 -03:00
2026-02-02 13:42:14 -03:00
#include "../knitpkg/include/douglasrechia/bar/BarMqlRates.mqh"
#include "../knitpkg/include/douglasrechia/bar/BarWatcher.mqh"
#include "../knitpkg/include/douglasrechia/bar/TimeSeriesArray.mqh"
2025-12-17 11:30:54 -03:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TestBarRefresh()
{
2026-02-02 12:52:43 -03:00
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
2025-12-17 11:30:54 -03:00
if(!bar.Refresh(5))
return false;
MqlRates m_rates[];
ArraySetAsSeries(m_rates, true);
if(CopyRates(_Symbol, PERIOD_CURRENT, 0, 10, m_rates) <= 0)
return false;
2025-12-18 06:54:34 -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() == 5;
2025-12-17 11:30:54 -03:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TestNewBar()
{
2026-02-02 12:52:43 -03:00
douglasrechia::BarWatcher watcher(_Symbol, PERIOD_M1);
2025-12-17 11:30:54 -03:00
watcher.Update();
bool ok;
ok = watcher.IsFirstTick() && !watcher.IsNewBar();
datetime now=TimeLocal();
int nowint = (int)now;
int wait=((int)(nowint/60)+1)*60-nowint+2;
PrintFormat("Waiting for a new bar - %d seconds...", wait);
Sleep(wait*1000);
watcher.Update();
ok = !watcher.IsFirstTick() && watcher.IsNewBar();
if(!ok)
{
PrintFormat("Watcher failed. Check if market is open, this test could fail if %s don't get updated from the server", _Symbol);
return false;
}
watcher.Update();
ok = !watcher.IsFirstTick() && !watcher.IsNewBar();
if(!ok)
{
PrintFormat("Watcher failed. Check if market is open, this test could fail if %s don't get updated from the server", _Symbol);
return false;
}
return true;
}
//+------------------------------------------------------------------+
2025-12-18 06:54:34 -03:00
//| |
//+------------------------------------------------------------------+
bool TestBarSeries()
{
2026-02-02 12:52:43 -03:00
douglasrechia::BarMqlRates bar(_Symbol, PERIOD_CURRENT);
2025-12-18 06:54:34 -03:00
if(!bar.Refresh(5))
return false;
2025-12-18 10:57:54 -03:00
return
bar.TimeSeries().ValueAtShift(0) == bar.Time(0) &&
bar.TimeSeries().ValueAtShift(1) == bar.Time(1) &&
bar.TimeSeries().ValueAtShift(2) == bar.Time(2) &&
bar.TimeSeries().ValueAtShift(3) == bar.Time(3) &&
bar.TimeSeries().ValueAtShift(4) == bar.Time(4) &&
bar.OpenSeries().ValueAtShift(0) == bar.Open(0) &&
bar.OpenSeries().ValueAtShift(1) == bar.Open(1) &&
bar.OpenSeries().ValueAtShift(2) == bar.Open(2) &&
bar.OpenSeries().ValueAtShift(3) == bar.Open(3) &&
bar.OpenSeries().ValueAtShift(4) == bar.Open(4) &&
bar.HighSeries().ValueAtShift(0) == bar.High(0) &&
bar.HighSeries().ValueAtShift(1) == bar.High(1) &&
bar.HighSeries().ValueAtShift(2) == bar.High(2) &&
bar.HighSeries().ValueAtShift(3) == bar.High(3) &&
bar.HighSeries().ValueAtShift(4) == bar.High(4) &&
bar.LowSeries().ValueAtShift(0) == bar.Low(0) &&
bar.LowSeries().ValueAtShift(1) == bar.Low(1) &&
bar.LowSeries().ValueAtShift(2) == bar.Low(2) &&
bar.LowSeries().ValueAtShift(3) == bar.Low(3) &&
bar.LowSeries().ValueAtShift(4) == bar.Low(4) &&
bar.CloseSeries().ValueAtShift(0) == bar.Close(0) &&
bar.CloseSeries().ValueAtShift(1) == bar.Close(1) &&
bar.CloseSeries().ValueAtShift(2) == bar.Close(2) &&
bar.CloseSeries().ValueAtShift(3) == bar.Close(3) &&
bar.CloseSeries().ValueAtShift(4) == bar.Close(4) &&
bar.VolumeSeries().ValueAtShift(0) == bar.Volume(0) &&
bar.VolumeSeries().ValueAtShift(1) == bar.Volume(1) &&
bar.VolumeSeries().ValueAtShift(2) == bar.Volume(2) &&
bar.VolumeSeries().ValueAtShift(3) == bar.Volume(3) &&
bar.VolumeSeries().ValueAtShift(4) == bar.Volume(4) &&
bar.TickVolumeSeries().ValueAtShift(0) == bar.TickVolume(0) &&
bar.TickVolumeSeries().ValueAtShift(1) == bar.TickVolume(1) &&
bar.TickVolumeSeries().ValueAtShift(2) == bar.TickVolume(2) &&
bar.TickVolumeSeries().ValueAtShift(3) == bar.TickVolume(3) &&
bar.TickVolumeSeries().ValueAtShift(4) == bar.TickVolume(4) &&
2025-12-18 06:54:34 -03:00
bar.OpenSeries().Size() == bar.Size();
}
2025-12-18 10:57:54 -03:00
//+------------------------------------------------------------------+
2025-12-17 11:30:54 -03:00
//| DoTests |
//+------------------------------------------------------------------+
void DoTests(int &tests_performed,int &tests_passed)
{
string test_name="";
//--- TestBarRefresh
tests_performed++;
test_name="TestBarRefresh";
if(TestBarRefresh())
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
2025-12-18 10:57:54 -03:00
//--- TestBarSeries
2025-12-17 11:30:54 -03:00
tests_performed++;
2025-12-18 10:57:54 -03:00
test_name="TestBarSeries";
if(TestBarSeries())
2025-12-17 11:30:54 -03:00
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
2025-12-18 10:57:54 -03:00
//--- TestNewBar
tests_performed++;
test_name="TestNewBar";
if(TestNewBar())
2025-12-18 06:54:34 -03:00
{
tests_passed++;
PrintFormat("%s pass",test_name);
}
else
PrintFormat("%s failed",test_name);
2025-12-17 11:30:54 -03:00
return;
}
//+------------------------------------------------------------------+
//| 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()
{
2025-12-23 08:09:46 -03:00
UnitTests("BarMqlRates");
2025-12-17 11:30:54 -03:00
}
//+------------------------------------------------------------------+