TradeClear
- Утилиты
- Daophet Seng Athit
- Версия: 1.0
- Активации: 10
Overview
Smart Greedy Basket Close is an intelligent position-management block for Mannual trade that automatically resolves your worst losing trade using a curated basket of winning positions — without touching your account's overall equity more than necessary.
Instead of waiting for a painful stop-loss, the algorithm identifies your single biggest loser, then greedily selects the minimum number of winners needed to offset that loss and meet a configurable net-profit target before closing the entire group simultaneously.
- How it works — 8-step logic
1.Scan all positions
Collects every open position on the current symbol — any Magic Number, any EA, manual trades included.
2.Identify worst loser
Finds the position with the largest negative P&L (profit + commission + swap combined).
3.Collect winning candidates
Gathers all profitable positions, optionally filtering out same-direction trades via the SameSideWin parameter.
4.Sort by profit descending
Ranks winners from highest to lowest profit using an insertion sort, ensuring the fewest closes needed.
5.Greedy selection
Picks winners one by one — largest first — until their combined profit covers the loser's loss, respecting MaxWinners cap.
6.Net profit check
Computes net = loser P&L + winners P&L. Aborts if the result falls below MinNetProfit — protecting from low-value triggers.
7.Close the loser
Sends a market close order with FOK fill; auto-retries with IOC if FOK is rejected by the broker.
8.Close selected winners
Closes each selected winner with the same FOK→IOC fallback logic. Logs every action and the final realized net.
Input parameters
- SG_MaxWinners Maximum number of winning positions that can be closed in a single basket. Caps exposure and prevents closing too many trades at once.
- SG_MinNetProfit Minimum acceptable net profit (in account currency) after closing the loser + winners. The block does nothing if this threshold isn't met.
- SG_SameSideWin If true, allows same-direction positions to be used as winners. If false, only opposite-direction positions qualify — ideal for hedge/grid EAs.
