There is no explicit cap on grid entries or total exposure. Multipliers stop increasing at the final array value, but additional positions can still be attempted.
Hedging Grid: Martingale / Anti-Martingale
Runs separate buy and sell grids, with different volume sequences for favorable and adverse price moves.
How the source is structured
- Create both a buy grid and a sell grid and initiate each when its ticket list is empty.
- Add positions after a 1% favorable or 3% adverse move from the last execution reference.
- Apply volume multipliers and update the tracked basket's stop or target as deals arrive.
Default inputs
| Base volume | 0.1 lot |
|---|---|
| Favorable / adverse spacing | 1% / 3% |
| Favorable multipliers | 1, 1, 2, 4, 8, 16, 32 |
| Adverse multipliers | 1, 1, 2, 3, 5, 8, 13, 21, 34 |
Source-review notes
These observations come from reading this uploaded version. They are not compiler output or a complete code audit. The download retains the source as supplied.
Trade-transaction handling has no symbol or magic-number filter. Unrelated deals can affect basket management.
The design assumes independent long and short baskets; its behavior must be checked against the account's position model. It is not a loss-limiting hedge.
Dependencies
MetaTrader 5 standard Trade and Arrays libraries. Independent opposite positions are part of the intended design.
Source & attribution
No author or license declaration appears in the supplied source.
Included in the Edge Quant source collection with existing notices preserved. Free download does not imply a new license grant.
Inspect the original source
The browser preview is decoded for reading; the download keeps the original file bytes.
Expand full MQL5 source · 209 lines
//includes#include <trade/trade.mqh>#include <arrays/arrayobj.mqh>#include <arrays/arraylong.mqh>//enumerationsenum ENUM_GRID_DIRECTION { GRID_BUY, GRID_SELL};enum ENUM_GRID_MODE { GRID_NEUTRAL, GRID_PROFIT, GRID_LOSS};//grid classclass CGrid : public CObject {private://private variables ENUM_GRID_DIRECTION dir; //grid direction ENUM_GRID_MODE mode; //grid mode CArrayLong tickets; //array to store grid positions double last; //last execution price of gridpublic://constructor CGrid(ENUM_GRID_DIRECTION direction) : dir(direction) { mode = GRID_NEUTRAL; };//destructor ~CGrid() {};//public functions string ToString(){ string txt; StringConcatenate(txt,EnumToString(dir)," (",EnumToString(mode),") > Tickets: ",tickets.Total(),", Last: ",DoubleToString(last,2)); return txt; } void OnTickEvent(){ double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); if(tickets.Total() == 0){ //open first position if tickets array is empty execute(); }else{ //open more positions if next trigger level it hit if(dir == GRID_BUY){ if(mode == GRID_NEUTRAL || mode == GRID_PROFIT){ if(bid > last + last * GridPercentProfit / 100){ execute(); mode = GRID_PROFIT; } } if(mode == GRID_NEUTRAL || mode == GRID_LOSS){ if(bid < last - last * GridPercentLoss / 100){ execute(); mode = GRID_LOSS; } } } if(dir == GRID_SELL){ if(mode == GRID_NEUTRAL || mode == GRID_PROFIT){ if(bid < last - last * GridPercentProfit / 100){ execute(); mode = GRID_PROFIT; } } if(mode == GRID_NEUTRAL || mode == GRID_LOSS){ if(bid > last + last * GridPercentLoss / 100){ execute(); mode = GRID_LOSS; } } } } } void OnTradeTransactionEvent(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result){ //select deal if(trans.type == TRADE_TRANSACTION_DEAL_ADD){ if(HistorySelect(TimeCurrent()-100,TimeCurrent()+100)){ CDealInfo deal; deal.Ticket(trans.deal); if(deal.Entry() == DEAL_ENTRY_IN){ //modify tp or sl after entry deal if(mode != GRID_NEUTRAL){ double tp = 0; double sl = 0; if(deal.DealType() == DEAL_TYPE_BUY){ if(dir == GRID_SELL) return; if(mode == GRID_PROFIT){ sl = last - last * slProfit[MathMin(ArraySize(slProfit)-1,tickets.Total()-1)] / 100; }else if(mode == GRID_LOSS){ tp = last + last * tpLoss[MathMin(ArraySize(tpLoss)-1,tickets.Total()-1)] / 100; } }else if(deal.DealType() == DEAL_TYPE_SELL){ if(dir == GRID_BUY) return; if(mode == GRID_PROFIT){ sl = last + last * slProfit[MathMin(ArraySize(slProfit)-1,tickets.Total()-1)] / 100; }else if(mode == GRID_LOSS){ tp = last - last * tpLoss[MathMin(ArraySize(tpLoss)-1,tickets.Total()-1)] / 100; } } CTrade trade; for(int i = tickets.Total()-1; i >= 0; i--){ CPositionInfo pos; if(pos.SelectByTicket(tickets.At(i))){ trade.PositionModify(pos.Ticket(),sl,tp); } } } }else if(deal.Entry() == DEAL_ENTRY_OUT){ //remove ticket from tickets array, close all remaining positions and reset grid if all trades were closed if(deal.DealType() == DEAL_TYPE_BUY && dir == GRID_BUY) return; if(deal.DealType() == DEAL_TYPE_SELL && dir == GRID_SELL) return; CTrade trade; for(int i = tickets.Total()-1; i >= 0; i--){ CPositionInfo pos; if(pos.SelectByTicket(tickets.At(i))){ trade.PositionClose(pos.Ticket()); } if(tickets.At(i) == deal.PositionId()) tickets.Delete(i); } if(tickets.Total() == 0) mode = GRID_NEUTRAL; } } } } private: //private functions void execute(){ CTrade trade; double lots = Lots; //calculate lotsize for next position if(dir == GRID_BUY){ if(mode == GRID_PROFIT) lots *= lotsProfit[MathMin(ArraySize(lotsProfit)-1,tickets.Total())]; if(mode == GRID_LOSS) lots *= lotsLoss[MathMin(ArraySize(lotsLoss)-1,tickets.Total())]; trade.Buy(lots); } if(dir == GRID_SELL){ if(mode == GRID_PROFIT) lots *= lotsProfit[MathMin(ArraySize(lotsProfit)-1,tickets.Total())]; if(mode == GRID_LOSS) lots *= lotsLoss[MathMin(ArraySize(lotsLoss)-1,tickets.Total())]; trade.Sell(lots); } //update last grid exection price last = SymbolInfoDouble(_Symbol,SYMBOL_BID); //store new position ticket in tickets array ulong order = trade.ResultOrder(); if(order > 0) tickets.Add(order); }};//input variablesinput double Lots = 0.1;input double GridPercentProfit = 1;input double GridPercentLoss = 3;input bool IsChartComment = true;//global variables//grid objectsCGrid gridBuy(GRID_BUY);CGrid gridSell(GRID_SELL);//arrays for lot scalingdouble lotsProfit[] = {1,1,2,4,8,16,32};double lotsLoss[] = {1,1,2,3,5,8,13,21,34};//arrays for sl and tp leveldouble slProfit[] = {0.25};double tpLoss[] = {0.5,1.0,3.5,6.0,12.0};int OnInit(){ return(INIT_SUCCEEDED);}void OnDeinit(const int reason){}void OnTick(){ gridBuy.OnTickEvent(); gridSell.OnTickEvent();}void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result){ gridBuy.OnTradeTransactionEvent(trans,request,result); gridSell.OnTradeTransactionEvent(trans,request,result); if(IsChartComment){ Comment("\n\n",gridBuy.ToString(),"\n",gridSell.ToString()); }}Educational source material, not investment advice. No profitability, execution quality or live-readiness claim is made for this upload.