// Supply and Demand Zone Expert Advisor
// Input parameters
input double LotSize = 0.01; // Trading lot size
input double StopLoss = 50; // Stop loss level in pips
input double TakeProfit = 100; // Take profit level in pips
input int MaxTrades = 5; // Maximum number of trades to open
// Global variables
int tradeCount = 0; // Number of trades opened
// Supply and Demand Zone parameters
input int SupplyDemandLookbackBars = 20; // Number of bars to look back for supply and demand zones
// Indicator buffers
double SupplyBuffer[];
double DemandBuffer[];
// Initialization function
void OnInit()
{
// Define the required indicator buffers
SetIndexBuffer(0, SupplyBuffer);
SetIndexBuffer(1, DemandBuffer);
}
// Trading function
void OnTick()
{
// Check if the maximum number of trades has been reached
if (tradeCount >= MaxTrades)
return;
// Check for the presence of a supply or demand zone
// If a supply zone is detected
if (CheckSupplyZone())
{
// Open a sell trade
SellTrade();
}
// If a demand zone is detected
if (CheckDemandZone())
{
// Open a buy trade
BuyTrade();
}
}
// Function to check for supply zone
bool CheckSupplyZone()
{
double highestHigh = 0;
// Iterate over the previous bars to find the highest high
for (int i = 1; i <= SupplyDemandLookbackBars; i++)
{
double high = High;
// Check if the current high is higher than the previous highest high
if (high > highestHigh)
{
highestHigh = high;
}
}
// Check if the current price is near the highest high
if (highestHigh > 0 && Bid <= highestHigh + (StopLoss * Point))
{
// Draw a supply zone arrow
SupplyBuffer[0] = highestHigh;
return true;
}
return false;
}
// Function to check for demand zone
bool CheckDemandZone()
{
double lowestLow = 0;
// Iterate over the previous bars to find the lowest low
for (int i = 1; i <= SupplyDemandLookbackBars; i++)
{
double low = Low;
// Check if the current low is lower than the previous lowest low
if (low < lowestLow || lowestLow == 0)
{
lowestLow = low;
}
}
// Check if the current price is near the lowest low
if (lowestLow > 0 && Ask >= lowestLow - (StopLoss * Point))
{
// Draw a demand zone arrow
DemandBuffer[0] = lowestLow;
return true;
}
return false;
}
// Function to open a sell trade
void SellTrade()
{
// Calculate stop loss and take profit levels
double stopLossLevel = Ask + StopLoss * Point;
double takeProfitLevel = Ask - TakeProfit * Point;
// Place the sell trade
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 0, stopLossLevel, takeProfitLevel, "Sell Trade", MagicNumber());
// Check if the trade was successful
if (ticket > 0)
{
tradeCount++;
}
}
// Function to open a buy trade
void BuyTrade()
{
// Calculate stop loss and take profit levels
double stopLossLevel = Bid - StopLoss * Point;
double takeProfitLevel = Bid + TakeProfit * Point;
// Place the buy trade
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 0, stopLossLevel, takeProfitLevel, "Buy Trade", MagicNumber());
// Check if the trade was successful
if (ticket > 0)
{
tradeCount++;
}
}
// Function to close all open trades
void CloseAllTrades()
{
int total = OrdersTotal();
for (int i = total - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber())
{
OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
}
}
}
}
// Functio// Includes the trading function from the previous response
#include "TradingFunction.cpp"
// Additional strategy parameters
input double RiskPerTrade = 0.02; // Risk per trade as a percentage of account balance
input int RSIPeriod = 14; // RSI period
input double RSIOverboughtLevel = 70;
input double RSIOversoldLevel = 30;
// Indicator buffers
double RSIBuffer[];
// Initialization function
void OnInit()
{
// Initialize the trading function
OnInit();
// Define the required RSI indicator buffer
SetIndexBuffer(2, RSIBuffer);
}
// Trading function
void OnTick()
{
// Calculate the current RSI value
double currentRSI = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, 0);
RSIBuffer[0] = currentRSI;
// Check if the maximum number of trades has been reached
if (tradeCount >= MaxTrades)
return;
// Check for the presence of a supply or demand zone
bool supplyZoneDetected = CheckSupplyZone();
bool demandZoneDetected = CheckDemandZone();
// If a supply zone is detected and the RSI is overbought
if (supplyZoneDetected && currentRSI >= RSIOverboughtLevel)
{
// Open a sell trade
SellTrade();
}
// If a demand zone is detected and the RSI is oversold
if (demandZoneDetected && currentRSI <= RSIOversoldLevel)
{
// Open a buy trade
BuyTrade();
}
}
// Function to close all open trades
void CloseAllTrades()
{
// Close all trades opened by the trading function
CloseAllTrades();
}n to get the EA's magic number
int MagicNumber()
{
return 123456; // Replace with your desired magic number
}