my-ea-template/MyEA.mq5

45 lines
1.6 KiB
MQL5
Raw Permalink Normal View History

2025-08-18 07:39:28 +00:00
//+------------------------------------------------------------------+
//| MyEA.mq5 |
2025-08-18 07:52:26 +00:00
//| Copyright 2025, Daruma00 |
2025-08-18 07:39:28 +00:00
//| https://forge.mql5.io/Daruma00/my-ea-template |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Daruma00"
#property link "https://forge.mql5.io/Daruma00/my-ea-template"
#property version "1.00"
#property strict
2025-08-18 07:52:26 +00:00
// === 1. Input Parameters ===
// These can be changed by the user from the EA settings in MetaTrader
input double LotSize = 0.1; // Trading lot size
input int StopLoss = 100; // Stop loss in points
input int TakeProfit = 200; // Take profit in points
2025-08-18 07:39:28 +00:00
2025-08-18 07:52:26 +00:00
// === 2. Include Required Library ===
// Provides trading functions (buy, sell, etc.)
2025-08-18 07:39:28 +00:00
#include <Trade\Trade.mqh>
2025-08-18 07:52:26 +00:00
// === 3. Global Variables ===
CTrade trade; // Trading object for executing orders
2025-08-18 07:39:28 +00:00
2025-08-18 07:52:26 +00:00
// === 4. Main Event Functions ===
// Called once when the EA is attached to a chart
2025-08-18 07:39:28 +00:00
int OnInit() {
Print("EA started");
return(INIT_SUCCEEDED);
}
2025-08-18 07:52:26 +00:00
// Called once when the EA is removed from the chart
2025-08-18 07:39:28 +00:00
void OnDeinit(const int reason) {
Print("EA stopped");
}
2025-08-18 07:52:26 +00:00
// Called on every new market tick
2025-08-18 07:39:28 +00:00
void OnTick() {
2025-08-18 07:52:26 +00:00
// Simple trading logic: open buy if no position exists
2025-08-18 07:39:28 +00:00
if(PositionsTotal() == 0) {
2025-08-18 07:52:26 +00:00
// Open a buy position with the specified lot size, stop loss, and take profit
2025-08-18 07:39:28 +00:00
trade.Buy(LotSize, _Symbol, Bid, (Bid-StopLoss*_Point), (Bid+TakeProfit*_Point));
}
}