The Manan (satyavir sachin)

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

//|                                                    The Manan.mq5 |

//|                                  Copyright 2023, MetaQuotes Ltd. |

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

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

#property copyright "Copyright 2023, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"

#include <Trade\Trade.mqh>

CTrade trade;

input string TradeStartTime = "00:00";

input string TradeStopTime = "13:25";

input int MagicNumber=12345;

input double rangeround=5;

input double TradeDistance=1;

input double Buylotsize=0.01;

input double Selllotsize=0.01;

input double Buylotsize_secondary=0.01;

input double Selllotsize_secondary=0.01;

input int minutesToAdd=10;

input double MaxProfitStartegyWise=10;

input double MaxTradeCycle=5;

int tradecount =0;

datetime currentTime,newTime=NULL;

int count=0;

bool runOnce=false,first=false;

double TradePrice=0,MemoryPrice=0,buylot,selllot,buylot_sec,selllot_sec,rounded_val,running_pnl;

bool onlyonce=false,EAActivated=false;

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

//| Expert initialization function                                   |

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

double GetPnLByMagicNumber(int magic_number)

  {

   double total_pnl = 0.0;

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

     {

      ulong ticket = PositionGetTicket(i);

      int position_magic = (int)PositionGetInteger(POSITION_MAGIC);

      if(position_magic == magic_number)

        {

         total_pnl += PositionGetDouble(POSITION_PROFIT);

        }


     }


   return total_pnl;

  }

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

//|                                                                  |

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

int OnInit()

  {

   buylot=Buylotsize;

   selllot=Selllotsize;

   buylot_sec=Buylotsize_secondary;

   selllot_sec=Selllotsize_secondary;

   return(INIT_SUCCEEDED);

  }

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

//|                                                                  |

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

double RoundPrice(double price, double roundTo)

  {

   double remainder = fmod(price, roundTo);

   double roundedPrice = price - remainder;

   if(remainder >= roundTo / 2)

      roundedPrice += roundTo;


   return roundedPrice;

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---


  }

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

//|                                                                  |

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

bool HasDayChanged()

  {

   static datetime lastDay = 0; // Stores the last recorded day


   datetime currentDay = TimeCurrent(); // Get the current server time

   datetime currentDayOnly = currentDay - (currentDay % 86400); // Strip time to get only the date


   if(lastDay != currentDayOnly)

     {

      lastDay = currentDayOnly; // Update last recorded day to current day

      return true; // Day has changed

     }


   return false; // Day has not changed

  }


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

//| Expert tick function                                             |

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

