FUGT/Experts/FUGT/FUGT.mq5
super.admin 1469a8759b convert
2025-05-30 14:56:33 +02:00

235 lines
23 KiB
MQL5

//+------------------------------------------------------------------+
//| FUGT.mq5 |
//| Copyright 2020, Thomas Schwabhaeuser |
//| schwabts@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Thomas Schwabhaeuser"
#property link "https://www.mql5.com/en/articles/648"
#property description "Multi-Currency Expert Advisor - Simple, Neat and Quick Approach"
#property version "1.0"
//--- Number of traded symbols
#define NUMBER_OF_SYMBOLS 1
//--- 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/FUGT/grid_properties.mqh"
#include "../../Include/FUGT/Enums.mqh"
#include "../../Include/FUGT/InitializeArrays.mqh"
#include "../../Include/FUGT/Errors.mqh"
#include "../../Include/FUGT/TradeSignals.mqh"
#include "../../Include/FUGT/TradeFunctions.mqh"
#include "../../Include/FUGT/ToString.mqh"
#include "../../Include/FUGT/Auxiliary.mqh"
//--- input parameters
sinput long MagicNumber = 777; // Magic number
sinput int Deviation = 10; // Slippage
input double InpRiskPercent = 2.0; // Accepted risk per symbol
sinput string _1 = ""; // settings for symbol 1
sinput string InpSymbol_01 = "Ger30";
input int InpGridStopLoss_01 = 1; // | Grid levels after which to SL ...
input int InpGridTakeProfit_01 = 4; // | ... and TP (>2SL+1, "risk-free"?)
input double InpRetracementRequiredTP_01 = 50.0; // | Retracement required for TP
input double InpRetracmentForBrkOut_01 = 121.4; // | Retracemt. before Break Out sig., TODO: Get level from indicator
input double InpSpreadFilter_01 = 2.0; // | Minimum ratio "Spacing/Spread"
input int InpPeriod_01 = 71; // | Donchian channel for symbol 1
input double InpChannelPercentSell_01 = 78.6; // | Start sell grid percentage in DC
input double InpChannelPercentBuy_01 = 21.4; // | Start buy grid percentage in DC
input double InpStochMinSell_01 = 80.0; // | Min. value of Stoch for sell grid
input double InpStochMaxBuy_01 = 20.0; // | Max. value of Stoch for sell grid
input int InpStochK_01 = 5; // | Stochastic K for symbol 1
input int InpStochD_01 = 3; // | Stochastic D for symbol 1
input int InpStochSlowing_01 = 3; // |
//---
//sinput string delimeter_01=""; // --------------------------------
//sinput string Symbol_02 = "EURUSD"; // 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
////---
//sinput string delimeter_02=""; // --------------------------------
//sinput string Symbol_03 = "NZDUSD"; // Symbol 1
//input int IndicatorPeriod_03 = 5; // | Indicator period
//input double TakeProfit_03 = 100; // | Take Profit
//input double StopLoss_03 = 50; // | Stop Loss
//input double TrailingStop_03 = 10; // | Trailing Stop
//input bool Reverse_03 = true; // | Position reversal
//input double Lot_03 = 0.1; // | Lot
//input double VolumeIncrease_03 = 0.1; // | Position volume increase
//input double VolumeIncreaseStep_03 = 10; // | Volume increase step
//--- Arrays for storing external parameters
string Symbols[NUMBER_OF_SYMBOLS]; // Symbol
double SpacingOverSpread[NUMBER_OF_SYMBOLS];
//---
int GridTakeProfit[NUMBER_OF_SYMBOLS]; // Take Profit in Spacing Units
int GridStopLoss[NUMBER_OF_SYMBOLS]; // Stop Loss in Spacing Units
double TakeProfit[NUMBER_OF_SYMBOLS]; // Take Profit price in points
double StopLoss[NUMBER_OF_SYMBOLS]; // Stop Loss price in points
//---
double RetracementRequiredTP[NUMBER_OF_SYMBOLS];
double SpreadFilter[NUMBER_OF_SYMBOLS];
double RetracmentForBreakOut[NUMBER_OF_SYMBOLS];
//---
int PeriodDC[NUMBER_OF_SYMBOLS]; //
double ChannelPercentBuy[NUMBER_OF_SYMBOLS]; //
double ChannelPercentSell[NUMBER_OF_SYMBOLS]; //
double StochMaxBuy[NUMBER_OF_SYMBOLS]; //
double StochMinSell[NUMBER_OF_SYMBOLS]; //
double StochK[NUMBER_OF_SYMBOLS]; // Stochastic K
double StochD[NUMBER_OF_SYMBOLS]; // Stochastic D
double StochSlowing[NUMBER_OF_SYMBOLS]; // Stochastic Slowing
//--- Arrays formerly needed for storing parameters of MultiSymbolExpert:
int IndicatorPeriod[NUMBER_OF_SYMBOLS]; // Indicator period
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
int last_boundary = 0;
PriceData indicator_HH[NUMBER_OF_SYMBOLS]; // Array of indicator HH values
PriceData indicator_UB[NUMBER_OF_SYMBOLS]; // Array of indicator UB values
PriceData indicator_LB[NUMBER_OF_SYMBOLS]; // Array of indicator LB values
PriceData indicator_LL[NUMBER_OF_SYMBOLS]; // Array of indicator LL values
PriceData indicator_BD[NUMBER_OF_SYMBOLS]; // Array of indicator LL values
//---
grid_properties grid[NUMBER_OF_SYMBOLS]; // requires a default constructor
//--- 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();
//--- ok
Print("leaving OnInit() ... ok");
}
//+------------------------------------------------------------------+
//| 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)
{
//--- DONE: Pass symbol name in sparam to processing function
//--- DONE: Add parameter s: string to CheckSignalsAndTrade() for preventing
// to call all processing functions for symbols without a new tick
//--- Check signals and trade on them
CheckSignalsAndTrade(sparam);
return;
}
}
}
//+------------------------------------------------------------------+
//| Checking signals and trading based on the new bar event |
//+------------------------------------------------------------------+
void CheckSignalsAndTrade(string symbol)
{
//--- Iterate over all specified symbols
for(int s=0; s<NUMBER_OF_SYMBOLS; s++)
{
//--- If tisk was not reported for this symbol
if(Symbols[s]!=symbol)
continue;
//--- If trading for this symbol is allowed
if(Symbols[s]!="")
{
//--- If the bar is not new, proceed to the next symbol
//if(!CheckNewBar(s))
// {
// PrintFormat(__FUNCTION__+": CheckNewBar(%d)==false",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
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);
//}
}
}
}
//+------------------------------------------------------------------+