Think I might have the simplest, yet one of the most profitable scalping strategies, but experiencing problems with the code...

 

Hello everyone,

I'm gonna be completely honest with you. I am new to all that EA coding and turning trading strategies into fully working robots...! :)))

BUT, I have been trading the markets for a few years now, and I think I have found a strategy, that can be very profitable and yet is one of the simplest there is!

Here comes the problem - as I mentioned I am very new to coding. That is why, somebody recommended me going on Fiverr and paying someone to do it for me.

So, that's what I did - I paid ''ahadmurtaza8'' $50 to code the strategy I am talking about.

Long story short, I received the EA but it seems that it doesn't behave as I expected. It doesn't place any trades, I think I managed to make it place a trade or two but when it did, it placed the sl at completely different points than those I had required from the guy to code.. and it kind of looks a bit overcomplicated and with a bunch of unneeded stuff, in my opinion(sorry if I am wrong). Also, when I ran the strategy tester on it, it wasn't profitable at all, even though I have been very profitable with this strategy for months...

However, I was told that here are quite a few experienced traders and developers. So, I will be very happy if some of you can suggest some changes to the code so that it gets to do what it is really supposed to. :)

Here is the strategy:


Strategy will be used on M15 chart.

 

What is needed: 

- 34 EMA(high) 

- 34 EMA(low) 

- 200 SMA(open) 


1. When a bearish candle opens above and closes below the 34 EMA(low) and the whole candle is below the 200 SMA

 ——> SELL (right after close of the candle that I am talking about above)

SL: 10 pips above the 34 EMA(high) 

TP: 1.8R 

And SL should move to break-even right after market has moved 1/10 of the distance from SL to Order, in my direction.


2. When a bullish candle opens below and closes above the 34 EMA(high) and the whole candle is above the 200 SMA 

——> BUY (right after close of the candle that I am talking about above)

SL: 10 pips below the 34 EMA(low) 

TP: 1.8R

SL moved to break even as described above.


And here is the source code I was provided with:


#property version   "1.00"

#property strict



input string S1 = "<===== MA Trend =====>";//==========

input int MA_Period = 200;

input ENUM_MA_METHOD MA_Method = MODE_SMA;

input ENUM_APPLIED_PRICE MA_Price = PRICE_CLOSE;

input string S2 = "<===== MA Signal =====>";//==========

input int MA_High_Period = 34;

input ENUM_MA_METHOD MA_High_Method = MODE_EMA;

input int MA_Low_Period = 34;

input ENUM_MA_METHOD MA_Low_Method = MODE_EMA;

input bool CloseByOpposite = false;

input string S4 = "<===== EA =====>";//==========

input int Magic = 123;

input string Comments = "";

input double LotSize = 0.01;

input bool UseMM = false;

input double RiskPercent = 2.0;

input double MaxLot = 20.0;

input int MaxSellTrades = 1;

input int MaxBuyTrades = 1;

input double TP = 1.8;// Take Profit Ratio

input int SL = 100;// Extra Stop Loss Points

input bool UseBE = false;

input int BreakevenPips = 150;// Move SL To Breakeven After Points

input bool UseTrailing = false;

input int TrailingStart = 100;// Trailing Start Points

input int TrailingStop = 100;// Trailing Stop Points

input int TrailingStep = 10;// Trailing Step Points

input bool SoundAlert = true;

input bool PushAlert = true;



#include <Trade\AccountInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\OrderInfo.mqh>

#include <Trade\SymbolInfo.mqh>



CAccountInfo A;

CTrade T;

CPositionInfo P;

CSymbolInfo S;

COrderInfo O;



datetime lastTime = 0;

datetime LastTime = 0;

datetime LastCross = 0;

int Cross = 0;

int maHandle;

int highHandle;

int lowHandle;

double highMA = 0;

double lowMA = 0;

//+------------------------------------------------------------------+

int OnInit()

  {

   T.SetExpertMagicNumber(Magic);

   maHandle = iMA(Symbol(), 0, MA_Period, 0, MA_Method, MA_Price);

   highHandle = iMA(Symbol(), 0, MA_High_Period, 0, MA_High_Method, PRICE_HIGH);

   lowHandle = iMA(Symbol(), 0, MA_Low_Period, 0, MA_Low_Method, PRICE_LOW);

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   Comment("");

   EventKillTimer();

  }

//+------------------------------------------------------------------+

void OnTimer()

  {

   OnTick();

  }

//+------------------------------------------------------------------+

