Vizion-Trading-EA/Experts/moneyglitchx5.mq5

790 lines
48 KiB
MQL5
Raw Permalink Normal View History

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| QuarterTheory_AGGRESSIVE.mq5 |
//| AGGRESSIVE - Takes Trades on Confirmations |
//| Fibonacci Stacking + MA Alignment + TRIX + Break/Retest |
//+------------------------------------------------------------------+
#property copyright "Aggressive Trading System"
#property version "8.00"
#property strict
#include <Trade/Trade.mqh>
CTrade Trade;
//================ INPUT PARAMETERS ==================//
input group "=== GENERAL ==="
input int MagicNumber = 456789;
input double Risk_Per_Trade = 1.5;
input int Max_Simultaneous_Trades = 6; // More trades allowed
input group "=== PRICE LEVELS ==="
input int Lookback_Bars = 200;
input int Level_Buffer_Points = 50; // Wider buffer = more entries
input bool Show_Levels = true;
input bool Trade_Fibonacci_Retests = true; // Fib retest strategy
input int Fib_Retest_Buffer = 30; // Buffer for Fib retests
input group "=== MOVING AVERAGES ==="
input int MA_1 = 7;
input int MA_2 = 14;
input int MA_3 = 21;
input int MA_4 = 50;
input int MA_5 = 140;
input int MA_6 = 230;
input int MA_7 = 500; // TP MA
input int MA_8 = 1000; // TP MA
input int MA_9 = 1100; // TP MA
input int MA_10 = 1300; // TP MA
input int Min_MA_Aligned = 3; // AGGRESSIVE: Only need 3 aligned
input bool Allow_Single_Cross = true; // Allow 1 cross to trigger
input group "=== TRIX LEVELS ==="
input int TRIX_Period = 18;
input double TRIX_Level_Increment = 0.25;
input bool Use_TRIX_Filter = false; // AGGRESSIVE: Don't require TRIX
input group "=== AGGRESSIVE ENTRY ==="
input bool Trade_On_MA_Alignment = true; // Enter if MAs aligned
input bool Trade_On_Price_Level = true; // Enter at price levels
input bool Trade_Breakouts = true; // Trade level breaks
input bool Trade_Retests = true; // Trade level retests
input bool Stack_Fibs_At_032 = true; // Stack Fibs at 0.382
input group "=== DYNAMIC MA TPs ==="
input double Close_At_MA7_Percent = 15.0;
input double Close_At_MA8_Percent = 25.0;
input double Close_At_MA9_Percent = 30.0;
input group "=== RISK ==="
input int Initial_SL_Points = 150;
input int BreakEven_Points = 50; // Earlier BE
input int Trailing_Start = 80; // Earlier trailing
input int Trailing_Step = 20;
input int Trailing_Stop = 30;
input group "=== DAILY LIMITS ==="
input double Max_Daily_Loss_Percent = 3.0;
input double Max_Daily_Profit_Percent = 15.0; // Higher profit target
input int Max_Trades_Per_Day = 20; // More trades allowed
//================ GLOBALS ==================//
double PriceLevels[];
string LevelTypes[]; // "FIB", "QUARTER", "HALF"
int TotalLevels = 0;
int MA_Handles[10];
double MA_Current[10];
double MA_Previous[10];
int TRIX_Handle;
double TRIX_Current = 0;
double ATH = 0;
double ATL = 0;
struct Position
{
ulong ticket;
double entry;
double original_lot;
bool is_buy;
bool tp_ma7_hit;
bool tp_ma8_hit;
bool tp_ma9_hit;
};
Position OpenPositions[];
double DailyStart = 0;
int TodayTrades = 0;
datetime LastDay = 0;
//+------------------------------------------------------------------+
int OnInit()
{
Print("========================================");
Print("AGGRESSIVE EA v8.0");
Print("Takes Trades on Confirmations");
Print("========================================");
Trade.SetExpertMagicNumber(MagicNumber);
Trade.SetDeviationInPoints(50);
// Initialize MAs
int periods[10] = {MA_1, MA_2, MA_3, MA_4, MA_5, MA_6, MA_7, MA_8, MA_9, MA_10};
for(int i=0; i<10; i++)
{
MA_Handles[i] = iMA(_Symbol, PERIOD_CURRENT, periods[i], 0, MODE_EMA, PRICE_CLOSE);
if(MA_Handles[i] == INVALID_HANDLE)
{
Print("ERROR: MA Handle ", i, " failed");
return INIT_FAILED;
}
}
// Initialize TRIX
TRIX_Handle = iMA(_Symbol, PERIOD_CURRENT, TRIX_Period, 0, MODE_EMA, PRICE_CLOSE);
CalculatePriceLevels();
if(Show_Levels) DrawLevels();
DailyStart = AccountInfoDouble(ACCOUNT_BALANCE);
Print("Entry on: ", Min_MA_Aligned, "+ MAs aligned");
Print("Total Levels: ", TotalLevels);
Print("AGGRESSIVE MODE ACTIVE");
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
for(int i=0; i<10; i++) IndicatorRelease(MA_Handles[i]);
IndicatorRelease(TRIX_Handle);
ObjectsDeleteAll(0, "Level_");
ObjectsDeleteAll(0, "Arrow_");
Print("EA Stopped");
}
//+------------------------------------------------------------------+
void CalculatePriceLevels()
{
ArrayResize(PriceLevels, 0);
ArrayResize(LevelTypes, 0);
TotalLevels = 0;
double high = iHigh(_Symbol, PERIOD_CURRENT, 1);
double low = iLow(_Symbol, PERIOD_CURRENT, 1);
for(int i=2; i<=Lookback_Bars; i++)
{
double h = iHigh(_Symbol, PERIOD_CURRENT, i);
double l = iLow(_Symbol, PERIOD_CURRENT, i);
if(h > high) high = h;
if(l < low) low = l;
}
ATH = high;
ATL = low;
double range = high - low;
// Fibonacci levels (KEY LEVELS)
AddLevel(low, "FIB_0.0");
AddLevel(low + range * 0.236, "FIB_0.236");
AddLevel(low + range * 0.382, "FIB_0.382"); // STACK HERE!
AddLevel(low + range * 0.5, "FIB_0.5");
AddLevel(low + range * 0.618, "FIB_0.618");
AddLevel(low + range * 0.786, "FIB_0.786");
AddLevel(high, "FIB_1.0");
AddLevel(high + range * 0.618, "FIB_1.618");
// Quarter levels
AddLevel(low + range * 0.25, "QUARTER_0.25");
AddLevel(low + range * 0.75, "QUARTER_0.75");
// Eighth levels
AddLevel(low + range * 0.125, "EIGHTH_0.125");
AddLevel(low + range * 0.375, "EIGHTH_0.375");
AddLevel(low + range * 0.625, "EIGHTH_0.625");
AddLevel(low + range * 0.875, "EIGHTH_0.875");
Print("ATH: ", high, " | ATL: ", low, " | Range: ", range/_Point, " points");
}
void AddLevel(double price, string type)
{
int size = ArraySize(PriceLevels);
ArrayResize(PriceLevels, size+1);
ArrayResize(LevelTypes, size+1);
PriceLevels[size] = price;
LevelTypes[size] = type;
TotalLevels++;
}
void DrawLevels()
{
ObjectsDeleteAll(0, "Level_");
for(int i=0; i<TotalLevels; i++)
{
string name = "Level_" + IntegerToString(i);
color clr = clrGray;
int width = 1;
// Highlight important Fib levels
if(StringFind(LevelTypes[i], "FIB_0.382") >= 0)
{
clr = clrYellow;
width = 2;
}
else if(StringFind(LevelTypes[i], "FIB_0.618") >= 0)
{
clr = clrOrange;
width = 2;
}
else if(StringFind(LevelTypes[i], "FIB_0.5") >= 0)
{
clr = clrLime;
width = 2;
}
else if(StringFind(LevelTypes[i], "FIB") >= 0)
{
clr = clrDodgerBlue;
width = 1;
}
ObjectCreate(0, name, OBJ_HLINE, 0, 0, PriceLevels[i]);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT);
ObjectSetInteger(0, name, OBJPROP_WIDTH, width);
ObjectSetInteger(0, name, OBJPROP_BACK, true);
ObjectSetString(0, name, OBJPROP_TEXT, LevelTypes[i] + " - " + DoubleToString(PriceLevels[i], _Digits));
}
}
//+------------------------------------------------------------------+
//| UPDATE MA VALUES
//+------------------------------------------------------------------+
void UpdateMAs()
{
for(int i=0; i<10; i++)
{
double curr[1], prev[1];
CopyBuffer(MA_Handles[i], 0, 0, 1, curr);
CopyBuffer(MA_Handles[i], 0, 1, 1, prev);
MA_Current[i] = curr[0];
MA_Previous[i] = prev[0];
}
}
//+------------------------------------------------------------------+
//| CHECK MA ALIGNMENT (Not crosses, just alignment!)
//+------------------------------------------------------------------+
bool CheckMAAlignment(bool bullish)
{
UpdateMAs();
int aligned = 0;
// Check if fast MAs (0-5) are properly ordered
for(int i=0; i<5; i++)
{
if(bullish)
{
// For bullish: Faster MA > Slower MA
if(MA_Current[i] > MA_Current[i+1])
aligned++;
}
else
{
// For bearish: Faster MA < Slower MA
if(MA_Current[i] < MA_Current[i+1])
aligned++;
}
}
return aligned >= Min_MA_Aligned;
}
//+------------------------------------------------------------------+
//| COUNT RECENT MA CROSSES
//+------------------------------------------------------------------+
int CountRecentCrosses(bool bullish)
{
UpdateMAs();
int crosses = 0;
// Check recent crosses (fast MAs only)
for(int i=0; i<3; i++) // Check first 3 MAs
{
for(int j=i+1; j<6; j++)
{
if(bullish)
{
if(MA_Previous[i] <= MA_Previous[j] && MA_Current[i] > MA_Current[j])
crosses++;
}
else
{
if(MA_Previous[i] >= MA_Previous[j] && MA_Current[i] < MA_Current[j])
crosses++;
}
}
}
return crosses;
}
//+------------------------------------------------------------------+
//| GET TRIX VALUE
//+------------------------------------------------------------------+
double GetTRIX()
{
double buf[2];
CopyBuffer(TRIX_Handle, 0, 0, 2, buf);
if(buf[1] != 0)
return ((buf[0] - buf[1]) / buf[1]) * 100.0;
return 0;
}
//+------------------------------------------------------------------+
//| CHECK IF PRICE AT LEVEL
//+------------------------------------------------------------------+
bool IsPriceAtLevel(double price, bool &is_fib_382)
{
double buffer = Level_Buffer_Points * _Point;
is_fib_382 = false;
for(int i=0; i<TotalLevels; i++)
{
if(MathAbs(price - PriceLevels[i]) <= buffer)
{
// Check if it's the 0.382 Fib level (stacking level)
if(StringFind(LevelTypes[i], "FIB_0.382") >= 0)
is_fib_382 = true;
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
//| CHECK FOR BREAKOUT
//+------------------------------------------------------------------+
bool IsBreakout(double current, double previous, bool &bullish_break, bool &bearish_break)
{
bullish_break = false;
bearish_break = false;
for(int i=0; i<TotalLevels; i++)
{
double level = PriceLevels[i];
// Bullish breakout: Previous below, current above
if(previous < level && current > level)
{
bullish_break = true;
Print("BREAKOUT UP: Price broke ", LevelTypes[i], " @ ", level);
}
// Bearish breakout: Previous above, current below
if(previous > level && current < level)
{
bearish_break = true;
Print("BREAKOUT DOWN: Price broke ", LevelTypes[i], " @ ", level);
}
}
return (bullish_break || bearish_break);
}
//+------------------------------------------------------------------+
//| CHECK FOR RETEST
//+------------------------------------------------------------------+
bool IsRetest(double current, bool &bullish_retest, bool &bearish_retest)
{
bullish_retest = false;
bearish_retest = false;
double retest_buffer = Fib_Retest_Buffer * _Point;
for(int i=0; i<TotalLevels; i++)
{
double level = PriceLevels[i];
// Bullish retest: Price came back to test from above
if(current >= level && current <= level + retest_buffer)
{
// Check if price was above recently
double high_5_bars_ago = iHigh(_Symbol, PERIOD_CURRENT, 5);
if(high_5_bars_ago > level + retest_buffer)
{
bullish_retest = true;
Print("RETEST SUPPORT: ", LevelTypes[i], " @ ", level);
}
}
// Bearish retest: Price came back to test from below
if(current <= level && current >= level - retest_buffer)
{
// Check if price was below recently
double low_5_bars_ago = iLow(_Symbol, PERIOD_CURRENT, 5);
if(low_5_bars_ago < level - retest_buffer)
{
bearish_retest = true;
Print("RETEST RESISTANCE: ", LevelTypes[i], " @ ", level);
}
}
}
return (bullish_retest || bearish_retest);
}
//+------------------------------------------------------------------+
double GetLotSize(int sl_points)
{
double risk = AccountInfoDouble(ACCOUNT_BALANCE) * Risk_Per_Trade / 100.0;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double lot = risk / ((sl_points * _Point / tickSize) * tickValue);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lot = MathMax(lot, minLot);
lot = MathMin(lot, maxLot);
lot = NormalizeDouble(lot / step, 0) * step;
return lot;
}
//+------------------------------------------------------------------+
bool CheckLimits()
{
MqlDateTime dt;
TimeCurrent(dt);
dt.hour = 0; dt.min = 0; dt.sec = 0;
datetime today = StructToTime(dt);
if(today != LastDay)
{
DailyStart = AccountInfoDouble(ACCOUNT_BALANCE);
TodayTrades = 0;
LastDay = today;
}
if(TodayTrades >= Max_Trades_Per_Day) return false;
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double pl = ((balance - DailyStart) / DailyStart) * 100.0;
if(pl <= -Max_Daily_Loss_Percent || pl >= Max_Daily_Profit_Percent)
return false;
return true;
}
//+------------------------------------------------------------------+
void OpenTrade(bool buy, double price, string reason)
{
double lot = GetLotSize(Initial_SL_Points);
if(lot < SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN)) return;
double sl = buy ? price - Initial_SL_Points * _Point : price + Initial_SL_Points * _Point;
bool result = false;
if(buy)
result = Trade.Buy(lot, _Symbol, price, sl, 0, reason);
else
result = Trade.Sell(lot, _Symbol, price, sl, 0, reason);
if(result)
{
TodayTrades++;
ulong ticket = Trade.ResultOrder();
int size = ArraySize(OpenPositions);
ArrayResize(OpenPositions, size+1);
OpenPositions[size].ticket = ticket;
OpenPositions[size].entry = price;
OpenPositions[size].original_lot = lot;
OpenPositions[size].is_buy = buy;
OpenPositions[size].tp_ma7_hit = false;
OpenPositions[size].tp_ma8_hit = false;
OpenPositions[size].tp_ma9_hit = false;
Print("========== TRADE ", TodayTrades, " ==========");
Print(buy ? "BUY" : "SELL", " @ ", price);
Print("Reason: ", reason);
Print("=============================");
// Draw entry arrow
string arrow_name = "Arrow_" + IntegerToString(ticket);
ObjectCreate(0, arrow_name, OBJ_ARROW, 0, TimeCurrent(), price);
ObjectSetInteger(0, arrow_name, OBJPROP_COLOR, buy ? clrLime : clrRed);
ObjectSetInteger(0, arrow_name, OBJPROP_ARROWCODE, buy ? 233 : 234);
ObjectSetInteger(0, arrow_name, OBJPROP_WIDTH, 3);
}
}
//+------------------------------------------------------------------+
void ManagePositions()
{
UpdateMAs();
for(int i=PositionsTotal()-1; i>=0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
if(PositionGetInteger(POSITION_MAGIC) != MagicNumber) continue;
int idx = -1;
for(int j=0; j<ArraySize(OpenPositions); j++)
if(OpenPositions[j].ticket == ticket)
{
idx = j;
break;
}
if(idx == -1) continue;
double entry = PositionGetDouble(POSITION_PRICE_OPEN);
double sl = PositionGetDouble(POSITION_SL);
bool is_buy = OpenPositions[idx].is_buy;
double current = is_buy ? SymbolInfoDouble(_Symbol, SYMBOL_BID)
: SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double profit_points = is_buy ? (current - entry) / _Point
: (entry - current) / _Point;
// Breakeven
if(profit_points >= BreakEven_Points)
{
if((is_buy && sl < entry) || (!is_buy && sl > entry))
{
Trade.PositionModify(ticket, entry, 0);
}
}
double lot = PositionGetDouble(POSITION_VOLUME);
// Dynamic MA TPs
if(!OpenPositions[idx].tp_ma7_hit)
{
bool hit = is_buy ? (current >= MA_Current[6]) : (current <= MA_Current[6]);
if(hit)
{
double close_size = NormalizeDouble(OpenPositions[idx].original_lot * Close_At_MA7_Percent / 100.0, 2);
if(close_size > 0 && close_size <= lot)
{
Trade.PositionClosePartial(ticket, close_size);
OpenPositions[idx].tp_ma7_hit = true;
Print("TP1: MA 500 reached | Closed ", Close_At_MA7_Percent, "%");
}
}
}
if(!OpenPositions[idx].tp_ma8_hit)
{
bool hit = is_buy ? (current >= MA_Current[7]) : (current <= MA_Current[7]);
if(hit)
{
double close_size = NormalizeDouble(OpenPositions[idx].original_lot * Close_At_MA8_Percent / 100.0, 2);
if(close_size > 0 && close_size <= lot)
{
Trade.PositionClosePartial(ticket, close_size);
OpenPositions[idx].tp_ma8_hit = true;
Print("TP2: MA 1000 reached | Closed ", Close_At_MA8_Percent, "%");
}
}
}
if(!OpenPositions[idx].tp_ma9_hit)
{
bool hit = is_buy ? (current >= MA_Current[8]) : (current <= MA_Current[8]);
if(hit)
{
double close_size = NormalizeDouble(OpenPositions[idx].original_lot * Close_At_MA9_Percent / 100.0, 2);
if(close_size > 0 && close_size <= lot)
{
Trade.PositionClosePartial(ticket, close_size);
OpenPositions[idx].tp_ma9_hit = true;
Print("TP3: MA 1100 reached | Closed ", Close_At_MA9_Percent, "%");
}
}
}
// Trailing
if(profit_points >= Trailing_Start)
{
double newSL = 0;
if(is_buy)
{
newSL = current - Trailing_Stop * _Point;
if(newSL > sl + Trailing_Step * _Point)
Trade.PositionModify(ticket, newSL, 0);
}
else
{
newSL = current + Trailing_Stop * _Point;
if(newSL < sl - Trailing_Step * _Point)
Trade.PositionModify(ticket, newSL, 0);
}
}
}
}
//+------------------------------------------------------------------+
void OnTick()
{
static int tick_count = 0;
tick_count++;
ManagePositions();
// Check every tick for aggressive entries
if(!CheckLimits()) return;
// Count open positions
int open = 0;
for(int i=0; i<PositionsTotal(); i++)
{
if(PositionGetTicket(i) == 0) continue;
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == MagicNumber)
open++;
}
if(open >= Max_Simultaneous_Trades) return;
// Recalculate levels every 30 minutes
static datetime last_calc = 0;
if(TimeCurrent() - last_calc > 1800)
{
CalculatePriceLevels();
if(Show_Levels) DrawLevels();
last_calc = TimeCurrent();
}
// Get prices
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double current = (bid + ask) / 2;
double previous = iClose(_Symbol, PERIOD_CURRENT, 1);
// Check MA alignment
bool ma_bullish = CheckMAAlignment(true);
bool ma_bearish = CheckMAAlignment(false);
// Check for crosses
int crosses_bull = CountRecentCrosses(true);
int crosses_bear = CountRecentCrosses(false);
// Get TRIX
TRIX_Current = GetTRIX();
// Check price at level
bool is_fib_382 = false;
bool at_level = IsPriceAtLevel(current, is_fib_382);
// Check breakouts
bool bull_break = false;
bool bear_break = false;
IsBreakout(current, previous, bull_break, bear_break);
// Check retests
bool bull_retest = false;
bool bear_retest = false;
IsRetest(current, bull_retest, bear_retest);
// === AGGRESSIVE ENTRY LOGIC ===
// BUY CONDITIONS (Any of these trigger)
bool buy_signal = false;
string buy_reason = "";
// Condition 1: MA alignment + at level
if(Trade_On_MA_Alignment && ma_bullish && at_level)
{
buy_signal = true;
buy_reason = "MA Aligned + Level";
}
// Condition 2: Breakout + MA confirmation
if(Trade_Breakouts && bull_break && ma_bullish)
{
buy_signal = true;
buy_reason = "Breakout + MA";
}
// Condition 3: Retest + MA confirmation
if(Trade_Retests && bull_retest && ma_bullish)
{
buy_signal = true;
buy_reason = "Retest + MA";
}
// Condition 4: Fib 0.382 stack + MA crosses
if(Stack_Fibs_At_032 && is_fib_382 && crosses_bull > 0)
{
buy_signal = true;
buy_reason = "Fib 0.382 Stack + Cross";
}
// Condition 5: Multiple MA crosses + TRIX positive
if(Allow_Single_Cross && crosses_bull >= 1 && TRIX_Current > 0 && at_level)
{
buy_signal = true;
buy_reason = "MA Cross + TRIX + Level";
}
// SELL CONDITIONS (Any of these trigger)
bool sell_signal = false;
string sell_reason = "";
// Condition 1: MA alignment + at level
if(Trade_On_MA_Alignment && ma_bearish && at_level)
{
sell_signal = true;
sell_reason = "MA Aligned + Level";
}
// Condition 2: Breakout + MA confirmation
if(Trade_Breakouts && bear_break && ma_bearish)
{
sell_signal = true;
sell_reason = "Breakout + MA";
}
// Condition 3: Retest + MA confirmation
if(Trade_Retests && bear_retest && ma_bearish)
{
sell_signal = true;
sell_reason = "Retest + MA";
}
// Condition 4: Fib 0.382 stack + MA crosses
if(Stack_Fibs_At_032 && is_fib_382 && crosses_bear > 0)
{
sell_signal = true;
sell_reason = "Fib 0.382 Stack + Cross";
}
// Condition 5: Multiple MA crosses + TRIX negative
if(Allow_Single_Cross && crosses_bear >= 1 && TRIX_Current < 0 && at_level)
{
sell_signal = true;
sell_reason = "MA Cross + TRIX + Level";
}
// EXECUTE TRADES
if(buy_signal)
{
OpenTrade(true, ask, buy_reason);
}
if(sell_signal)
{
OpenTrade(false, bid, sell_reason);
}
// Debug info every 100 ticks
if(tick_count % 100 == 0)
{
Print("--- Status Check ---");
Print("MA Bullish: ", ma_bullish, " | Bearish: ", ma_bearish);
Print("At Level: ", at_level, " | Fib 0.382: ", is_fib_382);
Print("Crosses Bull: ", crosses_bull, " | Bear: ", crosses_bear);
Print("TRIX: ", DoubleToString(TRIX_Current, 2));
Print("Today's Trades: ", TodayTrades);
}
}
//+------------------------------------------------------------------+