MultiSymbolSignals/Experts/MultiSymbolExpert/MultiSymbolExpert.mq5

201 lines
9.1 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:10:44 +02:00
//+------------------------------------------------------------------+
//| MultiSymbolExpert.mq5 |
//| Copyright 2013, https://login.mql5.com/ru/users/tol64 |
//| Site, http://tol64.blogspot.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, http://tol64.blogspot.com"
#property link "https://www.mql5.com/de/articles/648"
#property description "email: hello.tol64@gmail.com"
#property version "1.0"
//--- Number of traded symbols
#define NUMBER_OF_SYMBOLS 2
//--- Name of the Expert Advisor
#define EXPERT_NAME MQL5InfoString(MQL5_PROGRAM_NAME)
//--- Include a class of the Standard Library
#include <Trade/Trade.mqh>
//--- Load the class
CTrade trade;
//--- Include custom libraries
#include "../../Include/MultiSymbolExpert/Enums.mqh"
#include "../../Include/MultiSymbolExpert/InitializeArrays.mqh"
#include "../../Include/MultiSymbolExpert/Errors.mqh"
#include "../../Include/MultiSymbolExpert/TradeSignals.mqh"
#include "../../Include/MultiSymbolExpert/TradeFunctions.mqh"
#include "../../Include/MultiSymbolExpert/ToString.mqh"
#include "../../Include/MultiSymbolExpert/Auxiliary.mqh"
//--- External parameters of the Expert Advisor
sinput long MagicNumber = 777; // Magic number
sinput int Deviation = 10; // Slippage
//---
sinput string delimeter_00=""; // --------------------------------
sinput string Symbol_01 = "EURUSD"; // Symbol 1
input int IndicatorPeriod_01 = 5; // | Indicator period
input double TakeProfit_01 = 100; // | Take Profit
input double StopLoss_01 = 50; // | Stop Loss
input double TrailingStop_01 = 10; // | Trailing Stop
input bool Reverse_01 = true; // | Position reversal
input double Lot_01 = 0.1; // | Lot
input double VolumeIncrease_01 = 0.1; // | Position volume increase
input double VolumeIncreaseStep_01 = 10; // | Volume increase step
//---
sinput string delimeter_01=""; // --------------------------------
sinput string Symbol_02 = "NZDUSD"; // Symbol 2
input int IndicatorPeriod_02 = 5; // | Indicator period
input double TakeProfit_02 = 100; // | Take Profit
input double StopLoss_02 = 50; // | Stop Loss
input double TrailingStop_02 = 10; // | Trailing Stop
input bool Reverse_02 = true; // | Position reversal
input double Lot_02 = 0.1; // | Lot
input double VolumeIncrease_02 = 0.1; // | Position volume increase
input double VolumeIncreaseStep_02 = 10; // | Volume increase step
//--- Arrays for storing external parameters
string Symbols[NUMBER_OF_SYMBOLS]; // Symbol
int IndicatorPeriod[NUMBER_OF_SYMBOLS]; // Indicator period
double TakeProfit[NUMBER_OF_SYMBOLS]; // Take Profit
double StopLoss[NUMBER_OF_SYMBOLS]; // Stop Loss
double TrailingStop[NUMBER_OF_SYMBOLS]; // Trailing Stop
bool Reverse[NUMBER_OF_SYMBOLS]; // Position reversal
double Lot[NUMBER_OF_SYMBOLS]; // Lot
double VolumeIncrease[NUMBER_OF_SYMBOLS]; // Position volume increase
double VolumeIncreaseStep[NUMBER_OF_SYMBOLS]; // Volume increase step
//--- Array of indicator agent handles
int spy_indicator_handles[NUMBER_OF_SYMBOLS];
//--- Array of signal indicator handles
int signal_indicator_handles[NUMBER_OF_SYMBOLS];
//--- Data arrays for checking trading conditions
struct PriceData
{
double value[];
};
PriceData open[NUMBER_OF_SYMBOLS]; // Opening price of the bar
PriceData high[NUMBER_OF_SYMBOLS]; // High price of the bar
PriceData low[NUMBER_OF_SYMBOLS]; // Low price of the bar
PriceData close[NUMBER_OF_SYMBOLS]; // Closing price of the bar
PriceData indicator[NUMBER_OF_SYMBOLS]; // Array of indicator values
//--- Arrays for getting the opening time of the current bar
struct Datetime
{
datetime time[];
};
Datetime lastbar_time[NUMBER_OF_SYMBOLS];
//--- Array for checking the new bar for each symbol
datetime new_bar[NUMBER_OF_SYMBOLS];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- Initializing external parameter arrays (with input parameters)
InitializeInputParameters();
//--- Initializing arrays of indicator handles (with value INVALID_HANDLE)
InitializeArrayHandles();
//--- Get agent handles (loading spies as custom indicators which do not indicate anything
// but create chart events reporting foreign ticks)
GetSpyHandles();
//--- Get indicator handles (iMA foreach symbol providing data for MX cross strategy)
GetIndicatorHandles();
//--- Initialize the new bar
InitializeArrayNewBar();
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- Print the deinitialization reason to the journal
Print(GetDeinitReasonText(reason));
//--- When deleting from the chart
if(reason==REASON_REMOVE)
{
//--- Delete the indicator handles
for(int s=NUMBER_OF_SYMBOLS-1; s>=0; s--)
{
IndicatorRelease(spy_indicator_handles[s]);
IndicatorRelease(signal_indicator_handles[s]);
}
}
}
//+------------------------------------------------------------------+
//| Chart events handler |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, // Event identifier
const long &lparam, // Long type event parameter
const double &dparam, // Double type event parameter
const string &sparam) // String type event parameter
{
//--- If this is a custom event
if(id>=CHARTEVENT_CUSTOM)
{
//--- Exit if trading is not allowed
if(CheckTradingPermission()>0)
return;
//--- If there was a tick event
if(lparam==CHARTEVENT_TICK)
{
//--- Check signals and trade on them
CheckSignalsAndTrade();
return;
}
}
}
//+------------------------------------------------------------------+
//| Checking signals and trading based on the new bar event |
//+------------------------------------------------------------------+
void CheckSignalsAndTrade()
{
//--- Iterate over all specified symbols
for(int s=0; s<NUMBER_OF_SYMBOLS; s++)
{
//--- If trading for this symbol is allowed
if(Symbols[s]!="")
{
//--- If the bar is not new, proceed to the next symbol
if(!CheckNewBar(s))
continue;
//--- If there is a new bar
else
{
//--- Get indicator data. If there is no data, proceed to the next symbol
if(!GetIndicatorsData(s))
continue;
//--- Get bar data (for the last 3 bars, n=3):
GetBarsData(s); // Signals.mqh: Copy..(Symbols[s],p,0,n,..[]); ...
//--- Check the conditions and trade
// Functions.mqh:
// TradingBlock(s):
// Signals.mqh:
// GetTradingSignal(s):
// GetSignal(s):
// if(indicator[s].v[1]<indicator[s].v[2]):
// return(SELL);
// if(indicator[s].v[1]>indicator[s].v[2]):
// return(BUY);
// if(!pos) return(GetSignal(s))
// else
// if(pos.type==? && GetSignal(s)==?)
// return(?);
// // CalculateLot() DOES NOT CALCULATE ANYTHING,
// // IT IS JUST NORMALIZING TO LotStep()!
// // THE TRADE SIZE IS FIXED BY INPUT PARAMETER BY SYMBOL
// // THE TRADE SIZE IS NOT CALCULATED, AT ALL!
// Calculate{StopLoss,TakeProfit,Lot}(s,Lot[s]);
// OpenPosition();
//
TradingBlock(s);
//--- Trailing Stop
// Functions.mqh:
// CalculateTrailingStop(s,pos.type);
// if(pos.sl>0)
// if(calculated stop is better):
// trade.PositionModify()
// if(pos.sl==0):
// trade.PositionModify()
ModifyTrailingStop(s);
}
}
}
}
//+------------------------------------------------------------------+