This is a sample code provided by Forex Robot Easy to demonstrate how the Stop Loss Hedge System works. Please note that Forex Robot Easy is not the official developer of this product. To find the official developer of this product, please use MQL5.
For detailed reviews and trading results of this product, please visit Forex Robot Easy - Stop Loss Hedge System Review.
The Stop Loss Hedge System is an expert advisor that automates the process of minimizing trading risks through the use of a hedge trade. It monitors the profit/loss of the current position and triggers a hedge trade if the profit/loss falls below a certain threshold.
The necessary libraries and functions are included at the beginning of the code:
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
A constant HEDGE_PIPS
is defined to specify the number of pips required to trigger a hedge trade:
const int HEDGE_PIPS = 20;
The trade and position objects are initialized:
CTrade trade;
CPositionInfo position;
The CheckHedgeTrade()
function is responsible for checking if a hedge trade is needed. It refreshes the current position information using the Refresh()
function and checks if the position is long and below the hedge pips threshold. If the conditions are met, it opens a sell trade of the same lot size:
void CheckHedgeTrade()
{
if(!position.Refresh())
return;
if(position.PositionType() == POSITION_TYPE_BUY && position.Profit() < -HEDGE_PIPS)
{
trade.Sell(position.Volume());
}
}
The OnInit()
function is the entry point of the expert advisor. It initializes the trade and position objects, sets the deviation in pips for trade execution, and selects the currency pair:
int OnInit()
{
trade.SetDeviationInPips(5);
position.Select('EURUSD');
return(INIT_SUCCEEDED);
}
The OnTick()
function is the main loop of the expert advisor. It is executed on every tick. In this case, it checks if a hedge trade is needed by calling the CheckHedgeTrade()
function:
void OnTick()
{
CheckHedgeTrade();
}
The OnTradeError()
function is called when a trade error occurs. It simply prints the error code:
void OnTradeError(const int error)
{
Print('Trade error occurred: ', error);
}
The OnTimer()
function is called by the timer event and is used to optimize performance by limiting the execution frequency. In this case, it also checks if a hedge trade is needed by calling the CheckHedgeTrade()
function:
void OnTimer()
{
CheckHedgeTrade();
}
The OnDeinit()
function is called before the expert advisor is unloaded. It closes any open trades and prints a message indicating that the expert advisor has been unloaded:
void OnDeinit(const int reason)
{
trade.CloseAll();
Print('Expert advisor unloaded');
}
The Stop Loss Hedge System is an expert advisor designed to minimize trading risks by automatically triggering a hedge trade when the profit/loss of the current position falls below a certain threshold.
Key Features:
- Automatically monitors the profit/loss of the current position
- Triggers a hedge trade if the profit/loss falls below the specified threshold
- Supports any currency pair
- Allows customization of the hedge pips threshold and trade deviation
- Optimizes performance by limiting the execution frequency
Please note that this code is a sample provided by Forex Robot Easy and is not the official product. To find the official developer of this product, please use MQL5.
For detailed reviews and trading results of this product, please visit Forex Robot Easy - Stop Loss Hedge System Review.