Friday, December 16, 2022

forexpark average order

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

//|                                                       ForexPerk-Copy.mq4 |

//|                        Copyright 2022, MetaQuotes Software Corp. |

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

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

extern int MagicNumber=10001;

extern double Lots =0.01;

extern int Takeprofit=100;

extern int gap = 100;

extern int Slippage=3;

extern double Maxbuyposition=10;

extern double Maxsellposition=10;

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

//| Expert initialization function                                   |

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


int OpenBuyOrders;


int OpenSellOrders;


int total;

 

void OnTick()

 

int speared = SymbolInfoInteger(OrderSymbol(), SYMBOL_SPREAD);

double profit = AccountProfit();  

  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 && OrderSymbol() == Symbol())


        OpenBuyOrders++;


      if (OrderType()==OP_SELL && OrderSymbol() == Symbol())


        OpenSellOrders++;


     }


    total=OpenBuyOrders + OpenSellOrders;


   }

//last close

datetime ctime = Time[0];

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

              {      

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

             lastclose = OrderCloseTime();

              }

     }

 //lastprice

 double buyticket = 0;


double lastbuy = 0;


double buytarget = 0;


double buyprofit = 0;


datetime buydate = 0;


double sellticket = 0;


double lastsell = 0;


double selltarget = 0;


double sellprofit = 0;


datetime selldate = 0;

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


      {


if(OrderSelect(cntl,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()== MagicNumber){


//buy details

if(OrderMagicNumber()== MagicNumber && OrderType() == OP_BUY && OrderSymbol() == Symbol()){


buyticket = OrderTicket();


lastbuy = OrderOpenPrice();


buydate = OrderOpenTime();


buyprofit = OrderProfit();

}


//sell details

if(OrderMagicNumber()==  MagicNumber && OrderType() == OP_SELL && OrderSymbol() == Symbol()){


sellticket = OrderTicket();


lastsell = OrderOpenPrice();


selldate = OrderOpenTime();


sellprofit = OrderProfit();

}

}}    

double buygap = lastbuy-gap*Point;

double sellgap = lastsell+gap*Point;

   //signal

string signal="";   

if ((OpenBuyOrders < Maxbuyposition) && (ctime > lastclose) && (ctime > OrderOpenTime()) && (Close[0] > High[1]))

{

//set the signal variable to buy

signal="buy";

}

//buy 2nd

if ((OpenBuyOrders < Maxbuyposition) && (ctime > lastclose) && (ctime > OrderOpenTime())&& (buygap > Ask) && (OrderOpenPrice() > Ask) && (Close[0] > High[1]))

{

//set the signal variable to buy

signal="buy";

}


//if the moving average above the current price

if ((OpenSellOrders < Maxsellposition) && (ctime > lastclose) && (ctime > OrderOpenTime()) &&  (Close[0] < Low[1]))

{

//set the signal variable to sell

signal="sell";

}  

//sell 2nd

if ((OpenSellOrders < Maxsellposition) && (ctime > lastclose) && (ctime > OrderOpenTime()) && (sellgap < Bid) && (OrderOpenPrice() < Bid)  &&  (Close[0] < Low[1]))

{

//set the signal variable to sell

signal="sell";

}   

   //

//average buy

double Lotsb   = 0.0;

   double OrderPriceb = 0.0;

   double LotsPriceb = 0.0;

   double averagebuy = 0.0;

   double buytar = 0.0;

   for (int ba = OrdersTotal() - 1; ba >= 0; ba--)

   {

      OrderSelect(ba, SELECT_BY_POS, MODE_TRADES);

      if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)

      {

         Lotsb = Lotsb + OrderLots();

         LotsPriceb = LotsPriceb + OrderLots()*OrderOpenPrice();


         //Print("Lotsb : ", OrderLots(), "   OrderOpenPrice : ", OrderOpenPrice());

buytar = LotsPriceb/Lotsb+Takeprofit*Point;  

      }

    

   }   

//average sell

