DislikedWoohoo! Success! Thank you for the help, broketrader! Now, I am going to have to study this and see if I can figure out how to apply it to a 100,000 element struct array.Ignored
Simplicity is the Ultimate Sophistication.
What is better to learn, MQL or AFL (Amibroker)?? 14 replies
starting with c++ before mql.. how much c++ should one learn? 2 replies
I Will Learn Coding in 1 Month - Give me the 80/20 of MT4 Coding 14 replies
MQL Coding - Let’s talk about how to code mql 0 replies
Book to Learn MQL? 8 replies
DislikedWoohoo! Success! Thank you for the help, broketrader! Now, I am going to have to study this and see if I can figure out how to apply it to a 100,000 element struct array.Ignored
Disliked{quote} It doesn't matter wether you have 1 or 1 million elements, you are just passing a reference to the c++ function, nothing more.Ignored
Disliked{quote}This is, of course, based on the knowledge that data created within a function is destroyed after that function returns.Ignored
struct TestStruct { int x, y; }; TestStruct* TestFunk() { TestStruct A; // This resides on the stack. A.x = 1; A.y = 2; return &A; }
struct TestStruct { int x, y; }; TestStruct* TestFunk() { TestStruct* pA = new TestStruct; // This resides on the heap. pA->x = 1; pA->y = 2; return pA; }
namespace NinjaTrader.Indicator { public class TestIndicator : Indicator { [StructLayout(LayoutKind.Sequential)] public struct TestStruct { public int x, y; } static TestStruct[] testy = new TestStruct[2]; protected override void OnBarUpdate() { if(CurrentBar < Count - 2) {return;} GetDLL.TestFunk( ref testy[0] ); Print("X0: " + testy[0].x.ToString() + " Y0: " + testy[0].y.ToString()); Print("X1: " + testy[1].x.ToString() + " Y1: " + testy[1].y.ToString()); } class GetDLL { GetDLL() {} ~GetDLL() {} [DllImport("testdll.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "TestFunk")] public static extern void TestFunk( ref TestStruct testy ); } } }
struct TestStruct { int x, y; }; extern "C" __declspec(dllexport) void __stdcall TestFunk( void *t) { TestStruct* ptE = (TestStruct*)t; ptE->x = 10; ptE->y = 2; ptE++; ptE->x = 20; ptE->y = 9; }
namespace DLLTest { [StructLayout(LayoutKind.Sequential)] public struct TestStruct { public int x, y; } class GetDLL { GetDLL() { } ~GetDLL() { } [DllImport("DLL.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "TestFunk")] public static extern TestStruct TestFunk( ref TestStruct x, int arraysize ); } class Program { static TestStruct[] testy = new TestStruct[2]; static void Main(string[] args) { testy[0].x = 10; testy[0].y = 2; testy[1].x = 20; testy[1].y = 9; GetDLL.TestFunk( ref testy[0], 2 ); // ref to first element in the array. Console.WriteLine( "X0: " + testy[0].x.ToString() + " Y0: " + testy[0].y.ToString()); Console.WriteLine( "X1: " + testy[1].x.ToString() + " Y1: " + testy[1].y.ToString()); } } }
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdlib.h> #include <stdio.h> BOOL APIENTRY DllMain( HMODULE, DWORD, LPVOID ) { return TRUE; } struct TestStruct { int x, y; }; extern "C" __declspec(dllexport) void __stdcall TestFunk( TestStruct* pts, int arraysize ) { for( int i=0; i<arraysize; i++ ){ pts[i].x += 20; } }
DislikedUsing MetaTrader 4's MetaEditor, can I use classes from MetaTrader 5? Looking thru the MQL5 Reference help, there is a Standard Library of classes that does now show up in the MQL4 Reference. Can I use the MQL5 Standard Library classes via my MT4 Editor? For instance where are the header include files for the MQL5 Trade Classes, say SymbolInfo.mqh? I don't have them in my MQL4 Include folder structure???Ignored
class Grid { protected: string gridSymbol; // EURUSD,USDTRY, etc. public: Grid(string symbol) { gridSymbol = symbol; } string getSymbol() { return symbol; } bool buildGrid() { //some algo code for building levels in grid } };
class Level { protected: Grid* linkedGrid; double price; Order* orders[]; public: Level(Grid* grid, double levelPrice) { linkedGrid = grid; price = NormalizeDouble( levelPrice, Digits ); } Grid* getGrid() { return linkedGrid; } bool addOrder(Order* order) { // code for adding orders // may be like CList arrays } };
class Order { protected: int m_TicketNumber; int m_MagicNumber; double m_OpenPrice; double m_Lots; Order( int magicnumber ){ m_MagicNumber = magicnumber; m_TicketNumber = -1; } };
class DataManager { private: int _days[], _price[], _plus[], _minus[]; public: DataManager(){}; void AddArrays(int &days[], int &price[], int &plus[], int &minus[]); int Date(int index); int Price(int index); int Plus(int index); int Minus(int index); int Days[]() { return _days; } };
void Days( int& arr[] ) { arr[0] = _days[0]; }
// Encapsulation class class IntArray{ public: int Data[]; }; class DataManager { private: ... IntArray m_Days; public: ... IntArray* Days(){ return &m_Days; } };
DataManager DM; IntArray* pDays = DM.Days(); pDays.Data[0] = 0;
Of course, not every line in your EA will be suitable for tests, there are things that depend for example on the environment which simply cannot be tested as is. But you would be surprised by the quantity of code that could finally be tested as long as the programmer had the unit testing in his mind at the time he writes his code.
To be continued...
#include "MindyEA-1.01.mq4"
#ifdef ABCD // some code here #endif #ifndef ABCD // some code here #endif
#define ABCD
#ifndef UNIT_TESTS int OnInit() { . . . } void OnDeinit(const int reason) { . . . } #endif
#define UNIT_TESTS
#define UNIT_TESTS #include "MindyEA-1.01.mq4" int OnInit() { return INIT_FAILED; }
class UnitTester{ int m_TestCount; int m_FailedTests; public: UnitTester(){ m_TestCount = 0; m_FailedTests = 0; } void Test(){ MessageBox( StringFormat("%d Tests done, %d Failed.", m_TestCount, m_FailedTests), "Unit Tests" ); } };
int OnInit() { UnitTester UT; UT.Test(); return INIT_FAILED; }
void TestPricePushBuy(){ Price P( BUY, 1.0020 ); P.Push( 16 ); if( P.Value() != 1.0004 ){ // Test failed. update statistics and return. } // Test succeeded, update statistics. }
#define SAVE_RESULT(val) m_TestCount++; if(!(val)){m_FailedTests++;return;}
// pushing the price in a BUY scenario lowers its value. void TestPricePushBuy(){ Price P( BUY, 1.0020 ); P.Push( 16 ); SAVE_RESULT( P.Value() == 1.0004 ); }
// pushing the price in a SELL scenario lowers its value. void TestPricePushSell(){ Price P( SELL, 1.0020 ); P.Push( 16 ); SAVE_RESULT( P.Value() == 1.0036 ) }
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Mindyyost Tester EA Copyright (c) 2015, broketrader, Switzerland. HISTORY: 1.00 / 10.11.2015 Tests EA Draft <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ #define UNIT_TESTS #include "MindyEA-1.01.mq4" /****************************************************************************************** Global macro definitions ******************************************************************************************/ #define SAVE_RESULT(val) m_TestCount++; if(!(val)){m_FailedTests++;return;} /****************************************************************************************** Tester Class ******************************************************************************************/ class UnitTester{ int m_TestCount; int m_FailedTests; public: UnitTester(){ m_TestCount = 0; m_FailedTests = 0; } // pushing the price in a BUY scenario lowers its value. void TestPricePushBuy(){ Price P( BUY, 1.0020 ); P.Push( 16 ); SAVE_RESULT( P.Value() == 1.0004 ); } // pushing the price in a SELL scenario lowers its value. void TestPricePushSell(){ Price P( SELL, 1.0020 ); P.Push( 16 ); SAVE_RESULT( P.Value() == 1.0036 ) } void Test(){ TestPricePushBuy(); TestPricePushSell(); MessageBox( StringFormat("%d Tests done, %d Failed.", m_TestCount, m_FailedTests), "Unit Tests" ); } }; int OnInit() { UnitTester UT; UT.Test(); return INIT_FAILED; }