Experts: Fractured Fractals - page 2

 
/* Fractured Fractals (barabashkakvn's edition) EA Explanation: General Purpose: - This EA, created in 2005 by tageiger, uses the classic iFractals indicator to identify potential reversal points in the market. - It makes trading decisions based on fractal values by placing pending orders (BuyStop/SellStop) or modifying existing positions (e.g., adjusting Stop Loss levels). - The EA incorporates risk management by calculating an optimal trade size based on account margin and a user-defined maximum risk percentage. Key Components: - Libraries: The code includes several MQL5 trading libraries (CTrade, CPositionInfo, CSymbolInfo, CAccountInfo, CDealInfo, COrderInfo) to handle trade operations, manage positions, and retrieve symbol/account details. - Input Parameters: • MaximumRisk: The maximum risk per trade as a percentage of account equity. • DecreaseFactor: A factor used to reduce the trade size after consecutive losses. • Expiration: The lifespan (in hours) of pending orders. • m_magic: A unique identifier (magic number) to differentiate trades executed by this EA from others. Initialization (OnInit): - The EA sets up the trading symbol and refreshes market data. - It configures the trading object with the specified magic number and selects an appropriate order filling type (FOK, IOC, or a default method) based on what the broker supports. - A handle for the iFractals indicator is created using the current symbol and timeframe. If this fails, an error is logged and initialization is stopped. - Various fractal-related variables are initialized to EMPTY_VALUE, and the time of the last profitable deal is stored. OnTick Function: - The OnTick function is triggered on each new bar. It first checks if a new bar has formed by comparing the current bar’s time with a stored timestamp. - It retrieves the upper and lower fractal values using the iFractals indicator. - The EA updates a history of fractal values by shifting older values and saving the newest ones. - It displays current fractal values on the chart through comments for easy monitoring. - The EA counts current open positions and pending orders. If conditions are met (e.g., a rising sequence for Buy orders or a falling sequence for Sell orders), it calculates an optimized trade size and places a pending order (BuyStop or SellStop). - Additionally, it adjusts Stop Loss levels for open positions based on the new fractal levels and removes outdated pending orders if market conditions have changed. Risk and Trade Size Management: - The TradeSizeOptimized function calculates the ideal lot size based on the account’s free margin, the defined maximum risk percentage, and the margin requirement per lot. - If a series of losses is detected, the EA reduces the trade size further based on the DecreaseFactor. - The LotCheck function ensures the computed lot size respects the broker’s minimum, maximum, and lot step limits. Additional Utility Functions: - RefreshRates: Refreshes market data for the symbol to ensure current rates. - IsFillingTypeAllowed: Checks if a particular order filling type (e.g., FOK, IOC) is allowed by the broker. - iTime: Retrieves the time of a specified bar, ensuring the EA operates on new bars. - LastProfitDeal: Scans the trading history to find the time of the last profitable deal, which is used in risk management calculations. - iFractalsGet: Retrieves fractal values from the indicator’s buffers, with error logging if the data cannot be copied. - CompareDoubles: Compares two double values to a specified precision to determine if they are effectively equal. - PrintComments: Updates on-chart comments with the current fractal values and time for monitoring purposes. Summary: - The EA leverages the iFractals indicator to detect market reversals. - It automatically places pending orders when certain fractal conditions are met. - Open positions are dynamically managed by adjusting Stop Loss levels. - It employs comprehensive risk management to optimize trade size based on current account conditions and recent performance. This well-structured code demonstrates the use of object-oriented programming in MQL5 to efficiently manage trading operations and risk. */