677 lines
No EOL
44 KiB
MQL5
677 lines
No EOL
44 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| QuarterTheory_TREND_FIXED.mq5 |
|
|
//| MA TREND BIAS PRIMARY + Stoch Secondary Confirmation |
|
|
//| Higher MAs (50+) = Priority Re-entry | 8-20 Active Trades |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "MA Trend Bias System - Fixed"
|
|
#property version "13.00"
|
|
#property strict
|
|
|
|
#include <Trade/Trade.mqh>
|
|
CTrade Trade;
|
|
|
|
//================ INPUT PARAMETERS ==================//
|
|
input group "=== GENERAL ==="
|
|
input int MagicNumber = 456789;
|
|
input double Risk_Per_Trade = 1.2;
|
|
input int Min_Active_Entries = 8; // Minimum 8 trades
|
|
input int Max_Simultaneous_Trades = 20; // Maximum 20 trades
|
|
|
|
input group "=== MA TREND BIAS (PRIMARY!) ==="
|
|
input int MA_1 = 7;
|
|
input int MA_2 = 14;
|
|
input int MA_3 = 21;
|
|
input int MA_4 = 50; // CRITICAL for trend
|
|
input int MA_5 = 140;
|
|
input int MA_6 = 230;
|
|
input int MA_7 = 500;
|
|
input int MA_8 = 1000;
|
|
input int MA_9 = 1100;
|
|
input int MA_10 = 1300;
|
|
input bool Require_MA_Trend_Bias = true; // MUST determine trend first
|
|
input int Min_MA7_Crosses = 2; // MA 7 must cross 2+ MAs
|
|
|
|
input group "=== STOCHASTIC (SECONDARY CONFIRMATION) ==="
|
|
input int Stoch_K_Period = 5;
|
|
input int Stoch_D_Period = 3;
|
|
input int Stoch_Slowing = 3;
|
|
input double Stoch_Overbought = 80.0;
|
|
input double Stoch_Oversold = 20.0;
|
|
input bool Use_Stoch_Confirmation = true; // Optional filter
|
|
|
|
input group "=== HIGHER MA RE-ENTRY (50+) ==="
|
|
input bool ReEnter_At_MA50 = true; // Re-enter at MA 50
|
|
input bool ReEnter_At_MA140 = true; // Re-enter at MA 140
|
|
input bool ReEnter_At_MA230 = true; // Re-enter at MA 230
|
|
input bool ReEnter_At_MA500 = true; // Re-enter at MA 500
|
|
input int MA_Touch_Buffer = 30; // Buffer for MA touch
|
|
|
|
input group "=== FIBONACCI LEVELS ==="
|
|
input int Lookback_Bars = 200;
|
|
input bool Enter_On_Fib_Break = true;
|
|
input bool Show_Levels = true;
|
|
|
|
input group "=== TAKE PROFIT & RISK ==="
|
|
input int Partial_TP_Points = 2300;
|
|
input double Partial_TP_Percent = 50.0;
|
|
input int BreakEven_Points = 500;
|
|
input int Trailing_SL_Points = 500;
|
|
input int Initial_SL_Points = 800;
|
|
|
|
input group "=== DAILY LIMITS ==="
|
|
input double Max_Daily_Loss_Percent = 5.0;
|
|
input double Max_Daily_Profit_Percent = 30.0;
|
|
input int Max_Trades_Per_Day = 40;
|
|
|
|
//================ GLOBALS ==================//
|
|
int Stoch_Handle;
|
|
double Stoch_K_Current = 0;
|
|
double Stoch_K_Previous = 0;
|
|
|
|
double PriceLevels[];
|
|
string LevelTypes[];
|
|
int TotalLevels = 0;
|
|
|
|
int MA_Handles[10];
|
|
double MA_Current[10];
|
|
double MA_Previous[10];
|
|
|
|
// TREND BIAS
|
|
bool Current_Trend_Bullish = false;
|
|
bool Current_Trend_Bearish = false;
|
|
datetime Last_Trend_Check = 0;
|
|
|
|
struct Position
|
|
{
|
|
ulong ticket;
|
|
double entry;
|
|
double original_lot;
|
|
bool is_buy;
|
|
bool partial_tp_hit;
|
|
bool be_set;
|
|
};
|
|
Position OpenPositions[];
|
|
|
|
double DailyStart = 0;
|
|
int TodayTrades = 0;
|
|
datetime LastDay = 0;
|
|
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
Print("========================================");
|
|
Print("TREND FIXED v13.0");
|
|
Print("MA Trend Bias PRIMARY");
|
|
Print("Stochastic SECONDARY");
|
|
Print("========================================");
|
|
|
|
Trade.SetExpertMagicNumber(MagicNumber);
|
|
Trade.SetDeviationInPoints(50);
|
|
|
|
// Initialize Stochastic
|
|
Stoch_Handle = iStochastic(_Symbol, PERIOD_CURRENT, Stoch_K_Period, Stoch_D_Period,
|
|
Stoch_Slowing, MODE_SMA, STO_LOWHIGH);
|
|
if(Stoch_Handle == INVALID_HANDLE)
|
|
{
|
|
Print("ERROR: Stochastic failed");
|
|
return INIT_FAILED;
|
|
}
|
|
|
|
// 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 ", periods[i], " failed");
|
|
return INIT_FAILED;
|
|
}
|
|
}
|
|
|
|
CalculatePriceLevels();
|
|
if(Show_Levels) DrawLevels();
|
|
|
|
DailyStart = AccountInfoDouble(ACCOUNT_BALANCE);
|
|
|
|
Print("Min/Max Trades: ", Min_Active_Entries, " / ", Max_Simultaneous_Trades);
|
|
Print("MA Trend Bias: REQUIRED (prevents opposite direction)");
|
|
Print("Higher MAs (50, 140, 230, 500) = Priority Re-entry");
|
|
Print("Initial SL: ", Initial_SL_Points, " points");
|
|
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
for(int i=0; i<10; i++)
|
|
if(MA_Handles[i] != INVALID_HANDLE)
|
|
IndicatorRelease(MA_Handles[i]);
|
|
|
|
if(Stoch_Handle != INVALID_HANDLE)
|
|
IndicatorRelease(Stoch_Handle);
|
|
|
|
ObjectsDeleteAll(0, "Level_");
|
|
ObjectsDeleteAll(0, "Arrow_");
|
|
ObjectsDeleteAll(0, "TrendLabel");
|
|
|
|
Print("EA Stopped");
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void CalculatePriceLevels()
|
|
{
|
|
ArrayResize(PriceLevels, 0);
|
|
ArrayResize(LevelTypes, 0);
|
|
TotalLevels = 0;
|
|
|
|
double high = iHigh(_Symbol, PERIOD_CURRENT, 0);
|
|
double low = iLow(_Symbol, PERIOD_CURRENT, 0);
|
|
|
|
for(int i=1; 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;
|
|
}
|
|
|
|
double range = high - low;
|
|
|
|
AddLevel(low, "FIB_0.0");
|
|
AddLevel(low + range * 0.236, "FIB_0.236");
|
|
AddLevel(low + range * 0.382, "FIB_0.382");
|
|
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");
|
|
}
|
|
|
|
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);
|
|
ObjectCreate(0, name, OBJ_HLINE, 0, 0, PriceLevels[i]);
|
|
ObjectSetInteger(0, name, OBJPROP_COLOR, clrDodgerBlue);
|
|
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT);
|
|
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
|
|
ObjectSetInteger(0, name, OBJPROP_BACK, true);
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void UpdateMAs()
|
|
{
|
|
for(int i=0; i<10; i++)
|
|
{
|
|
double curr[1], prev[1];
|
|
if(CopyBuffer(MA_Handles[i], 0, 0, 1, curr) > 0)
|
|
MA_Current[i] = curr[0];
|
|
if(CopyBuffer(MA_Handles[i], 0, 1, 1, prev) > 0)
|
|
MA_Previous[i] = prev[0];
|
|
}
|
|
}
|
|
|
|
void UpdateStochastic()
|
|
{
|
|
double k_curr[1], k_prev[1];
|
|
if(CopyBuffer(Stoch_Handle, MAIN_LINE, 0, 1, k_curr) > 0)
|
|
Stoch_K_Current = k_curr[0];
|
|
if(CopyBuffer(Stoch_Handle, MAIN_LINE, 1, 1, k_prev) > 0)
|
|
Stoch_K_Previous = k_prev[0];
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| DETERMINE TREND BIAS (PRIMARY!)
|
|
//+------------------------------------------------------------------+
|
|
void DetermineTrendBias()
|
|
{
|
|
UpdateMAs();
|
|
|
|
// Count how many MAs are in bullish/bearish alignment
|
|
int bullish_alignment = 0;
|
|
int bearish_alignment = 0;
|
|
|
|
// Check MA stacking (faster above slower = bullish)
|
|
for(int i=0; i<6; i++) // Check first 6 MAs (7, 14, 21, 50, 140, 230)
|
|
{
|
|
if(i < 5 && MA_Current[i] > MA_Current[i+1])
|
|
bullish_alignment++;
|
|
if(i < 5 && MA_Current[i] < MA_Current[i+1])
|
|
bearish_alignment++;
|
|
}
|
|
|
|
// Critical: MA 7 vs MA 50
|
|
bool ma7_above_ma50 = MA_Current[0] > MA_Current[3];
|
|
bool ma7_below_ma50 = MA_Current[0] < MA_Current[3];
|
|
|
|
// Determine trend
|
|
if(bullish_alignment >= 3 && ma7_above_ma50)
|
|
{
|
|
Current_Trend_Bullish = true;
|
|
Current_Trend_Bearish = false;
|
|
|
|
// Update visual
|
|
ObjectDelete(0, "TrendLabel");
|
|
ObjectCreate(0, "TrendLabel", OBJ_LABEL, 0, 0, 0);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_CORNER, CORNER_LEFT_UPPER);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_XDISTANCE, 10);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_YDISTANCE, 30);
|
|
ObjectSetString(0, "TrendLabel", OBJPROP_TEXT, "TREND: BULLISH ↑");
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_COLOR, clrLime);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_FONTSIZE, 12);
|
|
}
|
|
else if(bearish_alignment >= 3 && ma7_below_ma50)
|
|
{
|
|
Current_Trend_Bullish = false;
|
|
Current_Trend_Bearish = true;
|
|
|
|
ObjectDelete(0, "TrendLabel");
|
|
ObjectCreate(0, "TrendLabel", OBJ_LABEL, 0, 0, 0);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_CORNER, CORNER_LEFT_UPPER);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_XDISTANCE, 10);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_YDISTANCE, 30);
|
|
ObjectSetString(0, "TrendLabel", OBJPROP_TEXT, "TREND: BEARISH ↓");
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_COLOR, clrRed);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_FONTSIZE, 12);
|
|
}
|
|
else
|
|
{
|
|
// Neutral/ranging - maintain previous trend
|
|
ObjectDelete(0, "TrendLabel");
|
|
ObjectCreate(0, "TrendLabel", OBJ_LABEL, 0, 0, 0);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_CORNER, CORNER_LEFT_UPPER);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_XDISTANCE, 10);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_YDISTANCE, 30);
|
|
ObjectSetString(0, "TrendLabel", OBJPROP_TEXT, "TREND: RANGING");
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_COLOR, clrYellow);
|
|
ObjectSetInteger(0, "TrendLabel", OBJPROP_FONTSIZE, 12);
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| COUNT MA 7 CROSSES
|
|
//+------------------------------------------------------------------+
|
|
int CountMA7Crosses(bool check_bullish)
|
|
{
|
|
int crosses = 0;
|
|
|
|
for(int i=1; i<6; i++)
|
|
{
|
|
if(check_bullish)
|
|
{
|
|
if(MA_Current[0] > MA_Current[i])
|
|
crosses++;
|
|
}
|
|
else
|
|
{
|
|
if(MA_Current[0] < MA_Current[i])
|
|
crosses++;
|
|
}
|
|
}
|
|
|
|
return crosses;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| CHECK IF PRICE TOUCHING HIGHER MA (50, 140, 230, 500)
|
|
//+------------------------------------------------------------------+
|
|
bool IsPriceAtHigherMA(double price, int &ma_index, string &ma_name)
|
|
{
|
|
double buffer = MA_Touch_Buffer * _Point;
|
|
|
|
// Check higher MAs (priority order: 50, 140, 230, 500)
|
|
int higher_mas[4] = {3, 4, 5, 6}; // MA 50, 140, 230, 500
|
|
string ma_names[4] = {"MA 50", "MA 140", "MA 230", "MA 500"};
|
|
|
|
for(int i=0; i<4; i++)
|
|
{
|
|
int idx = higher_mas[i];
|
|
if(MathAbs(price - MA_Current[idx]) <= buffer)
|
|
{
|
|
ma_index = idx;
|
|
ma_name = ma_names[i];
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| CHECK STOCHASTIC CONFIRMATION (SECONDARY)
|
|
//+------------------------------------------------------------------+
|
|
bool StochConfirms(bool check_bullish)
|
|
{
|
|
if(!Use_Stoch_Confirmation) return true;
|
|
|
|
if(check_bullish)
|
|
{
|
|
// For BUY: Stoch should be low or rising
|
|
return (Stoch_K_Current < 70 && Stoch_K_Current >= Stoch_K_Previous);
|
|
}
|
|
else
|
|
{
|
|
// For SELL: Stoch should be high or falling
|
|
return (Stoch_K_Current > 30 && Stoch_K_Current <= Stoch_K_Previous);
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
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;
|
|
|
|
string trend = buy ? "BULL" : "BEAR";
|
|
string comment = trend + " | Stoch:" + DoubleToString(Stoch_K_Current,1) + " | " + reason;
|
|
|
|
bool result = false;
|
|
if(buy)
|
|
result = Trade.Buy(lot, _Symbol, price, sl, 0, comment);
|
|
else
|
|
result = Trade.Sell(lot, _Symbol, price, sl, 0, comment);
|
|
|
|
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].partial_tp_hit = false;
|
|
OpenPositions[size].be_set = false;
|
|
|
|
Print("========== TRADE ", TodayTrades, " ==========");
|
|
Print(buy ? "BUY" : "SELL", " @ ", price);
|
|
Print("Trend: ", (Current_Trend_Bullish ? "BULLISH" : "BEARISH"));
|
|
Print("Stoch: ", Stoch_K_Current);
|
|
Print("Reason: ", reason);
|
|
Print("===================================");
|
|
|
|
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();
|
|
UpdateStochastic();
|
|
|
|
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;
|
|
|
|
// Partial TP
|
|
if(!OpenPositions[idx].partial_tp_hit && profit_points >= Partial_TP_Points)
|
|
{
|
|
double lot = PositionGetDouble(POSITION_VOLUME);
|
|
double close_size = NormalizeDouble(OpenPositions[idx].original_lot * Partial_TP_Percent / 100.0, 2);
|
|
|
|
if(close_size > 0 && close_size <= lot)
|
|
{
|
|
Trade.PositionClosePartial(ticket, close_size);
|
|
OpenPositions[idx].partial_tp_hit = true;
|
|
Print("PARTIAL TP: ", Partial_TP_Points, " points");
|
|
}
|
|
}
|
|
|
|
// Breakeven
|
|
if(!OpenPositions[idx].be_set && profit_points >= BreakEven_Points)
|
|
{
|
|
if((is_buy && sl < entry) || (!is_buy && sl > entry))
|
|
{
|
|
Trade.PositionModify(ticket, entry, 0);
|
|
OpenPositions[idx].be_set = true;
|
|
}
|
|
}
|
|
|
|
// Trailing
|
|
if(profit_points >= Trailing_SL_Points + 100)
|
|
{
|
|
double newSL = 0;
|
|
|
|
if(is_buy)
|
|
{
|
|
newSL = current - Trailing_SL_Points * _Point;
|
|
if(newSL > sl + 50 * _Point)
|
|
Trade.PositionModify(ticket, newSL, 0);
|
|
}
|
|
else
|
|
{
|
|
newSL = current + Trailing_SL_Points * _Point;
|
|
if(newSL < sl - 50 * _Point)
|
|
Trade.PositionModify(ticket, newSL, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void OnTick()
|
|
{
|
|
// Update trend bias every 5 seconds
|
|
if(TimeCurrent() - Last_Trend_Check >= 5)
|
|
{
|
|
DetermineTrendBias();
|
|
Last_Trend_Check = TimeCurrent();
|
|
}
|
|
|
|
ManagePositions();
|
|
|
|
if(!CheckLimits()) return;
|
|
|
|
// Count positions by direction
|
|
int buy_count = 0;
|
|
int sell_count = 0;
|
|
|
|
for(int i=0; i<PositionsTotal(); i++)
|
|
{
|
|
if(PositionGetTicket(i) == 0) continue;
|
|
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
|
|
PositionGetInteger(POSITION_MAGIC) == MagicNumber)
|
|
{
|
|
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
|
|
buy_count++;
|
|
else
|
|
sell_count++;
|
|
}
|
|
}
|
|
|
|
int total_open = buy_count + sell_count;
|
|
|
|
// Don't exceed max
|
|
if(total_open >= Max_Simultaneous_Trades) return;
|
|
|
|
// Force entry if below minimum
|
|
bool force_entry = (total_open < Min_Active_Entries);
|
|
|
|
static datetime last_calc = 0;
|
|
if(TimeCurrent() - last_calc > 1800)
|
|
{
|
|
CalculatePriceLevels();
|
|
if(Show_Levels) DrawLevels();
|
|
last_calc = TimeCurrent();
|
|
}
|
|
|
|
UpdateMAs();
|
|
UpdateStochastic();
|
|
|
|
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
|
|
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
|
|
double current = (bid + ask) / 2;
|
|
double previous = iClose(_Symbol, PERIOD_CURRENT, 1);
|
|
|
|
// Count MA crosses
|
|
int ma7_crosses_bull = CountMA7Crosses(true);
|
|
int ma7_crosses_bear = CountMA7Crosses(false);
|
|
|
|
// Check higher MA touches
|
|
int higher_ma_idx = -1;
|
|
string ma_name = "";
|
|
bool at_higher_ma = IsPriceAtHigherMA(current, higher_ma_idx, ma_name);
|
|
|
|
// === ENTRY LOGIC ===
|
|
|
|
// BUY ONLY IF TREND IS BULLISH
|
|
if(Current_Trend_Bullish || force_entry)
|
|
{
|
|
bool buy_signal = false;
|
|
string buy_reason = "";
|
|
|
|
// 1. MA 7 crossed 2+ MAs + Stoch confirms
|
|
if(ma7_crosses_bull >= Min_MA7_Crosses && StochConfirms(true))
|
|
{
|
|
buy_signal = true;
|
|
buy_reason = "MA7 " + IntegerToString(ma7_crosses_bull) + "x + Stoch OK";
|
|
}
|
|
|
|
// 2. Price touching higher MA (50, 140, 230, 500) - PRIORITY!
|
|
if(at_higher_ma && ma7_crosses_bull >= 1 && StochConfirms(true))
|
|
{
|
|
buy_signal = true;
|
|
buy_reason = ma_name + " TOUCH + Trend";
|
|
}
|
|
|
|
// 3. Force entry if below minimum
|
|
if(force_entry && ma7_crosses_bull >= 1 && buy_count < sell_count)
|
|
{
|
|
buy_signal = true;
|
|
buy_reason = "FORCE: Below min (" + IntegerToString(total_open) + "/" + IntegerToString(Min_Active_Entries) + ")";
|
|
}
|
|
|
|
if(buy_signal)
|
|
{
|
|
OpenTrade(true, ask, buy_reason);
|
|
}
|
|
}
|
|
|
|
// SELL ONLY IF TREND IS BEARISH
|
|
if(Current_Trend_Bearish || force_entry)
|
|
{
|
|
bool sell_signal = false;
|
|
string sell_reason = "";
|
|
|
|
// 1. MA 7 crossed 2+ MAs + Stoch confirms
|
|
if(ma7_crosses_bear >= Min_MA7_Crosses && StochConfirms(false))
|
|
{
|
|
sell_signal = true;
|
|
sell_reason = "MA7 " + IntegerToString(ma7_crosses_bear) + "x + Stoch OK";
|
|
}
|
|
|
|
// 2. Price touching higher MA (50, 140, 230, 500) - PRIORITY!
|
|
if(at_higher_ma && ma7_crosses_bear >= 1 && StochConfirms(false))
|
|
{
|
|
sell_signal = true;
|
|
sell_reason = ma_name + " TOUCH + Trend";
|
|
}
|
|
|
|
// 3. Force entry if below minimum
|
|
if(force_entry && ma7_crosses_bear >= 1 && sell_count < buy_count)
|
|
{
|
|
sell_signal = true;
|
|
sell_reason = "FORCE: Below min (" + IntegerToString(total_open) + "/" + IntegerToString(Min_Active_Entries) + ")";
|
|
}
|
|
|
|
if(sell_signal)
|
|
{
|
|
OpenTrade(false, bid, sell_reason);
|
|
}
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+ |