Whats App
Whats App
Whats App
Whats App
//+------------------------------------------------------------------+
//| FIB CONNECT.mq5 |
//| Copyright 2024, @OXYGEN |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, @OXYGEN"
#property link "https://www.mql5.com"
#property version "1.01"
#property strict

#include // Include the trade library

input double RiskPercentage = 1.0; // Risk percentage per trade
input double StopLossPips = 50; // Stop loss in pips
input double TakeProfitPips = 100; // Take profit in pips
input int BarsToCheck = 100; // Number of bars to check for high and low points
input ENUM_TIMEFRAMES Timeframe = PERIOD_H1; // Timeframe to check for high and low points
input double LotSize = 2.0; // Default lot size
input int MaxEntries = 1; // Maximum number of open trades
input double TrailingStopAmount = 5.0; // Amount in profit to trigger trailing stop in dollars
input double TrailingStopDistance = 20; // Distance in points for trailing stop

CTrade trade; // Create an instance of the trade class

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60); // Set a timer to call OnTick every 60 seconds
Print("EA Initialized. Checking market conditions...");
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer(); // Kill the timer when the EA is deinitialized
ObjectsDeleteAll(0, 1); // Delete all objects when EA is removed
Print("EA Deinitialized");
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
Print("Tick received");

// Identify significant high and low points
double highPoint = iHigh(_Symbol, Timeframe, iHighest(_Symbol, Timeframe, MODE_HIGH, BarsToCheck, 0));
double lowPoint = iLow(_Symbol, Timeframe, iLowest(_Symbol, Timeframe, MODE_LOW, BarsToCheck, 0));

Print("High Point: ", highPoint, " Low Point: ", lowPoint);

// Calculate Fibonacci retracement levels
double fibLevels[6];
CalculateFibonacciLevels(highPoint, lowPoint, fibLevels);

Print("Fib Levels: 0.236=", fibLevels[0], " 0.382=", fibLevels[1], " 0.5=", fibLevels[2], " 0.618=", fibLevels[3], " 0.786=", fibLevels[4], " 0.886=", fibLevels[5]);

// Draw Fibonacci levels on the chart
DrawFibonacciLevels(highPoint, lowPoint, fibLevels);

// Get the current price
double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

Print("Current Price: ", currentPrice);

// Check the number of open trades
int totalOrders = 0;
for (int i = 0; i < PositionsTotal(); i++)
{
if (PositionGetSymbol(i) == _Symbol)
totalOrders++;
}

// Check if conditions are met for a buy
if (currentPrice <= fibLevels[2] && currentPrice > fibLevels[3] && totalOrders < MaxEntries)
{
Print("Buy Condition Met");
// Check if there are no open positions
if (!PositionSelect(_Symbol))
{
Print("No Open Positions - Placing Buy Order");
double lotSize = CalculateLotSize(StopLossPips);
if (trade.Buy(_Symbol, lotSize, 0, currentPrice, StopLossPips * _Point, TakeProfitPips * _Point))
{
Print("Buy Order Placed Successfully");
bool result = ObjectCreate(0, "BuySignal", OBJ_ARROW, 0, TimeCurrent(), currentPrice);
if (!result)
{
Print("Failed to create BuySignal. Error code: ", GetLastError());
}
else
{
ObjectSetInteger(0, "BuySignal", OBJPROP_COLOR, clrGreen);
ObjectSetInteger(0, "BuySignal", OBJPROP_WIDTH, 2);
ObjectSetInteger(0, "BuySignal", OBJPROP_ARROWCODE, 233);
}
}
else
{
Print("Buy Order Failed. Error code: ", GetLastError());
}
}
}
// Check if conditions are met for a sell
else if (currentPrice >= fibLevels[2] && currentPrice < fibLevels[1] && totalOrders < MaxEntries)
{
Print("Sell Condition Met");
// Check if there are no open positions
if (!PositionSelect(_Symbol))
{
Print("No Open Positions - Placing Sell Order");
double lotSize = CalculateLotSize(StopLossPips);
if (trade.Sell(_Symbol, lotSize, 0, currentPrice, StopLossPips * _Point, TakeProfitPips * _Point))
{
Print("Sell Order Placed Successfully");
bool result = ObjectCreate(0, "SellSignal", OBJ_ARROW, 0, TimeCurrent(), currentPrice);
if (!result)
{
Print("Failed to create SellSignal. Error code: ", GetLastError());
}
else
{
ObjectSetInteger(0, "SellSignal", OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, "SellSignal", OBJPROP_WIDTH, 2);
ObjectSetInteger(0, "SellSignal", OBJPROP_ARROWCODE, 234);
}
}
else
{
Print("Sell Order Failed. Error code: ", GetLastError());
}
}
}
else
{
// Update trailing stop if position is in profit
UpdateTrailingStop();
}
}

