455 lines
17 KiB
MQL5
455 lines
17 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| UnitTests.mq5 |
|
|
//| Douglas Rechia |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Douglas Rechia"
|
|
#property link "https://www.mql5.com"
|
|
#property version "2.00"
|
|
|
|
#include "../knitpkg/include/douglasrechia/bar/BarArray.mqh"
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool TestBarArray()
|
|
{
|
|
|
|
datetime timeArray[];
|
|
double openArray[];
|
|
double highArray[];
|
|
double lowArray[];
|
|
double closeArray[];
|
|
long volumeArray[];
|
|
long tickVolumeArray[];
|
|
|
|
if(CopyTime(_Symbol, PERIOD_CURRENT, 0, 5, timeArray) != 5)
|
|
{
|
|
Print("CopyTime error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyOpen(_Symbol, PERIOD_CURRENT, 0, 5, openArray) != 5)
|
|
{
|
|
Print("CopyOpen error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyHigh(_Symbol, PERIOD_CURRENT, 0, 5, highArray) != 5)
|
|
{
|
|
Print("CopyHigh error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyLow(_Symbol, PERIOD_CURRENT, 0, 5, lowArray) != 5)
|
|
{
|
|
Print("CopyLow error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyClose(_Symbol, PERIOD_CURRENT, 0, 5, closeArray) != 5)
|
|
{
|
|
Print("CopyClose error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyRealVolume(_Symbol, PERIOD_CURRENT, 0, 5, volumeArray) != 5)
|
|
{
|
|
Print("CopyRealVolume error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyTickVolume(_Symbol, PERIOD_CURRENT, 0, 5, tickVolumeArray) != 5)
|
|
{
|
|
Print("CopyTickVolume error");
|
|
return false;
|
|
}
|
|
|
|
douglasrechia::BarArray bar(timeArray, openArray, highArray, lowArray, closeArray, volumeArray, tickVolumeArray);
|
|
|
|
return
|
|
bar.TimeSeries().ValueAtShift(0) == timeArray[4] &&
|
|
bar.TimeSeries().ValueAtShift(1) == timeArray[3] &&
|
|
bar.TimeSeries().ValueAtShift(2) == timeArray[2] &&
|
|
bar.TimeSeries().ValueAtShift(3) == timeArray[1] &&
|
|
bar.TimeSeries().ValueAtShift(4) == timeArray[0] &&
|
|
|
|
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.OpenSeries().ValueAtShift(0) == openArray[4] &&
|
|
bar.OpenSeries().ValueAtShift(1) == openArray[3] &&
|
|
bar.OpenSeries().ValueAtShift(2) == openArray[2] &&
|
|
bar.OpenSeries().ValueAtShift(3) == openArray[1] &&
|
|
bar.OpenSeries().ValueAtShift(4) == openArray[0] &&
|
|
|
|
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.HighSeries().ValueAtShift(0) == highArray[4] &&
|
|
bar.HighSeries().ValueAtShift(1) == highArray[3] &&
|
|
bar.HighSeries().ValueAtShift(2) == highArray[2] &&
|
|
bar.HighSeries().ValueAtShift(3) == highArray[1] &&
|
|
bar.HighSeries().ValueAtShift(4) == highArray[0] &&
|
|
|
|
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.LowSeries().ValueAtShift(0) == lowArray[4] &&
|
|
bar.LowSeries().ValueAtShift(1) == lowArray[3] &&
|
|
bar.LowSeries().ValueAtShift(2) == lowArray[2] &&
|
|
bar.LowSeries().ValueAtShift(3) == lowArray[1] &&
|
|
bar.LowSeries().ValueAtShift(4) == lowArray[0] &&
|
|
|
|
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.CloseSeries().ValueAtShift(0) == closeArray[4] &&
|
|
bar.CloseSeries().ValueAtShift(1) == closeArray[3] &&
|
|
bar.CloseSeries().ValueAtShift(2) == closeArray[2] &&
|
|
bar.CloseSeries().ValueAtShift(3) == closeArray[1] &&
|
|
bar.CloseSeries().ValueAtShift(4) == closeArray[0] &&
|
|
|
|
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.VolumeSeries().ValueAtShift(0) == volumeArray[4] &&
|
|
bar.VolumeSeries().ValueAtShift(1) == volumeArray[3] &&
|
|
bar.VolumeSeries().ValueAtShift(2) == volumeArray[2] &&
|
|
bar.VolumeSeries().ValueAtShift(3) == volumeArray[1] &&
|
|
bar.VolumeSeries().ValueAtShift(4) == volumeArray[0] &&
|
|
|
|
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) &&
|
|
|
|
bar.TickVolumeSeries().ValueAtShift(0) == tickVolumeArray[4] &&
|
|
bar.TickVolumeSeries().ValueAtShift(1) == tickVolumeArray[3] &&
|
|
bar.TickVolumeSeries().ValueAtShift(2) == tickVolumeArray[2] &&
|
|
bar.TickVolumeSeries().ValueAtShift(3) == tickVolumeArray[1] &&
|
|
bar.TickVolumeSeries().ValueAtShift(4) == tickVolumeArray[0] &&
|
|
|
|
bar.OpenSeries().Size() == bar.Size();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool TestBarArray2()
|
|
{
|
|
|
|
datetime timeArray[];
|
|
double openArray[];
|
|
double highArray[];
|
|
double lowArray[];
|
|
double closeArray[];
|
|
long volumeArray[];
|
|
long tickVolumeArray[];
|
|
|
|
if(CopyTime(_Symbol, PERIOD_CURRENT, 0, 5, timeArray) != 5)
|
|
{
|
|
Print("CopyTime error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyOpen(_Symbol, PERIOD_CURRENT, 0, 5, openArray) != 5)
|
|
{
|
|
Print("CopyOpen error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyHigh(_Symbol, PERIOD_CURRENT, 0, 5, highArray) != 5)
|
|
{
|
|
Print("CopyHigh error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyLow(_Symbol, PERIOD_CURRENT, 0, 5, lowArray) != 5)
|
|
{
|
|
Print("CopyLow error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyClose(_Symbol, PERIOD_CURRENT, 0, 5, closeArray) != 5)
|
|
{
|
|
Print("CopyClose error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyRealVolume(_Symbol, PERIOD_CURRENT, 0, 5, volumeArray) != 5)
|
|
{
|
|
Print("CopyRealVolume error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyTickVolume(_Symbol, PERIOD_CURRENT, 0, 5, tickVolumeArray) != 5)
|
|
{
|
|
Print("CopyTickVolume error");
|
|
return false;
|
|
}
|
|
|
|
douglasrechia::BarArray bar(timeArray, openArray, highArray, lowArray, closeArray, volumeArray, tickVolumeArray,-1,-1,false);
|
|
|
|
return
|
|
bar.TimeSeries().ValueAtShift(0) == timeArray[0] &&
|
|
bar.TimeSeries().ValueAtShift(1) == timeArray[1] &&
|
|
bar.TimeSeries().ValueAtShift(2) == timeArray[2] &&
|
|
bar.TimeSeries().ValueAtShift(3) == timeArray[3] &&
|
|
bar.TimeSeries().ValueAtShift(4) == timeArray[4] &&
|
|
|
|
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.OpenSeries().ValueAtShift(0) == openArray[0] &&
|
|
bar.OpenSeries().ValueAtShift(1) == openArray[1] &&
|
|
bar.OpenSeries().ValueAtShift(2) == openArray[2] &&
|
|
bar.OpenSeries().ValueAtShift(3) == openArray[3] &&
|
|
bar.OpenSeries().ValueAtShift(4) == openArray[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.HighSeries().ValueAtShift(0) == highArray[0] &&
|
|
bar.HighSeries().ValueAtShift(1) == highArray[1] &&
|
|
bar.HighSeries().ValueAtShift(2) == highArray[2] &&
|
|
bar.HighSeries().ValueAtShift(3) == highArray[3] &&
|
|
bar.HighSeries().ValueAtShift(4) == highArray[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.LowSeries().ValueAtShift(0) == lowArray[0] &&
|
|
bar.LowSeries().ValueAtShift(1) == lowArray[1] &&
|
|
bar.LowSeries().ValueAtShift(2) == lowArray[2] &&
|
|
bar.LowSeries().ValueAtShift(3) == lowArray[3] &&
|
|
bar.LowSeries().ValueAtShift(4) == lowArray[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.CloseSeries().ValueAtShift(0) == closeArray[0] &&
|
|
bar.CloseSeries().ValueAtShift(1) == closeArray[1] &&
|
|
bar.CloseSeries().ValueAtShift(2) == closeArray[2] &&
|
|
bar.CloseSeries().ValueAtShift(3) == closeArray[3] &&
|
|
bar.CloseSeries().ValueAtShift(4) == closeArray[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.VolumeSeries().ValueAtShift(0) == volumeArray[0] &&
|
|
bar.VolumeSeries().ValueAtShift(1) == volumeArray[1] &&
|
|
bar.VolumeSeries().ValueAtShift(2) == volumeArray[2] &&
|
|
bar.VolumeSeries().ValueAtShift(3) == volumeArray[3] &&
|
|
bar.VolumeSeries().ValueAtShift(4) == volumeArray[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) &&
|
|
|
|
bar.TickVolumeSeries().ValueAtShift(0) == tickVolumeArray[0] &&
|
|
bar.TickVolumeSeries().ValueAtShift(1) == tickVolumeArray[1] &&
|
|
bar.TickVolumeSeries().ValueAtShift(2) == tickVolumeArray[2] &&
|
|
bar.TickVolumeSeries().ValueAtShift(3) == tickVolumeArray[3] &&
|
|
bar.TickVolumeSeries().ValueAtShift(4) == tickVolumeArray[4] &&
|
|
|
|
bar.OpenSeries().Size() == bar.Size();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool TestBarArray3()
|
|
{
|
|
|
|
datetime timeArray[];
|
|
double openArray[];
|
|
double highArray[];
|
|
double lowArray[];
|
|
double closeArray[];
|
|
long volumeArray[];
|
|
long tickVolumeArray[];
|
|
|
|
if(CopyTime(_Symbol, PERIOD_CURRENT, 0, 5, timeArray) != 5)
|
|
{
|
|
Print("CopyTime error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyOpen(_Symbol, PERIOD_CURRENT, 0, 5, openArray) != 5)
|
|
{
|
|
Print("CopyOpen error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyHigh(_Symbol, PERIOD_CURRENT, 0, 5, highArray) != 5)
|
|
{
|
|
Print("CopyHigh error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyLow(_Symbol, PERIOD_CURRENT, 0, 5, lowArray) != 5)
|
|
{
|
|
Print("CopyLow error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyClose(_Symbol, PERIOD_CURRENT, 0, 5, closeArray) != 5)
|
|
{
|
|
Print("CopyClose error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyRealVolume(_Symbol, PERIOD_CURRENT, 0, 5, volumeArray) != 5)
|
|
{
|
|
Print("CopyRealVolume error");
|
|
return false;
|
|
}
|
|
|
|
if(CopyTickVolume(_Symbol, PERIOD_CURRENT, 0, 5, tickVolumeArray) != 5)
|
|
{
|
|
Print("CopyTickVolume error");
|
|
return false;
|
|
}
|
|
|
|
douglasrechia::BarArray bar(timeArray, openArray, highArray, lowArray, closeArray, volumeArray, tickVolumeArray,1,3,false);
|
|
|
|
return
|
|
bar.TimeSeries().ValueAtShift(0) == timeArray[1] &&
|
|
bar.TimeSeries().ValueAtShift(1) == timeArray[2] &&
|
|
bar.TimeSeries().ValueAtShift(2) == timeArray[3] &&
|
|
|
|
bar.OpenSeries().ValueAtShift(0) == openArray[1] &&
|
|
bar.OpenSeries().ValueAtShift(1) == openArray[2] &&
|
|
bar.OpenSeries().ValueAtShift(2) == openArray[3] &&
|
|
|
|
bar.HighSeries().ValueAtShift(0) == highArray[1] &&
|
|
bar.HighSeries().ValueAtShift(1) == highArray[2] &&
|
|
bar.HighSeries().ValueAtShift(2) == highArray[3] &&
|
|
|
|
bar.LowSeries().ValueAtShift(0) == lowArray[1] &&
|
|
bar.LowSeries().ValueAtShift(1) == lowArray[2] &&
|
|
bar.LowSeries().ValueAtShift(2) == lowArray[3] &&
|
|
|
|
bar.CloseSeries().ValueAtShift(0) == closeArray[1] &&
|
|
bar.CloseSeries().ValueAtShift(1) == closeArray[2] &&
|
|
bar.CloseSeries().ValueAtShift(2) == closeArray[3] &&
|
|
|
|
bar.VolumeSeries().ValueAtShift(0) == volumeArray[1] &&
|
|
bar.VolumeSeries().ValueAtShift(1) == volumeArray[2] &&
|
|
bar.VolumeSeries().ValueAtShift(2) == volumeArray[3] &&
|
|
|
|
bar.TickVolumeSeries().ValueAtShift(0) == tickVolumeArray[1] &&
|
|
bar.TickVolumeSeries().ValueAtShift(1) == tickVolumeArray[2] &&
|
|
bar.TickVolumeSeries().ValueAtShift(2) == tickVolumeArray[3] &&
|
|
|
|
bar.OpenSeries().Size() == bar.Size() &&
|
|
bar.Size() == 3;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| DoTests |
|
|
//+------------------------------------------------------------------+
|
|
void DoTests(int &tests_performed,int &tests_passed)
|
|
{
|
|
string test_name="";
|
|
|
|
//--- TestBarArray
|
|
tests_performed++;
|
|
test_name="TestBarArray";
|
|
if(TestBarArray())
|
|
{
|
|
tests_passed++;
|
|
PrintFormat("%s pass",test_name);
|
|
}
|
|
else
|
|
PrintFormat("%s failed",test_name);
|
|
|
|
//--- TestBarArray2
|
|
tests_performed++;
|
|
test_name="TestBarArray2";
|
|
if(TestBarArray2())
|
|
{
|
|
tests_passed++;
|
|
PrintFormat("%s pass",test_name);
|
|
}
|
|
else
|
|
PrintFormat("%s failed",test_name);
|
|
|
|
//--- TestBarArray3
|
|
tests_performed++;
|
|
test_name="TestBar3";
|
|
if(TestBarArray3())
|
|
{
|
|
tests_passed++;
|
|
PrintFormat("%s pass",test_name);
|
|
}
|
|
else
|
|
PrintFormat("%s failed",test_name);
|
|
|
|
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()
|
|
{
|
|
UnitTests("BarMqlRates");
|
|
}
|
|
//+------------------------------------------------------------------+
|