Indicators Combiner In One Indicator

TopicStarter

Moderator
Apr 15, 2024
9,282
4
38
# [Title] Expert Advisor - README

## Overview

This project contains an MQL5 Expert Advisor (EA) for MetaTrader 5, designed to execute automated trading strategies. The EA includes a built-in error logging mechanism and allows for configurable trading parameters like lot size, stop loss, and take profit levels.

**Generated by AI Based on User's Request**

For more comprehensive trading solutions and in-depth reviews, please visit [Forex Robot Easy](https://forexroboteasy.com/forex-robot-review/comprehensive-review-of-indicators-combiner-in-one-indicator-trading-system/).

## Files in the Project

1. **[Title].mq5**: The main source code file for the Expert Advisor.

## Functionality

The code consists of two primary classes within a namespace `Product`:

1. **Logger**:
- Handles error logging to a specified file.
- Methods:
- `Logger(string logFile)`: Constructor to initialize the log file.
- `LogError(string message)`: Logs an error message with a timestamp.

2. **Application** (inherits from Logger):
- Manages the trading logic, including lot size, stop loss, and take profit.
- Methods:
- `Application(string logFile)`: Constructor to initialize application settings and the log file.
- `Initialize(double lot, double sl, double tp)`: Initializes the application with specific trading parameters.
- `OpenTrade(string symbol, int type)`: Opens a trade (buy/sell) with specified parameters.

## Code Walkthrough

### Logger Class

```mql5
class Logger {
protected:
string logFileName;

public:
Logger(string logFile) {
logFileName = logFile;
}

void LogError(string message) {
string timeStamp = TimeToString(TimeCurrent(), TIME_DATE | TIME_MINUTES);
message = timeStamp + - + message;

int fileHandle = FileOpen(logFileName, FILE_WRITE|FILE_CSV|FILE_READ|FILE_ANSI);
if (fileHandle != INVALID_HANDLE) {
FileSeek(fileHandle, 0, SEEK_END);
FileWrite(fileHandle, message);
FileClose(fileHandle);
}
}
};
```
- **LogFileName**: The name of the log file.
- **LogError**: Appends error messages to the log file with a timestamp.

### Application Class

```mql5
class Application : public Logger {
protected:
double lotSize;
double stopLoss;
double takeProfit;

public:
Application(string logFile) : Logger(logFile) {
lotSize = 0.1;
stopLoss = 50;
takeProfit = 100;
}

void Initialize(double lot, double sl, double tp) {
lotSize = lot;
stopLoss = sl;
takeProfit = tp;
}

bool OpenTrade(string symbol, int type) {
double price = 0;
int ticket;

if (type == OP_BUY) {
price = SymbolInfoDouble(symbol, SYMBOL_ASK);
} else if (type == OP_SELL) {
price = SymbolInfoDouble(symbol, SYMBOL_BID);
} else {
LogError(Invalid trade type specified.);
return false;
}

double slPrice, tpPrice;
if (type == OP_BUY) {
slPrice = price - (stopLoss * _Point);
tpPrice = price + (takeProfit * _Point);
} else {
slPrice = price + (stopLoss * _Point);
tpPrice = price - (takeProfit * _Point);
}

ticket = OrderSend(symbol, type, lotSize, price, 3, slPrice, tpPrice, Trade opened by EA, 0, clrNONE);
if (ticket < 0) {
LogError(OrderSend failed with error: + ErrorDescription(GetLastError()));
return false;
}
return true;
}
};
```
- **Initializer**: Sets up default values for trading parameters.
- **Initialize**: Configures custom values for trading parameters.
- **OpenTrade**: Executes the trade by determining the price, setting stop loss, and take profit levels, and then sending the order. Logs errors if any occur.

### Expert Functions

#### OnInit

```mql5
int OnInit() {
// Create an instance of the Application class
Product::Application app(logfile.csv);

// Initialize application with custom settings
app.Initialize(0.2, 60, 120);

// Open a buy trade
if(!app.OpenTrade(Symbol(), OP_BUY)) {
Print(Failed to open trade. Check log for details.);
}

return INIT_SUCCEEDED;
}
```
- Initializes the EA with custom settings and attempts to open a buy trade.

#### OnDeinit

```mql5
void OnDeinit(const int reason) {
}
```
- Placeholder for deinitialization code.

#### OnTick

```mql5
void OnTick() {
}
```
- Placeholder for code to be executed on every tick.

## Conclusion

This example demonstrates how to structure an MQL5 Expert Advisor for automated trading, complete with error logging capabilities. Configure the trading parameters according to your strategy and let the EA handle the rest.

For more advanced trading strategies and tools, visit [Forex Robot Easy](https://forexroboteasy.com/forex-robot-review/comprehensive-review-of-indicators-combiner-in-one-indicator-trading-system/).
 

Attachments

  • Indicators Combiner In One Indicator.mq5
    4.3 KB · Views: 0
Looks like this setup isn't activating any trades in MT2 for me either. Might need to double-check the signal parameters or consider tweaking the algorithm. Any insights, fellow traders? Let's crack this together! 🚀