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

}

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


Thursday, September 15, 2022

count all open order in mt4

int OpenBuyOrders;

int OpenSellOrders;

int total;


int start()

{

  OpenBuyOrders=0;

  OpenSellOrders=0;

  total=0;


//Count Pending Stop Orders

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

   {

    if(OrderSelect(i, SELECT_BY_POS)==true)

     {

      if (OrderType()==OP_BUY)

        OpenBuyOrders++;

      if (OrderType()==OP_SELL)

        OpenSellOrders++;

     }

    total=OpenBuyOrders + OpenSellOrders;

   }

   Comment ("buy_stop=",OpenBuyOrders," sell_stop=",OpenSellOrders," total=",total);

}

 

Wednesday, September 14, 2022

close all orders in mt4 script

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

//|                                            close-all-orders.mq4  |

//|                                  Copyright © 2005  |

//|                                       Custom Metatrader Systems. |

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


#property copyright "Copyright © 2005"

int start()

{

  int total = OrdersTotal();

  for(int i=total-1;i>=0;i--)

  {

    OrderSelect(i, SELECT_BY_POS);

    int type   = OrderType();


    bool result = false;

    

    switch(type)

    {

      //Close opened long positions

      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

                          break;

      

      //Close opened short positions

      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

                          break;


      //Close pending orders

      case OP_BUYLIMIT  :

      case OP_BUYSTOP   :

      case OP_SELLLIMIT :

      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );

    }

    

    if(result == false)

    {

      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );

      Sleep(3000);

    }  

  }

  

  return(0);

}





Tuesday, September 13, 2022

Daily pivot Point Indicator

 


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


//    expert start function


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


int start()


{


  double MyPoint=Point;


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


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

  double close = iClose(Symbol(),PERIOD_D1,1);


  double low = iLow(Symbol(),PERIOD_D1,1);


  double high = iHigh(Symbol(),PERIOD_D1,1);


  double pivot = (close+low+high)/3;

  double s1 = (pivot*2)-high;

  double s2 = pivot-(high-low);

  double s3 = s1-(high-low);

  double r1 = (pivot*2)-low;

  double r2 = pivot+(high-low);

  double r3 = r1+(high-low);



 ObjectDelete("lowtrend");

//ObjectCreate(0,"lowtrend",OBJ_TREND,0,Time[pivot],Low[pivot],Time[0],Low[0]);

ObjectCreate(0,"lowtrend",OBJ_HLINE,0,Time[0],pivot);

   

  ObjectSetInteger(0,"lowtrend",OBJPROP_COLOR,Yellow);

  ObjectSetInteger(0,"lowtrend",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"lowtrend",OBJPROP_WIDTH,1);

//r line

ObjectDelete("r1");

ObjectCreate(0,"r1",OBJ_HLINE,0,Time[0],r1);

   

  ObjectSetInteger(0,"r1",OBJPROP_COLOR,Red);

  ObjectSetInteger(0,"r1",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"r1",OBJPROP_WIDTH,1);


ObjectDelete("r2");

ObjectCreate(0,"r2",OBJ_HLINE,0,Time[0],r2);

   

  ObjectSetInteger(0,"r2",OBJPROP_COLOR,White);

  ObjectSetInteger(0,"r2",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"r2",OBJPROP_WIDTH,1);

  

  ObjectDelete("r3");

ObjectCreate(0,"r3",OBJ_HLINE,0,Time[0],r3);

   

  ObjectSetInteger(0,"r3",OBJPROP_COLOR,Blue);

  ObjectSetInteger(0,"r3",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"r3",OBJPROP_WIDTH,1);


//s line

ObjectDelete("s1");

ObjectCreate(0,"s1",OBJ_HLINE,0,Time[0],s1);

   

  ObjectSetInteger(0,"s1",OBJPROP_COLOR,Red);

  ObjectSetInteger(0,"s1",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"s1",OBJPROP_WIDTH,1);


ObjectDelete("s2");

ObjectCreate(0,"s2",OBJ_HLINE,0,Time[0],s2);

   

  ObjectSetInteger(0,"s2",OBJPROP_COLOR,White);

  ObjectSetInteger(0,"s2",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"s2",OBJPROP_WIDTH,1);

  

  ObjectDelete("s3");

ObjectCreate(0,"s3",OBJ_HLINE,0,Time[0],s3);

   

  ObjectSetInteger(0,"s3",OBJPROP_COLOR,Blue);

  ObjectSetInteger(0,"s3",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"s3",OBJPROP_WIDTH,1);


// open order line

ObjectDelete("buyline");

//ObjectCreate(0,"lowtrend",OBJ_TREND,0,Time[pivot],Low[pivot],Time[0],Low[0]);

ObjectCreate(0,"buyline",OBJ_VLINE,0,OrderOpenTime(),OrderOpenPrice());

   

  ObjectSetInteger(0,"buyline",OBJPROP_COLOR,Yellow);

  ObjectSetInteger(0,"buyline",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"buyline",OBJPROP_WIDTH,1);  

 Comment("Pivot ",pivot , "\n S1 ",s1, " S2 ",s2 , " S3",s3 ,"\n R1 ",r1 ," R2 ",r2," R3 ",r3);

  }

  

