In the sell branch, slProfit is indexed using the size of tpLoss and tpLoss using the size of slProfit. The former can exceed the one-element stop array; the latter restricts target selection to its first element.
Martingale / Anti-Hedging · Variant
A closely related version of the dual-grid source, retained separately because its array indexing differs.
How the source is structured
- Run simultaneous buy and sell grids with favorable and adverse spacing rules.
- Scale subsequent entries using the supplied volume arrays.
- Adjust basket exit levels and close tracked positions when exit deals arrive.
Default inputs
| Base volume | 0.1 lot |
|---|---|
| Favorable / adverse spacing | 1% / 3% |
| Largest volume multipliers | 32 favorable / 34 adverse |
| Hard basket exposure cap | Not implemented |
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 is not isolated by symbol or magic number.
Grid entries have no explicit total-exposure cap. This is a source variant of the other grid upload, not evidence of an independently validated strategy.
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 · 207 lines
#include <trade/trade.mqh>#include <arrays/arraylong.mqh>#include <arrays/arrayobj.mqh>enum ENUM_GRID_DIRECTION { GRID_BUY, GRID_SELL};enum ENUM_GRID_MODE { GRID_NEUTRAL, GRID_PROFIT, GRID_LOSS };class CGrid : public CObject {private: ENUM_GRID_DIRECTION dir; ENUM_GRID_MODE mode; CArrayLong tickets; double last; public: CGrid(ENUM_GRID_DIRECTION direction) : dir(direction) { mode = GRID_NEUTRAL; }; ~CGrid() {}; 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){ execute(); }else{ 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){ 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){ 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(tpLoss)-1,tickets.Total()-1)]/100; }else if(mode == GRID_LOSS){ tp = last - last*tpLoss[MathMin(ArraySize(slProfit)-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){ 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: void execute(){ CTrade trade; double lots = Lots; 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); } last = SymbolInfoDouble(_Symbol,SYMBOL_BID); ulong order = trade.ResultOrder(); if(order > 0) tickets.Add(order); }};input double Lots = 0.1;input double GridPercentProfit = 1;input double GridPercentLoss = 3;input bool IsChartComment = true;CGrid gridBuy(GRID_BUY);CGrid gridSell(GRID_SELL);double lotsProfit[] = {1,1,2,4,8,16,32};double lotsLoss[] = {1,1,2,3,5,8,13,21,34};double 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.