the following EA opens up two buy stops and one sell stops , this is incorrect. the EA must open one of each. one sell stop and one buy stop.//+------------------------------------------------------------------+
//| GoldenBreakout.mq5
//+------------------------------------------------------------------+
#property copyright "You"
#property link "https://…"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
input double LotSize = 0.1; // Lot Size
input int StopLossPoints = 50; // Stop Loss in Points (0 to disable)
input int TakeProfitPoints = 100; // Take Profit in Points (0 to disable)
input double StopLossUSD = 0.0; // Stop Loss in USD (0 to disable)
input double TakeProfitUSD = 0.0; // Take Profit in USD (0 to disable)
input bool UseATRStopLoss = false; // Use ATR-based Stop Loss
input int ATRPeriod = 14; // ATR Period
input double ATRMultiplier = 2.0; // ATR Multiplier for Stop Loss
input bool UseTrailingStop = false; // Use Trailing Stop
input int TrailingStopPoints = 30; // Trailing Stop in Points
input int MagicNumber = 12345; // Magic Number for Trades
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
double prevDayHigh = 0.0;
double prevDayLow = 0.0;
datetime prevDay = 0;
CTrade trade;
CSymbolInfo symInfo;
int atrHandle = INVALID_HANDLE;
static int buyTradesToday = 0; // Track buy trades today
static int sellTradesToday = 0; // Track sell trades today
//+------------------------------------------------------------------+
//| Expert Initialization Function |
//+------------------------------------------------------------------+
int OnInit()
{
symInfo.Name(_Symbol);
symInfo.Refresh();
if (UseATRStopLoss)
{
atrHandle = iATR(_Symbol, PERIOD_D1, ATRPeriod);
if (atrHandle == INVALID_HANDLE)
Print("Failed to create ATR handle");
}
UpdateDailyLevels();
PlacePendingOrders();
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert Deinitialization Function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if (atrHandle != INVALID_HANDLE)
IndicatorRelease(atrHandle);
DeleteHorizontalLines();
}
//+------------------------------------------------------------------+
//| Expert Tick Function |
//+------------------------------------------------------------------+
void OnTick()
{
datetime currentDay = iTime(_Symbol, PERIOD_D1, 0);
// Reset trade counters at new day
if (currentDay != prevDay)
{
buyTradesToday = 0;
sellTradesToday = 0;
DeletePendingOrders();
UpdateDailyLevels();
PlacePendingOrders();
prevDay = currentDay;
}
if (UseTrailingStop)
ApplyTrailingStop();
}
//+------------------------------------------------------------------+
//| Update Daily High and Low Levels |
//+------------------------------------------------------------------+
void UpdateDailyLevels()
{
prevDayHigh = iHigh(_Symbol, PERIOD_D1, 1);
prevDayLow = iLow(_Symbol, PERIOD_D1, 1);
DrawHorizontalLines();
}
//+------------------------------------------------------------------+
//| Draw Horizontal Lines on Chart |
//+------------------------------------------------------------------+
void DrawHorizontalLines()
{
DeleteHorizontalLines();
ObjectCreate(0, "PrevDayHigh", OBJ_HLINE, 0, 0, prevDayHigh);
ObjectSetInteger(0, "PrevDayHigh", OBJPROP_COLOR, clrGreen);
ObjectSetInteger(0, "PrevDayHigh", OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, "PrevDayHigh", OBJPROP_WIDTH, 1);
ObjectCreate(0, "PrevDayLow", OBJ_HLINE, 0, 0, prevDayLow);
ObjectSetInteger(0, "PrevDayLow", OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, "PrevDayLow", OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, "PrevDayLow", OBJPROP_WIDTH, 1);
}
//+------------------------------------------------------------------+
//| Delete Horizontal Lines on Chart |
//+------------------------------------------------------------------+
void DeleteHorizontalLines()
{
ObjectDelete(0, "PrevDayHigh");
ObjectDelete(0, "PrevDayLow");
}
//+------------------------------------------------------------------+
//| Delete All Pending Orders |
//+------------------------------------------------------------------+
void DeletePendingOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if (OrderSelect(ticket))
{
if (OrderGetInteger(ORDER_MAGIC) == MagicNumber &&
OrderGetString(ORDER_SYMBOL) == _Symbol)
{
trade.OrderDelete(ticket);
}
}
}
}
//+------------------------------------------------------------------+
//| Place Pending Buy Stop and Sell Stop Orders |
//+------------------------------------------------------------------+
void PlacePendingOrders()
{
// Only place orders if we haven't reached daily limit
if (buyTradesToday < 1)
{
double slPoints = StopLossPoints;
double tpPoints = TakeProfitPoints;
if (UseATRStopLoss && atrHandle != INVALID_HANDLE)
{
double atrBuffer[];
if (CopyBuffer(atrHandle, 0, 1, 1, atrBuffer) > 0)
slPoints = atrBuffer[0] * ATRMultiplier / _Point;
else
Print("ATR CopyBuffer failed");
}
if (StopLossUSD > 0.0)
slPoints = USDToPoints(StopLossUSD);
if (TakeProfitUSD > 0.0)
tpPoints = USDToPoints(TakeProfitUSD);
// Place Buy Stop Order (only if not already traded today)
double buyStopPrice = prevDayHigh;
double buyStopSL = (slPoints > 0) ? buyStopPrice - slPoints * _Point : 0.0;
double buyStopTP = (tpPoints > 0) ? buyStopPrice + tpPoints * _Point : 0.0;
trade.BuyStop(LotSize, buyStopPrice, _Symbol, buyStopSL, buyStopTP, ORDER_TIME_DAY, 0, "Buy Stop Breakout");
trade.SetExpertMagicNumber(MagicNumber);
}
if (sellTradesToday < 1)
{
double slPoints = StopLossPoints;
double tpPoints = TakeProfitPoints;
if (UseATRStopLoss && atrHandle != INVALID_HANDLE)
{
double atrBuffer[];
if (CopyBuffer(atrHandle, 0, 1, 1, atrBuffer) > 0)
slPoints = atrBuffer[0] * ATRMultiplier / _Point;
else
Print("ATR CopyBuffer failed");
}
if (StopLossUSD > 0.0)
slPoints = USDToPoints(StopLossUSD);
if (TakeProfitUSD > 0.0)
tpPoints = USDToPoints(TakeProfitUSD);
// Place Sell Stop Order (only if not already traded today)
double sellStopPrice = prevDayLow;
double sellStopSL = (slPoints > 0) ? sellStopPrice + slPoints * _Point : 0.0;
double sellStopTP = (tpPoints > 0) ? sellStopPrice - tpPoints * _Point : 0.0;
trade.SellStop(LotSize, sellStopPrice, _Symbol, sellStopSL, sellStopTP, ORDER_TIME_DAY, 0, "Sell Stop Breakout");
trade.SetExpertMagicNumber(MagicNumber);
}
}
//+------------------------------------------------------------------+
//| Convert USD to Points |
//+------------------------------------------------------------------+
double USDToPoints(double usdAmount)
{
symInfo.Refresh();
double tickValue = symInfo.TickValue();
double tickSize = symInfo.TickSize();
double points = usdAmount / (tickValue * LotSize) * tickSize / _Point;
return points;
}
//+------------------------------------------------------------------+
//| Apply Trailing Stop to Open Positions |
//+------------------------------------------------------------------+
void ApplyTrailingStop()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
if (PositionGetInteger(POSITION_MAGIC) == MagicNumber &&
PositionGetString(POSITION_SYMBOL) == _Symbol)
{
double currentPrice = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double stopLoss = PositionGetDouble(POSITION_SL);
double takeProfit = PositionGetDouble(POSITION_TP);
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
double newSL = currentPrice - TrailingStopPoints * _Point;
if (newSL > stopLoss && newSL > openPrice)
{
trade.PositionModify(ticket, newSL, takeProfit);
}
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
double newSL = currentPrice + TrailingStopPoints * _Point;
if ((newSL < stopLoss || stopLoss == 0) && newSL < openPrice)
{
trade.PositionModify(ticket, newSL, takeProfit);
}
}
}
}
}
}