ALGORITHMIC BUILDER
One rule. Five platforms.
The same spec compiles to a working expert for MT4, MT5, cTrader, TradingView, and MatchTrader. Smart trail, taken from APEXNEURO and tightened, only moves a stop deeper into profit. One position. No grid. Read the annotated example.
Long and short on H1 when the 20 EMA crosses the 50. Risk 1% . Stop 1.6 ATR. Target 2R. At most 3 fills a day. Smart trail arms at 0.75R and keeps 0.55 of the gain.
- One position
- Fixed fraction
- Stop and target
- 3 fills a day
- Smart trail
- All sessions
- Spread unchecked
ATELIER
Sessiondrift.mq5
// QUANTNEXUS × FLOWNEXUSFORGE × SENTRA APEX.
One position. Fixed fraction of equity. Hard stop. No grid. No martingale.
This file trades only inside the platform you attach it to. It is not advice.
// EMA 20 crossing EMA 50 on the closed bar.
#property copyright "FLOWNEXUSFORGE"
#property version "1.00"
#include <Trade/Trade.mqh>
input double RiskPercent = 1;
input int AtrPeriod = 14;
input double AtrMult = 1.6;
input double RewardR = 2;
input int FastPeriod = 20;
input int SlowPeriod = 50;
input int RsiPeriod = 14;
input double RsiLo = 30;
input double RsiHi = 70;
input int Lookback = 20;
input int Slippage = 3;
input int Magic = 58021;
input bool UseSession = false;
input int SessionFrom = 7;
input int SessionTo = 16;
input int MaxSpread = 0;
input bool UseBreakeven = false;
input bool UseTrail = true;
input double TrailArm = 0.75;
input double TrailRetain = 0.55;
input int TrailStep = 2;
input int MaxPerDay = 3;
// Attach this expert to a H1 chart.
CTrade trade;
int fastHandle = INVALID_HANDLE;
int slowHandle = INVALID_HANDLE;
int rsiHandle = INVALID_HANDLE;
int atrHandle = INVALID_HANDLE;
datetime barTime = 0;
string dayKey = "";
int fillsToday = 0;
double Buf(int handle, int shift) {
double v[];
ArraySetAsSeries(v, true);
if (CopyBuffer(handle, 0, shift, 1, v) != 1) return 0;
return v[0];
}
double High(int start, int count, bool wantHigh) {
double best = wantHigh ? -DBL_MAX : DBL_MAX;
for (int i = start; i < start + count; i++) {
double px = wantHigh ? iHigh(_Symbol, PERIOD_CURRENT, i) : iLow(_Symbol, PERIOD_CURRENT, i);
if (wantHigh && px > best) best = px;
if (!wantHigh && px < best) best = px;
}
return best;
}
double Low(int start, int count, bool unused) {
return High(start, count, false);
if (unused) return 0;
}
int OnInit() {
trade.SetExpertMagicNumber(Magic);
trade.SetDeviationInPoints(Slippage);
atrHandle = iATR(_Symbol, PERIOD_CURRENT, AtrPeriod);
if (atrHandle == INVALID_HANDLE) return INIT_FAILED;
barTime = iTime(_Symbol, PERIOD_CURRENT, 0);
fastHandle = iMA(_Symbol, PERIOD_CURRENT, FastPeriod, 0, MODE_EMA, PRICE_CLOSE);
slowHandle = iMA(_Symbol, PERIOD_CURRENT, SlowPeriod, 0, MODE_EMA, PRICE_CLOSE);
return (fastHandle != INVALID_HANDLE && slowHandle != INVALID_HANDLE) ? INIT_SUCCEEDED : INIT_FAILED;
}
void OnDeinit(const int reason) {
if (fastHandle != INVALID_HANDLE) IndicatorRelease(fastHandle);
if (slowHandle != INVALID_HANDLE) IndicatorRelease(slowHandle);
if (rsiHandle != INVALID_HANDLE) IndicatorRelease(rsiHandle);
if (atrHandle != INVALID_HANDLE) IndicatorRelease(atrHandle);
}
bool InSession() {
if (!UseSession || SessionFrom == SessionTo) return true;
MqlDateTime dt;
TimeToStruct(TimeGMT(), dt);
if (SessionFrom < SessionTo) return dt.hour >= SessionFrom && dt.hour < SessionTo;
return dt.hour >= SessionFrom || dt.hour < SessionTo;
}
bool HasPosition() {
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if (!PositionSelectByTicket(ticket)) continue;
if (PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == Magic)
return true;
}
return false;
}
double LotsFor(double stopDist) {
double tick = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double value = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
if (tick <= 0 || value <= 0 || stopDist <= 0) return 0;
double riskCash = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100.0;
double lots = riskCash / ((stopDist / tick) * value);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
if (step <= 0) step = 0.01;
lots = MathFloor(lots / step) * step;
if (lots < minLot || lots > maxLot) return 0;
return NormalizeDouble(lots, 2);
}
bool RoomToday() {
string key = TimeToString(TimeCurrent(), TIME_DATE);
if (key != dayKey) { dayKey = key; fillsToday = 0; }
return fillsToday < MaxPerDay;
}
double SentraTrail(int side, double entry, double oldStop, double market, double best, double risk, double tick) {
if ((side != 1 && side != -1) || entry <= 0 || market <= 0 || best <= 0 || risk <= 0 || tick <= 0) return 0;
double excursion = side * (best - entry);
if (excursion < TrailArm * risk) return 0;
double share = MathMax(0.25, MathMin(0.88, TrailRetain + (excursion >= 2.0 * risk ? 0.08 : 0.0)));
double gain = MathMax(tick, share * excursion);
double edge = market - side * tick;
double raw = entry + side * gain;
raw = side > 0 ? MathMin(raw, edge) : MathMax(raw, edge);
double next = tick * (side > 0 ? MathFloor(raw / tick + 1e-9) : MathCeil(raw / tick - 1e-9));
double eps = tick * 1e-7;
if (next <= 0 || side * (next - entry) + eps < tick || side * (market - next) + eps < tick) return 0;
if (oldStop > 0 && side * (next - oldStop) + eps < TrailStep * tick) return 0;
return next;
}
void Protect() {
if (!UseTrail && !UseBreakeven) return;
static ulong seen = 0;
static double extreme = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if (!PositionSelectByTicket(ticket)) continue;
if (PositionGetString(POSITION_SYMBOL) != _Symbol || PositionGetInteger(POSITION_MAGIC) != Magic) continue;
double open = PositionGetDouble(POSITION_PRICE_OPEN);
double sl = PositionGetDouble(POSITION_SL);
double tp = PositionGetDouble(POSITION_TP);
if (sl <= 0) continue;
int side = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? 1 : -1;
if (ticket != seen) { seen = ticket; extreme = open; }
double market = side > 0 ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
extreme = side > 0 ? MathMax(extreme, market) : MathMin(extreme, market);
double risk = MathAbs(open - sl);
double tick = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double next = 0;
if (UseTrail) next = SentraTrail(side, open, sl, market, extreme, risk, tick);
else if (side > 0 && market - open >= risk && sl < open) next = open;
else if (side < 0 && open - market >= risk && sl > open) next = open;
if (next > 0) trade.PositionModify(ticket, next, tp);
return;
}
}
void OnTick() {
Protect();
datetime now = iTime(_Symbol, PERIOD_CURRENT, 0);
if (now == barTime) return;
barTime = now;
if (!InSession() || !RoomToday()) return;
if (MaxSpread > 0 && SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) > MaxSpread) return;
if (HasPosition()) return;
double atr = Buf(atrHandle, 1);
double stopDist = atr * AtrMult;
long stops = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
double minDist = stops * _Point;
if (stopDist < minDist) stopDist = minDist;
double fast1 = Buf(fastHandle, 1), fast2 = Buf(fastHandle, 2);
double slow1 = Buf(slowHandle, 1), slow2 = Buf(slowHandle, 2);
bool longSig = fast2 <= slow2 && fast1 > slow1;
bool shortSig = fast2 >= slow2 && fast1 < slow1;
if (!longSig && !shortSig) return;
double lots = LotsFor(stopDist);
if (lots <= 0) return;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
if (longSig) { if (trade.Buy(lots, _Symbol, 0, ask - stopDist, ask + stopDist * RewardR, "SENTRA")) fillsToday++; }
else if (shortSig) { if (trade.Sell(lots, _Symbol, 0, bid + stopDist, bid - stopDist * RewardR, "SENTRA")) fillsToday++; }
}
- In MetaTrader 5, open File, then Open Data Folder, then MQL5, then Experts.
- Compile in MetaEditor. Allow algorithmic trading on the toolbar.
- Attach it to one chart. It refuses a second position on that symbol and magic number.
PROMPT
Ask with the rule in context.
Pick the context to attach. Grok is the only model this station calls. Gemini, ChatGPT, and Claude stay off until a key exists. Copy the API body or the context bundle if you want to paste the same note somewhere else.
# Rule Long and short on H1 when the 20 EMA crosses the 50. Risk 1% . Stop 1.6 ATR. Target 2R. At most 3 fills a day. Smart trail arms at 0.75R and keeps 0.55 of the gain. # mt5 source // QUANTNEXUS × FLOWNEXUSFORGE × SENTRA APEX. One position. Fixed fraction of equity. Hard stop. No grid. No martingale. This file trades only inside the platform you attach it to. It is not advice. // EMA 20 crossing EMA 50 on the closed bar. #property copyright "FLOWNEXUSFORGE" #property version "1.00" #include <Trade/Trade.mqh> input double RiskPercent = 1; input int AtrPeriod = 14; input double AtrMult = 1.6; input double RewardR = 2; input int FastPeriod = 20; input int SlowPeriod = 50; input int RsiPeriod = 14; input double RsiLo = 30; input double RsiHi = 70; input int Lookback = 20; input int Slippage = 3; input int Magic = 58021; input bool UseSession = false; input int SessionFrom = 7; input int SessionTo = 16; input int MaxSpread = 0; input bool UseBreakeven = false; input bool UseTrail = true; input double TrailArm = 0.75; input double TrailRetain = 0.55; input int TrailStep = 2; input int MaxPerDay = 3; // Attach this expert to a H1 chart. CTrade trade; int fastHandle = INVALID_HANDLE; int slowHandle = INVALID_HANDLE; int rsiHandle = INVALID_HANDLE; int atrHandle = I
The API body is what this page posts to Grok. The context bundle is the same notes, named, for you to paste elsewhere. Neither one leaves this browser until you send or copy it.