HedgeHog_1[1].0
Download scriptThe HedgeHog 1.0 script is an automated trading tool designed to manage market positions by executing simultaneous buy and sell orders while actively protecting profits through dynamic trailing mechanisms. Below is a breakdown of how the logic operates in plain language:
Trade Entry Logic
The script follows a specific set of rules before initiating any trades:
- Account Safety Check: Before opening a position, the script verifies that your account has sufficient free margin to cover the required costs. If the funds are insufficient, it will block the trade and log an error.
- Time-Based Execution: The primary trigger for the script is the start of a new trading day (based on the GMT offset provided by the user). At exactly midnight (00:00) according to your broker's server time, the script automatically opens two opposite positions: one "Buy" and one "Sell."
- Position Parameters: Every trade is assigned a unique identifier (Magic Number) based on the specific currency pair and timeframe. This ensures that the script only manages its own trades and doesn't interfere with other manual or automated orders.
Risk Management & Profit Protection
Once a trade is live, the script actively monitors it to lock in gains:
- Stop Loss (SL) and Take Profit (TP): Every trade is entered with predefined limits to protect against significant losses and to secure profits automatically once a certain target is reached.
- Trailing Stop Mechanism: This is one of the core features of the script. It moves the "Stop Loss" level in real-time as the price moves in your favor:
- Fixed Mode: The stop loss follows the price at a set distance.
- ATR Mode (Volatility Based): The stop loss uses the Average True Range (ATR)—a measure of market volatility—to calculate how far away the protection should be. This allows the script to give the trade more "breathing room" during volatile periods and tighten it up during stable periods.
- Sensitivity Filters: The script includes sensitivity settings to ensure that the trailing stop only moves when the price has moved significantly enough to justify a change, preventing the stop loss from moving too frequently due to minor market noise.
Technical Stability Features
To ensure smooth operation in a live trading environment, the script includes several "under-the-hood" safeguards:
- Execution Locking: It uses a "lock" system to prevent the script from trying to send multiple orders at the exact same microsecond, which can sometimes cause errors with certain brokers.
- Trade Delay: During live trading (non-testing mode), the script includes slight random delays before executing orders. This is often used to help navigate broker restrictions regarding high-frequency order execution.
- Multi-Pair Compatibility: The logic is designed to identify different currency pairs (like EURUSD, GBPUSD, GOLD, etc.) individually, allowing it to run on multiple charts simultaneously without confusing the data from one pair with another.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| HedgeHog 1.0.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#include <stdlib.mqh>
#include <WinUser32.mqh>
//---- input parameters
extern int BrokerOffsetToGMT = 0; // InterbankFX.com TimeZone GMT+0
// Change to your server's timezone (in GMT)
extern double Lots=1;
extern int TakeProfit=14;
extern int SL=50;
extern int TS_Mode=2; // 0 = disabled, 1 = Fixed SL, 2 = ATR
extern int TS_Trigger=5;
extern int TS_Sensitivity=5;
extern int ATR_Period=14;
string expert_name = "HedgeHog 1.0";
int bars_count = 0;
int magicnum;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
magicnum = 1000 + GetTimeframeConstant(Period()) + GetSymbolConstant(Symbol());
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
// control open orders
int ticket = OrderTicketByMagicNum(magicnum);
ControlTrailingStop(ticket);
// check for entry ( enough money > safe mode off > signal given > enter)
if (AccountFreeMargin()<(1000*Lots))
{
Print("Error: We don't have enough money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for begining of Day
if (TimeHour(Time[0])==0+BrokerOffsetToGMT && TimeMinute(Time[0])==0)
{
EnterSell();
EnterBuy();
}
return(0);
}
//+------------------------------------------------------------------+
//| Buy |
//+------------------------------------------------------------------+
int EnterBuy()
{
// Calculate true SL
double TrueSL;
double yesterdayclose = Close[1];
if (SL > 0) TrueSL = Ask-(SL*Point);
int ret = OrderSendEx(Symbol(), OP_BUY, Lots, yesterdayclose, TrueSL, Ask+TakeProfit*Point, expert_name + Symbol() + Period(), magicnum, 0, Yellow);
return(ret);
}
//+------------------------------------------------------------------+
//| Sell |
//+------------------------------------------------------------------+
int EnterSell()
{
// Calculate true SL
double TrueSL;
double yesterdayclose = Close[1];
if (SL > 0) TrueSL = Bid+(SL*Point);
// Send order
int ret = OrderSendEx(Symbol(), OP_SELL, Lots ,yesterdayclose, TrueSL, Bid-TakeProfit*Point, expert_name + Symbol() + Period(), magicnum, 0, Yellow);
return(ret);
}
//+------------------------------------------------------------------+
//| Control trailing stop |
//+------------------------------------------------------------------+
void ControlTrailingStop(int ticket)
{
if (ticket == 0 || TS_Mode == 0) return;
double ts;
if (OrderSelectEx(ticket, SELECT_BY_TICKET, MODE_TRADES)==false) return;
if (OrderType() == OP_BUY)
{
switch (TS_Mode)
{
case 1: ts = Bid-(Point*SL); break;
case 2: ts = Low[0] - (0.5 * iATR(NULL,0,ATR_Period,0)); break;
}
if ((ts >= OrderStopLoss() + TS_Sensitivity*Point) && (Bid >= OrderOpenPrice() + TS_Trigger*Point ))
{
OrderModify(ticket, OrderOpenPrice(), ts, OrderTakeProfit(), 0);
}
}else if(OrderType() == OP_SELL){
switch (TS_Mode)
{
case 1: ts = Ask+(Point*SL); break;
case 2: ts = High[0] + (0.5 * iATR(NULL,0,ATR_Period,0)); break;
}
if ((ts <= OrderStopLoss() - TS_Sensitivity*Point) && (Ask <= OrderOpenPrice() - TS_Trigger*Point))
{
OrderModify(ticket, OrderOpenPrice(), ts, OrderTakeProfit(), 0);
}
}
}
//+------------------------------------------------------------------+
//| Extended OrderSend() for used in multiple pairs |
//+------------------------------------------------------------------+
int OrderSendEx(string symbol, int cmd, double volume, double price, double stoploss, double takeprofit, string comment, int magic, datetime expiration=0, color arrow_color=CLR_NONE)
{
datetime OldCurTime;
int timeout=5;
if (!IsTesting()) {
MathSrand(LocalTime());
Sleep(MathRand()/6);
}
OldCurTime=CurTime();
while (GlobalVariableCheck("InTrade") && !IsTradeAllowed()) {
if(OldCurTime+timeout <= CurTime()) {
Print("Error in OrderSendEx(): Timeout encountered");
return(0);
}
Sleep(1000);
}
GlobalVariableSet("InTrade", CurTime()); // set lock indicator
int ticket = OrderSend(symbol, cmd, volume, price, 0, stoploss, takeprofit, comment, magic, expiration, arrow_color);
GlobalVariableDel("InTrade"); // clear lock indicator
return(ticket);
}
//+------------------------------------------------------------------+
//| Extended OrderSelect() |
//+------------------------------------------------------------------+
bool OrderSelectEx(int index, int select, int pool = MODE_TRADES)
{
if (OrderSelect(index,select,pool)==true)
{
return(true);
}else{
Print("Error: Order #", index ," cannot be selected.");
}
}
//+------------------------------------------------------------------+
//| Get order ticket by magic number |
//+------------------------------------------------------------------+
int OrderTicketByMagicNum(int magic_number) {
for(int i=0;i<OrdersTotal();i++)
{
if (OrderSelectEx(i, SELECT_BY_POS) == false) continue;
if (OrderMagicNumber() == magic_number) return(OrderTicket());
}
}
//+------------------------------------------------------------------+
//| Time frame interval appropriation function |
//+------------------------------------------------------------------+
int GetTimeframeConstant(int chart_period) {
switch(chart_period) {
case 1: // M1
return(50);
case 5: // M5
return(100);
case 15:
return(150);
case 30:
return(200);
case 60:
return(250);
case 240:
return(300);
case 1440:
return(350);
case 10080:
return(400);
case 43200:
return(450);
}
}
//+------------------------------------------------------------------+
//| Symbol to index |
//+------------------------------------------------------------------+
int GetSymbolConstant(string symbol) {
if(symbol=="EURUSD") { return(1);
} else if(symbol=="GBPUSD") { return(2);
} else if(symbol=="USDCHF") { return(3);
} else if(symbol=="USDJPY") { return(4);
} else if(symbol=="USDCAD") { return(5);
} else if(symbol=="AUDUSD") { return(6);
} else if(symbol=="CHFJPY") { return(7);
} else if(symbol=="EURAUD") { return(8);
} else if(symbol=="EURCAD") { return(9);
} else if(symbol=="EURCHF") { return(10);
} else if(symbol=="EURGBP") { return(11);
} else if(symbol=="EURJPY") { return(12);
} else if(symbol=="GBPCHF") { return(13);
} else if(symbol=="GBPJPY") { return(14);
} else if(symbol=="GOLD") { return(15);
} else {Print("Error: Unexpected symbol."); return(0);
}
}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments