#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input ENUM_APPLIED_PRICE IchimokuPrice = PRICE_CLOSE; // Ichimoku Applied Price
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
int handleIchimoku;
int handleStochastic;
double ichimoku[]; // Array for Ichimoku
double stochMain[], stochSignal[]; // Arrays for Stochastic
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Copy Ichimoku data
CopyBuffer(handleIchimoku, 0, 0, 1, ichimoku); // Tenkan Sen
CopyBuffer(handleIchimoku, 1, 0, 1, ichimoku); // Kijun Sen
CopyBuffer(handleIchimoku, 2, 0, 1, ichimoku); // Senkou Span A
CopyBuffer(handleIchimoku, 3, 0, 1, ichimoku); // Senkou Span B
CopyBuffer(handleIchimoku, 4, 0, 1, ichimoku); // Chikou Span
// Copy Stochastic data
CopyBuffer(handleStochastic, 0, 0, 1, stochMain);
CopyBuffer(handleStochastic, 1, 0, 1, stochSignal);
// Trading logic - SELL Signal
if (stochMain[0] > StochasticOverbought && stochSignal[0] > StochasticOverbought)
{
if (ichimoku[0] < ichimoku[1] && bid < ichimoku[3]) // Kijun Sen < Tenkan Sen && Price below Senkou Span B
{
Print(__FUNCTION__, " > SELL Signal...");
trade.Sell(Lots);
}
}
// Trading logic - BUY Signal
else if (stochMain[0] < StochasticOversold && stochSignal[0] < StochasticOversold)
{
if (ichimoku[0] > ichimoku[1] && bid > ichimoku[2]) // Tenkan Sen > Kijun Sen && Price above Senkou Span A
{
Print(__FUNCTION__, " > BUY Signal...");
trade.Buy(Lots);
}
}
}
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
code
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
It looks like you're facing an issue with the parameters being passed to the iIchimoku and iStochastic functions in your MQL5 code. The iIchimoku and iStochastic functions do not directly accept the parameters you're using. Instead, they are called to create indicator handles.
In MQL5, you would typically use iCustom to create custom indicator handles for more complex scenarios or to call standard indicators properly. For both Ichimoku and Stochastic, you have to use their respective functions correctly.
Here's an updated version of your code to fix the errors by using the correct methods to handle Ichimoku and Stochastic indicators:
mql5
//+------------------------------------------------------------------+
//| AI Ichimoku Stochastic Scalper.mq5 |
//| Alfr5d |
//| https://www.49design.co.za |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input ENUM_APPLIED_PRICE IchimokuPrice = PRICE_CLOSE; // Ichimoku Applied Price
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
double ichimoku[]; // Array for Ichimoku
double stochMain[], stochSignal[]; // Arrays for Stochastic
CTrade trade;
int barsTotal;
// ---- EXPERT INIT ----
int OnInit()
{
// No need to initialize indicator handles as we're using CopyBuffer later
return(INIT_SUCCEEDED);
}
// ---- EXPERT DEINIT ----
void OnDeinit(const int reason)
{
// Clean up code if necessary here
}
// ---- ON TICK FUNCTION ----
void OnTick()
{
int bars = iBars(_Symbol, TimeFrame);
if (barsTotal != bars)
{
barsTotal = bars;
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Copy Ichimoku data
CopyBuffer(_Symbol, iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods), 0, 0, 1, ichimoku); // Tenkan Sen
CopyBuffer(_Symbol, iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods), 1, 0, 1, ichimoku); // Kijun Sen
CopyBuffer(_Symbol, iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods), 2, 0, 1, ichimoku); // Senkou Span A
CopyBuffer(_Symbol, iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods), 3, 0, 1, ichimoku); // Senkou Span B
CopyBuffer(_Symbol, iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods), 4, 0, 1, ichimoku); // Chikou Span
// Copy Stochastic data
CopyBuffer(_Symbol, iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, MODE_SMA, 0, PRICE_CLOSE), 0, 0, 1, stochMain);
CopyBuffer(_Symbol, iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, MODE_SMA, 0, PRICE_CLOSE), 1, 0, 1, stochSignal);
// Trading logic - SELL Signal
if (stochMain[0] > StochasticOverbought && stochSignal[0] > StochasticOverbought)
{
if (ichimoku[0] < ichimoku[1] && bid < ichimoku[3]) // Kijun Sen < Tenkan Sen && Price below Senkou Span B
{
Print(__FUNCTION__, " > SELL Signal...");
trade.Sell(Lots);
}
}
// Trading logic - BUY Signal
else if (stochMain[0] < StochasticOversold && stochSignal[0] < StochasticOversold)
{
if (ichimoku[0] > ichimoku[1] && bid > ichimoku[2]) // Tenkan Sen > Kijun Sen && Price above Senkou Span A
{
Print(__FUNCTION__, " > BUY Signal...");
trade.Buy(Lots);
}
}
}
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if (posType == POSITION_TYPE_BUY)
{
pointsBuy += (int)((bid - posOpenPrice) / _Point);
}
else if (posType == POSITION_TYPE_SELL)
{
pointsSell += (int)((posOpenPrice - ask) / _Point);
}
}
}
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
}
// Display relevant information
Comment("\nStochastic Main: ", DoubleToString(stochMain[0], 5),
"\nStochastic Signal: ", DoubleToString(stochSignal[0], 5),
"\nIchimoku Tenkan: ", DoubleToString(ichimoku[0], 5),
"\nIchimoku Kijun: ", DoubleToString(ichimoku[1], 5),
"\nPoints Buy: ", pointsBuy,
"\nPoints Sell: ", pointsSell);
}
In this code, I've modified the parts that initialize the handles of the indicators. Note that iIchimoku and iStochastic are used in conjunction with CopyBuffer, but they don't need to be assigned to a handle variable beforehand, as previously attempted.
Make sure the strategy reflects what you're aiming to achieve, especially in relation to your trading logic and indicator parameters. Always test any changes in a safe environment (like a demo account) before using them in live trading.
#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input ENUM_APPLIED_PRICE IchimokuPrice = PRICE_CLOSE; // Ichimoku Applied Price
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
double ichimoku[]; // Array for Ichimoku
double stochMain[], stochSignal[]; // Arrays for Stochastic
CTrade trade;
int barsTotal;
// ---- EXPERT INIT ----
int OnInit()
{
// No need to initialize indicator handles as we're using CopyBuffer later
return(INIT_SUCCEEDED);
}
// ---- EXPERT DEINIT ----
void OnDeinit(const int reason)
{
// Clean up code if necessary here
}
// ---- ON TICK FUNCTION ----
void OnTick()
{
int bars = iBars(_Symbol, TimeFrame);
if (barsTotal != bars)
{
barsTotal = bars;
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
code
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
'Algo_Trading_V1.mq5' 1
Trade.mqh
Object.mqh
StdLibErr.mqh
OrderInfo.mqh
HistoryOrderInfo.mqh
PositionInfo.mqh
DealInfo.mqh
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 68 7
could be one of 3 function(s) Algo_Trading_V1.mq5 68 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 68 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 68 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 68 7
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 69 7
could be one of 3 function(s) Algo_Trading_V1.mq5 69 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 69 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 69 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 69 7
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 70 7
could be one of 3 function(s) Algo_Trading_V1.mq5 70 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 70 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 70 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 70 7
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 71 7
could be one of 3 function(s) Algo_Trading_V1.mq5 71 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 71 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 71 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 71 7
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 72 7
could be one of 3 function(s) Algo_Trading_V1.mq5 72 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 72 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 72 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 72 7
'iStochastic' - wrong parameters count Algo_Trading_V1.mq5 75 27
built-in: int iStochastic(const string,ENUM_TIMEFRAMES,int,int,int,ENUM_MA_METHOD,ENUM_STO_PRICE) Algo_Trading_V1.mq5 75 27
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 75 7
could be one of 3 function(s) Algo_Trading_V1.mq5 75 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 75 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 75 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 75 7
'iStochastic' - wrong parameters count Algo_Trading_V1.mq5 76 27
built-in: int iStochastic(const string,ENUM_TIMEFRAMES,int,int,int,ENUM_MA_METHOD,ENUM_STO_PRICE) Algo_Trading_V1.mq5 76 27
'CopyBuffer' - no one of the overloads can be applied to the function call Algo_Trading_V1.mq5 76 7
could be one of 3 function(s) Algo_Trading_V1.mq5 76 7
built-in: int CopyBuffer(int,int,int,int,double&[]) Algo_Trading_V1.mq5 76 7
built-in: int CopyBuffer(int,int,datetime,int,double&[]) Algo_Trading_V1.mq5 76 7
built-in: int CopyBuffer(int,int,datetime,datetime,double&[]) Algo_Trading_V1.mq5 76 7
The issues you're experiencing in your MQL5 code stem from the incorrect usage of the CopyBuffer function with indicator handles and the incorrect parameters for the iStochastic function.
Issues Addressed
CopyBuffer Issues: The CopyBuffer function requires the first parameter to be a handle of an indicator, which means you need to create an indicator handle using the appropriate function (iIchimoku and iStochastic). You cannot directly pass the result of these functions as parameters for CopyBuffer.
iStochastic Parameters: The usage of iStochastic is incorrect as it does not require the additional MODE_SMA and 0 parameters. It needs only the instrument symbol, timeframe, K and D periods, slowing, and price type.
Revised Code
Here is a corrected version of your Expert Advisor that incorporates these fixes:
mql5
//+------------------------------------------------------------------+
//| AI Ichimoku Stochastic Scalper.mq5 |
//| Alfr5d |
//| https://www.49design.co.za |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input ENUM_APPLIED_PRICE IchimokuPrice = PRICE_CLOSE; // Ichimoku Applied Price
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input ENUM_APPLIED_PRICE StochasticPrice = PRICE_CLOSE; // Stochastic Applied Price
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
double ichimoku[5]; // Array for Ichimoku
double stochMain[], stochSignal[]; // Arrays for Stochastic
CTrade trade;
int barsTotal;
// ---- EXPERT INIT ----
int OnInit()
{
return(INIT_SUCCEEDED);
}
// ---- EXPERT DEINIT ----
void OnDeinit(const int reason)
{
// Cleanup code can go here if needed
}
// ---- ON TICK FUNCTION ----
void OnTick()
{
int bars = iBars(_Symbol, TimeFrame);
if (barsTotal != bars)
{
barsTotal = bars;
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Get Ichimoku data
double tenkan_sen = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, IchimokuPrice, 0);
double kijun_sen = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, IchimokuPrice, 1);
double senkou_span_a = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, IchimokuPrice, 2);
double senkou_span_b = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, IchimokuPrice, 3);
double chikou_span = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, IchimokuPrice, 4);
ichimoku[0] = tenkan_sen;
ichimoku[1] = kijun_sen;
ichimoku[2] = senkou_span_a;
ichimoku[3] = senkou_span_b;
ichimoku[4] = chikou_span;
// Get Stochastic data
stochMain[0] = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, StochasticPrice, 0);
stochSignal[0] = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, StochasticPrice, 1);
// Trading logic - SELL Signal
if (stochMain[0] > StochasticOverbought && stochSignal[0] > StochasticOverbought)
{
if (ichimoku[0] < ichimoku[1] && bid < ichimoku[3]) // Kijun Sen < Tenkan Sen && Price below Senkou Span B
{
Print(__FUNCTION__, " > SELL Signal...");
trade.Sell(Lots);
}
}
// Trading logic - BUY Signal
else if (stochMain[0] < StochasticOversold && stochSignal[0] < StochasticOversold)
{
if (ichimoku[0] > ichimoku[1] && bid > ichimoku[2]) // Tenkan Sen > Kijun Sen && Price above Senkou Span A
{
Print(__FUNCTION__, " > BUY Signal...");
trade.Buy(Lots);
}
}
}
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if (posType == POSITION_TYPE_BUY)
{
pointsBuy += (int)((bid - posOpenPrice) / _Point);
}
else if (posType == POSITION_TYPE_SELL)
{
pointsSell += (int)((posOpenPrice - ask) / _Point);
}
}
}
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
}
// Display relevant information
Comment("\nStochastic Main: ", DoubleToString(stochMain[0], 5),
"\nStochastic Signal: ", DoubleToString(stochSignal[0], 5),
"\nIchimoku Tenkan: ", DoubleToString(ichimoku[0], 5),
"\nIchimoku Kijun: ", DoubleToString(ichimoku[1], 5),
"\nPoints Buy: ", pointsBuy,
"\nPoints Sell: ", pointsSell);
}
Key Changes
iIchimoku and iStochastic Calls: The code now directly fetches values from the indicatiors without the need for CopyBuffer. The last parameter in these function calls indicates which value to retrieve:
0 retrieves the K value for Stochastic.
1 retrieves the D value for Stochastic.
For Ichimoku, you can specify 0 for Tenkan, 1 for Kijun, etc.
Removed CopyBuffer Calls: Since you call the indicator values directly, there is no need for the CopyBuffer function, resolving the error you encountered.
Testing Strategy
Before deploying on a live account, ensure to test this on a demo account for proper functioning and to verify that your strategy works as intended. Adjust the trading logic or indicator parameters based on your trading style and market conditions.
It seems there are some misunderstandings regarding the parameter requirements for the iIchimoku and iStochastic functions, as well as the correct usage of ENUM types. Let's address these issues.
Correct Parameters for iIchimoku and iStochastic
iIchimoku: The iIchimoku function in MQL5 requires only 4 parameters: the symbol, the timeframe, and the three period parameters for Tenkan, Kijun, and Senkou Span B. It does not accept an ENUM_APPLIED_PRICE parameter.
iStochastic: The iStochastic function requires parameters for periods, slowing, the price type, and the MA method. Instead of trying to pass ENUM_APPLIED_PRICE, you should directly use the appropriate values from ENUM_STO_PRICE.
Revised Code
Here's an updated version of your code with corrections applied:
mql5
//+------------------------------------------------------------------+
//| AI Ichimoku Stochastic Scalper.mq5 |
//| Alfr5d |
//| https://www.49design.co.za |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input ENUM_APPLIED_PRICE StochasticPrice = PRICE_CLOSE; // Stochastic Applied Price
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
double ichimoku[5]; // Array for Ichimoku
double stochMain[], stochSignal[]; // Arrays for Stochastic
CTrade trade;
int barsTotal;
// ---- EXPERT INIT ----
int OnInit()
{
return(INIT_SUCCEEDED);
}
// ---- EXPERT DEINIT ----
void OnDeinit(const int reason)
{
// Cleanup code can go here if needed
}
// ---- ON TICK FUNCTION ----
void OnTick()
{
int bars = iBars(_Symbol, TimeFrame);
if (barsTotal != bars)
{
barsTotal = bars;
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Get Ichimoku data
double tenkan_sen = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 0);
double kijun_sen = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 1);
double senkou_span_a = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 2);
double senkou_span_b = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 3);
double chikou_span = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 4);
ichimoku[0] = tenkan_sen;
ichimoku[1] = kijun_sen;
ichimoku[2] = senkou_span_a;
ichimoku[3] = senkou_span_b;
ichimoku[4] = chikou_span;
// Get Stochastic data
stochMain[0] = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, StochasticPrice, 0);
stochSignal[0] = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, StochasticPrice, 1);
// Trading logic - SELL Signal
if (stochMain[0] > StochasticOverbought && stochSignal[0] > StochasticOverbought)
{
if (ichimoku[0] < ichimoku[1] && bid < ichimoku[3]) // Kijun Sen < Tenkan Sen && Price below Senkou Span B
{
Print(__FUNCTION__, " > SELL Signal...");
trade.Sell(Lots);
}
}
// Trading logic - BUY Signal
else if (stochMain[0] < StochasticOversold && stochSignal[0] < StochasticOversold)
{
if (ichimoku[0] > ichimoku[1] && bid > ichimoku[2]) // Tenkan Sen > Kijun Sen && Price above Senkou Span A
{
Print(__FUNCTION__, " > BUY Signal...");
trade.Buy(Lots);
}
}
}
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if (posType == POSITION_TYPE_BUY)
{
pointsBuy += (int)((bid - posOpenPrice) / _Point);
}
else if (posType == POSITION_TYPE_SELL)
{
pointsSell += (int)((posOpenPrice - ask) / _Point);
}
}
}
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
}
// Display relevant information
Comment("\nStochastic Main: ", DoubleToString(stochMain[0], 5),
"\nStochastic Signal: ", DoubleToString(stochSignal[0], 5),
"\nIchimoku Tenkan: ", DoubleToString(ichimoku[0], 5),
"\nIchimoku Kijun: ", DoubleToString(ichimoku[1], 5),
"\nPoints Buy: ", pointsBuy,
"\nPoints Sell: ", pointsSell);
}
Key Changes
Function Parameters: Removed the ENUM_APPLIED_PRICE parameter from iIchimoku calls and replaced them with the correct parameters.
Stochastic Price Parameter: Ensure that the StochasticPrice is using an appropriate value from the ENUM_STO_PRICE enumeration.
Arrays Declaration: Ensure that stochMain and stochSignal are declared with the right sizes before using them.
Testing
Make sure you perform comprehensive testing using a demo account before going live to avoid potential losses. Adjust parameters as necessary based on your market analysis and trading strategy. If you continue to encounter issues, provide the exact line of code or the specific functions you're using, and I can offer more guidance.
#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input ENUM_APPLIED_PRICE StochasticPrice = PRICE_CLOSE; // Stochastic Applied Price
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
double ichimoku[5]; // Array for Ichimoku
double stochMain[], stochSignal[]; // Arrays for Stochastic
CTrade trade;
int barsTotal;
// ---- EXPERT INIT ----
int OnInit()
{
return(INIT_SUCCEEDED);
}
// ---- EXPERT DEINIT ----
void OnDeinit(const int reason)
{
// Cleanup code can go here if needed
}
// ---- ON TICK FUNCTION ----
void OnTick()
{
int bars = iBars(_Symbol, TimeFrame);
if (barsTotal != bars)
{
barsTotal = bars;
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
code
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
The errors you are encountering relate to the incorrect usage of the iIchimoku and iStochastic functions in MQL5. Let's address each of the problems.
Problems Identified
iIchimoku: The error indicates that you are passing the wrong number of parameters.
The correct signature for iIchimoku should include the symbol, timeframe, and the three period parameters but does not include an ENUM_APPLIED_PRICE parameter.
iStochastic: You are trying to pass an ENUM_APPLIED_PRICE as the price type which is incorrect.
You must use the correct parameters for iStochastic, which includes the K periods, D periods, slowing, MA method, and the price type.
Correct Usage
Here's the corrected code that should fix the errors:
mql5
//+------------------------------------------------------------------+
//| AI Ichimoku Stochastic Scalper.mq5 |
//| Alfr5d |
//| https://www.49design.co.za |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Alfred Mandala. "
#property description "Ichimoku and Stochastic Scalping EA"
#property version "1.5"
#property description "Ichimoku & Stochastic Scalping EA, TIMEFRAME = M5"
#include <Trade/Trade.mqh>
// ---- INPUTS ----
input group "--- Trading Inputs ---";
input double Lots = 0.01; // Lot Size
input int TpPoints = 50; // Take Profit in points [m5-50p, h1-200p]
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe
input group "--- Ichimoku Inputs ---";
input int TenkanSenPeriods = 9; // Tenkan Sen Period
input int KijunSenPeriods = 26; // Kijun Sen Period
input int SenkouSpanBPeriods = 52; // Senkou Span B Period
input group "--- Stochastic Inputs ---";
input int StochasticKPeriods = 14; // Stochastic K Period
input int StochasticDPeriods = 3; // Stochastic D Period
input int StochasticSlowing = 3; // Stochastic Slowing
input ENUM_APPLIED_PRICE StochasticPrice = PRICE_CLOSE; // Stochastic Applied Price
input double StochasticOverbought = 80; // Overbought level
input double StochasticOversold = 20; // Oversold level
// ---- GLOBAL VARIABLES ----
double ichimoku[5]; // Array for Ichimoku
double stochMain[1]; // Array for Stochastic main line
double stochSignal[1]; // Array for Stochastic signal line
CTrade trade;
int barsTotal;
// ---- EXPERT INIT ----
int OnInit()
{
return(INIT_SUCCEEDED);
}
// ---- EXPERT DEINIT ----
void OnDeinit(const int reason)
{
// Cleanup code can go here if needed
}
// ---- ON TICK FUNCTION ----
void OnTick()
{
int bars = iBars(_Symbol, TimeFrame);
if (barsTotal != bars)
{
barsTotal = bars;
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Get Ichimoku data
double tenkan_sen = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 0); // Tenkan Sen
double kijun_sen = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 1); // Kijun Sen
double senkou_span_a = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 2); // Senkou Span A
double senkou_span_b = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 3); // Senkou Span B
double chikou_span = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, 4); // Chikou Span
ichimoku[0] = tenkan_sen;
ichimoku[1] = kijun_sen;
ichimoku[2] = senkou_span_a;
ichimoku[3] = senkou_span_b;
ichimoku[4] = chikou_span;
// Get Stochastic data
stochMain[0] = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, 0, StochasticPrice, 0); // K value
stochSignal[0] = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, 1, StochasticPrice, 0); // D value
// Trading logic - SELL Signal
if (stochMain[0] > StochasticOverbought && stochSignal[0] > StochasticOverbought)
{
if (ichimoku[0] < ichimoku[1] && bid < ichimoku[3]) // Kijun Sen < Tenkan Sen && Price below Senkou Span B
{
Print(__FUNCTION__, " > SELL Signal...");
trade.Sell(Lots);
}
}
// Trading logic - BUY Signal
else if (stochMain[0] < StochasticOversold && stochSignal[0] < StochasticOversold)
{
if (ichimoku[0] > ichimoku[1] && bid > ichimoku[2]) // Tenkan Sen > Kijun Sen && Price above Senkou Span A
{
Print(__FUNCTION__, " > BUY Signal...");
trade.Buy(Lots);
}
}
}
// Loop through open positions and close based on TP points
int pointsBuy = 0;
int pointsSell = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if (posType == POSITION_TYPE_BUY)
{
pointsBuy += (int)((bid - posOpenPrice) / _Point);
}
else if (posType == POSITION_TYPE_SELL)
{
pointsSell += (int)((posOpenPrice - ask) / _Point);
}
}
}
// Close positions based on TP points
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket))
{
ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
if (pointsBuy > TpPoints)
{
// Close buy positions
if (posType == POSITION_TYPE_BUY)
{
trade.PositionClose(posTicket);
}
}
if (pointsSell > TpPoints)
{
// Close sell positions
if (posType == POSITION_TYPE_SELL)
{
trade.PositionClose(posTicket);
}
}
}
}
// Display relevant information
Comment("\nStochastic Main: ", DoubleToString(stochMain[0], 5),
"\nStochastic Signal: ", DoubleToString(stochSignal[0], 5),
"\nIchimoku Tenkan: ", DoubleToString(ichimoku[0], 5),
"\nIchimoku Kijun: ", DoubleToString(ichimoku[1], 5),
"\nPoints Buy: ", pointsBuy,
"\nPoints Sell: ", pointsSell);
}
Summary of Changes
iIchimoku Calls: The iIchimoku function is now called correctly with only the required parameters of symbol, timeframe, and the three period settings (correctly indexed).
iStochastic Calls: The values for the stochastic main and signal lines are directly fetched using the iStochastic function, with the last parameter being the signal index.
Proper Handling of ENUM Types: The parameters related to applied price types in stochastic are set correctly.
Adjusting Array Sizes: The stochMain and stochSignal arrays are now initialized with the right sizes to hold one value each.
Testing
Make sure to thoroughly test the code in a demo environment to ensure it behaves as expected before deploying it on a live account. This will help catch any remaining issues or logical errors that may not have been addressed. If you encounter more issues, please share the specific lines or functions causing concern, and I can provide further assistance.