Author: Müller Peter
0 Views
0 Downloads
0 Favorites
HedgingEA
ÿþ//+------------------------------------------------------------------+

//|                                                    HedgingEA.mq5 |

//|                                  Copyright 2023, MetaQuotes Ltd. |

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

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

#property copyright "Müller Peter"

#property link      "https://www.mql5.com/en/users/mullerp04/seller"

#property version   "1.00"



#include <HedgeClass.mqh> // Include the header file for the hedging strategy class



input int OrderDistancePoints = 300; // Distance in points to place orders

input int TPPoints = 250; // Take profit points

input double Startlotsize = 0.1; // Initial lot size

input double Gainperlot = 10.0; // Gain per lot



HedgingStrategy* Strat = new HedgingStrategy(); // Create a new instance of the HedgingStrategy class



// Initialization function

int OnInit()

{

   return(INIT_SUCCEEDED); // Return initialization success

}



// Deinitialization function

void OnDeinit(const int reason)

{

   delete Strat; // Delete the strategy instance

}



// Main function that runs on each tick

void OnTick()

{

   // Check if the market is open

   if(!IsMarketOpen(_Symbol))

      return;



   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get the current bid price

   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get the current ask price



   bool Breakout = HugeCandle(_Symbol,PERIOD_CURRENT); // Check for a breakout using the HugeCandle function



   // If the strategy is not running and there is a breakout, initialize and run the strategy

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

   {

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

      Strat.Run();

   }



   Strat.onTick(); // Update the strategy with the current tick data



   // Check if the conditions to build from the inside are met

   bool EnoughDistance = Strat.LastLongPrice() - Strat.LastShortPrice() > 2.5 * OrderDistancePoints * _Point;

   bool BothSides = Strat.LongVolume() && Strat.ShortVolume() && (Strat.LongVolume() + Strat.ShortVolume()) < 3 * Startlotsize;

   bool Between = bid > Strat.LastShortPrice() + OrderDistancePoints * _Point && bid < Strat.LastLongPrice() - OrderDistancePoints * _Point;



   // If conditions are met, build from the inside

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

   {

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

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

   }



   // Stop the strategy if account equity drops below 80% of the balance

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

      Strat.Stop();

}

Comments