Tuesday, September 13, 2022

supertrend

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

//|                                                       S_Trend.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 double StopLoss=0;

extern double TakeProfit=0;

extern int TrailingStop=1;

extern int Slippage=3;


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

//| Expert tick 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(Ask+2*speared,Digits);


double buystoploss = DoubleToStr(Bid-2*speared,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();

              }

     }

  double uptrend=iCustom(NULL,0,"SuperTrend",10,3.0,0,0);

  double dntrend=iCustom(NULL,0,"SuperTrend",10,3.0,1,0);

      

 if (uptrend!=EMPTY_VALUE && (Open[1] < Close[1]) && (Low[1] > Low[2]) && (Close[0] > High[1]))

{

//set the signal variable to buy

signal="buy";

}

//if the moving average above the current price

if (dntrend!=EMPTY_VALUE && (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, 

"\nuptrend ",uptrend," / ",dntrend,"\n The current signal is : ",signal);   

 

 double TheStopLoss=0;

  double TheTakeProfit=0;

  if( TotalOrdersCount()<=10 )

  {

     int result=0;

if ((signal == "buy" && ctime > lastclose && TotalOrdersCount() ==0))

// || (signal == "buy" && ctime > lastclose && OrderType() == OP_SELL)) // Here is your open buy rule

     {

        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,buystoploss,0,"EMA crossover Robot 15 Minutes https://forexwithcandlestick.blogspot.com",MagicNumber,0,Green);

        if(result>0)

        {

         TheStopLoss=buystoploss;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

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

        }

        return(0);

     }

     //if the fast SMA is now above

if ((signal == "sell" && ctime > lastclose && TotalOrdersCount() ==0))

// || (signal == "sell" && ctime > lastclose && OrderType() == OP_BUY)) // Here is your open Sell rule

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sellstoploss,0,"EMA crossover Robot 15 Minutes https://forexwithcandlestick.blogspot.com ",MagicNumber,0,Red);

        if(result>0)

        {

         TheStopLoss=sellstoploss;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

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

        }

        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