double Lotss   = 0.0;

   double OrderPrices = 0.0;

   double LotsPrices = 0.0;

   double averagesell = 0.0;

   double selltar = 0.0;

   for (int sa = OrdersTotal() - 1; sa >= 0; sa--)

   {

      OrderSelect(sa, SELECT_BY_POS, MODE_TRADES);

      if (OrderSymbol() == Symbol() && OrderType() == OP_SELL)

      {

         Lotss = Lotss + OrderLots();

         LotsPrices = LotsPrices + OrderLots()*OrderOpenPrice();


        // Print("Lotss : ", OrderLots(), "   OrderOpenPrice : ", OrderOpenPrice());

selltar = LotsPrices/Lotss-Takeprofit*Point;

      }

      

   }   

//

double buytp = Ask+Takeprofit*Point;

double selltp = Bid-Takeprofit*Point;

//double target = 1.00+(speared*0.01);

   Comment ("Speared",speared , " Equity ",profit," Ask ",Ask, " Bid ",Bid, " buygap ",buygap," sellgap ",sellgap,

   "\n Total buy ",OpenBuyOrders," Total sell ",OpenSellOrders," total ",total," order close ",OrderClosePrice()," order open ",OrderOpenPrice(),

   "\n current time " ,TimeCurrent(), " current open time ",ctime," last closed order ",lastclose, " buytp ",buytp, " selltp ",selltp,

   "\n last buy ",lastbuy, "average buy",averagebuy, " buy tar ",buytar," last sell ",lastsell, "average sell",averagesell," selltar ",selltar,

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

 int result=0;

double lots = Lots;//0;

//if(OrdersTotal() == 0){lots = Lots;}else{lots = Lots*(OrdersTotal()+1);}

if ((signal == "buy" && OpenBuyOrders < Maxbuyposition))//Here is your open Buy rule

     {Alert("open buy");

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

  if(result>0)

        {

         OrderSelect(result,SELECT_BY_TICKET);

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

        }      

     }

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

     { Alert("open sell");

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

        if(result>0)

        {

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),0,selltp,0,Red);

        }

       

        

     } 

if(Ask < selltar && OrderClosePrice() < selltar)

{

for (int ss = OrdersTotal()-1; ss >= 0; ss--)

{    

if(OrderSelect(ss,SELECT_BY_POS) && OrderType() == OP_SELL && OrderSymbol() == Symbol()){

   ulong orderticket = OrderTicket();

   Print(orderticket);

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

  }



}//order closed end


//

if(Bid > buytar && OrderClosePrice() > buytar)

{

for (int bb = OrdersTotal()-1; bb >= 0; bb--)

{    

if(OrderSelect(bb,SELECT_BY_POS) && OrderType() == OP_BUY && OrderSymbol() == Symbol()){

   ulong orderticketb = OrderTicket();

   Print(orderticketb);

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

  }


}

}//buy closed  

 

 }  

Saturday, November 26, 2022

range trading

this mt4 ea open trade in 14 candles low and high range.

this ea good for 1 hour and biggest chart.

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

//|                                                        range_trading.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=10002;

extern double Lots =0.01;

extern int TrailingStop=100;

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(High[1]+(High[1]-Low[1]),Digits);

double selltp = DoubleToStr(Low[1]-2*(High[1]-Low[1]),Digits);


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

double buytp = DoubleToStr(High[1]+2*(High[1]-Low[1]),Digits);


//double rsi = iRSI(NULL,0,14,PRICE_CLOSE,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  && OrderMagicNumber()== MagicNumber)

              {      

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

             lastclose = OrderCloseTime();

              }

     }

int tllow =   iLowest(_Symbol,_Period,MODE_LOW,14,0);

int tlhigh =   iHighest(_Symbol,_Period,MODE_HIGH,14,0);

double lowa = Low[tllow];

double higha = High[tlhigh];


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

{

//set the signal variable to buy

signal="buy";

}

//if the moving average above the current price

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

{

//set the signal variable to sell

signal="sell";

}    

     

Comment("ASK ",Ask, " BID ",Bid, " SPEARD ", speared , "\n CURRENT TIME ",ctime, " LAST ORDER CLOSED TIME ",lastclose,

"\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,"range_trading 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 the fast SMA is now above

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

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sellstoploss,0,"range_trading 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);

     }

     

  } 

   ObjectDelete("tpl");

