0 Views
0 Downloads
0 Favorites
HedgingEA
ÿþ//+------------------------------------------------------------------+

//|                                                    HedgingEA.mq5 |

//|                                  Copyright 2023, MetaQuotes Ltd. |

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

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

#property copyright "Müller Peter" // Copyright holder

#property link      "https://www.mql5.com/en/users/mullerp04/seller" // Link to seller's page

#property version   "1.00" // Version of the EA



#include <HedgeClass.mqh> // Include the custom HedgeClass header file



// Input parameters for the EA

input int OrderDistancePoints = 300; // Distance in points for placing orders

input int TPPoints = 80; // Take profit target in points

input double Startlotsize = 0.1; // Initial lot size for trades

input double Gainperlot = 10.0; // Gain per lot size



HedgingStrategy* Strat = new HedgingStrategy(); // Create an instance of HedgingStrategy



// Initialization function

int OnInit()

{

   return(INIT_SUCCEEDED); // Return initialization success

}



// Deinitialization function

void OnDeinit(const int reason)

{

   delete Strat; // Delete the HedgingStrategy instance

}



// OnTick function - executed on every tick

void OnTick()

{

   if(!IsMarketOpen(_Symbol)) // If market is not open, return

      return;



   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Current bid price

   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Current ask price

   

   bool Breakout = HugeCandle(_Symbol, PERIOD_CURRENT); // Check for a huge candle breakout



   // If strategy is not running and there's a breakout, set parameters and run the strategy

   if(!Strat.Running() && Breakout)

   {

      // Set strategy parameters based on input values

      Strat.SetParameters(TPPoints * _Point, Startlotsize, Gainperlot, ask + OrderDistancePoints * _Point, ask - OrderDistancePoints * _Point);

      Strat.Run(); // Start the strategy

   }



   Strat.onTick(); // Call strategy's OnTick function



   // Conditions for opening new positions from inside the range

   bool EnoughDistance = Strat.LastLongPrice() - Strat.LastShortPrice() > 2.5 * OrderDistancePoints * _Point; // Check if there's enough distance between last long and short prices

   bool BothSides = Strat.LongVolume() && Strat.ShortVolume() && (Strat.LongVolume() + Strat.ShortVolume()) < 3 * Startlotsize; // Check if there are positions on both sides within a certain volume range

   bool Between = bid > Strat.LastShortPrice() + OrderDistancePoints * _Point && bid < Strat.LastLongPrice() - OrderDistancePoints * _Point; // Check if current bid price is between last short and long prices



   // If all conditions are met, build positions from the inside

   if(EnoughDistance && BothSides && Between && Breakout)

   {

      // Calculate volume based on distance and target profit

      double Vol = RoundtoLots(MathMin(ask - Strat.LastShortPrice(), Strat.LastLongPrice() - bid) / Strat.TargetProfit() * MathMin(Strat.LongVolume(), Strat.ShortVolume()));

      // Build positions from inside the range

      Strat.BuildFromTheInside(Vol, bid + OrderDistancePoints * _Point, bid - OrderDistancePoints * _Point);

   }



   // If equity to balance ratio is less than 0.8, stop the strategy

   if(AccountInfoDouble(ACCOUNT_EQUITY) / AccountInfoDouble(ACCOUNT_BALANCE) < 0.8)

      Strat.Stop(); // Stop the strategy

}

Comments