Spécifications
This is a simple Martingale based EA with RSI indicator used as buy/sell order conditions
Mandatory Inputs:
extern double TakeProfit = 120.0; // self explanatory for first order, for martingale orders it will adjust TP of all orders of the same type (buy / sell) to one, for example if an order opened at 100, 2nd opens at 105, the both of their tps will be at the same point, if an order is manually closed, it will adjust existing order tps accordingly.
extern double StopLoss = 1000.0; // self explanatory for each order it has its own stoploss
extern double InitialLots = 0.03;
extern double MartingaleFactor = 2; // Self explanatory - previous lot size will be multiplied to the next lotsize, so if initial lot is 0.03, 2nd order will be multiplied to 0.03, and 3rd will be multiplied to the 2nd order and so on
extern int MagicNumberBuy = 1986; // self explanatory
extern int MagicNumberSell = 5591; // self explanatory
extern int StartHour = 3; // self explanatory - ea will start working
extern int EndHour = 23; // // self explanatory - ea will stop working
extern double RSI_Period = 14; // self explanatory - variable rsi period setting
extern double RSI_Upper = 60.0; // ea will place sell order if rsi goes above this point -- only first order will follow this rule, martingale orders will be placed according to PointsAfterDistance has reached
extern double RSI_Lower = 30.0; // ea will place buy order if rsi goes below this point -- only first order will follow this rule, martingale orders will be placed according to PointsAfterDistance
extern int MaxTrades = 3; // Maximum number of trades per type (buy/sell) for martingale, when this number has reached it will not place any more orders until closed orders either hit tp or in case of one or more orders hit sl and then number of existing orders changes so it will place orders upto this limit.
extern int PointsAfterDistance = 400; // After the initial order is placed, if the price is going away from tp and this distance has reached, the martingale order will open. this martingale order will not open if the market has gone away from the tp and then coming back, all subsequent orders will have minimum PointsAfterDistance between them, it could be more, but not less than this. If this distance has reached, the order will wait for the new bar to open even if the distance has increased. The ea will make sure all orders follow relevant magic sell/buy numbers and their original type it should not interfere or be interrupted by any other ea's magic numbers All subsequent martingale order should follow the original order type, for example if original order is buy, the 2nd and subsequent orders should be buy and same for sell orders. BarTrade = true/false - if true it will place order only in the first minute of bar and if false, it will place order at any point when conditions are met (for example if placed on 1hr chart and conditions are met it will place order between 07:00:00 - 07:00:59)
Overview and Explanation:
This Expert Advisor is to be designed to trade based on an RSI under/oversold with an integrated Martingale Strategy. The key features include dynamic adjustment of Take Profit (TP) levels, flexible trade placement rules, and strict adherence to the original order's direction (buy/sell). Below is a brief summary of the functionality, followed by clarifications of each parameter.
Summary of the EA Functionality:
-
Initial Trade Conditions: The EA places an initial buy or sell order based on the Relative Strength Index (RSI) indicator's thresholds. It monitors the RSI level against user-defined upper and lower limits.
-
Martingale Strategy: If the price moves against the initial trade by a specified distance ( PointsAfterDistance ), the EA places additional trades in the same direction as the initial trade, increasing the lot size by a multiplier ( MartingaleFactor ).
-
Dynamic TP Adjustment: When a new Martingale order is placed, the EA adjusts the TP for all open orders of the same type (buy or sell) to a unified TP level. This level is recalculated based on the latest order's price.
-
Order Management: The EA continues placing Martingale orders until the maximum number of trades ( MaxTrades ) is reached. It only opens new orders if the market conditions meet the specified criteria, and orders are placed within the allowed trading hours.
-
Bar Trading Mode: The EA can be configured to place orders only at the beginning of a new bar or immediately when conditions are met, depending on the BarTrade setting.
Clarifications of EA Parameters:
-
TakeProfit (TP):
- First Order: The TP for the first order is placed based on this value.
- Martingale Orders: When subsequent Martingale orders are placed, the EA adjusts the TP of all open orders of the same type to a single, unified TP level. This ensures that all trades close simultaneously if the price reaches this level. If an order is manually closed, the EA will recalculate and adjust the TP for the remaining orders accordingly.
-
StopLoss (SL):
- Each order has its own SL based on the StopLoss value provided. The SL does not change after the order is placed and is independent for each trade.
-
InitialLots:
- This parameter defines the lot size for the initial trade. Subsequent Martingale orders will increase the lot size based on the MartingaleFactor .
-
MartingaleFactor:
- The lot size of each Martingale order is multiplied by this factor relative to the previous order. For example, if InitialLots is 0.03 and MartingaleFactor is 2, the lot size progression will be 0.03, 0.06, 0.12, etc.
-
MagicNumberBuy & MagicNumberSell:
- These are identifiers to differentiate buy and sell orders placed by the EA. The EA ensures that it only manages orders with these magic numbers and does not interfere with other EAs or manual trades.
-
StartHour & EndHour:
- The EA will only place trades between these hours. This ensures that the EA operates within a defined trading window.
-
RSI_Period, RSI_Upper, RSI_Lower:
- The EA uses the RSI indicator with the specified period to determine the initial trade. If the RSI crosses above the RSI_Upper , a sell order is placed. If it crosses below the RSI_Lower , a buy order is placed. Martingale orders do not rely on the RSI but are based on price movement away from the initial order's TP.
-
MaxTrades:
- Limits the maximum number of trades (buy or sell) the EA will open in a Martingale series. Once this limit is reached, the EA will not place any more orders until one or more existing orders are closed, either by hitting the TP or SL.
-
PointsAfterDistance:
- Defines the minimum distance (in points) from the initial order’s TP where a Martingale order will be triggered. The EA waits for a new bar to open before placing the Martingale order, ensuring that trades are not clustered too closely.
-
BarTrade (true/false):
- true: The EA will only place orders in the first minute of a new bar (e.g., between 07:00:00 and 07:00:59 on an H1 chart).
- false: The EA will place orders immediately when the conditions are met, regardless of the time within the current bar.
Additional Considerations:
- Manual Order Closures: If an order is manually closed, the EA will recalculate the TP for the remaining open orders to ensure they are aligned with the latest market conditions.
- Trade Type Consistency: Martingale orders will always follow the direction of the initial trade. For example, if the first order is a buy, all subsequent Martingale orders will also be buys, and the same applies to sell orders.
NOTES:
Ideally the code needs to be less than 400 lines no mored than 500 lines, this is an experimental ea and any comments in the code would be appreciated.
The code will be tested on real market as well as demo. I have made a code through AI which works fine except it just stops sometimes and doesn't work. I would rather pay someone to code the above for me than just use AI to generate the code as there's always something wrong with AI generated codes.