This is the logic I got from mql4programmer.com but it doesn't work
/* Input Parameters */
input int EntryHour = 7; // Hour to scan for trades
input int EntryMinute = 20; // Minute of hour to scan for trades
input int BarsToScan = 1; // Number of previous bars to scan
input double RiskPercent = 1; // Lot size based on % risk
input double MaxLotSize = 10; // Maximum lot size used for trades
input double LotSize = 0.01; // Fixed lot size used for trades
input bool UseFixedLotSize = false; // Option to use fixed lot size instead of risk %
input double LotSizePercentIncrease = 10; // % increase for lot size when adding to winning trades
input double DistanceBetweenOrders = 10; // Distance between additional orders in points
input int StopLoss = 50; // Fixed stop loss in points
input bool UseTrailingStop = true; // Option to use trailing stop
input int TrailingStart = 20; // Trailing stop start in points
input int TrailingStep = 20; // Trailing stop step in points
input int StopBuffer = 2; // Buffer added to stop loss
input bool ShowMaxFloatingLoss = true; // Option to show max floating loss on screen
input bool ShowProfit = true; // Option to show profit on screen
input bool ShowWinPercentage = true; // Option to show win percentage on screen
input bool ShowConsecutiveWins = true; // Option to show consecutive wins on screen
input bool ShowConsecutiveLosses = true; // Option to show consecutive losses on screen
input int GMTOffset = 0; // GMT offset for broker time (in hours)
/* Global Variables */
double Lots;
double StopLossPrice;
double TakeProfit;
double LastTradePrice;
double FloatingProfit;
double MaxFloatingLoss;
double RiskAmount;
int NumberOfWinningShortTrades;
int NumberOfWinningLongTrades;
int ConsecutiveWins;
int ConsecutiveLosses;
/* Trading Functions */
void OpenPosition(int type) {
int ticket;
if (type == OP_SELL) {
ticket = Sell(Lots);
LastTradePrice = Bid;
NumberOfWinningShortTrades++;
} else if (type == OP_BUY) {
ticket = Buy(Lots);
LastTradePrice = Ask;
NumberOfWinningLongTrades++;
}
if (ticket > 0) {
if (UseTrailingStop) {
if (type == OP_SELL) {
// Set initial trailing stop for sell trade
SetTrailingStop(ticket, LastTradePrice - TrailingStart * _Point, TrailingStep * _Point);
} else if (type == OP_BUY) {
// Set initial trailing stop for buy trade
SetTrailingStop(ticket, LastTradePrice + TrailingStart * _Point, TrailingStep * _Point);
}
}
// Set stop loss and take profit levels for the trade
SetStopLoss(ticket, StopLossPrice);
SetTakeProfit(ticket, TakeProfit);
}
}
void ClosePosition(int ticket) {
// Close the trade
if (ticket > 0) {
CloseTrade(ticket);
}
// Reset trade variables
LastTradePrice = 0;
FloatingProfit = 0;
StopLossPrice = 0;
TakeProfit = 0;
// Update consecutive wins/losses counter
if (ticket > 0) {
ConsecutiveWins++;
ConsecutiveLosses = 0;
} else {
ConsecutiveLosses++;
ConsecutiveWins = 0;
}
}
/* Event Functions */
void OnTick() {
int hour = TimeHour(TimeCurrent()) - GMTOffset;
int minute = TimeMinute(TimeCurrent());
int allowedHour = EntryHour - GMTOffset;
int allowedMinute = EntryMinute;
// Only scan for trades at specified time
if (hour != allowedHour || minute != allowedMinute) {
return;
}
// Calculate the breakout price range
int barsBack = BarsToScan + 1;
double rangeLow = Low[barsBack];
double rangeHigh = High[barsBack];
for (int i = barsBack; i > 0; i--) {
if (Low < rangeLow) {
rangeLow = Low;
}
if (High > rangeHigh) {
rangeHigh = High;
}
}
// Check for sell signal
if (Bid < rangeLow) {
// Calculate lot size
if (UseFixedLotSize) {
Lots = LotSize;
} else {
RiskAmount = AccountBalance() * RiskPercent / 100;
Lots = RiskAmount / StopLossPrice / 10000;
if (Lots > MaxLotSize) {
Lots = MaxLotSize;
}
}
// Calculate stop loss and take profit
StopLossPrice = rangeHigh + StopBuffer * _Point;
TakeProfit = Bid - (StopLossPrice - Bid) * 2;
// Open sell position
if (OrderType() == -1) {
// Close existing sell trade if present
int ticket = OrderTicket();
if (ticket > 0) {
ClosePosition(ticket);
}
} else {
OpenPosition(OP_SELL);
}
}
// Check for buy signal
if (Ask > rangeHigh) {
// Calculate lot size
if (UseFixedLotSize) {
Lots = LotSize;
} else {
RiskAmount = AccountBalance() * RiskPercent / 100;
Lots = RiskAmount / (Ask - StopLossPrice) / 10000;
if (Lots > MaxLotSize) {
Lots = MaxLotSize;
}
}
// Calculate stop loss and take profit
StopLossPrice = rangeLow - StopBuffer * _Point;
TakeProfit = Ask + (Ask - StopLossPrice) * 2;
// Open buy position
if (OrderType() == -1) {
OpenPosition(OP_BUY);
} else {
// Close existing buy trade if present
int ticket = OrderTicket();
if (ticket > 0) {
ClosePosition(ticket);
}
}
}
}
void OnTrade() {
int ticket;
double openPrice;
double closePrice;
double lotSize;
int type;
// Loop through all trades and update floating profit
for (int i = 0; i < OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
ticket = OrderTicket();
openPrice = OrderOpenPrice();
lotSize = OrderLots();
type = OrderType();
if (type == OP_SELL) {
if (UseTrailingStop) {
// Update trailing stop for sell trade
SetTrailingStop(ticket, openPrice - TrailingStart * _Point, TrailingStep * _Point);
}
if (Bid < openPrice) {
// Close sell trade due to opposite signal
ClosePosition(ticket);
} else {
// Update stop loss and take profit for sell trade based on average open price
FloatingProfit += (openPrice - Bid) * lotSize;
StopLossPrice = openPrice + (FloatingProfit / OrdersTotal() - StopBuffer * _Point);
SetStopLoss(ticket, StopLossPrice);
SetTakeProfit(ticket, LastTradePrice - (StopLossPrice - LastTradePrice) * 2);
}
} else if (type == OP_BUY) {
if (UseTrailingStop) {
// Update trailing stop for buy trade
SetTrailingStop(ticket, openPrice + TrailingStart * _Point, TrailingStep * _Point);
}
if (Ask > openPrice) {
// Close buy trade due to opposite signal
ClosePosition(ticket);
} else {
// Update stop loss and take profit for buy trade based on average open price
FloatingProfit += (Ask - openPrice) * lotSize;
StopLossPrice = openPrice - (FloatingProfit / OrdersTotal() + StopBuffer * _Point);
SetStopLoss(ticket, StopLossPrice);
SetTakeProfit(ticket, LastTradePrice + (LastTradePrice - StopLossPrice) * 2);
}
}
}
}
// Add to winning trades if distance between orders is reached
if (FloatingProfit >= DistanceBetweenOrders * OrdersTotal() * _Point) {
if (OrderType() == OP_SELL) {
// Add sell order below current market price
int ticket = Sell(Lots * (1 + LotSizePercentIncrease / 100.0), 0, Ask - DistanceBetweenOrders * _Point, 0, 0, 0);
LastTradePrice = OrderOpenPrice();
FloatingProfit = 0;
} else if (OrderType() == OP_BUY) {
// Add buy order above current market price
int ticket = Buy(Lots * (1 + LotSizePercentIncrease / 100.0), 0, Bid + DistanceBetweenOrders * _Point, 0, 0, 0);
LastTradePrice = OrderOpenPrice();
FloatingProfit = 0;
}
}
// Update max floating loss
double floatingLoss = AccountBalance() - (FloatingProfit + Profit());
if (floatingLoss < MaxFloatingLoss) {
MaxFloatingLoss = floatingLoss;
}
}
void OnInit() {
// Set variables initial values
LastTradePrice = 0;
FloatingProfit = 0;
MaxFloatingLoss = 0;
NumberOfWinningShortTrades = 0;
NumberOfWinningLongTrades = 0;
ConsecutiveWins = 0;
ConsecutiveLosses = 0;
// Calculate stop loss price buffer as points
StopBuffer = StopLoss / _Point;
}
void OnDeinit(const int reason) {
// Print statistics on screen when EA is closed
if (reason == REASON_REMOVE || reason == REASON_CHARTCHANGE || reason == REASON_ACCOUNT) {
if (ShowProfit) {
Pr
int("Profit: ", NormalizeDouble(Profit(), 2));
}
if (ShowMaxFloatingLoss) {
Print("Max Floating Loss: ", NormalizeDouble(MaxFloatingLoss, 2));
}
if (ShowWinPercentage) {
Print("Winning Short Trades: ", NumberOfWinningShortTrades, "%\n");
Print("Winning Long Trades: ", NumberOfWinningLongTrades, "%\n");
}
if (ShowConsecutiveWins) {
Print("Consecutive Wins: ", ConsecutiveWins);
}
if (ShowConsecutiveLosses) {
Print("Consecutive Losses: ", ConsecutiveLosses);
}
}
}