DeanvH/Dean1

594 lines
22 KiB
Text
Raw Permalink Normal View History

2026-03-13 08:36:20 +00:00
///+------------------------------------------------------------------+
// | |
// | DeanvH |
// | |
// +------------------------------------------------------------------+
#property copyright "DeanvH"
#property link ""
#property version "3.0"
#include<Trade\Trade.mqh>
input int StopLoss = 200; // Stop loss in points
input int TakeProfit1 = 300; // Take profit 1 in points
input int TakeProfit2 = 450; // Take profit 2 in points
input int TakeProfit3 = 800; // Take profit 3 in points
input int BECheck = 200; // When Trade goes to BE
input double riskPercentage = 1; // Risk %
input int StartLondon = 3; // Hour when the bot becomes active for London
input int EndLondon = 14; // Hour when the bot stops being active for London
input int StartNY = 16; // Hour when the bot becomes active for NY
input int EndNY = 22; // Hour when the bot stops being active for NY
input int MaxTrades = 6; // Maximum Trades allowed at once
input int PosMax = 3; // Maximum Set(3) of Trades per Pair at once (Amount devided by 3)
input int RSIStocMax = 70; // Over Bought Stochastic level
input int RSIStocMin = 30; // Over Sold Stochastic level
//Fibo values
datetime highTime = 0;
datetime lowTime = 0;
double highestHigh = 0;
double lowestLow = 0;
int period = 150;
bool trendUp = false;
bool trendDown = false;
bool GoldZoneDown = false;
bool GoldZoneUp = false;
double golden = 0;
//Creat CTrade
CTrade trade;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool InTradingTime()
{
datetime Now = TimeCurrent();
MqlDateTime NowStruct;
TimeToStruct(Now,NowStruct);
int StartTradingSeconds1 = (StartLondon*3600);
int EndTradingSeconds1 = (EndLondon*3600);
int StartTradingSeconds2 = (StartNY*3600);
int EndTradingSeconds2 = (EndNY*3600);
int runningseconds = (NowStruct.hour*3600);
ZeroMemory(NowStruct);
if(((runningseconds>StartTradingSeconds1)&&(runningseconds<EndTradingSeconds1)) || ((runningseconds>StartTradingSeconds2)&&(runningseconds<EndTradingSeconds2)))
{
/*Print("Close Candle Price = ", iClose(Symbol(), PERIOD_H1, 1));
Print("Open Candle Price = ", iOpen(Symbol(), PERIOD_H1, 0));*/
return(true);
}
//Print("outside of trading time");
return(false);
}
//------------------------------------------------------------------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize the indicator
UpdateFibonacciRetracement();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Remove the Fibonacci retracement when the indicator is removed
RemoveFibonacciRetracement();
}
//------------------------------------------------------------------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
InTradingTime();
//Create string for signal
string signal ="";
//int count = 0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AskPrice = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits);
double BidPrice = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits);
// Calculate lot size based on risk percentage (e.g., 1%)
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double LotSize = NormalizeDouble((accountBalance * riskPercentage / 100 / StopLoss),2);
// Get candle closes
double currentClose = iClose(Symbol(), PERIOD_H1, 0);
double LastClose = iClose(Symbol(),PERIOD_H1,1);
double Last2Close = iClose(Symbol(),PERIOD_H1,2);
double currentLow = iLow(Symbol(), PERIOD_H1, 0);
double LastLow = iLow(Symbol(), PERIOD_H1, 1);
double SecondLastLow = iLow(Symbol(), PERIOD_H1, 2);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double CurrentOpen = iOpen(Symbol(), PERIOD_H1, 0);
double LastOpen = iOpen(Symbol(), PERIOD_H1, 1);
double Last2Open = iOpen(Symbol(), PERIOD_H1, 2);
double currentHigh = iHigh(Symbol(), PERIOD_H1, 0);
double LastHigh = iHigh(Symbol(), PERIOD_H1, 1);
double SecondLastHigh = iHigh(Symbol(), PERIOD_H1, 2);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int LowestIndex = iLowest(Symbol(),PERIOD_H1,MODE_LOW,100,0);
int HighestIndex = iHighest(Symbol(),PERIOD_H1,MODE_HIGH,100,0);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lowest = iLow(Symbol(),PERIOD_H1,LowestIndex);
double Highest = iHigh(Symbol(),PERIOD_H1,HighestIndex);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int HighIndex1 = iHighest(Symbol(),PERIOD_H1,MODE_HIGH,20);
int HighIndex2 = iHighest(Symbol(),PERIOD_H1,MODE_HIGH,40,21);
int HighIndex3 = iHighest(Symbol(),PERIOD_H1,MODE_HIGH,40,61);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double High1 = iHigh(Symbol(),PERIOD_H1,HighIndex1);
double High2 = iHigh(Symbol(),PERIOD_H1,HighIndex2);
double High3 = iHigh(Symbol(),PERIOD_H1,HighIndex3);
//Comment();
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
ObjectCreate(0,"High1",OBJ_HLINE,0,0,High1);
ObjectSetInteger(0,"High1",OBJPROP_WIDTH,2); //set object width
ObjectSetInteger(0,"High1",OBJPROP_COLOR,clrWhite); //set object colour
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
ObjectCreate(0,"High2",OBJ_HLINE,0,0,High2);
ObjectSetInteger(0,"High2",OBJPROP_WIDTH,2); //set object width
ObjectSetInteger(0,"High2",OBJPROP_COLOR,clrLimeGreen); //set object colour
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
ObjectCreate(0,"High3",OBJ_HLINE,0,0,High3);
ObjectSetInteger(0,"High3",OBJPROP_WIDTH,2); //set object width
ObjectSetInteger(0,"High3",OBJPROP_COLOR,clrMagenta); //set object colour
//------------------------------------------------------------------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Defining Trends |
//+------------------------------------------------------------------+
if(highTime > lowTime)
{
trendUp = true;
trendDown = false;
}
if(lowTime > highTime)
{
trendUp = false;
trendDown = true;
}
//+------------------------------------------------------------------+
//| Defining Gold Zone |
//+------------------------------------------------------------------+
if(trendDown && (currentHigh > golden || LastHigh > golden || SecondLastHigh > golden))
{
GoldZoneDown = true;
GoldZoneUp = false;
}
else
if(trendUp && (currentLow < golden || LastLow < golden || SecondLastLow < golden))
{
GoldZoneDown = false;
GoldZoneUp = true;
}
else
{
GoldZoneDown = false;
GoldZoneUp = false;
}
UpdateFibonacciRetracement();
//------------------------------------------------------------------------------------------------------------------------------------------------
double MacdArrayH1[];
//Define MACD
int macdDefH1 = iMACD(NULL, PERIOD_H1, 5, 15, 9, PRICE_WEIGHTED);
//Add values to MACD Array
ArraySetAsSeries(MacdArrayH1,true);
CopyBuffer(macdDefH1,0,0,3,MacdArrayH1);
double macdValueH1 = MacdArrayH1[0];
double LastmacdValueH1 = MacdArrayH1[1];
double Last2macdValueH1 = MacdArrayH1[2];
//--------------------------------------------------------------
//STOCHASTIC
//Stochastic ArrayH1
double stochKArrayH1[];
double stochDArrayH1[];
//Define Stochastic
int stochDefH1 = iStochastic(NULL, PERIOD_H1, 7, 3, 2, MODE_EMA, STO_CLOSECLOSE);
//Add values
ArraySetAsSeries(stochKArrayH1,true);
ArraySetAsSeries(stochDArrayH1,true);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CopyBuffer(stochDefH1,0,0,3,stochKArrayH1);
CopyBuffer(stochDefH1,1,0,3,stochDArrayH1);
double stochKValue0H1 = stochKArrayH1[0];
double stochDValue0H1 = stochDArrayH1[0];
double stochKValue1H1 = stochKArrayH1[1];
double stochDValue1H1 = stochDArrayH1[1];
//----------------------
//--------------------------------------------------------------
//RSI
//RSI Array H1
double rsiArrayH1[];
//Define RSI H1
int rsiDefH1 = iRSI(NULL, PERIOD_H1, 8, PRICE_TYPICAL);
//Add values to array
ArraySetAsSeries(rsiArrayH1,true);
CopyBuffer(rsiDefH1,0,0,3,rsiArrayH1);
double rsiValueH1 = NormalizeDouble(rsiArrayH1[0],2);
//--------------------------------------------------------------
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Comment(/*" StochDArray[1] = ", stochDArrayH1[1],
"\n StochKArray[1] = ", stochKArrayH1[1],
"\n Last MACD Value = ", LastmacdValueH1,
"\n 2nd Last MACD Value = ", Last2macdValueH1,
"\n Current Open Price = ", CurrentOpen,
"\n Last Open Price = ", LastOpen,
"\n 2nd Last Open = ", Last2Open,
"\n RSI Current = ", rsiArrayH1[0],
"\n RSI Last = ", rsiArrayH1[1],
"\nHigh1 = ",High1, " ", HighIndex1,
"\nHigh2 = ", High2, " ", HighIndex2, " Green",
"\nHigh3 = ", High3, " ", HighIndex3, " Magenta",
"\n Highest = ", Highest,*/
"\n C.Low = ", currentLow,
"\n 1.Low = ", LastLow,
"\n 2.Low = ", SecondLastLow,
"\n C.High = ", currentHigh,
"\n 1.High = ", LastHigh,
"\n 2.High = ", SecondLastHigh,
"\n Golden 61.8% = ", golden,
"\n trendUp = ", trendUp,
"\n trendDown = ", trendDown,
"\n GoldZoneUp = ", GoldZoneUp,
"\n GoldZoneDown = ", GoldZoneDown);
if(InTradingTime())
{
if(trendDown && GoldZoneDown)
{
// Check for sell trade condition
if(stochDArrayH1[1] > stochKArrayH1[1] &&
stochDArrayH1[1] > RSIStocMax &&
LastmacdValueH1 < Last2macdValueH1 &&
CurrentOpen < LastOpen &&
CurrentOpen < Last2Open &&
rsiArrayH1[0] < rsiArrayH1[2]
)
{
signal="sell";
}
if(signal == "sell" && OpenTrades() < PosMax && PositionsTotal() < MaxTrades)
{
trade.Sell(LotSize,NULL,BidPrice,(BidPrice + StopLoss * _Point),(BidPrice - TakeProfit1 * _Point));
trade.Sell(LotSize,NULL,BidPrice,(BidPrice + StopLoss * _Point),(BidPrice - TakeProfit2 * _Point));
trade.Sell(LotSize,NULL,BidPrice,(BidPrice + StopLoss * _Point),(BidPrice - TakeProfit3 * _Point));
}
//Call Break Even Check
CheckBreakEvenSell(BidPrice);
}
}
//------------------------------------------------------------------
// Check for buy trade condition
if(InTradingTime())
{
// Check for buy trade condition
if(trendUp && GoldZoneUp)
{
if(stochDArrayH1[1] < stochKArrayH1[1] &&
stochDArrayH1[1] < RSIStocMin &&
LastmacdValueH1 > Last2macdValueH1 &&
CurrentOpen > LastOpen &&
CurrentOpen > Last2Open &&
rsiArrayH1[0] > rsiArrayH1[2]
)
{
signal="buy";
}
if(signal == "buy" && OpenTrades() < PosMax && PositionsTotal() < MaxTrades)
{
trade.Buy(LotSize,NULL,AskPrice,(AskPrice - StopLoss * _Point),(AskPrice + TakeProfit1 * _Point));
trade.Buy(LotSize,NULL,AskPrice,(AskPrice - StopLoss * _Point),(AskPrice + TakeProfit2 * _Point));
trade.Buy(LotSize,NULL,AskPrice,(AskPrice - StopLoss * _Point),(AskPrice + TakeProfit3 * _Point));
}
//Call Break Even Check
CheckBreakEvenBuy(AskPrice);
//--------------------------------------------
}
}
//END
}
//---------------------------------------------------------------------
//Check for amount of open trades
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OpenTrades()
{
int count = 0;
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
ulong PositionType = PositionGetInteger(POSITION_TYPE);
if(Symbol() == symbol)
{
if(PositionType==POSITION_TYPE_BUY || PositionType ==POSITION_TYPE_SELL)
{
count++;
}
}
}
return count;
}
//---------------------------------------------------------------------
//+------------------------------------------------------------------+
//|BreakEven Function |
//+------------------------------------------------------------------+
void CheckBreakEvenBuy(double AskPrice)
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double PositionSL = PositionGetDouble(POSITION_SL);
double PositionTP = PositionGetDouble(POSITION_TP);
double PositionCP = PositionBuyPrice + BECheck*_Point;
ulong PositionType = PositionGetInteger(POSITION_TYPE);
if(_Symbol == symbol)
{
if(PositionType==POSITION_TYPE_BUY)
{
if(PositionSL<PositionBuyPrice)
{
if(AskPrice > PositionCP)
{
trade.PositionModify(PositionTicket,PositionBuyPrice+10*_Point,PositionTP);
Print("BE Achieved");
}
}
}
}
}
}
//---------------------------------------------------------------------
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CheckBreakEvenSell(double BidPrice)
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
double PositionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double PositionSL = PositionGetDouble(POSITION_SL);
double PositionTP = PositionGetDouble(POSITION_TP);
double PositionCP = PositionOpenPrice - BECheck*_Point;
ulong PositionType = PositionGetInteger(POSITION_TYPE);
if(_Symbol == symbol)
{
if(PositionType==POSITION_TYPE_SELL)
{
if(PositionSL>PositionOpenPrice)
{
if(BidPrice < PositionCP)
{
trade.PositionModify(PositionTicket,PositionOpenPrice-10*_Point,PositionTP);
Print("BE Achieved");
}
}
}
}
}
}
//+--------------------------------------------------------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CreateFibonacciRetracementDown(string name, datetime highTime, double highestHigh, datetime lowTime, double lowestLow)
{
// Create Fibonacci retracement object
if(ObjectFind(0, name) < 0)
{
ObjectCreate(0, name, OBJ_FIBO, 0, highTime, highestHigh, lowTime, lowestLow);
ObjectSetInteger(0, name, OBJPROP_COLOR, clrBlue); // Set color of Fibonacci retracement
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID); // Set style
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1); // Set width
ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT,true);
ObjectSetInteger(0, name, OBJPROP_LEVELCOLOR,clrAntiqueWhite);
golden = highestHigh - (highestHigh - lowestLow) * 0.382;
}
else
{
// Update existing Fibonacci retracement object
ObjectSetDouble(0, name, OBJPROP_PRICE, highestHigh);
ObjectSetDouble(0, name, OBJPROP_PRICE, lowestLow);
ObjectSetInteger(0, name, OBJPROP_TIME, highTime);
ObjectSetInteger(0, name, OBJPROP_TIME, lowTime);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CreateFibonacciRetracementUp(string name, datetime highTime, double highestHigh, datetime lowTime, double lowestLow)
{
// Create Fibonacci retracement object
if(ObjectFind(0, name) < 0)
{
ObjectCreate(0, name, OBJ_FIBO, 0, lowTime, lowestLow, highTime, highestHigh);
ObjectSetInteger(0, name, OBJPROP_COLOR, clrBlue); // Set color of Fibonacci retracement
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DASH); // Set style
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1); // Set width
ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT,true);
ObjectSetInteger(0, name, OBJPROP_LEVELCOLOR,clrAntiqueWhite);
// Optional: Set properties for Fibonacci retracement levels (percentage, etc.)
//ObjectSetDouble(0, name, OBJPROP_LEVELVALUE, 0.000); // 0% level
//sixty = ObjectGetDouble(0, name, OBJPROP_PRICE , 0.618); // 61.8% level
//zero = ObjectGetDouble(0, name, OBJPROP_PRICE, 3); // 76.4% level
golden = lowestLow + (highestHigh - lowestLow) * 0.382;
}
else
{
// Update existing Fibonacci retracement object
ObjectSetDouble(0, name, OBJPROP_PRICE, highestHigh);
ObjectSetDouble(0, name, OBJPROP_PRICE, lowestLow);
ObjectSetInteger(0, name, OBJPROP_TIME, highTime);
ObjectSetInteger(0, name, OBJPROP_TIME, lowTime);
}
}
//+--------------------------------------------------------------------------------------------------------------------------------------------------+
void UpdateFibonacciRetracement()
{
// Find the highest high and lowest low in the specified period
highestHigh = iHigh(NULL, PERIOD_H1, iHighest(NULL, PERIOD_H1, MODE_HIGH, period, 0));
lowestLow = iLow(NULL, PERIOD_H1, iLowest(NULL, PERIOD_H1, MODE_LOW, period, 0));
highTime = iTime(NULL, PERIOD_H1, iHighest(NULL, PERIOD_H1, MODE_HIGH, period, 0));
lowTime = iTime(NULL, PERIOD_H1, iLowest(NULL, PERIOD_H1, MODE_LOW, period, 0));
// Remove old Fibonacci retracement if it exists
RemoveFibonacciRetracement();
// Create a new Fibonacci retracement object
if(trendUp)
{
CreateFibonacciRetracementUp("FiboRetracement", highTime, highestHigh, lowTime, lowestLow);
}
else
{
CreateFibonacciRetracementDown("FiboRetracement", highTime, highestHigh, lowTime, lowestLow);
}
}
//+--------------------------------------------------------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void RemoveFibonacciRetracement()
{
// Remove Fibonacci retracement if it exists
string name = "FiboRetracement";
if(ObjectFind(0, name) >= 0)
{
ObjectDelete(0, name);
}
}
//+------------------------------------------------------------------+