Halaman

Robot BBMA_Reversal_Continuation

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

//|                                              BBMA_Reversal_Continuation |

//|                        Copyright 2024, MetaQuotes Software Corp. |

//|                                       http://www.metaquotes.net/ |

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

#property strict


// Input parameters

input double Lots = 0.1;                   // Lot size

input int ReversalPeriod = 14;             // Period for reversal pattern detection

input int ContinuationPeriod = 14;         // Period for continuation pattern detection

input int BBand_Period = 20;               // Period for Bollinger Bands

input double BBand_Deviation = 2.0;        // Standard deviation for Bollinger Bands

input int TrailingStopPips = 50;           // Trailing stop in pips


// Indicator handles

int BBMA_Handle;

int BBand_Handle;


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

//| Expert initialization function                                   |

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

int OnInit()

  {

   // Initializing the BBMA indicator

   BBMA_Handle = iMA(NULL, 0, ReversalPeriod, 0, MODE_SMA, PRICE_CLOSE,0);

   if(BBMA_Handle == INVALID_HANDLE)

     {

      Print("Failed to initialize BBMA indicator! Error code:", GetLastError());

      return INIT_FAILED;

     }

   

   // Initializing the Bollinger Bands indicator

   BBand_Handle = iBands(NULL, 0, BBand_Period,20, BBand_Deviation, 2, PRICE_CLOSE,0);

   if(BBand_Handle == INVALID_HANDLE)

     {

      Print("Failed to initialize Bollinger Bands indicator! Error code:", GetLastError());

      return INIT_FAILED;

     }


   // Initialization succeeded

   Print("Initialization successful");

   return INIT_SUCCEEDED;

  }


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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

   // No need to release the indicator handle in MetaTrader 5

  }


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

//| Expert tick function                                             |

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

void OnTick()

  {

   // Calculate reversal pattern

   double reversalPattern = CalculatePattern(ReversalPeriod);

   // Calculate continuation pattern

   double continuationPattern = CalculatePattern(ContinuationPeriod);

   

   // Calculate Bollinger Bands values

   double upperBBand = iBands(NULL, 0, BBand_Period, BBand_Deviation, 0, PRICE_CLOSE, MODE_UPPER, 0);

   double lowerBBand = iBands(NULL, 0, BBand_Period, BBand_Deviation, 0, PRICE_CLOSE, MODE_LOWER, 0);

   double middleBBand = iBands(NULL, 0, BBand_Period, BBand_Deviation, 0, PRICE_CLOSE, MODE_MAIN, 0);


   Print("Reversal Pattern:", reversalPattern);

   Print("Continuation Pattern:", continuationPattern);

   Print("Upper Bollinger Band:", upperBBand);

   Print("Lower Bollinger Band:", lowerBBand);

   Print("Middle Bollinger Band:", middleBBand);


   // Check for reversal pattern

   if(reversalPattern > 0.0 && Close[0] > upperBBand)

     {

      // Open sell position

      double openPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

      ulong ticket = OrderSend(_Symbol, OP_SELL, Lots, openPrice, 3, 0, 0, "Sell Order", 0, TrailingStopPips * Point, clrRed);

      if(ticket > 0)

        {

         Print("Sell order opened at price:", openPrice);

        }

      else

        {

         Print("Failed to open sell order! Error code:", GetLastError());

        }

     }

   // Check for continuation pattern

   else if(continuationPattern > 0.0 && Close[0] < lowerBBand)

     {

      // Open buy position

      double openPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

      ulong ticket = OrderSend(_Symbol, OP_BUY, Lots, openPrice, 3, 0, 0, "Buy Order", 0, TrailingStopPips * Point, clrGreen);

      if(ticket > 0)

        {

         Print("Buy order opened at price:", openPrice);

        }

      else

        {

         Print("Failed to open buy order! Error code:", GetLastError());

        }

     }

  }


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

//| Function to calculate pattern based on given period              |

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

double CalculatePattern(int period)

  {

   double maCurrent = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_CLOSE,0);

   double maPrevious = iMA(NULL, 0, period, 1, MODE_SMA, PRICE_CLOSE,0);


   // Return 1 if current price is above the MA and previous price was below the MA (reversal pattern)

   if((Close[0] > maCurrent) && (Close[1] < maPrevious))

     {

      return 1.0;

     }

   // Return -1 if current price is below the MA and previous price was above the MA (continuation pattern)

   else if((Close[0] < maCurrent) && (Close[1] > maPrevious))

     {

      return -1.0;

     }

   else

     {

      return 0.0;

     }

  }


Tidak ada komentar:

Posting Komentar