ObjectCreate(0,"tpl",OBJ_HLINE,0,Time[20],

OrderTakeProfit(),Time[1],OrderTakeProfit());

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

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

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

//object pivot

   ObjectDelete("SL");

ObjectCreate(0,"SL",OBJ_HLINE ,0,Time[20],

OrderStopLoss(),Time[1],OrderStopLoss());

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

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

ObjectSetInteger(0,"SL",OBJPROP_WIDTH,3);


   ObjectDelete("trendlow");

ObjectCreate(0,"trendlow",OBJ_TREND,0,Time[14],

lowa,Time[0],lowa);

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

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

ObjectSetInteger(0,"trendlow",OBJPROP_WIDTH,3);


   ObjectDelete("trendhigh");

ObjectCreate(0,"trendhigh",OBJ_TREND,0,Time[14],

higha,Time[0],higha);

ObjectSetInteger(0,"trendhigh",OBJPROP_COLOR,Green);

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

ObjectSetInteger(0,"trendhigh",OBJPROP_WIDTH,3);


  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 && OrderStopLoss()<Bid-buystoploss)

                    {

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

Wednesday, November 9, 2022

pivot and poc for 1 hour timeframe

pivot and poc for 1 hour timeframe

ea calculate pivot weakly

poc = ( 20 candle high - 20 candle low )/2


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

//|                                                        pivot-poc.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=10002;

extern double Lots =0.01;

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(High[1]+(High[1]-Low[1]),Digits);

double selltp = DoubleToStr(Low[1]-2*(High[1]-Low[1]),Digits);


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

double buytp = DoubleToStr(High[1]+2*(High[1]-Low[1]),Digits);


//double rsi = iRSI(NULL,0,14,PRICE_CLOSE,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  && OrderMagicNumber()== MagicNumber)

              {      

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

             lastclose = OrderCloseTime();

              }

     }

int LowestCandle =   iLowest(_Symbol,_Period,MODE_LOW,20,0);

int HighestCandle = iHighest(_Symbol,_Period,MODE_HIGH,20,0);

double poc = Low[LowestCandle]+(High[HighestCandle]-Low[LowestCandle])/2;

double MH =  iHigh(_Symbol,PERIOD_W1,1); //iHigh(_Symbol,PERIOD_MN1,1);  

double ML =  iLow(_Symbol,PERIOD_W1,1); //iLow(_Symbol,PERIOD_MN1,1);  

double MC =  iClose(_Symbol,PERIOD_W1,1); //iClose(_Symbol,PERIOD_MN1,1);

double pivot = (MH+ML+MC)/3;  

  if ((Open[1] < Close[1]) && (Close[0] > High[1]) && (Close[1] > pivot) && (Close[1] > poc))

{

//set the signal variable to buy

signal="buy";

}

//if the moving average above the current price

if ((Open[1] > Close[1]) && (Close[0] < Low[1]) && (Close[1] < pivot) && (Close[1] < poc))

{

//set the signal variable to sell

signal="sell";

}    

     

Comment("ASK ",Ask, " BID ",Bid, " SPEARD ", speared , "\n CURRENT TIME ",ctime, " LAST ORDER CLOSED TIME ",lastclose,

"\n PIVOT POINT ",pivot , " POC POINT ",poc ,"\n The current signal is : ",signal , "",PERIOD_CURRENT );   

 

  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,"Pivot-POC 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 the fast SMA is now above

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

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sellstoploss,0,"Pivot-POC 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);

     }

     

  } 

   ObjectDelete("tpl");

ObjectCreate(0,"tpl",OBJ_HLINE,0,Time[20],

OrderTakeProfit(),Time[1],OrderTakeProfit());

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

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

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

//object pivot

   ObjectDelete("PIVOTL");

ObjectCreate(0,"PIVOTL",OBJ_HLINE ,0,Time[20],

pivot,Time[1],pivot);

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

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

ObjectSetInteger(0,"PIVOTL",OBJPROP_WIDTH,3);


   ObjectDelete("POC");

ObjectCreate(0,"POC",OBJ_HLINE,0,Time[20],

poc,Time[1],poc);

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

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

