This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
// ------------------------------------------------------------------ //| A.I. Grand.mq5 | //| Copyright 2024, Blackmammath | // ------------------------------------------------------------------
property strict
property indicator_chart_window
property indicator_buffers 14
property indicator_plots 11
property indicator_label1 "High Band"
property indicator_type1 DRAW_LINE
property indicator_color1 clrRed
property indicator_label2 "Low Band"
property indicator_type2 DRAW_LINE
property indicator_color2 clrBlue
property indicator_label3 "Range Filter"
property indicator_type3 DRAW_LINE
property indicator_color3 clrGreen
property indicator_label4 "Volatility Oscillator"
property indicator_type4 DRAW_LINE
property indicator_color4 clrOrange
property indicator_label5 "Volume"
property indicator_type5 DRAW_HISTOGRAM
property indicator_color5 clrBlue
property indicator_label6 "Signal"
property indicator_type6 DRAW_LINE
property indicator_color6 clrRed
property indicator_label7 "Heiken Ashi Open"
property indicator_type7 DRAW_LINE
property indicator_color7 clrGreen
property indicator_label8 "Heiken Ashi High"
property indicator_type8 DRAW_LINE
property indicator_color8 clrGreen
property indicator_label9 "Heiken Ashi Low"
property indicator_type9 DRAW_LINE
property indicator_color9 clrRed
property indicator_label10 "Heiken Ashi Close"
property indicator_type10 DRAW_LINE
property indicator_color10 clrRed
property indicator_label11 "Heiken Ashi Candles"
property indicator_type11 DRAW_CANDLES
property indicator_color11 clrGreen, clrRed
//--- input parameters for VolatilityOscillator input int VolatilityPeriod = 14; // Period for Volatility calculation input ENUM_APPLIED_PRICE AppliedPrice = PRICE_CLOSE; // Applied price
//--- input parameters for RangeFilterIndicator input int ATR_Period = 14; input double Multiplier = 3.0; input int Range_Filter_Period = 100; input string Filter_Type = "Type 1"; input bool Smooth = true; input int Smooth_Period = 14; input bool Apply_Average = true; input int Average_Period = 10;
//--- input parameters for VolumeProfileIndicator input string SymbolOverride = ""; // Override symbol (e.g., "EURUSD") input bool OverrideInstrument = false; // Whether to override the instrument or not input int SmaPeriod = 10; // SMA period input double VolumeMultiplier = 1.5; input double HighVolumeMultiplier = 2.0;
//--- input parameters for AdvancedVectorZone input int ZonePeriod = 20; // Period to identify zones input double VolumeThreshold = 2.0; // Volume threshold multiplier input double VolatilityThreshold = 1.5; // Volatility threshold multiplier input color BullishZoneColor = clrGreen; // Color for bullish zones (Green) input color BearishZoneColor = clrRed; // Color for bearish zones (Red) input color NeutralZoneColor = clrGray; // Color for neutral zones input int ZoneTransparency = 50; // Transparency for the zones
//--- input parameters for SupplyDemandIndicator input int SwingLength = 10; // Swing High/Low Length input int HistoryToKeep = 20; // History To Keep input double BoxWidth = 2.5; // Supply/Demand Box Width input bool ShowZigZag = false; // Show Zig Zag input bool ShowPriceActionLabels = false; // Show Price Action Labels input color SupplyColor = clrRed; // Supply Color (Red for Sell) input color SupplyOutlineColor = clrWhite; // Supply Outline Color input color DemandColor = clrGreen; // Demand Color (Green for Buy) input color DemandOutlineColor = clrWhite; // Demand Outline Color input color POILabelColor = clrWhite; // POI Label Color input color SwingTypeColor = clrBlack; // Price Action Label Color input color ZigZagColor = clrRed; // Zig Zag Color
//--- input parameters for the EA input double InitialLotSize = 0.05; input double MartingaleFactor = 1.2; input double RiskRatioPerBalance = 0.01; input int MaxTrades = 6; input int TradeIncreaseThreshold = 500; input double LotSizeIncrease = 0.02; input double MaxLotSize = 2.0;
//--- indicator buffers double HiBandBuffer[]; double LoBandBuffer[]; double RngFiltBuffer[]; double VolatilityBuffer[]; double VolumeBuffer[]; double SignalBuffer[]; double VolumeSmaBuffer[]; double haOpenBuffer[]; // Declare Heiken Ashi buffers double haHighBuffer[]; double haLowBuffer[]; double haCloseBuffer[]; double TempArray[]; // Temporary array to hold values for MA calculations
//--- Handles for the iStdDev indicator int stdDevHandle; int zoneIndex = 0; // Used to track the zones created by the Advanced Vector Zone
//--- Global variables for the EA double accountBalance; double lotSize; bool tradeOpen = false; int tradeDirection = 0; // 0 = No Trade, 1 = Buy, -1 = Sell
// ------------------------------------------------------------------ //| Custom indicator initialization function | // ------------------------------------------------------------------ int OnInit() { // Set the EA name on the chart (Method 1) ChartSetString(0, CHART_COMMENT, "Custom EA: My Custom Expert Advisor");
// OR (only include this if you prefer the text label method)
// Create a text label to display the EA name (Method 2)
string labelName = "EA_Name_Label";
if (!ObjectCreate(0, labelName, OBJ_LABEL, 0, 0, 0))
{
Print("Failed to create label: ", labelName);
}
else
{
ObjectSetString(0, labelName, OBJPROP_TEXT, "Custom EA: My Custom Expert Advisor");
ObjectSetInteger(0, labelName, OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, labelName, OBJPROP_XDISTANCE, 10);
ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, labelName, OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 14);
ObjectSetInteger(0, labelName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, labelName, OBJPROP_HIDDEN, true);
}
// Initialization code for indicators and other settings
SetIndexBuffer(0, HiBandBuffer);
SetIndexBuffer(1, LoBandBuffer);
SetIndexBuffer(2, RngFiltBuffer);
SetIndexBuffer(3, VolatilityBuffer);
SetIndexBuffer(4, VolumeBuffer);
SetIndexBuffer(5, SignalBuffer);
SetIndexBuffer(6, VolumeSmaBuffer);
SetIndexBuffer(7, haOpenBuffer);
SetIndexBuffer(8, haHighBuffer);
SetIndexBuffer(9, haLowBuffer);
SetIndexBuffer(10, haCloseBuffer);
// Set the colors for Heiken Ashi candles
SetIndexBuffer(11, haCloseBuffer, INDICATOR_COLOR_INDEX);
// Ensure TempArray is set as a series
ArraySetAsSeries(TempArray, true);
// Create handle for the standard deviation indicator (Volatility Oscillator)
stdDevHandle = iStdDev(NULL, 0, VolatilityPeriod, 0, MODE_SMA, AppliedPrice);
if(stdDevHandle == INVALID_HANDLE)
{
Print("Failed to create iStdDev handle");
return(INIT_FAILED);
}
// Initialize EA variables
accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
lotSize = InitialLotSize;
return(INIT_SUCCEEDED);
}
// ------------------------------------------------------------------ //| Copy data from another instrument | // ------------------------------------------------------------------ void CopyInstrumentData(string symbol, ENUM_TIMEFRAMES timeframe, int rates_total) { MqlRates rates[]; ArraySetAsSeries(rates, true);
if (CopyRates(symbol, timeframe, 0, rates_total, rates) > 0)
{
for (int i = 0; i < rates_total; i )
{
VolumeBuffer[i] = (double)rates[i].tick_volume;
SignalBuffer[i] = rates[i].close; // Placeholder, modify as needed
}
}
else
{
Print("Failed to copy data for symbol: ", symbol);
}
}
// ------------------------------------------------------------------ //| Calculate SMA of Volume | // ------------------------------------------------------------------ double CalculateVolumeSma(int start, int period) { double sum = 0.0; for (int i = start; i < start period && i < ArraySize(VolumeBuffer); i ) { sum = VolumeBuffer[i]; } return sum / period; }
// ------------------------------------------------------------------ //| Find the highest value in an array | // ------------------------------------------------------------------ double FindHighest(double &array[], int start, int period) { double maxValue = array[start]; for (int i = start; i < start period && i < ArraySize(array); i ) { if (array[i] > maxValue) maxValue = array[i]; } return maxValue; }
// ------------------------------------------------------------------ //| Calculate Heiken Ashi values | // ------------------------------------------------------------------ void CalculateHeikenAshi(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) { haCloseBuffer[0] = (open[0] high[0] low[0] close[0]) / 4.0; if (rates_total > 1) { haOpenBuffer[0] = (haOpenBuffer[1] haCloseBuffer[1]) / 2.0; haHighBuffer[0] = MathMax(high[0], MathMax(haOpenBuffer[0], haCloseBuffer[0])); haLowBuffer[0] = MathMin(low[0], MathMin(haOpenBuffer[0], haCloseBuffer[0])); } else { haOpenBuffer[0] = (open[0] close[0]) / 2.0; haHighBuffer[0] = MathMax(high[0], MathMax(haOpenBuffer[0], haCloseBuffer[0])); haLowBuffer[0] = MathMin(low[0], MathMin(haOpenBuffer[0], haCloseBuffer[0])); } }
// ------------------------------------------------------------------ //| Calculate Supply/Demand Zones | // ------------------------------------------------------------------ void CalculateSupplyDemandZones(int rates_total, const double &high[], const double &low[], const datetime &time[]) { for(int i = 0; i < rates_total; i ) { double supplyLevel = high[i] BoxWidth * Point(); double demandLevel = low[i] - BoxWidth * Point();
string supplyName = "Supply_" IntegerToString(i);
string demandName = "Demand_" IntegerToString(i);
ObjectCreate(0, supplyName, OBJ_RECTANGLE, 0, time[i], supplyLevel, time[i 1], high[i]);
ObjectSetInteger(0, supplyName, OBJPROP_COLOR, (color)SupplyColor);
ObjectSetInteger(0, supplyName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, supplyName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, supplyName, OBJPROP_BACK, true);
ObjectCreate(0, demandName, OBJ_RECTANGLE, 0, time[i], demandLevel, time[i 1], low[i]);
ObjectSetInteger(0, demandName, OBJPROP_COLOR, (color)DemandColor);
ObjectSetInteger(0, demandName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, demandName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, demandName, OBJPROP_BACK, true);
}
}
// ------------------------------------------------------------------ //| Custom indicator iteration function | // ------------------------------------------------------------------ int OnCalculate(const int rates_total, // number of available bars in history at the current tick const int prev_calculated,// number of bars calculated in the previous call const datetime &time[], // Time const double &open[], // Open const double &high[], // High const double &low[], // Low const double &close[], // Close const long &tick_volume[],// Tick Volume const long &volume[], // Real Volume const int &spread[]) // Spread { int barsToProcess = rates_total - prev_calculated; if (barsToProcess <= 0) return rates_total;
string symbol = OverrideInstrument ? SymbolOverride : Symbol();
ENUM_TIMEFRAMES timeframe = PERIOD_CURRENT;
// Copy data from the selected instrument for Volume Profile
CopyInstrumentData(symbol, timeframe, rates_total);
// Calculate the SMA of the volume for Volume Profile
for (int i = 0; i < rates_total; i )
{
VolumeSmaBuffer[i] = CalculateVolumeSma(i, SmaPeriod);
}
// Calculate the Range Filter values
for (int i = prev_calculated; i < rates_total; i )
{
double rng = iATR(NULL, 0, ATR_Period); // Assuming the ATR is used for the Range Filter
double hi_band, lo_band, rng_filt_val;
hi_band = high[i] rng * Multiplier;
lo_band = low[i] - rng * Multiplier;
rng_filt_val = (hi_band lo_band) / 2;
HiBandBuffer[i] = hi_band;
LoBandBuffer[i] = lo_band;
RngFiltBuffer[i] = rng_filt_val;
}
// Retrieve the calculated standard deviation values from the iStdDev handle (Volatility Oscillator)
if(CopyBuffer(stdDevHandle, 0, 0, barsToProcess, VolatilityBuffer) <= 0)
{
Print("Failed to copy data from iStdDev buffer");
return prev_calculated;
}
// Calculate Heiken Ashi values
CalculateHeikenAshi(rates_total, open, high, low, close);
// Calculate Supply/Demand Zones
CalculateSupplyDemandZones(rates_total, high, low, time);
// Calculate Volume Profile Signal
for (int i = prev_calculated; i < rates_total; i )
{
double value2 = VolumeBuffer[i] * (high[i] - low[i]);
double highest_value2 = FindHighest(VolumeBuffer, i, SmaPeriod);
double imnt_override_pvsra_calc_part2 = (VolumeBuffer[i] >= VolumeSmaBuffer[i] * VolumeMultiplier) ? 2 : 0;
double va = (VolumeBuffer[i] >= VolumeSmaBuffer[i] * HighVolumeMultiplier || value2 >= highest_value2) ? 1 : imnt_override_pvsra_calc_part2;
SignalBuffer[i] = va;
}
// Process Advanced Vector Zone Logic
for (int i = prev_calculated; i < rates_total; i )
{
// Calculate the average volume and volatility for the period
double avgVolume = 0.0;
double avgVolatility = 0.0;
for (int j = i - ZonePeriod 1; j <= i; j )
{
avgVolume = (double)volume[j];
avgVolatility = high[j] - low[j];
}
avgVolume /= ZonePeriod;
avgVolatility /= ZonePeriod;
// Check for high volume and volatility
if ((double)volume[i] >= avgVolume * VolumeThreshold && (high[i] - low[i]) >= avgVolatility * VolatilityThreshold)
{
// Determine if it's a bullish or bearish zone
color zoneColor;
if (close[i] > open[i])
{
zoneColor = (color)(ColorToARGB(BullishZoneColor, ZoneTransparency));
}
else if (close[i] < open[i])
{
zoneColor = (color)(ColorToARGB(BearishZoneColor, ZoneTransparency));
}
else
{
zoneColor = (color)(ColorToARGB(NeutralZoneColor, ZoneTransparency));
}
// Create the zone on the chart
string zoneName = "Zone_" IntegerToString(zoneIndex );
ObjectCreate(0, zoneName, OBJ_RECTANGLE, 0, time[i], high[i], time[i 1], low[i]);
ObjectSetInteger(0, zoneName, OBJPROP_COLOR, zoneColor);
ObjectSetInteger(0, zoneName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, zoneName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, zoneName, OBJPROP_BACK, true);
}
}
// Process EA logic only after the indicator has been calculated
OnTick();
return rates_total;
}
// ------------------------------------------------------------------ //| Expert tick function | // ------------------------------------------------------------------ void OnTick() { // Since the indicator is calculated in OnCalculate, we'll use the last calculated values double volatilitySignal = VolatilityBuffer[0]; // Use the latest value from the buffer double rangeFilterSignal = RngFiltBuffer[0]; // Use the latest value from the buffer double volumeProfileSignal = SignalBuffer[0]; // Use the latest value from the buffer double currentPrice = iClose(_Symbol, _Period, 0); double supplyZone = CalculateSupplyZone(); double demandZone = CalculateDemandZone();
// Adjust lot size based on account balance
AdjustLotSize();
// Check if a trade is open
if(tradeOpen)
{
ManageOpenTrades(currentPrice, supplyZone, demandZone, volatilitySignal, rangeFilterSignal, volumeProfileSignal);
}
else
{
// No open trades, checking for new trade signals...
CheckForNewTrades(volatilitySignal, rangeFilterSignal, volumeProfileSignal);
}
}
// ------------------------------------------------------------------ //| Adjust lot size based on account balance | // ------------------------------------------------------------------ void AdjustLotSize() { double balanceIncreaseFactor = MathFloor(accountBalance / TradeIncreaseThreshold); double newLotSize = InitialLotSize (balanceIncreaseFactor * LotSizeIncrease);
if(newLotSize > MaxLotSize)
newLotSize = MaxLotSize;
lotSize = newLotSize;
}
// ------------------------------------------------------------------ //| Manage open trades based on conditions | // ------------------------------------------------------------------ void ManageOpenTrades(double currentPrice, double supplyZone, double demandZone, double volatilitySignal, double rangeFilterSignal, double volumeProfileSignal) { if(tradeDirection == 1) // Buy Trade { if(currentPrice >= supplyZone) { CloseTrade(); } else if(currentPrice <= demandZone && (volatilitySignal < 0 && rangeFilterSignal < 0 && volumeProfileSignal == 0)) { CloseTrade(); OpenSellTrade(); } } else if(tradeDirection == -1) // Sell Trade { if(currentPrice <= demandZone) { CloseTrade(); } else if(currentPrice >= supplyZone && (volatilitySignal > 0 && rangeFilterSignal > 0 && volumeProfileSignal == 1)) { CloseTrade(); OpenBuyTrade(); } } }
// ------------------------------------------------------------------ //| Check for new trades based on signals | // ------------------------------------------------------------------ void CheckForNewTrades(double volatilitySignal, double rangeFilterSignal, double volumeProfileSignal) { if(volatilitySignal > 0 && rangeFilterSignal > 0 && volumeProfileSignal == 1) { OpenBuyTrade(); } else if(volatilitySignal < 0 && rangeFilterSignal < 0 && volumeProfileSignal == 0) { OpenSellTrade(); } }
// ------------------------------------------------------------------ //| Open a buy trade | // ------------------------------------------------------------------ void OpenBuyTrade() { if(CheckTradeConditions()) { tradeDirection = 1; tradeOpen = true; ExecuteTrade(ORDER_TYPE_BUY); } }
// ------------------------------------------------------------------ //| Open a sell trade | // ------------------------------------------------------------------ void OpenSellTrade() { if(CheckTradeConditions()) { tradeDirection = -1; tradeOpen = true; ExecuteTrade(ORDER_TYPE_SELL); } }
// ------------------------------------------------------------------ //| Execute the trade | // ------------------------------------------------------------------ void ExecuteTrade(int tradeType) { double price = (tradeType == ORDER_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID); double stopLoss = 0; double takeProfit = 0;
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = lotSize;
request.type = tradeType;
request.price = price;
request.deviation = 3;
request.magic = 123456;
request.sl = stopLoss;
request.tp = takeProfit;
request.type_filling = (ENUM_ORDER_TYPE_FILLING)ORDER_FILLING_FOK;
Print("Attempting to open trade: Type = ", IntegerToString(tradeType), " Price = ", DoubleToString(price, 5), " Volume = ", DoubleToString(lotSize, 2));
if(!OrderSend(request, result))
{
Print("Error opening order: ", result.retcode);
}
else
{
Print("Trade opened successfully. Ticket: ", result.order);
}
}
// ------------------------------------------------------------------ //| Close current trade | // ------------------------------------------------------------------ void CloseTrade() { for(int i = PositionsTotal() - 1; i >= 0; i--) { if(PositionSelect(i)) { int positionType = (int)PositionGetInteger(POSITION_TYPE); if(positionType == ((tradeDirection == 1) ? POSITION_TYPE_BUY : POSITION_TYPE_SELL)) { ulong ticket = PositionGetInteger(POSITION_IDENTIFIER); double volume = (double)PositionGetDouble(POSITION_VOLUME); double closePrice = (tradeDirection == 1) ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = volume;
request.price = closePrice;
request.type = (tradeDirection == 1 ? ORDER_TYPE_SELL : ORDER_TYPE_BUY);
request.position = ticket;
request.type_filling = (ENUM_ORDER_TYPE_FILLING)ORDER_FILLING_FOK;
if(!OrderSend(request, result))
{
Print("Error closing order: ", result.retcode);
}
else
{
tradeOpen = false;
tradeDirection = 0;
}
}
}
}
}
// ------------------------------------------------------------------ //| Check if trade conditions are met | // ------------------------------------------------------------------ bool CheckTradeConditions() { if(accountBalance < 2000 && PositionsTotal() >= 1) return false; if(accountBalance >= 2000 && accountBalance < 10000 && PositionsTotal() >= 3) return false; if(accountBalance >= 10000 && PositionsTotal() >= 6) return false; return true; }
// ------------------------------------------------------------------ //| Calculate Supply Zone (Placeholder) | // ------------------------------------------------------------------ double CalculateSupplyZone() { // Implement your logic to calculate the supply zone here // Example: return a level above the current price where resistance is expected return iHigh(_Symbol, _Period, 0) 100 * _Point; // Placeholder logic }
// ------------------------------------------------------------------ //| Calculate Demand Zone (Placeholder) | // ------------------------------------------------------------------ double CalculateDemandZone() { // Implement your logic to calculate the demand zone here // Example: return a level below the current price where support is expected return iLow(_Symbol, _Period, 0) - 100 * _Point; // Placeholder logic }
// ------------------------------------------------------------------ //| Custom indicator deinitialization function | // ------------------------------------------------------------------ void OnDeinit(const int reason) { // Clean up all zone objects when the indicator is removed for (int i = 0; i < zoneIndex; i ) { string zoneName = "Zone_" IntegerToString(i); ObjectDelete(0, zoneName); }
// Release the handle for iStdDev indicator
IndicatorRelease(stdDevHandle);
}
Subreddit
Post Details
- Posted
- 4 months ago
- Reddit URL
- View post on reddit.com
- External URL
- i.redd.it/jvy46yk32gmd1....