Tuesday, May 24, 2022

SMA9 entry and candle 2 exit

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

//|                                                   sma-candle.mq4 |

//|                        Copyright 2022, MetaQuotes Software Corp. |

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

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

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

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

#property version   "1.00"

#property strict

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

//| Expert initialization function                                   |

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

//---

extern int MagicNumber=100013;

extern double Lots =0.01;

extern double StopLoss=100;

extern double TakeProfit=0;

extern int TrailingStop=10;

extern int Slippage=3;

 

//---

   

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   double sma9 = DoubleToStr(iMA(_Symbol,_Period,9,0,MODE_SMA,PRICE_CLOSE,0),Digits);

   double sma20 = DoubleToStr(iMA(_Symbol,_Period,20,0,MODE_SMA,PRICE_CLOSE,0),Digits);

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

   double buyon = 0;

   if(Ask > sma9 && Ask > High[1] && Low[1] > Low[2])

     {

      buyon = 1;

     }

   double sellon = 0;

   if(Bid < sma9 && Bid < Low[1] && High[1] < High[2])

     {

      sellon = 1;

     }

    if(TotalOrdersCount() < 6)

     {

      int result=0;

      int result1=0;

      if((buyon == 1 && OrdersTotal() < 6)) // Here is your open buy rule

        {

         result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Bid+TakeProfit*_Point,"Fibonacci EA",MagicNumber,0,Blue);

         result1=OrderSend(Symbol(),OP_SELLSTOP,Lots,Low[2]-speared*Digits,Slippage,Ask+speared*Digits,0,"Fibonacci EA",MagicNumber,0,Blue);

         if(result>0)

           {


            OrderSelect(result,SELECT_BY_TICKET);


            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Low[2]-speared,Digits),0,0,Green);

           }


         

        }


      if((sellon == 1 && OrdersTotal() < 6))   // Here is your open Sell rule


        {

         result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Ask-TakeProfit*_Point,"Fibonacci EA",MagicNumber,0,Red);

         result1=OrderSend(Symbol(),OP_BUYSTOP,Lots,High[2]+speared*Digits,Slippage,Bid-speared*Digits,0,"Fibonacci EA",MagicNumber,0,Red);

         if(result>0)

           {

            OrderSelect(result,SELECT_BY_TICKET);


            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(High[2]+speared,Digits),0,0,Green);

           }

         }

     } 

      

   Comment ("BUY ",buyon," SELL ",sellon, " SMA9 :",sma9 , " SMA20 " , sma20 , " tp " , OrderOpenPrice()+100*_Point);

  

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

   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((sma9 < Low[1] && Open[2] > Bid && Low[1] > Bid) || (High[1] > sma20 && Close[0] < Low[1])) //here is your close buy rule

              {

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

              }

            if(TrailingStop>0)

              {

               if(Bid-OrderOpenPrice()>TrailingStop*_Point)

                 {

                  if((OrderStopLoss()<Bid-TrailingStop*_Point) || (OrderStopLoss() == 0))

                    {

                    double bclose = 0;

                    if(Bid > OrderOpenPrice()+100*_Point && Open[2] < OrderOpenPrice()+100*_Point) 

                    { bclose = OrderOpenPrice()+100*_Point;}

                    else { bclose = DoubleToStr(Open[2]-speared,Digits);}

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

                     

                    }

                 }

              }

           }

         else

           {

            if((sma20 > High[1] && Open[2] < Ask && High[1] < Ask) || (Low[1] < sma9 && Close[0] > High[1])) // here is your close sell rule

              {

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

              }

            if(TrailingStop>0)

              {

               if((OrderOpenPrice()-Ask)>(TrailingStop*_Point))

                 {

                  if((OrderStopLoss()>(Ask+TrailingStop*_Point)) || (OrderStopLoss()==0))

                    {

                    double sclose = 0;

                    if(Ask < OrderOpenPrice()-100*_Point && Open[2] > OrderOpenPrice()-100*_Point) 

                    { sclose = OrderOpenPrice()-100*_Point;}

                    else { sclose = DoubleToStr(Open[2]+speared,Digits);}

     

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


                    }

                 }

              }

           }

        }


     }


  }


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

//|                                                                  |

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


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);

  }


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


Wednesday, May 18, 2022

Fibonacci 20 candle EA for mt4

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

//|                                                  fibonacci23.mq4 |

//|                        Copyright 2022, MetaQuotes Software Corp. |

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

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

#property copyright "Copyright 2022, 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 double StopLoss=100;

extern double TakeProfit=50;

extern int TrailingStop=50;

extern int Slippage=3;

extern int candle=20;

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

//    expert start function

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

int start()