ObjectSetInteger(0,"POC",OBJPROP_WIDTH,3);


  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 && OrderStopLoss()<Bid-buystoploss)

                    {

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

Tuesday, October 4, 2022

range trade

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

//|                                                     00000000.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 Slippage=3;


void OnTick()

  {

double buyticket = 0;


double lastbuy = 0;


double buytarget = 0;


double buyprofit = 0;


datetime buydate = 0;


double sellticket = 0;


double lastsell = 0;


double selltarget = 0;


double sellprofit = 0;


datetime selldate = 0;   

double buystop = 0;

double sellstop = 0;

double buytp = 0;

double selltp =0;

double buylots=0;

double selllots=0;

double buysl=0;

double sellsl=0;

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


 for (int cntl = 0; cntl < OrdersTotal(); cntl++){

if(OrderSelect(cntl,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()== MagicNumber){


//buy details

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


buyticket = OrderTicket();


lastbuy = OrderOpenPrice();


buydate = OrderOpenTime();


buyprofit = OrderProfit();

buytp = OrderTakeProfit();

buysl = OrderStopLoss();

buylots = OrderLots();}


//buystop details

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


buystop = OrderTicket();

}

//sell details

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


sellticket = OrderTicket();


lastsell = OrderOpenPrice();


selldate = OrderOpenTime();


sellprofit = OrderProfit();


selltp = OrderTakeProfit();

sellsl = OrderStopLoss();

selllots = OrderLots();}

}

//sellstop details

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


sellstop = OrderTicket();

}

}

double lotsb = DoubleToStr(buylots*1.5,2);

double lotss = DoubleToStr(selllots*1.5,2);

double accountplus2 = AccountProfit();

if(OrdersTotal() == 0){

OrderSend(Symbol(),OP_BUY,0.01,Ask,Slippage,Ask-0.003,Ask+0.002,"SELL_Double_Order_EA",MagicNumber,0,Green);

OrderSend(Symbol(),OP_SELLSTOP,0.02,Ask-0.001,Slippage,Ask+0.002,Ask-0.003,"SELL_Double_Order_EA",MagicNumber,0,Blue);

}

double pending =0;

if(buyticket < 1 && sellticket < 1){

pending = 1;

 OrderDelete(buystop);

 OrderDelete(sellstop);

}

double pending1 =0;

if(buystop < 1 && sellstop < 1){

pending1 = 1;

if(selldate > buydate){

OrderSend(Symbol(),OP_BUYSTOP,lotss,lastbuy,Slippage,buysl,buytp,"SELL_Double_Order_EA",MagicNumber,0,Blue);

}

if(buydate > selldate){

OrderSend(Symbol(),OP_SELLSTOP,lotsb,lastsell,Slippage,sellsl,selltp,"SELL_Double_Order_EA",MagicNumber,0,Blue);

}

}


Comment(" Account Balance ", AccountBalance() ," Equity " , AccountEquity(), " speard ", speared," ask ",Ask,

    "\n boy order ", buyticket, " last buy price " ,lastbuy , " buy target ", buytarget," buy profit ",buyprofit," buy date ",buydate, 


    "\n sell order ",sellticket, " last sell price ",lastsell,"sell target", selltarget," sell profit ",sellprofit," sell date ",selldate, " aplus2 ",accountplus2, " order count ",OrdersTotal(),

    "\n buystop ",buystop , " sellstop ",sellstop, " pending ",pending , " buytp ",buytp, " sell tp ",selltp,

    "\n buylots ",buylots," lotsb ", lotsb," buysl ",buysl, " selllots ",selllots," lotss ", lotss," sell sl ",sellsl);

//-----------------------------------

  }


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

}

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


Wednesday, July 27, 2022

Double Order same time

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

This EA place One BUT and One SELL same time. Before use on real account please test on demo account first. Profit-Loss your own responsitivy .


//|                                                 double-order.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 Slippage=3;