trendline signal

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

//|                                                new-trendline.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 TakeProfit=50;


extern int TrailingStop=50;


extern int Slippage=3;


extern int candle=20;


int OnInit()

  {

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

int CandlesOnChart=WindowFirstVisibleBar();

int HighestCandle = iHighest(_Symbol,_Period,MODE_HIGH,CandlesOnChart,2);

int lowestCandle = iLowest(_Symbol,_Period,MODE_LOW,CandlesOnChart,2);

int HighestCandle0 = iHighest(_Symbol,_Period,MODE_HIGH,10,2);

int LowestCandle0 = iLowest(_Symbol,_Period,MODE_LOW,10,2);


ObjectDelete("hightrend");

ObjectCreate(0,"hightrend",OBJ_TREND,0,Time[HighestCandle],

High[HighestCandle],Time[2],High[2]);

   

  ObjectSetInteger(0,"hightrend",OBJPROP_COLOR,Red);

  ObjectSetInteger(0,"hightrend",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"hightrend",OBJPROP_WIDTH,1);

  ObjectSetInteger(0,"hightrend",OBJPROP_RAY,true);

  

  ObjectDelete("lowtrend");

ObjectCreate(0,"lowtrend",OBJ_TREND,0,Time[lowestCandle],

Low[lowestCandle],Time[2],Low[2]);

   

  ObjectSetInteger(0,"lowtrend",OBJPROP_COLOR,Yellow);

  ObjectSetInteger(0,"lowtrend",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"lowtrend",OBJPROP_WIDTH,1);

  ObjectSetInteger(0,"lowtrend",OBJPROP_RAY,true);

  

double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

static double lastBid = bid;


string objName = "lowtrend";

string objName1 = "hightrend";

datetime time = TimeCurrent();

double price = ObjectGetValueByTime(0,objName,time);

double priceup = ObjectGetValueByTime(0,objName1,time);

//if(bid >= price && lastBid < price){

if(Close[1] > price && Close[0] > High[1] && Close[0] > High[2]){

Print("we hit line below ok Do buyy");

}

if(Low[1] > Close[0]){

Print("we hit line above ok exit buy");

}

if(Close[1] < priceup && Close[0] < Low[1] && Close[0] < Low[2]){

//if(bid <= price && lastBid > price){

Print("we hit line above ok Do selll");

}

if(High[1] < Close[0]){

Print("we hit line above ok exit sell");

}

lastBid = bid;

Comment("price",price , " priceup ",priceup,"\n bid ",bid, " lastbid ",lastBid);   

  }

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


trend line indicator

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

//|                                                    trendline-indi.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                                   |

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


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

//| Expert tick function                                             |

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

void OnTick()

  {

int HighestCandle = iHighest(_Symbol,_Period,MODE_HIGH,50,10);

int lowestCandle = iLowest(_Symbol,_Period,MODE_LOW,50,10);

int HighestCandle0 = iHighest(_Symbol,_Period,MODE_HIGH,10,10);

int LowestCandle0 = iLowest(_Symbol,_Period,MODE_LOW,10,10);


ObjectDelete("hightrend");

ObjectCreate(0,"hightrend",OBJ_TREND,0,Time[HighestCandle],

High[HighestCandle],Time[2],High[2]);

   

  ObjectSetInteger(0,"hightrend",OBJPROP_COLOR,Red);

  ObjectSetInteger(0,"hightrend",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"hightrend",OBJPROP_WIDTH,1);

  ObjectSetInteger(0,"hightrend",OBJPROP_RAY,true);

  

  ObjectDelete("lowtrend");

ObjectCreate(0,"lowtrend",OBJ_TREND,0,Time[lowestCandle],

Low[lowestCandle],Time[2],Low[2]);

   

  ObjectSetInteger(0,"lowtrend",OBJPROP_COLOR,Yellow);

  ObjectSetInteger(0,"lowtrend",OBJPROP_STYLE,STYLE_SOLID);

  ObjectSetInteger(0,"lowtrend",OBJPROP_WIDTH,1);

  ObjectSetInteger(0,"lowtrend",OBJPROP_RAY,true);

 Comment("highest",High[HighestCandle],"highest0",High[HighestCandle0],"\n lowestlow ",Low[lowestCandle], " low ",Low[LowestCandle0]);

  }

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


