Manan The Power (satyavir)

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

//|                                                    10 ka dum.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 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;

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;


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

//|                                                                  |

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

double GetPnLByMagicNumber(int magic_number)

  {

   double total_pnl = 0.0;


// Loop through all open positions

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

     {

      // Get the position ticket by index

      ulong ticket = PositionGetTicket(i);


      // Select the position by ticket


      // Get the magic number of the position

      int position_magic = (int)PositionGetInteger(POSITION_MAGIC);


      // Check if the magic number matches

      if(position_magic == magic_number)

        {

         // Add the profit of the position to the total PnL

         total_pnl += PositionGetDouble(POSITION_PROFIT);

        }


     }


   return total_pnl;

  }



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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   buylot=Buylotsize;

   selllot=Selllotsize;

   buylot_sec=Buylotsize_secondary;

   selllot_sec=Selllotsize_secondary;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---


  }

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

//| Expert tick function                                             |

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

double RoundPrice(double price, double roundTo)

  {

// Calculate the modulus

   double remainder = fmod(price, roundTo);


// Calculate the rounded price

   double roundedPrice = price - remainder;


// Adjust if rounding up is needed

   if(remainder >= roundTo / 2)

      roundedPrice += roundTo;


   return roundedPrice;

  }

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

//|                                                                  |

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

void OnTick()

  {


   currentTime= TimeCurrent();

   running_pnl=GetPnLByMagicNumber(MagicNumber);


   Comment("TradePrice: "+TradePrice+"\n\nMemoryPrice: "+MemoryPrice+"\n\nStartegy trade: "+newTime+

           "\n\nrunOnce: "+runOnce+"\n\nfirst: "+first+"\n\nTradePrice: "+TradePrice+"\n\nMemoryPrice: "+MemoryPrice+"\n\nbuylot: "+buylot+"\n\nselllot: "+selllot+

           "\n\nbuylot_sec: "+buylot_sec+"\n\nselllot_sec: "+selllot_sec+"\n\nrunning_pnl: "+running_pnl);



   if(running_pnl>=MaxProfitStartegyWise)

     {


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

      close_sell_position();

      close_buy_position();

      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)

     {

      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)

     {


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


     }



   if(ltp<=TradePrice&&TradePrice>0&&runOnce==true&&first==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");



     }



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