void OnTick()

  {

//---

double lotmax = AccountEquity()/1000;

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

double open = 0;

if (Lots > lotmax){

   Alert(" Lotsize more then fund Please set lot size less then " , lotmax); } 

   else{ open = Ask; }



double buyticket = 0;

double lastbuy = 0;

double buytarget = 0;

double buyprofit = 0;

datetime buydate = 0;

double sellticket = 0;

double lastsell = 0;

double selltarget = 0;

double sellprofit = 0;

datetime selldate = 0;

double accountplus1 = AccountBalance()+0.10,Digits;

double askplus1 = Ask+TakeProfit*Point;


int result=0;    

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

      {

if(OrderSelect(cntl,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()== MagicNumber){


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

buyticket = OrderTicket();

lastbuy = OrderOpenPrice();

buydate = OrderOpenTime();

buyprofit = OrderProfit();

buytarget = lastbuy+(TakeProfit*Point);}



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

sellticket = OrderTicket();

lastsell = OrderOpenPrice();

selldate = OrderOpenTime();

sellprofit = OrderProfit();

selltarget = lastsell-(TakeProfit*Point);}

        


        }

}

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

double exit = 0;

 

 if(sellprofit > 0 && High[1] < Close[0] && selltarget >= Close[0]){Alert("Close sell and open send new order");

  

  if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

  { exit = 1;

  result=OrderClose(sellticket,OrderLots(),Ask,Slippage,Red);

  if(result > 0){

   OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"SELL_Double_Order_EA",MagicNumber,0,Green);

   OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"SELL_Double_Order_EA",MagicNumber,0,Green);

   }

   }

   

 }

   if(buyprofit > 0  &&  Low[1] > Close[0] && buytarget <= Close[0]){Alert("Close buy and open send new order");

   exit = 2;

   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

  { result=OrderClose(buyticket,OrderLots(),Bid,Slippage,Green);

   if(result>0){

   OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"BUY_Double_Order_EA",MagicNumber,0,Yellow);

   OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"BUY_Double_Order_EA",MagicNumber,0,Yellow);

   }

   }

 }

   if(lastbuy == 0  &&  lastsell == 0 && OrdersTotal() == 0){Alert("open send new order");

   exit=3;

   OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"open_send_new_order",MagicNumber,0,Blue);

   OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"open_send_new_order",MagicNumber,0,Blue);

  }

 // All close

 if((AccountEquity() > AccountBalance() && AccountEquity() > accountplus1 && High[1] < Close[0]) || (AccountEquity() > AccountBalance() &&  AccountEquity() > accountplus1 && Low[1] > Close[0])){Alert("All Close "); 

  if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

  { 

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

  }   

 }  

//----------------------------   

  

    Comment(" Account Balance ", AccountBalance() ," accountplus1 ",accountplus1, " Equity " , AccountEquity(), " lotmax ",lotmax, " open ",open , " speard ", speared," ask ",Ask," askplus1 ",askplus1," exit ", exit, 

    "\n boy order ", buyticket, " last buy price " ,lastbuy , " buy target ", buytarget," buy profit ",buyprofit," buy date ",buydate, 

    "\n sell order ",sellticket, " last sell price ",lastsell,"sell target", selltarget," sell profit ",sellprofit," sell date ",selldate);

}

Wednesday, July 20, 2022

Trend Line Indicator for mt4

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

//|                                                        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

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

//| Expert tick function                                             |

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

void OnTick()

  {

int CandlesOnChart=WindowFirstVisibleBar();

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

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


ObjectDelete("hightrend");

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

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

   

  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[0],Low[0]);

   

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

 

  };

  

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


Friday, July 8, 2022

buyticket under development mode

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


//|                                                  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=10001;


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(Close[0] > nmax){buyon = 1;}


double sellon = 0;


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


double buyticket = 0;


double sellticket = 0;

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

      {

      if(OrderSelect(cntl,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()== MagicNumber){


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


buyticket = OrderTicket();}

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


sellticket = OrderTicket();}

        


        }

}


double lotsize = OrderLots()+Lots;

double lot = 0;

if(lotsize > 0.00){lot = lotsize;}else{lot = Lots;}

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,lot,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,lot,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 ,"\n buy ticket " , buyticket, " sell ticket ", sellticket, " lot ", lot, " lotsize ", lotsize);




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




}