find first red and green candle

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

//|                                                  first-green.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                                   |

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

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   int last_Red_candle_index=1;

   int last_Green_candle_index=1;

   static datetime last_Red_candle_time=0;

   static datetime last_Green_candle_time=0;

   static datetime alert_reference_time=0;

   static datetime alert_reference_time0=0;

   while(true)

     {

      if(iOpen(Symbol(),0,last_Red_candle_index)>iClose(Symbol(),0,last_Red_candle_index))

        {

         last_Red_candle_time=iTime(Symbol(),0,last_Red_candle_index);

         break;

        }

      last_Red_candle_index++;

      

    if(iOpen(Symbol(),0,last_Green_candle_index)<iClose(Symbol(),0,last_Green_candle_index))

        {

         last_Green_candle_time=iTime(Symbol(),0,last_Green_candle_index);

         break;

        }

      last_Green_candle_index++;

     }

   if(alert_reference_time!=last_Red_candle_time)

     {

      double red_candle_high=iHigh(Symbol(),0,last_Red_candle_index);

      for(int x=last_Red_candle_index-1;x>0;x--)

        {

         if(iClose(Symbol(),0,x)>red_candle_high)

           {

            Alert("Close Above Red Candle High  "+Symbol(),"    ",TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),

                  "   M",Period(),"  ",Open[0]);

            alert_reference_time=last_Red_candle_time;

            break;

           }

        }

     }

     

   if(alert_reference_time0 != last_Green_candle_time)

     {

      double green_candle_low=iLow(Symbol(),0,last_Green_candle_index);

      for(int y=last_Green_candle_index-1;y>0;y--)

        {

         if(iClose(Symbol(),0,y)<green_candle_low)

           {

            Alert("Close Above green Candle low  "+Symbol(),"    ",TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),

                  "   M",Period(),"  ");

            alert_reference_time0 = last_Green_candle_time;

            break;

           }

        }

     }  

//---

      Comment("last red candle",last_Red_candle_time,"last green candle",last_Green_candle_time);

  }

find first red candle

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

//|                                                     find-red.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                                   |

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

int OnInit()

  {

//---

   

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   int last_red_bar=-1;

double red_high=-1;

for(int i=0;i<Bars-20;i++)

{

if(Close[i]<Open[i])

{

last_red_bar=i;

red_high=High[i];

break;

}

}

double redcandle =0;

for(int i=last_red_bar-1;i>=0 && last_red_bar>0;i--)

{

redcandle = Close[i]>Open[i];

if(High[i]>red_high)

{

if(i==0) Alert("Hihihhi",red_high);

break;

}

}

datetime last_Red_candle_time=0;

 while(true)

     {

      if(iOpen(Symbol(),0,last_red_bar)>iClose(Symbol(),0,last_red_bar))

        {

         last_Red_candle_time=iTime(Symbol(),0,last_red_bar);

         break;

        }}

 int      shift=iBarShift(Symbol(),0,last_Red_candle_time); 

 datetime newtime = iTime(Symbol(),0,shift);      

Comment("red high",red_high, " red candle ",last_Red_candle_time, " newtime ",newtime);

  }

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


fibonancci indicator

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

//|                                                        fibo.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 tick function                                             |

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

void OnTick()

  {

int HighestCandle = iHighest(_Symbol,_Period,MODE_CLOSE,66,0);

int lowestCandle = iLowest(_Symbol,_Period,MODE_CLOSE,66,0);

int HighestCandle0 = iHighest(_Symbol,_Period,MODE_HIGH,10,2);

int LowestCandle0 = iLowest(_Symbol,_Period,MODE_LOW,10,2);


ObjectDelete("hightrend");

ObjectCreate(0,"hightrend",OBJ_FIBO,0,Time[HighestCandle],

Close[HighestCandle],Time[lowestCandle],Close[lowestCandle]);

   

 // ObjectSetInteger(0,"hightrend",OBJPROP_COLOR,Red);

 // ObjectSetInteger(0,"hightrend",OBJPROP_STYLE,STYLE_SOLID);

 // ObjectSetInteger(0,"hightrend",OBJPROP_WIDTH,1);

  //ObjectSetInteger(0,"hightrend",OBJPROP_RAY,true);

  

  

 Comment("highest",Close[HighestCandle],"highest0",High[HighestCandle0],"\n lowestlow ",Low[lowestCandle], " low ",Low[LowestCandle0]);

  };

  

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


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

}

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