292 lines
11 KiB
MQL5
//+------------------------------------------------------------------+
| |||
//| SamRobot.mq5 |
| |||
//| Copyright 2026, SamRobot EA |
| |||
//| https://www.mql5.com |
| |||
//+------------------------------------------------------------------+
| |||
#property copyright "Copyright 2026"
| |||
#property link "https://www.mql5.com"
| |||
#property version "1.00"
| |||
#property strict
| |||
| |||
#include <Trade\Trade.mqh>
| |||
CTrade trade;
| |||
| |||
//--- Inputs
| |||
input group "--- General Settings ---"
| |||
input double InpLotSize = 0.01; // Lot Size (>= 0.01)
| |||
input int InpMagicNo = 123456; // Magic Number
| |||
| |||
input group "--- Strategy Selection ---"
| |||
input bool EnableStrategy1 = true; // Enable Strategy 1 (SamRobot SMC + Volume Profile)
| |||
input bool EnableStrategy2 = false; // Enable Strategy 2 (Pullback Strategy)
| |||
| |||
input group "--- Strategy 1 Settings (SamRobot) ---"
| |||
input int InpFastEMA = 9; // Fast EMA Period
| |||
input int InpSlowEMA = 21; // Slow EMA Period
| |||
input int InpRSIPeriod = 14; // RSI Period
| |||
input int InpStopLoss1 = 300; // Stop Loss in Points
| |||
input int InpTrailingStop = 50; // Trailing Stop in Points
| |||
input int InpTrailingStep = 30; // Trailing Step in Points
| |||
| |||
input group "--- Strategy 2 Settings (Pullback) ---"
| |||
input double InpEntryLinePrice = 0.00000; // Fixed Entry Line Price for Strategy 2
| |||
input int InpStopLoss2 = 1; // Pullback Stop Loss (1 point)
| |||
input int InpTakeProfit2 = 299; // Pullback Take Profit (299 points)
| |||
| |||
//--- Global Handles & Variables
| |||
int emaFastHandle, emaSlowHandle, rsiHandle;
| |||
datetime lastBarTime;
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Expert initialization function |
| |||
//+------------------------------------------------------------------+
| |||
int OnInit()
| |||
{
| |||
trade.SetExpertMagicNumber(InpMagicNo);
| |||
| |||
// Initialize Indicators
| |||
emaFastHandle = iMA(_Symbol, _Period, InpFastEMA, 0, MODE_EMA, PRICE_CLOSE);
| |||
emaSlowHandle = iMA(_Symbol, _Period, InpSlowEMA, 0, MODE_EMA, PRICE_CLOSE);
| |||
rsiHandle = iRSI(_Symbol, _Period, InpRSIPeriod, PRICE_CLOSE);
| |||
| |||
if(emaFastHandle == INVALID_HANDLE || emaSlowHandle == INVALID_HANDLE || rsiHandle == INVALID_HANDLE)
| |||
{
| |||
Print("Error initializing technical indicators.");
| |||
return(INIT_FAILED);
| |||
}
| |||
| |||
lastBarTime = 0;
| |||
Print("SamRobot Initialized Successfully.");
| |||
return(INIT_SUCCEEDED);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Expert deinitialization function |
| |||
//+------------------------------------------------------------------+
| |||
void OnDeinit(const int reason)
| |||
{
| |||
IndicatorRelease(emaFastHandle);
| |||
IndicatorRelease(emaSlowHandle);
| |||
IndicatorRelease(rsiHandle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Expert tick function |
| |||
//+------------------------------------------------------------------+
| |||
void OnTick()
| |||
{
| |||
// Check for new bar to avoid multiple triggers per candle
| |||
datetime currentBarTime = iTime(_Symbol, _Period, 0);
| |||
bool isNewBar = (currentBarTime != lastBarTime);
| |||
| |||
// --- EXECUTE STRATEGY 2 (Pullback Strategy) ---
| |||
if(EnableStrategy2)
| |||
{
| |||
CheckStrategy2();
| |||
}
| |||
| |||
// --- EXECUTE STRATEGY 1 (SamRobot SMC & Volume Profile) ---
| |||
if(EnableStrategy1 && isNewBar)
| |||
{
| |||
lastBarTime = currentBarTime;
| |||
CheckStrategy1();
| |||
}
| |||
| |||
// Manage Trailing Stop for Strategy 1 open positions
| |||
ManageTrailingStop();
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Strategy 1: SamRobot SMC, Volume Profile & EMAs |
| |||
//+------------------------------------------------------------------+
| |||
void CheckStrategy1()
| |||
{
| |||
// 1. Get Indicator Values
| |||
double emaFast[], emaSlow[], rsiValues[];
| |||
ArraySetAsSeries(emaFast, true);
| |||
ArraySetAsSeries(emaSlow, true);
| |||
ArraySetAsSeries(rsiValues, true);
| |||
| |||
CopyBuffer(emaFastHandle, 0, 0, 3, emaFast);
| |||
CopyBuffer(emaSlowHandle, 0, 0, 3, emaSlow);
| |||
CopyBuffer(rsiHandle, 0, 0, 3, rsiValues);
| |||
| |||
if(ArraySize(emaFast) < 3 || ArraySize(emaSlow) < 3 || ArraySize(rsiValues) < 3) return;
| |||
| |||
// 2. Volume Profile Key Levels Placeholder (POC, VAH, VAL calculation from previous day)
| |||
double poc, vah, val;
| |||
CalculateVolumeProfileLevels(poc, vah, val);
| |||
| |||
// 3. Market Structure & SMC Check (BOS, ChoCH, FVG, Liquidity Sweep simulation)
| |||
bool bullishStructure = CheckMarketStructure(true);
| |||
bool bearishStructure = CheckMarketStructure(false);
| |||
| |||
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
| |||
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
| |||
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
| |||
| |||
// --- Buy Conditions ---
| |||
// Checks: EMA trend confirmation, RSI bullish, price near Value Area / POC, SMC Bullish confirmation
| |||
if(emaFast[0] > emaSlow[0] && rsiValues[0] > 50 && bullishStructure)
| |||
{
| |||
if(PositionsTotal() == 0)
| |||
{
| |||
double sl = bid - (InpStopLoss1 * point);
| |||
double tp = 0; // No take profit specified for Strategy 1
| |||
| |||
trade.Buy(InpLotSize, _Symbol, ask, sl, tp, "SamRobot Buy (SMC)");
| |||
}
| |||
}
| |||
| |||
// --- Sell Conditions ---
| |||
if(emaFast[0] < emaSlow[0] && rsiValues[0] < 50 && bearishStructure)
| |||
{
| |||
if(PositionsTotal() == 0)
| |||
{
| |||
double sl = ask + (InpStopLoss1 * point);
| |||
double tp = 0; // No take profit specified for Strategy 1
| |||
| |||
trade.Sell(InpLotSize, _Symbol, bid, sl, tp, "SamRobot Sell (SMC)");
| |||
}
| |||
}
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Strategy 2: Pullback Immediate Execution |
| |||
//+------------------------------------------------------------------+
| |||
void CheckStrategy2()
| |||
{
| |||
if(InpEntryLinePrice <= 0) return;
| |||
| |||
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
| |||
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
| |||
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
| |||
| |||
// Check if market passes entry line by exactly 1 point
| |||
// For Buy: Market passes above entry line by 1 point
| |||
// For Sell: Market passes below entry line by 1 point
| |||
| |||
double triggerDistance = 1.0 * point;
| |||
| |||
if(PositionsTotal() == 0)
| |||
{
| |||
// Buy trigger: Price crosses above EntryLine + 1 point
| |||
if(bid >= (InpEntryLinePrice + triggerDistance) && bid < (InpEntryLinePrice + triggerDistance + (2 * point)))
| |||
{
| |||
double sl = ask - (InpStopLoss2 * point); // 1 point SL
| |||
double tp = ask + (InpTakeProfit2 * point); // 299 points TP
| |||
trade.Buy(InpLotSize, _Symbol, ask, sl, tp, "SamRobot Strategy 2 Buy");
| |||
}
| |||
| |||
// Sell trigger: Price crosses below EntryLine - 1 point
| |||
if(ask <= (InpEntryLinePrice - triggerDistance) && ask > (InpEntryLinePrice - triggerDistance - (2 * point)))
| |||
{
| |||
double sl = bid + (InpStopLoss2 * point); // 1 point SL
| |||
double tp = bid - (InpTakeProfit2 * point); // 299 points TP
| |||
trade.Sell(InpLotSize, _Symbol, bid, sl, tp, "SamRobot Strategy 2 Sell");
| |||
}
| |||
}
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Helper: Calculate Volume Profile (POC, VAH, VAL) |
| |||
//+------------------------------------------------------------------+
| |||
void CalculateVolumeProfileLevels(double &poc, double &vah, double &val)
| |||
{
| |||
// Basic approximation using previous day high/low/close distribution
| |||
MqlRates rates[];
| |||
ArraySetAsSeries(rates, true);
| |||
int copied = CopyRates(_Symbol, PERIOD_D1, 1, 1, rates);
| |||
| |||
if(copied > 0)
| |||
{
| |||
double high = rates[0].high;
| |||
double low = rates[0].low;
| |||
double close = rates[0].close;
| |||
| |||
poc = (high + low + close) / 3.0; // Estimated POC (Pivot)
| |||
vah = poc + (high - poc) * 0.7; // Value Area High estimation
| |||
val = poc - (poc - low) * 0.7; // Value Area Low estimation
| |||
}
| |||
else
| |||
{
| |||
poc = 0; vah = 0; val = 0;
| |||
}
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Helper: Market Structure & SMC Logic (ChoCH, BOS, FVG) |
| |||
//+------------------------------------------------------------------+
| |||
bool CheckMarketStructure(bool isBullish)
| |||
{
| |||
// Simplified structure validation wrapper checking higher high / lower low framework
| |||
MqlRates rates[];
| |||
ArraySetAsSeries(rates, true);
| |||
if(CopyRates(_Symbol, _Period, 0, 5, rates) < 5) return false;
| |||
| |||
if(isBullish)
| |||
{
| |||
// Higher High and Higher Low validation + FVG presence check
| |||
bool higherHigh = (rates[0].high > rates[1].high && rates[1].high > rates[2].high);
| |||
bool higherLow = (rates[0].low > rates[1].low);
| |||
return (higherHigh || higherLow);
| |||
}
| |||
else
| |||
{
| |||
// Lower Low and Lower High validation
| |||
bool lowerLow = (rates[0].low < rates[1].low && rates[1].low < rates[2].low);
| |||
bool lowerHigh = (rates[0].high < rates[1].high);
| |||
return (lowerLow || lowerHigh);
| |||
}
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Trailing Stop Management for Strategy 1 |
| |||
//+------------------------------------------------------------------+
| |||
void ManageTrailingStop()
| |||
{
| |||
if(InpTrailingStop <= 0) return;
| |||
| |||
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
| |||
| |||
for(int i = PositionsTotal() - 1; i >= 0; i--)
| |||
{
| |||
ulong ticket = PositionGetTicket(i);
| |||
if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNo)
| |||
{
| |||
// Check if it's Strategy 1 (we identify by checking if TP is 0)
| |||
if(PositionGetDouble(POSITION_TP) == 0)
| |||
{
| |||
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
| |||
double currentSL = PositionGetDouble(POSITION_SL);
| |||
long type = PositionGetInteger(POSITION_TYPE);
| |||
| |||
if(type == POSITION_TYPE_BUY)
| |||
{
| |||
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
| |||
if(bid - openPrice >= InpTrailingStop * point)
| |||
{
| |||
double newSL = bid - (InpTrailingStop * point);
| |||
if(newSL > currentSL + (InpTrailingStep * point) || currentSL == 0)
| |||
{
| |||
trade.PositionModify(ticket, newSL, 0);
| |||
}
| |||
}
| |||
}
| |||
else if(type == POSITION_TYPE_SELL)
| |||
{
| |||
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
| |||
if(openPrice - ask >= InpTrailingStop * point)
| |||
{
| |||
double newSL = ask + (InpTrailingStop * point);
| |||
if(newSL < currentSL - (InpTrailingStep * point) || currentSL == 0)
| |||
{
| |||
trade.PositionModify(ticket, newSL, 0);
| |||
}
| |||
}
| |||
}
| |||
}
| |||
}
| |||
}
| |||
}
| |||
//+------------------------------------------------------------------+
|