การใช้งาน DLL เพื่อเพิ่มความสามารถของ EA

IUX Markets Bonus

ตัวอย่างการใช้งาน DLL เพื่อเพิ่มความสามารถของ EA

Dynamic-Link Library (DLL) เป็นไลบรารีที่สามารถเพิ่มฟังก์ชันการทำงานให้กับ EA ได้อย่างมาก โดยเฉพาะอย่างยิ่งสำหรับการทำงานที่ต้องการประสิทธิภาพสูงหรือต้องการใช้ฟังก์ชันที่ไม่มีใน MQL4

ตัวอย่าง code ใช้ DLL
ตัวอย่าง code ใช้ DLL

ข้อดีของการใช้ DLL:

  1. เพิ่มประสิทธิภาพการทำงานสำหรับการคำนวณที่ซับซ้อน
  2. สามารถใช้ไลบรารีและฟังก์ชันจากภาษาอื่นๆ เช่น C++ หรือ C#
  3. ปกป้องโค้ดที่เป็นความลับหรือทรัพย์สินทางปัญญา
  4. เพิ่มความสามารถในการทำงานที่ไม่มีใน MQL4 โดยตรง เช่น การเชื่อมต่อกับฐานข้อมูลภายนอก

ตัวอย่างการสร้าง DLL:

สมมติว่าเราต้องการสร้าง DLL ที่มีฟังก์ชันคำนวณ Fibonacci levels แบบซับซ้อน เราจะสร้างไฟล์ C++ ดังนี้:

 



#include 

extern "C" __declspec(dllexport) void CalculateFibonacciLevels(double high, double low, double* levels, int size)
{
    std::vector fibLevels = {0.0, 23.6, 38.2, 50.0, 61.8, 76.4, 100.0};
    double range = high - low;

    for(int i = 0; i < size && i < fibLevels.size(); i++)
    {
        levels[i] = high - (range * fibLevels[i] / 100.0);
    }
}


 

คอมไพล์ไฟล์นี้เป็น DLL ชื่อ “FibonacciCalculator.dll”

ตัวอย่างการใช้งาน DLL ใน EA:

ต่อไปนี้เป็นตัวอย่าง EA ที่ใช้งาน DLL ที่เราสร้างขึ้น



#property copyright "Your Name"
#property link      "https://www.example.com"
#property version   "1.00"
#property strict

// Import DLL function
#import "FibonacciCalculator.dll"
   void CalculateFibonacciLevels(double high, double low, double& levels[], int size);
#import

// Input parameters
input int FibPeriod = 14;

// Global variables
double fibLevels[7];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Check if DLL is loaded successfully
    if(GetLastError() == ERR_FUNCTION_NOT_CONFIRMED)
    {
        Print("Error: Unable to load FibonacciCalculator.dll");
        return INIT_FAILED;
    }
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Cleanup code here
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Get high and low prices
    double high = High[iHighest(_Symbol, 0, MODE_HIGH, FibPeriod, 1)];
    double low = Low[iLowest(_Symbol, 0, MODE_LOW, FibPeriod, 1)];
    
    // Calculate Fibonacci levels using DLL function
    CalculateFibonacciLevels(high, low, fibLevels, 7);
    
    // Use Fibonacci levels for trading decisions
    double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    
    for(int i = 0; i < 6; i++) { if(currentPrice > fibLevels[i] && currentPrice < fibLevels[i+1])
        {
            Print("Price is between Fibonacci levels ", i, " and ", i+1);
            // Add your trading logic here
            break;
        }
    }
}

อธิบายองค์ประกอบของโค้ด:

  1. #import directive:
    • ใช้เพื่อนำเข้าฟังก์ชันจาก DLL
  2. OnInit():
    • ตรวจสอบว่า DLL ถูกโหลดสำเร็จหรือไม่
  3. OnTick():
    • คำนวณค่า high และ low
    • เรียกใช้ฟังก์ชัน CalculateFibonacciLevels จาก DLL
    • ใช้ค่า Fibonacci levels ในการตัดสินใจเทรด
 Exness Promotion
PNFPB Install PWA using share icon

For IOS and IPAD browsers, Install PWA using add to home screen in ios safari browser or add to dock option in macos safari browser