Files
mql-trading-bots/OrdersEA.mq5
Garfield 2e1c617e73 Fix compile error in OrdersEA - variable scope issue
Line 142: 'i' was declared inside for loop, not accessible outside.
Fixed by declaring 'i' before the loop:
  int i=0;
  for(i=0; i<logTicketsCounter; i++)
2026-03-24 13:38:42 -04:00

241 lines
7.6 KiB
Plaintext

//+------------------------------------------------------------------+
//| OrdersEA_MT5.mq5 |
//| Copyright 2024, Garfield Heron |
//| https://fetcherpay.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Garfield Heron"
#property link "https://fetcherpay.com"
#property version "2.0"
#property strict
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#define VERSION "Version 2.0 MT5"
// Trade object
CTrade trade;
CPositionInfo positionInfo;
#define MAX_TRADES 600
#define MAX_LOG_TRADES 1200
//---- input parameters
input string Email= "garfield@fetcherpay.com";
input int MagicNum= 333;
input double HIGH= 0;
input double LOW= 0;
input double Lots=0.1;
input double Entry= 1;
input double TP= 0.5;
input int StopLoss=0;
input int TRADE_RANGE= 50;
input double LongLimit= 0;
input double ShortLimit= 0;
input string GetOut= "N";
input string OpenNewTrades="Y";
input bool Opposite= false;
input int TakeProfitLevelPercent= 50;
input int TakeProfitLevelDollarAmount= 2000;
input bool Restart= true;
input int EquityFactorPercent= 0;
input int LotsFactorPercent= 0;
input string HoldingAccount= "";
input int BaseEquity= 10000;
input bool Master= false;
input bool DiagnosticModeOn= false;
input double moveRangeTrigger = 0.0;
// Global variables
int initialCycleEquity= 0;
int longs, shorts;
ulong longsTicket[MAX_TRADES];
ulong shortsTicket[MAX_TRADES];
ulong logTickets[MAX_LOG_TRADES];
datetime logTicketsTime[MAX_LOG_TRADES];
int logTicketsCounter;
int Levels;
double price[MAX_TRADES];
int stoplevel;
bool bFirstTick= false;
int logId;
bool bEnableLongs;
bool bEnableShorts;
double tickValue;
int MaximalLoss;
ulong lastTicketSentOpen= 0, lastTicketSentClose= 0;
double longAvgPrice= 0, longAvgLots= 0;
double shortAvgPrice= 0, shortAvgLots= 0;
double longProfit= 0;
double shortProfit= 0;
bool bInit= false;
int TakeProfit=0;
bool AboveHigh;
bool BelowLow;
int tradeLogId;
double closeOnProfit;
bool bGetOutOK, bOpenNewTradesOK;
int initEquity;
int lotDigits;
bool bWithdrawMailSent= false;
bool bGetOutHandled;
int PingTimeMinutes= 240;
bool bValidSettings;
string errMsg;
datetime PivotCalculationTime = 0;
bool TradeExecutedToday = false;
bool R1HitToday = false;
bool S1HitToday = false;
double P, R1, R2, S1, S2;
bool PivotsCalculated = false;
//+------------------------------------------------------------------+
//| Calculate Lot Size |
//+------------------------------------------------------------------+
double CalcLots()
{
double tmp= (AccountInfoDouble(ACCOUNT_EQUITY)-initEquity);
double a= EquityFactorPercent;
double b= LotsFactorPercent;
double lots;
if(0==EquityFactorPercent || 0==LotsFactorPercent)
lots= Lots;
else
{
a=initEquity*a/100.0;
b=b/100.0;
if(tmp>0)
tmp= MathPow(1+b,(tmp/a));
else if(tmp<0)
tmp= MathPow(1-b,MathAbs(tmp/a));
else
tmp= 1;
Log("DBG tmp="+DoubleToString(tmp)+",a="+DoubleToString(a)+",b="+DoubleToString(b)+",AccountEquity()="+DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY)));
lots= NormalizeDouble(Lots*tmp,lotDigits);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
if(lots<minLot)
lots= minLot;
}
if(lots<0)
Log("ERROR tmp="+DoubleToString(tmp)+",a="+DoubleToString(a)+",b="+DoubleToString(b)+",AccountEquity()="+DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY)));
Log("Equity="+DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY))+",lots="+DoubleToString(lots));
return(lots);
}
//+------------------------------------------------------------------+
//| Add Trade to Log |
//+------------------------------------------------------------------+
void AddTradeToLog(ulong ticket)
{
bool rc= false;
int i=0;
for(i=0; i<logTicketsCounter; i++)
{
if(logTickets[i]==ticket)
{
rc= true;
break;
}
}
if(!rc && i<MAX_LOG_TRADES)
{
logTickets[logTicketsCounter]= ticket;
logTicketsTime[logTicketsCounter]= TimeCurrent();
logTicketsCounter++;
}
}
//+------------------------------------------------------------------+
//| Send Notification |
//+------------------------------------------------------------------+
void SendNotificationEx(string title, string subject)
{
if(!MQLInfoInteger(MQL_OPTIMIZATION))
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
string msg = title + ": " + subject;
msg = msg + " | Price: " + DoubleToString(bid, _Digits);
msg = msg + " | Longs: " + IntegerToString(longs) + " @ " + DoubleToString(longAvgPrice, _Digits);
msg = msg + " | Shorts: " + IntegerToString(shorts) + " @ " + DoubleToString(shortAvgPrice, _Digits);
msg = msg + " | Equity: " + DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY), 2);
SendNotification(msg);
Print(msg);
}
}
//+------------------------------------------------------------------+
//| Log function |
//+------------------------------------------------------------------+
void Log(string st)
{
if(DiagnosticModeOn)
{
if(logId>=0)
{
FileWrite(logId, TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS) + ": " + st);
}
}
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(MagicNum);
trade.SetDeviationInPoints(10);
trade.SetTypeFilling(ORDER_FILLING_IOC);
initEquity = BaseEquity;
lotDigits = 2;
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double tickValueCurr = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
tickValue = (tickSize > 0) ? tickValueCurr / tickSize : 0;
Log("Version="+VERSION);
Log("Master="+IntegerToString(Master)+",Restart="+IntegerToString(Restart));
Print("OrdersEA MT5 Initialized - Magic: ", MagicNum);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(logId>=0)
FileClose(logId);
Print("OrdersEA MT5 Deinitialized");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Basic grid trading logic placeholder
// Full implementation would require converting all order management
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// Check if we need to place new grid orders
static datetime lastBarTime = 0;
datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);
if(currentBarTime != lastBarTime)
{
lastBarTime = currentBarTime;
// Placeholder for grid logic
// This is where the grid order placement would go
}
}