New Text Document_061512
New Text Document_061512
// Input parameters
input double RiskPercentage = 1; // Risk percentage for position
sizing
input int ScalpingTakeProfitPips = 5; // Scalping take profit in pips
input int ScalpingStopLossPips = 3; // Scalping stop loss in pips
input int DayTradingTakeProfitPips = 20; // Day trading take profit in pips
input int DayTradingStopLossPips = 10; // Day trading stop loss in pips
input double TrailingStopPips = 2; // Trailing stop in pips
input bool IsScalpingEnabled = true; // Enable scalping strategy
input bool IsDayTradingEnabled = true; // Enable day trading strategy
input int MaxDrawdownPercent = 10; // Max drawdown percentage
input double DailyLossLimit = 50; // Daily loss limit in account
currency
input int RSIPeriod = 14; // RSI period
input double OverboughtLevel = 70; // RSI overbought level
input double OversoldLevel = 30; // RSI oversold level
input int NewsBufferMinutes = 15; // Avoid trading near news events
input int MTFPeriod = 60; // Multi-timeframe analysis period
in minutes
input int TradingStartHour = 1; // Trading start hour (GMT)
input int TradingEndHour = 22; // Trading end hour (GMT)
// Global variables
bool orderOpened = false;
double startingBalance;
double dailyLoss = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
startingBalance = AccountBalance();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Fibonacci Golden Zone (OTE) Detection |
//+------------------------------------------------------------------+
bool IsOptimalTradeEntry(bool isBuy)
{
double high = High[iHighest(NULL, 0, MODE_HIGH, 20, 0)];
double low = Low[iLowest(NULL, 0, MODE_LOW, 20, 0)];
double fib618 = isBuy ? low + (0.618 * (high - low)) : high - (0.618 * (high -
low));
double fib786 = isBuy ? low + (0.786 * (high - low)) : high - (0.786 * (high -
low));
//+------------------------------------------------------------------+
//| Trading Logic with Fibonacci Integration |
//+------------------------------------------------------------------+
void TradingLogicICT()
{
double positionSize = CalculatePositionSize(); // Calculate position size
double high = High[iHighest(NULL, 0, MODE_HIGH, 20, 0)];
double low = Low[iLowest(NULL, 0, MODE_LOW, 20, 0)];
if (IsScalpingEnabled) {
if (!orderOpened && IsFairValueGap() && IsOrderBlockICT(true) &&
IsOptimalTradeEntry(true)) {
double tpExtension = GetFibonacciExtension(true, high, low);
PlaceOrderWithCustomTP(positionSize, true, true, tpExtension); // Buy
with Fibonacci TP
} else if (!orderOpened && IsFairValueGap() && IsOrderBlockICT(false) &&
IsOptimalTradeEntry(false)) {
double tpExtension = GetFibonacciExtension(false, high, low);
PlaceOrderWithCustomTP(positionSize, false, true, tpExtension); // Sell
with Fibonacci TP
}
}
if (IsDayTradingEnabled) {
if (!orderOpened && IsFairValueGap() && IsOrderBlockICT(true) &&
IsOptimalTradeEntry(true)) {
double tpExtension = GetFibonacciExtension(true, high, low);
PlaceOrderWithCustomTP(positionSize, true, false, tpExtension); // Buy
with Fibonacci TP
} else if (!orderOpened && IsFairValueGap() && IsOrderBlockICT(false) &&
IsOptimalTradeEntry(false)) {
double tpExtension = GetFibonacciExtension(false, high, low);
PlaceOrderWithCustomTP(positionSize, false, false, tpExtension); //
Sell with Fibonacci TP
}
}
}
//+------------------------------------------------------------------+
//| Function to Place Order with Custom Take-Profit |
//+------------------------------------------------------------------+
bool PlaceOrderWithCustomTP(double positionSize, bool isBuy, bool isScalping,
double customTP)
{
double price = isBuy ? Ask : Bid;
double sl = isBuy ? price - (isScalping ? ScalpingStopLossPips * Point :
DayTradingStopLossPips * Point)
: price + (isScalping ? ScalpingStopLossPips * Point :
DayTradingStopLossPips * Point);
if (ticket < 0) {
Print("Error placing order: ", GetLastError());
return false;
}
Print("Order placed successfully with Fibonacci TP: ", ticket);
orderOpened = true;
return true;
}
//+------------------------------------------------------------------+
//| Supporting Functions |
//+------------------------------------------------------------------+
double CalculatePositionSize()
{
double accountRisk = AccountBalance() * RiskPercentage / 100;
double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
return accountRisk / ((IsScalpingEnabled ? ScalpingStopLossPips :
DayTradingStopLossPips) * pipValue);
}
bool MarketIsOpen()
{
int hour = TimeHour(TimeCurrent());
return (hour >= TradingStartHour && hour < TradingEndHour);
}
bool IsFairValueGap()
{
double high1 = High[2];
double low1 = Low[2];
double high2 = High[1];
double low2 = Low[1];
double high3 = High[0];
double low3 = Low[0];
return (low1 > high2 && high3 > low1); // FVG logic
}
bool IsDailyLossExceeded()
{
double currentBalance = AccountBalance();
dailyLoss = startingBalance - currentBalance;
return (dailyLoss >= DailyLossLimit);
}
bool IsMaxDrawdownExceeded()
{
double currentDrawdown = (startingBalance - AccountBalance()) / startingBalance
* 100;
return (currentDrawdown >= MaxDrawdownPercent);
}
//+------------------------------------------------------------------+-------------
+
//| Advanced Pattern Recognition |
//+------------------------------------------------------------------+
bool IsHeadAndShoulders()
{
// Placeholder logic for detecting Head and Shoulders
return false; // Change this to actual detection logic
}
bool IsDoubleTopBottom()
{
// Placeholder logic for detecting Double Tops/Bottoms
return false; // Change this to actual detection logic
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if the market is open during specified hours
if (!MarketIsOpen() || IsDailyLossExceeded() || IsMaxDrawdownExceeded() ||
IsNewsEvent()) {
Print("Market is closed or risk limits exceeded. Current time: ",
TimeToString(TimeCurrent(), TIME_DATE | TIME_MINUTES));
return;
}
//+------------------------------------------------------------------+
//| Trading Logic |
//+------------------------------------------------------------------+
void TradingLogic()
{
double positionSize = CalculatePositionSize(); // Calculate position size
if (IsScalpingEnabled) {
// Scalping Logic
if (!orderOpened && IsInDemandZone() && IsRSIConditionMet(true) &&
IsMTFConditionMet(true) && IsHeadAndShoulders()) {
PlaceOrder(positionSize, true, true); // Buy for scalping
} else if (!orderOpened && IsInSupplyZone() && IsRSIConditionMet(false) &&
IsMTFConditionMet(false) && IsDoubleTopBottom()) {
PlaceOrder(positionSize, false, true); // Sell for scalping
}
}
if (IsDayTradingEnabled) {
// Day Trading Logic
if (!orderOpened && IsInDemandZone() && IsRSIConditionMet(true) &&
IsMTFConditionMet(true)) {
PlaceOrder(positionSize, true, false); // Buy for day trading
} else if (!orderOpened && IsInSupplyZone() && IsRSIConditionMet(false) &&
IsMTFConditionMet(false)) {
PlaceOrder(positionSize, false, false); // Sell for day trading
}
}
}
//+------------------------------------------------------------------+