Wednesday, June 8, 2022

2 candle breakout

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

//|                                                  2_candle_breakout.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=10015;

extern double Lots =0.01;

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 sma9 = DoubleToStr(iMA(_Symbol,_Period,9,0,MODE_SMA,PRICE_CLOSE,0),Digits);

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

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

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

double riskv = MathRound(AccountBalance()-AccountBalance()*98/100);

double StopLoss = riskv/1000;

double buystoploss = DoubleToStr(Bid-StopLoss,Digits);

double sellstoploss = DoubleToStr(Ask+StopLoss,Digits);

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

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

double sclose = DoubleToStr(nmax+speared,Digits);

double bclose = DoubleToStr(nmin-speared,Digits);

datetime ctime = TimeCurrent();

datetime lastclose = 0;

int c = OrdersHistoryTotal(); 

  int a = OrdersHistoryTotal() - 10;

  int b = OrdersHistoryTotal();

   

  for (c = a;c < b ;c++)

     {

           if (OrderSelect(c,SELECT_BY_POS, MODE_HISTORY) == true)

              {      

                

               //  Alert("orders found"+ OrderTicket() + " " + OrderCloseTime());

             lastclose = OrderCloseTime();

              }

     }

double buyon = 0;

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

if(Close[0] > nmax){buyon = 1;}

double sellon = 0;


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

if(Close[0] < nmin){sellon = 1;}

double buyticket =0;

double sellticket =0;

OrderSelect(OrdersTotal()-1,SELECT_BY_POS);

if(OrderMagicNumber()==MagicNumber && OrderType() == 0){

buyticket = OrderTicket();

}

if(OrderMagicNumber()==MagicNumber && OrderType() == 1){

sellticket = OrderTicket();

}

if( TotalOrdersCount() <= 7 )


  {


     int result=0;


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

if(buyon == 1 && OrdersTotal() <= 7 && ctime > lastclose)

     {


      result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,bclose,0,"2_candle_breakout EA",MagicNumber,0,Blue);


        if(result>0)


        {


         OrderSelect(result,SELECT_BY_TICKET);


         OrderModify(OrderTicket(),OrderOpenPrice(),bclose,OrderTakeProfit(),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() == 0) || (sellon == 1 && OrderType() == 0 && OrdersTotal() == 1) || (sellon == 1 && OrderType() == 0 && OrdersTotal() <= 6 && OrderOpenPrice() > Bid))  // Here is your open Sell rule

    if(sellon == 1 && OrdersTotal() <= 7 && ctime > lastclose)

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sclose,0,"2_candle_breakout EA",MagicNumber,0,Red);


        if(result>0)


        {                  


         OrderSelect(result,SELECT_BY_TICKET);


        OrderModify(OrderTicket(),OrderOpenPrice(),sclose,OrderTakeProfit(),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 , " amount " , AccountBalance(), " buystop " , buystoploss, "stop " , StopLoss , " type ", OrderType()," orderticket ", OrderTicket(), " nmax " , nmax , " nmin " , nmin , " lastclose " ,lastclose , "\n time ",Time[0],"time" ,ctime );


  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() <= Bid && nmin > Close[0]))//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() >= Ask && nmax < Close[0]))// 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);


}


No comments:

Post a Comment