//+------------------------------------------------------------------+
//| Function to calculate lot size based on risk percentage |
//+------------------------------------------------------------------+
double CalculateLotSize(double stopLossPips)
{
double riskAmount = AccountInfoDouble(ACCOUNT_BALANCE) * (RiskPercentage / 100.0);
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double stopLossInCurrency = stopLossPips * tickValue / tickSize;
double lotSize = riskAmount / stopLossInCurrency;
Print("Calculated Lot Size: ", lotSize);
return NormalizeDouble(lotSize, 2); // Normalize lot size to 2 decimal places
}

//+------------------------------------------------------------------+
//| Function to calculate Fibonacci levels |
//+------------------------------------------------------------------+
void CalculateFibonacciLevels(double highPoint, double lowPoint, double &fibLevels[])
{
fibLevels[0] = highPoint - 0.236 * (highPoint - lowPoint);
fibLevels[1] = highPoint - 0.382 * (highPoint - lowPoint);
fibLevels[2] = highPoint - 0.5 * (highPoint - lowPoint);
fibLevels[3] = highPoint - 0.618 * (highPoint - lowPoint);
fibLevels[4] = highPoint - 0.786 * (highPoint - lowPoint);
fibLevels[5] = highPoint - 0.886 * (highPoint - lowPoint); // Added 0.886 level
}

//+------------------------------------------------------------------+
//| Function to draw Fibonacci levels on the chart |
//+------------------------------------------------------------------+
void DrawFibonacciLevels(double highPoint, double lowPoint, double &fibLevels[])
{
// Delete existing Fibonacci objects
for (int i = 0; i < 6; i++)
{
string fibName = "FibLevel" + IntegerToString(i);
ObjectDelete(0, fibName);
}

// Draw new Fibonacci levels
for (int i = 0; i < 6; i++)
{
string fibName = "FibLevel" + IntegerToString(i);
ObjectCreate(0, fibName, OBJ_HLINE, 0, TimeCurrent(), fibLevels[i]);
ObjectSetInteger(0, fibName, OBJPROP_COLOR, clrBlue);
ObjectSetInteger(0, fibName, OBJPROP_WIDTH, 1);
}
}

//+------------------------------------------------------------------+
//| Function to update trailing stop loss |
//+------------------------------------------------------------------+
void UpdateTrailingStop()
{
for (int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if (PositionGetSymbol(i) == _Symbol && PositionSelectByTicket(ticket))
{
double profit = PositionGetDouble(POSITION_PROFIT);
if (profit >= TrailingStopAmount)
{
double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
double newStopLoss = entryPrice + TrailingStopDistance * _Point;
if (trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
{
Print("Updated Buy Stop Loss to Breakeven");
}
else
{
Print("Failed to modify Buy Stop Loss. Error code: ", GetLastError());
}
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
double newStopLoss = entryPrice - TrailingStopDistance * _Point;
if (trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
{
Print("Updated Sell Stop Loss to Breakeven");
}
else
{
Print("Failed to modify Sell Stop Loss. Error code: ", GetLastError());
}
}
}
}
}
}

//+------------------------------------------------------------------+
//| Timer function to periodically check and update positions |
//+------------------------------------------------------------------+
void OnTimer()
{
Print("Timer event received");
UpdateTrailingStop();
}

GOOD DAY MY CODE RUN WITHOUT ERRORS BUT HOW IT SAYS implicit conversion from 'string' to 'number' fiboconnection 1.01.mq5 121 28
AND IT WONT DEBUGG CAN I GET SOME ASSISTANCE PLEASE
Conor Mcnamara
Conor Mcnamara 2024.07.12
parameters are in the wrong place
should be: if (trade.Sell(lotSize, _Symbol, currentPrice, StopLossPips * _Point, TakeProfitPips * _Point)) and if (trade.Buy(lotSize, _Symbol, currentPrice, StopLossPips * _Point, TakeProfitPips * _Point))
Whats App
Added topic need help to solve these error message for fibo
good day am trying to complete a simple fib but keep getting error messages wrong parameter count 'ObjectName' - wrong parameters count   harmonic finder asap.mq5   55   27 'ObjectDelete' - wrong parameters count   harmonic finder
Whats App
Registered at MQL5.community