TradeLibraryMT5Example
Download scriptThis script is an automated trading system designed to manage and execute trades based on specific price levels. It operates as a "state machine," meaning it moves through different logical phases—Waiting, Opening, Working, and Closing—depending on the current market conditions and the status of your active orders.
How the Logic Works
1. Market Signal Detection (The Waiting Phase) The system constantly monitors price action over a specific look-back period (defaulting to 4 days). It identifies the highest price reached and the lowest price touched during this timeframe.
- High Signal: If the current market price climbs above the identified multi-day high, the script prepares to look for selling opportunities.
- Low Signal: If the current market price drops below the identified multi-day low, the script prepares to look for buying opportunities.
2. Order Deployment (The Opening Phase) Once a signal is triggered, the script doesn't just enter a trade immediately. Instead, it sets up a series of "Pending Orders." These are "limit" orders placed at specific distances from the current price:
- It calculates multiple entry points moving outward from the current price.
- It places both Buy Limit and Sell Limit orders simultaneously at these calculated intervals.
- After successfully placing these orders, the system moves into the "Working" phase.
3. Active Trade Management (The Working Phase) While the script is in the working state, it continuously monitors the profit and loss of your open positions. It calculates your net profit by accounting for:
- Gross profit from price movement.
- Trading commissions.
- Swap fees (overnight interest).
4. Automated Exit Strategy (The Closing Phase) The system is programmed to automatically manage your risk based on two key thresholds:
- Take Profit: If the total profit for the positions reaches your predefined dollar amount, the script triggers a closing sequence.
- Stop Loss: If the total loss exceeds your predefined negative dollar amount, the script will immediately trigger the same closing sequence to protect your capital.
5. Clean-Up and Reset When the closing phase is triggered, the system performs a comprehensive cleanup. It loops through all active positions and any remaining pending orders associated with its unique ID (Magic Number), closing or deleting them one by one. Once the market is cleared, the script resets its status back to "Waiting" to look for the next trading opportunity.
ÿþ/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
/ / | T r a d e L i b r a r y M T 5 E x a m p l e . m q 4 |
/ / | T r a d e L i b r a r y M T 5 E x a m p l e . m q 5 |
/ / | C o p y r i g h t 2 0 2 2 , Y u r i y B y k o v |
/ / | h t t p s : / / w w w . m q l 5 . c o m / r u / c o d e / 3 8 8 6 1 |
/ / | h t t p s : / / w w w . m q l 5 . c o m / e n / c o d e / 3 9 1 6 1 |
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
# p r o p e r t y c o p y r i g h t "