void OnTick()

  {

   int Signal = Signal();



   if(Signal == 2 && iTime(_Symbol, 0, 0) > LastTime)

     {

      string txt = Symbol()+": MA EA Sell Signal.";

      if(SoundAlert)

         Alert(txt);

      if(PushAlert)

         SendNotification(txt);

      LastTime = iTime(_Symbol, 0, 0);

     }



   if(Signal == 1 && iTime(_Symbol, 0, 0) > LastTime)

     {

      string txt = Symbol()+": MA EA Buy Signal.";

      if(SoundAlert)

         Alert(txt);

      if(PushAlert)

         SendNotification(txt);

      LastTime = iTime(_Symbol, 0, 0);

     }



   if(CloseByOpposite)

     {

      if(Counter(POSITION_TYPE_SELL) > 0 && Signal == 1)

         CloseAll(POSITION_TYPE_SELL);

      if(Counter(POSITION_TYPE_BUY) > 0 && Signal == 2)

         CloseAll(POSITION_TYPE_BUY);

     }

// Sell Condition

   if(Counter(POSITION_TYPE_SELL) < MaxSellTrades && Signal == 2 && iTime(_Symbol, 0, 0) > lastTime)

     {

      int Ticket = T.Sell(LotsCalc(), Symbol(), S.Bid(), 0, 0, Comments);

      if(Ticket > 0)

        {

         lastTime = iTime(_Symbol, 0, 0);

        }

     }



// Buy Condition

   if(Counter(POSITION_TYPE_BUY) < MaxBuyTrades && Signal == 1 && iTime(_Symbol, 0, 0) > lastTime)

     {

      int Ticket = T.Buy(LotsCalc(), Symbol(), S.Ask(), 0, 0, Comments);

      if(Ticket > 0)

        {

         lastTime = iTime(_Symbol, 0, 0);

        }

     }



   SLTP();

   if(UseBE)

      BE();

   if(UseTrailing)

      Trailing();

  }

//+------------------------------------------------------------------+

int Counter(ENUM_POSITION_TYPE type)

  {

   int i, count = 0;

   for(i=PositionsTotal()-1; i>=0; i--)

     {

      P.SelectByIndex(i);

      if(P.Symbol()==Symbol())

        {

         if(P.PositionType() == POSITION_TYPE_BUY && type == POSITION_TYPE_BUY)

            count++;

         if(P.PositionType() == POSITION_TYPE_SELL && type == POSITION_TYPE_SELL)

            count++;

        }

     }



   return(count);

  }

//+------------------------------------------------------------------+

void CloseAll(ENUM_POSITION_TYPE type)

  {

   for(int i = PositionsTotal() - 1; i >= 0; i--)

     {

      P.SelectByIndex(i);

      if(P.Symbol()==Symbol())

        {

         if(P.PositionType()==POSITION_TYPE_BUY && type == POSITION_TYPE_BUY)

            T.PositionClose(P.Ticket());

         if(P.PositionType()==POSITION_TYPE_SELL && type == POSITION_TYPE_SELL)

            T.PositionClose(P.Ticket());

        }

     }

  }

//+------------------------------------------------------------------+

void SLTP()

  {

   for(int i = PositionsTotal() - 1; i >= 0; i--)

     {

      P.SelectByIndex(i);

      if(P.Symbol() == Symbol())

        {

         if(P.PositionType()==POSITION_TYPE_BUY)

           {

            double sl = lowMA - SL*Point();

            double tp = 0;

            if(TP > 0)

               tp = P.PriceOpen() + TP*(P.PriceOpen() - sl);



            if(P.StopLoss() > 0)

               sl = P.StopLoss();

            if(P.TakeProfit() > 0)

               tp = P.TakeProfit();

               

            sl = NormalizeDouble(sl, _Digits);

            tp = NormalizeDouble(tp, _Digits);

            if(tp != P.TakeProfit() || sl != P.StopLoss())

               T.PositionModify(P.Ticket(), sl, tp);

           }

         if(P.PositionType()==POSITION_TYPE_SELL)

           {

            double sl = highMA + SL*Point();

            double tp = 0;

            if(TP > 0)

               tp = P.PriceOpen() - TP*(sl - P.PriceOpen());



            if(P.StopLoss() > 0)

               sl = P.StopLoss();

            if(P.TakeProfit() > 0)

               tp = P.TakeProfit();

               

            sl = NormalizeDouble(sl, _Digits);

            tp = NormalizeDouble(tp, _Digits);

            if(tp != P.TakeProfit() || sl != P.StopLoss())

               T.PositionModify(P.Ticket(), sl, tp);

           }

        }

     }

  }

//+------------------------------------------------------------------+

void Trailing()

  {

   for(int i = PositionsTotal() - 1; i >= 0; i--)

     {

      P.SelectByIndex(i);

      if(P.Symbol() == Symbol())

        {

         double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);

         double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

         if(P.PositionType()==POSITION_TYPE_BUY)

           {

            if(bid > NormalizeDouble(P.PriceOpen()+TrailingStart*_Point, _Digits))

               if(bid > NormalizeDouble(P.StopLoss()+(TrailingStop+TrailingStep)*_Point, _Digits) || P.StopLoss() == 0)

                  T.PositionModify(P.Ticket(), NormalizeDouble(bid-TrailingStop*_Point, _Digits), P.TakeProfit());

           }

         if(P.PositionType()==POSITION_TYPE_SELL)

           {

            if(ask < NormalizeDouble(P.PriceOpen()-TrailingStart*_Point, _Digits))

               if(ask < NormalizeDouble(P.StopLoss()-(TrailingStop+TrailingStep)*_Point, _Digits) || P.StopLoss() == 0)

                  T.PositionModify(P.Ticket(), NormalizeDouble(ask+TrailingStop*_Point, _Digits), P.TakeProfit());

           }

        }

     }

  }

