Friday, September 16, 2022

last candle breakeout

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

//|                                                       last-candle-breakeout.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

extern int MagicNumber=10001;

extern double Lots =0.01;

extern int TrailingStop=1;

extern int Slippage=3;

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

//| Expert function                                             |

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

int start()

  {

string signal="";

  double MyPoint=Point;

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

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

datetime ctime = Time[0];

datetime lastclose = 0;

double sellstoploss = DoubleToStr(High[1]+(High[1]-Low[1]),Digits);

double buystoploss = DoubleToStr(Low[1]-(High[1]-Low[1]),Digits);

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  && OrderMagicNumber()== MagicNumber)

              {      

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

             lastclose = OrderCloseTime();

              }

     }

  if ((Open[2] > Close[2] && Open[1] < Close[1]) && (Low[1] > Low[2]) && (Close[0] > High[1]))

{

//set the signal variable to buy

signal="buy";

}

if ((Open[2] < Close[2] && Open[1] > Close[1]) && (High[1] < High[2]) && (Close[0] < Low[1]))

{

//set the signal variable to sell

signal="sell";

}       

Comment("Ask ",Ask, " bid ",Bid, " speard ", speared , " ctime ",ctime, " lastclose ",lastclose, "\n last high ",High[1],"buy stop ",buystoploss, " sell stop ",sellstoploss,

"\n The current signal is : ",signal );   

 

  if( TotalOrdersCount() ==0)

  {

     int result=0;

if ((signal == "buy" && TotalOrdersCount() ==0))//Here is your open Buy rule

     {

        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,buystoploss,0,"New Candle EA https://forexwithcandlestick.blogspot.com",MagicNumber,0,Green);

        if(result>0)

        {         

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(buystoploss,Digits),0,0,Green);

        }

        return(0);

     }

     if ((signal == "sell" && TotalOrdersCount() <=3)) //Here is your open Sell rule

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sellstoploss,0,"New Canlde EA https://forexwithcandlestick.blogspot.com ",MagicNumber,0,Red);

        if(result>0)

        {

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(sellstoploss,Digits),0,0,Red);

        }

        return(0);

     }

     

  }


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

              { Alert("close buy",Bid);

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

              }


            if(TrailingStop>0)

              {               

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

                 {

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

                    {

                    // OrderModify(OrderTicket(),OrderOpenPrice(),buystoploss,OrderTakeProfit(),0,Green);

                     return(0);

                    }

                 }

              }

           }

         else

           {

           if((OrderProfit() > 0 && High[1]<Ask)) // here is your close sell rule

                {

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

                }


            if(TrailingStop>0)

              {               

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

                 {

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

                    {

                   //  OrderModify(OrderTicket(),OrderOpenPrice(),sellstoploss,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