Save this file as M1_CostAudit.mq4 in your MQL4\Experts directory.
Code: Select all
//+------------------------------------------------------------------+
//| M1_CostAudit.mq4 |
//| Quantitative Prototyping |
//+------------------------------------------------------------------+
#property copyright "Quantitative Insights"
#property link ""
#property version "1.00"
#property strict
//--- Input Parameters
input string Grp1 = "--- Primary Trigger Logic ---";
input int FastEMA = 9;
input int SlowEMA = 21;
input string Grp2 = "--- Execution Filters ---";
input bool UseHTFFilter = true;
input bool UseATRFilter = true;
input double MinATRPips = 3.0;
input string Grp3 = "--- Risk Management ---";
input double LotSize = 0.1;
input int MagicNumber = 100100;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Calculate Pip Size based on broker digit format
double pipSize = Point;
if(Digits == 3 || Digits == 5) pipSize = Point * 10;
// 1. Fetch Indicator Data
double fast0 = iMA(Symbol(), PERIOD_M1, FastEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
double fast1 = iMA(Symbol(), PERIOD_M1, FastEMA, 0, MODE_EMA, PRICE_CLOSE, 1);
double slow0 = iMA(Symbol(), PERIOD_M1, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
double slow1 = iMA(Symbol(), PERIOD_M1, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, 1);
double htfEMA = iMA(Symbol(), PERIOD_M15, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
double atr = iATR(Symbol(), PERIOD_M1, 14, 0);
// 2. Logic Compilation
bool longCross = (fast0 > slow0 && fast1 <= slow1);
bool shortCross = (fast0 < slow0 && fast1 >= slow1);
bool htfBullish = (Close[0] > htfEMA);
bool htfBearish = (Close[0] < htfEMA);
double atrInPips = atr / pipSize;
bool volatilitySufficient = (atrInPips >= MinATRPips);
bool validLongEnv = (!UseHTFFilter || htfBullish) && (!UseATRFilter || volatilitySufficient);
bool validShortEnv = (!UseHTFFilter || htfBearish) && (!UseATRFilter || volatilitySufficient);
// 3. Execution & Position Management
int openPositions = 0;
int orderType = -1;
int ticket = -1;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
openPositions++;
orderType = OrderType();
ticket = OrderTicket();
}
}
}
// Exit Logic (Reversion Cross)
if(openPositions > 0)
{
if(orderType == OP_BUY && shortCross)
bool closed = OrderClose(ticket, OrderLots(), Bid, 3, clrRed);
if(orderType == OP_SELL && longCross)
bool closed = OrderClose(ticket, OrderLots(), Ask, 3, clrBlue);
}
// Entry Logic
if(openPositions == 0)
{
if(longCross && validLongEnv)
int res = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "M1_Long", MagicNumber, 0, clrBlue);
if(shortCross && validShortEnv)
int res = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "M1_Short", MagicNumber, 0, clrRed);
}
}