void OnTick()

  {



   Comment("Trade Count: "+tradecount);


   datetime current_time = TimeCurrent();

   datetime start_time = StringToTime(TradeStartTime);

   datetime stop_time = StringToTime(TradeStopTime);


   if(current_time >= start_time && current_time <= stop_time)

     {

      EAActivated = true;

     }

   else

     {

      EAActivated = false;

     }



   if(

      HasDayChanged()==true&&

      EAActivated == true

   )

     {


      tradecount =0;

      runOnce=false;

      newTime=0;

     }


   currentTime= TimeCurrent();

   running_pnl=GetPnLByMagicNumber(MagicNumber);


   if(running_pnl>=MaxProfitStartegyWise)

     {


      Alert("running_pnl: "+running_pnl+"Symbol: "+Symbol());

      close_sell_position();

      close_buy_position();

      tradecount =0;

      newTime = currentTime + minutesToAdd * 60;

     }


   double  ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

   double  bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

   double   ltp=iClose(Symbol(),PERIOD_CURRENT,0);


   if(runOnce==false&&currentTime>=newTime&&EAActivated == true)

     {

      runOnce=true;

      if(onlyonce==false)

        {

         onlyonce=true;

         rounded_val= RoundPrice(ltp,  rangeround);

         TradePrice=rounded_val;

         MemoryPrice=rounded_val;

        }

      if(onlyonce==true)

        {

         TradePrice=ltp;

         MemoryPrice=ltp;

        }


      newTime=NULL;

     }




   if(ltp<=TradePrice&&TradePrice>0&&runOnce==true&&first==false&&EAActivated == true)

     {


      TradePrice=ltp-TradeDistance;

      first=true;

      Alert("Open buy trade @ "+Symbol()+"Lotsize"+buylot+"Open Sell trade @ "+Symbol()+" Lotsize: "+selllot);

      trade.SetExpertMagicNumber(MagicNumber);

      trade.Buy(buylot,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits),0,0,"AlgosysGridEA- StepMode");

      count++;

      trade.SetExpertMagicNumber(MagicNumber);

      trade.Sell(selllot,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits),0,0,"Programetix strategy");

      tradecount ++;

     }



   if(ltp<=TradePrice&&TradePrice>0&&runOnce==true&&first==true  && tradecount<MaxTradeCycle&&EAActivated == true)

     {

      TradePrice=ltp-TradeDistance;


      Alert("Close Previous Sell trade@: "+Symbol()+"Lot: "+ selllot);

      close_sell_position();


      Alert("Open buy trade @ "+Symbol()+"Lotsize"+buylot+"Open Sell trade @ "+Symbol()+" Lotsize: "+selllot);

      count++;


      trade.SetExpertMagicNumber(MagicNumber);

      trade.Buy(buylot_sec,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits),0,0,"AlgosysGridEA- StepMode");


      trade.SetExpertMagicNumber(MagicNumber);

      trade.Sell(selllot_sec,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits),0,0,"Programetix strategy");


      tradecount ++;

     }



//if(ltp>=MemoryPrice&&MemoryPrice>0&&runOnce==true&&first==true &&count>1&&newTime==NULL)

//  {

//    Alert("Closing all position Price reached Initial trade price @: "+Symbol());

//   close_sell_position();

//    close_buy_position();

//   TradePrice=0;

//   MemoryPrice=0;

//   newTime = currentTime + minutesToAdd * 60;

//   Alert("Strategy will take trade after "+newTime+" @ : "+Symbol());

//  }


   if(currentTime>=newTime&&newTime!=NULL)

     {

      runOnce=false;

      first=false;

      TradePrice=0;

      MemoryPrice=0;

      buylot=Buylotsize;

      selllot=Selllotsize;

      buylot_sec=Buylotsize_secondary;

      selllot_sec=Selllotsize_secondary;

      Alert("Strategy will take trade now @ "+Symbol());

      count=0;

     }



  }

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

void close_sell_position()

  {

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

     {

      string currencypair=PositionGetSymbol(i);

      int position_direction=PositionGetInteger(POSITION_TYPE);

      int posmagic=PositionGetInteger(POSITION_MAGIC);

      Alert("posmagic: "+posmagic);

      Alert("MagicNumber: "+MagicNumber);

      Alert("position_direction: "+position_direction);


      if(currencypair==Symbol() && position_direction == POSITION_TYPE_SELL&& int(posmagic) == int(MagicNumber))

        {

         Alert("MagicNumber: reaching");

         ulong ticket=PositionGetTicket(i);

         trade.PositionClose(ticket);

        }

     }


  }



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

//|                                                                  |

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

void close_buy_position()

  {

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

     {

      string currencypair=PositionGetSymbol(i);

      int position_direction=PositionGetInteger(POSITION_TYPE);

      int posmagic=PositionGetInteger(POSITION_MAGIC);

      Alert("posmagic: "+posmagic);


      Alert("MagicNumber: "+MagicNumber);

      Alert("position_direction: "+position_direction);

      if(currencypair==Symbol() && position_direction == POSITION_TYPE_BUY && int(posmagic) == int(MagicNumber)

        )

        {

         Alert("MagicNumber: reaching");

         ulong ticket=PositionGetTicket(i);

         trade.PositionClose(ticket);

        }

     }

  }

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


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


Comments

Popular posts from this blog

MQL5 : Add time to current time in mins

MQL5 : Get current trading session