Split HIT EA into entry and position manager

This commit is contained in:
Hiroaki86
2026-06-07 22:06:30 +09:00
parent f49769dc1e
commit b7789d4572
10 changed files with 1414 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
# HITEntryEA Specification
## 1. Overview
`HITEntryEA.mq5` is the entry-only Expert Advisor. It runs the H4/H1 Python signal workflow, applies the M15 confirmation rules, and submits pending orders. It does not manage filled positions. Stop management, trailing, and time-based position exits are delegated to `HITPositionManagerEA.mq5`.
The two EAs coordinate through matching `_Symbol` and `input_entry_magic_number` / `input_managed_magic_number`. The operating assumption is a retail hedging account.
## 2. Indicators
- No standard or custom indicators are used.
- Confirmed H4/H1 OHLC data is exported to Python. The EA reads `trend_state.txt`, `target_prices.txt`, and `target_zones.txt`.
- M15 confirmation uses `CopyRates(_Symbol, PERIOD_M15, OHLC_START_SHIFT, M15_CONFIRM_BARS, rates)`.
## 3. Parameters
- `lot_size = 0.01`: Standard order volume.
- `spread_limit = 60`: Maximum allowed spread in points.
- `input_entry_magic_number = 10001`: Magic number assigned to pending orders.
- `input_slippage_points = 10`: Allowed order deviation in points.
- `input_position_limit = 10`: Limit for same-symbol/same-magic pending orders plus positions.
- `use_split_entry_zone = false`: Enables split entries from the H1 prediction zone.
- `split_entry_count = 3`: Number of split entry orders.
- `split_lot_mode = SPLIT_LOT_TOTAL`: Total-volume split or fixed-volume split.
- `input_cancel_retry_cooldown_seconds = 60`: Ticket-level retry cooldown after pending cancellation failures.
- `use_m15_entry_filter = true`: Enables M15 closed-bar confirmation.
- `input_entry_max_candidate_age_minutes = 120`: Maximum age for using an H1 candidate for new execution.
## 4. Entry And Exit Conditions
- New pending orders are sent only after spread, H4 state, H1 candidate, M15 timing, price consistency, broker distance, volume, and same-symbol/same-magic limit checks pass.
- Pending orders receive `input_entry_magic_number`.
- The entry EA cancels expired pending orders, pending orders that conflict with the refreshed H4 state, and stale split-entry pending orders.
- Filled-position SL updates, trailing, and time-based exits are not performed by this EA.
## 5. Risk Management
- The order limit counts only pending orders and positions matching `_Symbol` and `input_entry_magic_number`.
- Python process markers, process IDs, done files, and timeouts are monitored to prevent duplicate launches and stale-result reuse.
- `OrderSend` return values and `MqlTradeResult.retcode` are checked and logged.
- Because the design assumes hedging accounts, per-ticket position management is handled by `HITPositionManagerEA.mq5`.
## 6. Unit Test Scope
- In the Strategy Tester, this EA is tested for pending-order creation, expired pending cancellation, and H4-state mismatch cancellation.
- Position management behavior is outside this EA's unit-test scope.
- Python linkage filenames include `_Symbol` and `input_entry_magic_number` to preserve reproducibility.
## 7. Changelog
- 2026-06-07: Split entry responsibilities from `HIT-EA_refactor_ver6.mq5`; moved SLTP panel and filled-position management responsibilities to `HITPositionManagerEA.mq5`.
+51
View File
@@ -0,0 +1,51 @@
# HITEntryEA 仕様書(日本語)
## 1. 概要
`HITEntryEA.mq5` は、H4/H1の外部Python判定とM15確認を使って新規pending注文までを実行する発注専用EAです。約定後ポジションのSL移動、トレーリング、時間決済は行わず、同じマジックナンバーを監視する `HITPositionManagerEA.mq5` に委譲します。
2つのEAは `_Symbol``input_entry_magic_number` / `input_managed_magic_number` の一致で連携します。運用口座はhedging口座を前提とします。
## 2. 使用インジケータ
- 標準・カスタムインジケータは使用しません。
- H4/H1の確定足OHLCをPythonへ渡し、`trend_state.txt`, `target_prices.txt`, `target_zones.txt` を読み込みます。
- M15確認は `CopyRates(_Symbol, PERIOD_M15, OHLC_START_SHIFT, M15_CONFIRM_BARS, rates)` を使用します。
## 3. パラメータ設定
- `lot_size = 0.01`: 通常注文ロット。
- `spread_limit = 60`: 許容スプレッド(point)。
- `input_entry_magic_number = 10001`: 発注EAがpending注文へ付与するマジックナンバー。
- `input_slippage_points = 10`: 注文送信時の許容偏差。
- `input_position_limit = 10`: 同一symbol/magicのpending注文 + ポジション上限。
- `use_split_entry_zone = false`: H1予測ゾーンによる分割エントリーを有効化するか。
- `split_entry_count = 3`: 分割本数。
- `split_lot_mode = SPLIT_LOT_TOTAL`: 総量分割または固定ロット分割。
- `input_cancel_retry_cooldown_seconds = 60`: pending取消失敗後のticket単位再試行間隔。
- `use_m15_entry_filter = true`: M15確定足確認を使うか。
- `input_entry_max_candidate_age_minutes = 120`: H1候補を新規発注に使える最大経過分数。
## 4. エントリー/エグジット条件
- 新規注文はスプレッド、H4状態、H1候補、M15確認、価格整合、ブローカー距離、ロット制約、同一symbol/magicの上限を満たす場合のみ送信します。
- pending注文には `input_entry_magic_number` を付与します。
- H4状態と矛盾するpending注文、期限切れpending注文、古い分割pending注文は発注EAが取消します。
- 約定後ポジションのSL変更、時間決済、トレーリングは発注EAでは行いません。
## 5. リスク管理
- 発注上限は口座全体ではなく `_Symbol``input_entry_magic_number` が一致するpending注文 + ポジションで判定します。
- Python連携は起動中/完了ファイルとプロセスIDを監視し、二重起動と古い結果の再利用を抑止します。
- `OrderSend` の戻り値と `MqlTradeResult.retcode` を確認し、失敗時はエラーまたはretcodeをログに出します。
- hedging口座前提のため、約定後の複数ticket管理は `HITPositionManagerEA.mq5` が担当します。
## 6. 単体テスト
- Strategy Testerでは発注EA単体で、pending注文の作成、期限切れ取消、H4状態不一致取消を確認します。
- 管理EA機能はこのテスト対象に含めません。
- Python連携ファイル名は `_Symbol``input_entry_magic_number` を含むprefixで分離し、再現性を保ちます。
## 7. 変更履歴
- 2026-06-07: `HIT-EA_refactor_ver6.mq5` から発注責務を分離し、SLTPパネルとポジション管理を `HITPositionManagerEA.mq5` へ移管する仕様を追加。
@@ -0,0 +1,51 @@
# HITPositionManagerEA Specification
## 1. Overview
`HITPositionManagerEA.mq5` is the filled-position manager for positions created by `HITEntryEA.mq5`. It never creates new orders or pending orders. It is a retail hedging account EA and fails initialization on non-hedging accounts.
The managed set is limited to positions whose `POSITION_SYMBOL` equals `_Symbol` and whose `POSITION_MAGIC` equals `input_managed_magic_number`. Multiple tickets, split entries, and simultaneous buy/sell positions are managed independently.
## 2. Indicators
- No standard or custom indicators are used.
- SLTP management uses the current tick, each position's open price, SL, TP, open time, and symbol stop/freeze levels.
- High-volatility stop tightening uses M1/M3/M5/M10/M15 open prices and the current market price.
## 3. Parameters
- `input_managed_magic_number = 10001`: Magic number of positions to manage. It should match the entry EA's `input_entry_magic_number`.
- `input_slippage_points = 10`: Allowed deviation for closes and SL modifications.
- `input_position_close_after_h1_bars = 12`: Time-based close threshold in H1 bars. `0` disables it.
- `input_sltp_manager_enabled = false`: Master switch for SLTP management.
- `input_sltp_show_panel = true`: Shows the SLTP control panel.
- `input_sltp_use_breakeven`: Standard breakeven.
- `input_sltp_use_elapsed_breakeven`: Elapsed-time breakeven.
- `input_sltp_use_active_trailing`: Active trailing.
- `input_sltp_use_tp_progress_stop`: TP-progress stop.
- `input_sltp_use_high_volatility_limit`: High-volatility stop tightening.
## 4. Entry And Exit Conditions
- There are no entry conditions because this EA does not create orders.
- On every tick, the EA first evaluates ticket-based time exits and then applies SLTP management.
- Time exits close target tickets when `POSITION_TIME` is older than `input_position_close_after_h1_bars * PeriodSeconds(PERIOD_H1)` by using `CTrade::PositionClose(ticket)`.
- SLTP management applies only candidates that improve the current SL in the profit-protection direction, preserving the current TP with `CTrade::PositionModify(ticket, new_sl, current_tp)`.
## 5. Risk Management
- All position processing uses `PositionGetTicket(i)` followed by `PositionSelectByTicket(ticket)`.
- Only positions matching `_Symbol` and `input_managed_magic_number` are touched.
- Different magic numbers, different symbols, and manual-position equivalents are ignored.
- Logs include ticket, symbol, magic, position type, trade retcode, and `GetLastError()`.
- Standard breakeven and active trailing cannot be enabled together; settings validation rejects that conflict.
## 6. Unit Test Scope
- The production manager EA contains no test-only order creation.
- In the Strategy Tester, `HITPositionManagerTestHarness.mq5` seeds test BUY/SELL positions and a different-magic control position, then calls the same production manager classes.
- Test coverage targets same-symbol/same-magic multi-ticket handling, simultaneous buy/sell positions, magic mismatch exclusion, time exits, and SL updates.
## 7. Changelog
- 2026-06-07: Split position-management responsibilities from `HIT-EA_refactor_ver6.mq5` and defined a hedging-account, ticket-based manager EA.
@@ -0,0 +1,51 @@
# HITPositionManagerEA 仕様書(日本語)
## 1. 概要
`HITPositionManagerEA.mq5` は、`HITEntryEA.mq5` が作成した約定済みポジションを管理する専用EAです。新規注文やpending注文は作成しません。hedging口座専用とし、起動時に `ACCOUNT_MARGIN_MODE_RETAIL_HEDGING` でない場合は `INIT_FAILED` とします。
管理対象は `_Symbol``input_managed_magic_number` が一致するポジションのみです。複数ポジション、両建て、分割エントリーをticket単位で独立管理します。
## 2. 使用インジケータ
- 標準・カスタムインジケータは使用しません。
- SLTP管理は現在tick、ポジションの建値/SL/TP/保有時間、シンボルのstop level / freeze levelを使用します。
- 急変時SL引き締めは M1/M3/M5/M10/M15 の始値と現在価格を使用します。
## 3. パラメータ設定
- `input_managed_magic_number = 10001`: 管理対象ポジションのマジックナンバー。発注EAの `input_entry_magic_number` と一致させます。
- `input_slippage_points = 10`: 決済とSL変更の許容偏差。
- `input_position_close_after_h1_bars = 12`: 保有時間決済のH1本数。0で無効。
- `input_sltp_manager_enabled = false`: SLTP管理全体のON/OFF。
- `input_sltp_show_panel = true`: SLTP操作パネルを表示するか。
- `input_sltp_use_breakeven`: 通常ブレークイーブン。
- `input_sltp_use_elapsed_breakeven`: 保有時間ベースのブレークイーブン。
- `input_sltp_use_active_trailing`: アクティブトレーリング。
- `input_sltp_use_tp_progress_stop`: TP進捗率SL。
- `input_sltp_use_high_volatility_limit`: 急変時SL引き締め。
## 4. エントリー/エグジット条件
- 新規エントリー条件はありません。このEAは注文作成を行いません。
- `OnTick()` では、まずticket単位で保有時間決済を評価し、その後SLTP管理を実行します。
- 時間決済は `POSITION_TIME` から `input_position_close_after_h1_bars * PeriodSeconds(PERIOD_H1)` 以上経過した対象ticketを `CTrade::PositionClose(ticket)` で閉じます。
- SLTP管理は、対象ticketの現在SLより利益保護方向へ改善する候補だけを `CTrade::PositionModify(ticket, new_sl, current_tp)` で反映します。
## 5. リスク管理
- すべてのポジション選択は `PositionGetTicket(i)``PositionSelectByTicket(ticket)` を使います。
- `POSITION_SYMBOL == _Symbol` かつ `POSITION_MAGIC == input_managed_magic_number` のみ処理します。
- magic違い、symbol違い、手動ポジション相当には触れません。
- ログにはticket、symbol、magic、position type、retcode、`GetLastError()` を含めます。
- 通常ブレークイーブンとアクティブトレーリングの同時ONは設定検証で拒否します。
## 6. 単体テスト
- 管理EA本体は本番安全性のため新規注文を持ちません。
- Strategy Testerでは `HITPositionManagerTestHarness.mq5` を使い、検証用のBUY/SELLおよびmagic違いポジションを作成してから同じ管理クラスを呼び出します。
- テスト観点は、同一symbol/magicの複数ticket管理、BUY/SELL両建て、magic違い非対象、時間決済、SL更新です。
## 7. 変更履歴
- 2026-06-07: `HIT-EA_refactor_ver6.mq5` からポジション管理責務を分離し、hedging口座専用のticket単位管理EAとして新規定義。
@@ -0,0 +1,45 @@
# HITPositionManagerTestHarness Specification
## 1. Overview
`HITPositionManagerTestHarness.mq5` is a Strategy Tester-only EA for unit-testing the management logic used by `HITPositionManagerEA.mq5`. It is not intended for live trading. It fails initialization outside `MQL_TESTER` and on non-hedging accounts.
The harness seeds test positions, then calls the same production `CSLTPManager` and `CHITPositionLifecycleManager` classes used by the manager EA.
## 2. Indicators
- No standard or custom indicators are used.
- The management logic uses the current tick, selected position data, and symbol trading constraints.
## 3. Parameters
- `input_managed_magic_number = 10001`: Magic number for managed test positions.
- `input_control_magic_number = 20002`: Magic number for a different-magic control position.
- `input_slippage_points = 10`: Allowed deviation for seed orders, closes, and SL modifications.
- `input_test_lot_size = 0.01`: Test position volume.
- `input_seed_managed_buy = true`: Seeds a managed BUY position.
- `input_seed_managed_sell = true`: Seeds a managed SELL position.
- `input_seed_control_position = true`: Seeds a different-magic control position.
- `input_position_close_after_h1_bars = 12`: Time-exit threshold in H1 bars.
- `input_sltp_*`: Same SLTP settings as the production manager EA.
## 4. Entry And Exit Conditions
- On the first tick, the harness creates the configured managed BUY, managed SELL, and different-magic BUY positions.
- After seeding, it runs the same management sequence as production: time exits, SLTP management, and high-volatility stop tightening.
- The different-magic control position is expected to remain unmanaged.
## 5. Risk Management
- This EA is tester-only and does not run in live mode.
- Test volume is normalized to `SYMBOL_VOLUME_MIN/MAX/STEP`.
- The target filter is the same as production: `_Symbol + input_managed_magic_number`.
## 6. Unit Test Scope
- The harness keeps test-only order creation out of the production manager EA.
- It verifies buy/sell coexistence, multiple-ticket handling, magic mismatch exclusion, time exits, and SL updates.
## 7. Changelog
- 2026-06-07: Created as the Strategy Tester unit-test harness for `HITPositionManagerEA.mq5`.
@@ -0,0 +1,45 @@
# HITPositionManagerTestHarness 仕様書(日本語)
## 1. 概要
`HITPositionManagerTestHarness.mq5` は、`HITPositionManagerEA.mq5` の管理ロジックをStrategy Testerで単体検証するためのテスト専用EAです。本番運用には使用しません。`MQL_TESTER` 以外では `INIT_FAILED` とし、hedging口座以外でも起動しません。
テスター内で検証用ポジションを作成した後、本番管理EAと同じ `CSLTPManager``CHITPositionLifecycleManager` を呼び出します。
## 2. 使用インジケータ
- 標準・カスタムインジケータは使用しません。
- 管理ロジックは現在tick、ポジション情報、シンボル制約を使用します。
## 3. パラメータ設定
- `input_managed_magic_number = 10001`: 管理対象として作成する検証用ポジションのmagic。
- `input_control_magic_number = 20002`: magic違い非対象確認用ポジションのmagic。
- `input_slippage_points = 10`: 検証用注文、決済、SL変更の許容偏差。
- `input_test_lot_size = 0.01`: 検証用ロット。
- `input_seed_managed_buy = true`: 管理対象BUYを作成するか。
- `input_seed_managed_sell = true`: 管理対象SELLを作成するか。
- `input_seed_control_position = true`: magic違いポジションを作成するか。
- `input_position_close_after_h1_bars = 12`: 保有時間決済のH1本数。
- `input_sltp_*`: 本番管理EAと同じSLTP管理設定。
## 4. エントリー/エグジット条件
- 初回tickで、設定に従って管理対象BUY、管理対象SELL、magic違いBUYを作成します。
- 作成後は本番管理ロジックと同じ順序で、保有時間決済、SLTP管理、急変時SL引き締めを実行します。
- magic違いポジションは管理対象外として残ることを確認します。
## 5. リスク管理
- 本EAはテスター専用であり、ライブ環境では起動しません。
- 検証用ポジションは `_Symbol` 上に作成し、ロットは `SYMBOL_VOLUME_MIN/MAX/STEP` に合わせて正規化します。
- 管理対象判定は本番と同じ `_Symbol + input_managed_magic_number` です。
## 6. 単体テスト
- 管理EA本体に新規注文コードを入れないためのテストハーネスです。
- BUY/SELL両建て、複数ticket、magic違い除外、時間決済、SL更新の再現性を確認します。
## 7. 変更履歴
- 2026-06-07: `HITPositionManagerEA.mq5` のStrategy Tester単体検証用ハーネスとして新規作成。