135 lines
5.8 KiB
MQL5
135 lines
5.8 KiB
MQL5
//+------------------------------------------------------------------+
|
|
// MQL5 (MetaQuotes Language 5) is a high-level programming language used for developing trading robots, technical indicators, scripts, and libraries on the MetaTrader 5 platform. It is designed specifically for trading and is used by traders to create automated trading strategies and custom technical analysis tools.
|
|
//
|
|
// Here is a basic overview of the key components and syntax of MQL5:
|
|
//
|
|
// ### Basic Structure
|
|
//
|
|
// An MQL5 program typically consists of the following sections:
|
|
//
|
|
// 1. **Preprocessor Directives**: Used to include files, define constants, etc.
|
|
// 2. **Global Variables**: Variables that are accessible throughout the entire program.
|
|
// 3. **Functions**: Blocks of code that perform specific tasks.
|
|
// 4. **Event Handlers**: Special functions that handle specific events like initialization, deinitialization, and new ticks.
|
|
//
|
|
// ### Example: Simple Expert Advisor
|
|
//
|
|
// Below is a simple example of an Expert Advisor (EA) in MQL5 that opens a buy trade when the price crosses above a moving average.
|
|
//
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| SimpleEA.mq5|
|
|
//| Copyright 2023, MetaQuotes Software Corp. |
|
|
//| https://www.metaquotes.net |
|
|
//+------------------------------------------------------------------+
|
|
#include <Trade\Trade.mqh> // Include the trading library
|
|
|
|
input int MA_Period = 14; // Moving Average period
|
|
input double Lot_Size = 0.1; // Lot size for trading
|
|
|
|
CTrade trade; // Create an instance of the trading class
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
Print("SimpleEA initialized.");
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
Print("SimpleEA deinitialized.");
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick()
|
|
{
|
|
double ma = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 0); // Calculate the moving average
|
|
double price = Close[0]; // Get the current price
|
|
|
|
// Check if the price crosses above the moving average
|
|
if (price > ma && PositionSelect(Symbol()) == false)
|
|
{
|
|
trade.Buy(Lot_Size, NULL, price, 0, 0, "Buy Order");
|
|
Print("Buy order placed at price: ", price);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
|
|
//
|
|
// ### Key Components Explained
|
|
//
|
|
// 1. **Preprocessor Directives**:
|
|
// ```mql5
|
|
// #include <Trade\Trade.mqh>
|
|
// ```
|
|
// This line includes the trading library which provides functions for trading operations.
|
|
//
|
|
// 2. **Input Variables**:
|
|
// ```mql5
|
|
// input int MA_Period = 14;
|
|
// input double Lot_Size = 0.1;
|
|
// ```
|
|
// These are user-defined inputs that can be adjusted without modifying the code.
|
|
//
|
|
// 3. **Global Variables**:
|
|
// ```mql5
|
|
// CTrade trade;
|
|
// ```
|
|
// This creates an instance of the `CTrade` class for executing trade operations.
|
|
//
|
|
// 4. **Event Handlers**:
|
|
// - `OnInit()`: Called when the EA is initialized.
|
|
// - `OnDeinit()`: Called when the EA is deinitialized.
|
|
// - `OnTick()`: Called on every new tick (price update).
|
|
//
|
|
// 5. **Trading Logic**:
|
|
// ```mql5
|
|
// double ma = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 0);
|
|
// double price = Close[0];
|
|
//
|
|
// if (price > ma && PositionSelect(Symbol()) == false)
|
|
// {
|
|
// trade.Buy(Lot_Size, NULL, price, 0, 0, "Buy Order");
|
|
// Print("Buy order placed at price: ", price);
|
|
// }
|
|
// ```
|
|
// This part of the code calculates the moving average and checks if the current price is above the moving average. If the condition is met and there is no open position, it places a buy order.
|
|
//
|
|
// ### Conclusion
|
|
//
|
|
// This example provides a basic introduction to MQL5 and how to create a simple Expert Advisor. MQL5 is a powerful language with many features for developing complex trading strategies and tools. To master MQL5, you should explore its extensive documentation and practice by developing and testing your own trading algorithms.
|
|
//
|
|
|
|
//| CloseAtProfit.mq5 |
|
|
//| Copyright 2019, Ricardo de Jong.|
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start function |
|
|
//+------------------------------------------------------------------+
|
|
|
|
#include <Trade/Trade.mqh>
|
|
CTrade m_trade;
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start function |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
for(int i=0; i<200; i++)
|
|
{
|
|
string SelectPosition=PositionGetSymbol(i);
|
|
double PositionInProfit=PositionGetDouble(POSITION_PROFIT);
|
|
if(PositionInProfit>0.05)
|
|
{
|
|
m_trade.PositionClose(SelectPosition,-1);
|
|
}
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|