//+------------------------------------------------------------------+

void BE()

  {

   for(int i = PositionsTotal() - 1; i >= 0; i--)

     {

      P.SelectByIndex(i);

      if(P.Symbol() == Symbol())

        {

         double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);

         double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

         if(P.PositionType()==POSITION_TYPE_BUY)

           {

            if(bid >= NormalizeDouble(P.PriceOpen()+BreakevenPips*_Point, _Digits) && P.StopLoss() < P.PriceOpen())

               T.PositionModify(P.Ticket(), NormalizeDouble(P.PriceOpen(), _Digits), P.TakeProfit());

           }

         if(P.PositionType()==POSITION_TYPE_SELL)

           {

            if(ask <= NormalizeDouble(P.PriceOpen()-BreakevenPips*_Point, _Digits)  && (P.StopLoss() > P.PriceOpen() || P.StopLoss() == 0))

               T.PositionModify(P.Ticket(), NormalizeDouble(P.PriceOpen(), _Digits), P.TakeProfit());

           }

        }

     }

  }

//+------------------------------------------------------------------+

int Signal()

  {

   double MA[];

   ArraySetAsSeries(MA, true);

   CopyBuffer(maHandle, 0, 0, 5, MA);



   double HMA[];

   ArraySetAsSeries(HMA, true);

   CopyBuffer(highHandle, 0, 0, 5, HMA);



   double LMA[];

   ArraySetAsSeries(LMA, true);

   CopyBuffer(lowHandle, 0, 0, 5, LMA);



   highMA = HMA[1];

   lowMA = LMA[1];



   if(iClose(Symbol(), 0, 1) >= HMA[1] && iOpen(Symbol(), 0, 1) < HMA[1] && iClose(Symbol(), 0, 1) > MA[1])

      return(1);

   if(iClose(Symbol(), 0, 1) <= LMA[1] && iOpen(Symbol(), 0, 1) > LMA[1] && iClose(Symbol(), 0, 1) < MA[1])

      return(2);

   return(0);

  }

//+------------------------------------------------------------------+

double LotsCalc()

  {

   double iLots = LotSize;

   double MinLots = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);

   double MaxLots = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);

   if(MaxLots > MaxLot)

      MaxLots = MaxLot;



   if(UseMM)

     {

      double sl = 0;

      if(sl == 0)

         sl = 1000;

      double MaxLoss = NormalizeDouble(AccountInfoDouble(ACCOUNT_BALANCE)*(RiskPercent/100), 2);

      iLots = NormalizeDouble(MaxLoss/sl/SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE), 2);

     }

   if(iLots < MinLots)

      iLots = MinLots;

   if(iLots > MaxLots)

      iLots = MaxLots;

   return(iLots);

  }

//+------------------------------------------------------------------+

Thanks everyone so much, even if you have just read to that point.. :)))

Have a great one!

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
Keith Watford:
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
Done, sorry.
 

Hi,


It would help if you also include the files:


#include <Trade\AccountInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\OrderInfo.mqh>

#include <Trade\SymbolInfo.mqh>

Since anyone looking at this would have to be certain about what these do as well...


Best wishes,


Steve.

 
Steve Miller:

Hi,

It would help if you also include the files:

Since anyone looking at this would have to be certain about what these do as well...

Best wishes,

Steve.

they are standard MQL libraries

 
enevpavel18:

Hello everyone,


int Signal()
{
   double MA[];
   ArraySetAsSeries(MA, true);
   CopyBuffer(maHandle, 0, 0, 5, MA);

   double HMA[];
   ArraySetAsSeries(HMA, true);
   CopyBuffer(highHandle, 0, 0, 5, HMA);

   double LMA[];
   ArraySetAsSeries(LMA, true);
   CopyBuffer(lowHandle, 0, 0, 5, LMA);

   highMA = HMA[1];
   lowMA = LMA[1];

//   if(iClose(Symbol(), 0, 1) >= HMA[1] && iOpen(Symbol(), 0, 1) < HMA[1] && iClose(Symbol(), 0, 1) > MA[1])  return(1);
//   if(iClose(Symbol(), 0, 1) <= LMA[1] && iOpen(Symbol(), 0, 1) > LMA[1] && iClose(Symbol(), 0, 1) < MA[1])  return(2);

   if(iClose(Symbol(), 0, 1) >= HMA[1] && iOpen(Symbol(), 0, 1) < HMA[1] && iLow(Symbol(), 0, 1) > MA[1])  return(1);

   if(iClose(Symbol(), 0, 1) <= LMA[1] && iOpen(Symbol(), 0, 1) > LMA[1] && iHigh(Symbol(), 0, 1) < MA[1])  return(2);

   return(0);

  }

I checked the signal,  the requirement "Whole candle above/below the MA" was not being considered only the close, amended lines above with the highlighted change



Documentation on MQL5: Common Functions / GetTickCount64
Documentation on MQL5: Common Functions / GetTickCount64
  • www.mql5.com
GetTickCount64 - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: