//+------------------------------------------------------------------+
//|                                            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 ----
int handleIchimoku;
int handleStochastic;
double ichimoku[];  // Array for Ichimoku
double stochMain[], stochSignal[];  // Arrays for Stochastic

CTrade trade;
int barsTotal;

// ---- EXPERT INIT ----

int OnInit()
{
   handleIchimoku = iIchimoku(_Symbol, TimeFrame, TenkanSenPeriods, KijunSenPeriods, SenkouSpanBPeriods, IchimokuPrice);
   handleStochastic = iStochastic(_Symbol, TimeFrame, StochasticKPeriods, StochasticDPeriods, StochasticSlowing, MODE_SMA, 0, PRICE_CLOSE);
   
   return(INIT_SUCCEEDED);
}

// ---- EXPERT DEINIT ----

void OnDeinit(const int reason)
{
   IndicatorRelease(handleIchimoku);
   IndicatorRelease(handleStochastic);
}

// ---- 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(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);
         
         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);
}


erros:
'iIchimoku' - wrong parameters count	Algo_Trading_V1.mq5	47	21
'iStochastic' - wrong parameters count	Algo_Trading_V1.mq5	48	23