Wednesday, January 12, 2022

fractal ea for mt4

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

//|                                                       fractal.mq4 |

//|                        Copyright 2021, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2021, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

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

//| Expert initialization function                                   |

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

extern int MagicNumber=10001;

extern double Lots =0.01;

extern int TrailingStop=1;

extern int Slippage=3;

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

//    expert start function

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

int start()

{

  double MyPoint=Point;

  if(Digits==3 || Digits==5) MyPoint=Point*10;

  double high5 = DoubleToStr(High[iHighest(NULL, 0, MODE_HIGH, 5, 1)]);   

  double low5 =  DoubleToStr(Low[iLowest(NULL, 0, MODE_LOW, 5, 1)]);

  double speared =DoubleToStr(Ask-Bid,Digits);

  double btar =DoubleToStr(Ask+speared,Digits);

  double star =DoubleToStr(Bid-speared,Digits);

  double bs = DoubleToStr(Ask-0.00100,Digits);

  double bs0 = DoubleToStr(low5-speared,Digits);

  double bstop = 0;

  if(bs < bs0){bstop = bs0;}else{bstop=bs;} 

  double ss = DoubleToStr(Bid+0.00100,Digits);

  double ss0 = DoubleToStr(high5+speared,Digits);

  double sstop = 0;

  if(ss < ss0){sstop = ss0;}else{sstop=ss;} 

  double bclose = DoubleToStr(Low[1]-speared,Digits);

  double sclose = DoubleToStr(High[1]+speared,Digits);

  double buyon = 0;

  if(High[3]>High[2] && Ask > high5 && Ask > High[1]){buyon = 1;}

  

  double sellon = 0;

  if(Low[3]<Low[2] && Bid < low5 && Bid < Low[1]){sellon = 1;}

     

  if( TotalOrdersCount()< 10 ) 

  {

     int result=0;

    if((buyon == 1 && OrdersTotal() == 0) || (buyon == 1 && OrderType() > 0 && OrdersTotal() > 1 && Ask > OrderOpenPrice())) // Here is your open buy rule

     {

        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,bstop,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);

        if(result>0)

        {

         OrderSelect(result,SELECT_BY_TICKET);

        // OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

        }

        return(0);

     }

    if((sellon == 1 && OrdersTotal() == 0) || (sellon == 1 && OrderType() < 1 && OrdersTotal() > 1 && Bid < OrderOpenPrice()))  // Here is your open Sell rule

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sstop,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);

        if(result>0)

        {                  

                

         OrderSelect(result,SELECT_BY_TICKET);

        // OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

         }

        return(0);

     }

  }

      

  Comment(" speared " , speared , " high5 " , high5 , " low5 " , low5, " btar " , btar, " star ", star, " bstop " , bstop , " sstop " , sstop , " otype " , OrderType() , " ortick " , OrderTicket(), " open price " , OrderOpenPrice(), " buyon ", buyon ," sellon " ,sellon , " bs ",bs);

  for(int cnt=0;cnt<OrdersTotal();cnt++)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL &&   

         OrderSymbol()==Symbol() &&

         OrderMagicNumber()==MagicNumber 

         )  

        {

         if(OrderType()==OP_BUY)  

           {

              if(OrderOpenPrice()+speared < Bid && bclose > Bid) //here is your close buy rule

              {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

              }

            if(TrailingStop>0)  

              {                 

               if(OrderOpenPrice() < Low[1])

                 {

                  if((OrderStopLoss() < bclose) || (OrderStopLoss()==0))

                    {

                     //OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     OrderModify(OrderTicket(),OrderOpenPrice(),bclose,OrderTakeProfit(),0,Green);

                     return(0);

                    }

                 }

              }

           }

         else 

           {

            if(OrderOpenPrice()-speared > Ask && sclose < Ask) // here is your close sell rule

                {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

                }

            if(TrailingStop>0)  

              {                 

               //if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

               if((OrderOpenPrice()> High[1]))

                 {

                  if((OrderStopLoss() > sclose) || (OrderStopLoss()==0))

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),sclose,OrderTakeProfit(),0,Red);

                     //OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);

                    }

                 }

              }

           }

        }

     }

   return(0);

}


int TotalOrdersCount()

{

  int result=0;

  for(int i=0;i<OrdersTotal();i++)

  {

     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);

     if (OrderMagicNumber()==MagicNumber) result++;


   }

  return (result);

}