{

double MyPoint=Point;

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

double high3 = DoubleToStr(High[iHighest(NULL, 0, MODE_HIGH, candle, 1)],Digits);   

double low3 =  DoubleToStr(Low[iLowest(NULL, 0, MODE_LOW, candle, 1)],Digits);

double fib23 = DoubleToStr(high3-(high3-low3)*77/100);

double fib77 = DoubleToStr(low3+(high3-low3)*77/100);

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

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

double buyon = 0;

 if(fib23 < Ask && Ask > High[1] && Low[1] > Low[2] && fib77 > Ask){buyon = 1;}

double sellon = 0;

if(fib77 > Bid && Bid < Low[1] && High[1] < High[2] && fib23 < Bid){sellon = 1;}


if( TotalOrdersCount() == 0 ) 

  {

     int result=0;

    if((buyon == 1 && OrdersTotal() == 0)) // Here is your open buy rule

     {

      result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Bid-StopLoss*MyPoint,Bid+TakeProfit*MyPoint,"Fibonacci EA",MagicNumber,0,Blue);


        if(result>0)

        {


         OrderSelect(result,SELECT_BY_TICKET);


         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-StopLoss*MyPoint,Digits),NormalizeDouble(Bid+TakeProfit*MyPoint,Digits),0,Green);

        }


        return(0);

     }


    if((sellon == 1 && OrdersTotal() == 0 ))  // Here is your open Sell rule


     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Ask-StopLoss*MyPoint,Ask+TakeProfit*MyPoint,"Fibonacci EA",MagicNumber,0,Red);

        if(result>0)

        {                  

         OrderSelect(result,SELECT_BY_TICKET);


        OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask-StopLoss*MyPoint,Digits),NormalizeDouble(Ask+TakeProfit*MyPoint,Digits),0,Green);

         }

        return(0);

     }

  }

  Comment(" speared " , speared , "candle high " , high3 , " candle low " , low3, " buy signal ", buyon ," sell signal " ,sellon , "fibo 77 ",fib77, " fibo 23 ", fib23);

  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 && fib77 > Bid && Low[1] > Bid) //here is your close buy rule

              {

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

              }

            if(TrailingStop>0)  

              {                 

               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {

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

                   return(0);

                    }

                 }

              }

           }

         else 

           {

            if(OrderOpenPrice()-speared > Ask && fib23 < Ask && High[1] < Ask) // here is your close sell rule

                {

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

                }

            if(TrailingStop>0)  

              {               

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

                 {

                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {

             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);

}


Tuesday, May 3, 2022

fibo candle

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


//|                                                       10fibo_candle_ma.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=10010;


extern double Lots =0.02;


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 high3 = DoubleToStr(High[iHighest(NULL, 0, MODE_HIGH, 10, 1)],Digits);   


  double low3 =  DoubleToStr(Low[iLowest(NULL, 0, MODE_LOW, 10, 1)],Digits);


  double fib23 = DoubleToStr(high3-(high3-low3)*77/100);


  double fib77 = DoubleToStr(low3+(high3-low3)*77/100);

  

  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(low3-speared,Digits);


  //double bs0 = DoubleToStr(low3,Digits);


  double bstop = 0;


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


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


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


  //double ss0 = DoubleToStr(high3,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 canb0 = DoubleToStr((High[1]-Low[1])/2);


  double canb = DoubleToStr(High[1]-canb0);  


  double cans0 = DoubleToStr((Low[1]-High[1])/2);


  double cans = DoubleToStr(Low[1]+cans0);  


  datetime lclose = 0; 


  double total_history = OrdersHistoryTotal()-1 ;


  


    OrderSelect(total_history, SELECT_BY_POS, MODE_HISTORY);


     if (OrderSymbol()==Symbol()) 


        {  


         lclose = OrderCloseTime() ; 


        }  


       


   double buyon = 0;


  if(fib23 < Ask && Ask > High[1] && Low[1] > Low[2] && fib77 > Ask){buyon = 1;}


//    if(Low[3] >= Low[2] && Low[1] >= Low[2] && Ask > High[1] && Time[0] > lclose){buyon = 1;}


  double sellon = 0;


  if(fib77 > Bid && Bid < Low[1] && High[1] < High[2] && fib23 < Bid){sellon = 1;}


//  if(High[3] <= High[2] && High[1] <= High[2] && Bid < Low[1] && Time[0] > lclose){sellon = 1;}   


  if( TotalOrdersCount()< 10 ) 


  {


     int result=0;


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


     {


        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,bs0,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() == 0 && OrdersTotal() == 1) || (sellon == 1 && OrderType() == 1 && OrdersTotal() >= 1 && OrderOpenPrice() < Bid))  // Here is your open Sell rule


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

     {


        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,ss0,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 , " high3 " , high3 , " low3 " , low3, " bstop " , bstop , " sstop " , sstop , " otype " , OrderType() , " buyon ", buyon ," sellon " ,sellon , " bs ",bs , " time0 " , Time[0], " canb ",canb, " cans " ,cans, " high ",High[1], "fib77 ",fib77, " fib23 ", fib23);


  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 && fib77 > Bid && Low[1] > Bid) //here is your close buy rule


              {


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


              }


            if(TrailingStop>0)  


              {                 


               if(OrderOpenPrice() < Low[1])


                 {


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


                    {


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


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


                     return(0);


                    }


                 }


              }


           }


         else 


           {


            if(OrderOpenPrice()-speared > Ask && fib23 < Ask && High[1] < 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() > ss0) || (OrderStopLoss()==0))


                    {


                     OrderModify(OrderTicket(),OrderOpenPrice(),ss0,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);


}