# Profit Target Workflow

> **Version:** v1.0.0 | **Updated:** 2026-04-12 | **Change:** Initial document — Type 1 and Type 2 profit target workflows

---

## Overview

The TWM strategy manages two types of profit targets for active long positions (states 09 YELLOW, 10 GREEN, 11 RED):

| Type | Name | Trigger | Exit Mechanism |
|------|------|---------|----------------|
| 1 | Premarket Resistance Target | Set at trade entry | Limit sell order below nearest premarket resistance |
| 2 | Exit Counter Tightening Target | Accumulates during trade | Limit sell order at `current_price + offset`; market sell when offset = 0 |

---

## Type 1 — Premarket Resistance Profit Target

### Purpose
Set an initial profit limit order at trade entry, anchored to the nearest premarket resistance level above the entry price.

### Calculation (Long trade)
```
premarket_profit_target = nearest_premarket_resistance_above_entry - premarket_profit_target_offset
```

- `nearest_premarket_resistance_above_entry`: the closest premarket key level above the fill price (from `get_premarket_limits`)
- `premarket_profit_target_offset`: a per-ticker, per-TF spread that keeps the order safely below the resistance level

### Timing
- Calculated and submitted **at trade entry** (State 09 YELLOW activation)
- Remains active until hit, superseded by a Type 2 recalculation, or trade exits by another route

### Order type
- Limit sell order at `premarket_profit_target`

### State machine transition
- When hit: `EXIT_PREMARKET` → `long_01_WAIT_BEAR_TREND`
  - State 09: `t_242_exit_premarket_s09` (242)
  - State 11: `t_261_exit_premarket_s11` (261)

### Config
See `config_6_profit_target.json` → `premarket_profit_target_offset`

---

## Type 2 — Exit Counter Tightening Profit Target

### Purpose
Progressively tighten the profit limit order as the trade ages and/or soft-stop violations accumulate. This ensures the strategy exits on a profit rather than waiting indefinitely or being stopped out.

### State machine change required
A new state `long_12_EXIT_COUNTER` will be introduced (Task 3). All current exit transitions (EXIT_MA, EXIT_LWR, EXIT_PREMARKET, soft stop violations) route through this state to accumulate violations before executing the exit. The counter drives recalculation of the profit limit; when the limit is hit, the transition to `long_01_WAIT_BEAR_TREND` fires.

---

### exit_counter

The strategy maintains a single `exit_counter` integer for each open trade. It starts at 0 on entry and accumulates until the trade closes.

#### Increment formula (evaluated every 1 second)
```
exit_counter = prior_exit_counter + (number_of_exit_violations × RYG_scale_factor)
```

| Variable | Definition |
|----------|-----------|
| `number_of_exit_violations` | Count of distinct soft-stop violations detected in the current 1-second cycle |
| `RYG_scale_factor` | Multiplier based on current market state: Red = 3, Yellow = 2, Green = 1 |

#### Soft-stop violation sources
Each of the following, when triggered intra-bar, counts as **one violation** in the current cycle:

| Violation | Condition |
|-----------|-----------|
| Initial stop (Type 1) | Price touches initial stop level intra-bar (does not close position; hard stop only at `max_initial_stop_distance`) |
| LWR trailing stop (Type 3) | Price touches LWR stop level intra-bar |
| Exit MA | Price touches or crosses the exit moving average intra-bar |
| Premarket resistance level | Price touches any premarket resistance level intra-bar |
| Break-even soft stop (Type 2 new) | Price touches the 3-point break-even lock-in level (new; see stop loss workflow Task 2) |

> **Hard stops do not increment the counter** — they exit the trade immediately (see stop loss workflow).

---

### Profit limit recalculation

Each second, after updating `exit_counter`, the strategy checks whether the counter has crossed the next range threshold:

```
IF exit_counter >= range_N.max_count AND exit_counter < range_{N+1}.max_count:
    current_range = range_N

IF current_range changed from last cycle:
    profit_limit_offset = exit_counter_config[ticker][tf][current_range].profit_limit_offset
    IF profit_limit_offset > 0:
        new_profit_limit = current_price + profit_limit_offset
        → cancel existing profit limit order
        → submit new limit sell order at new_profit_limit
    ELIF profit_limit_offset == 0:
        → cancel existing profit limit order
        → submit MARKET SELL ORDER immediately
        → transition: EXIT_PROFIT_TARGET → long_01_WAIT_BEAR_TREND
```

#### Key rules
- The profit limit only **tightens** — it is never moved further away from current price
- A recalculation is triggered only when `exit_counter` crosses a new range boundary
- `profit_limit_offset = 0` means submit a **market order sell** — do NOT place a limit order at current price
- The Type 1 premarket limit remains active until superseded by a tighter Type 2 recalculation

---

### Range table (from config_6_profit_target)

**@ES:**

| Range | max_count | ES offset (pts) | Meaning |
|-------|-----------|-----------------|---------|
| range_1 | 10 | 5 | Early trade — wide profit target |
| range_2 | 20 | 4 | |
| range_3 | 30 | 3 | |
| range_4 | 40 | 2 | |
| range_5 | 50 | 1 | Trade degrading — tight limit |
| range_6 | 60 | 0 | → **Market sell** |

**@NQ:** (3× ES offsets, same counts)

| Range | max_count | NQ offset (pts) |
|-------|-----------|-----------------|
| range_1 | 10 | 15 |
| range_2 | 20 | 12 |
| range_3 | 30 | 9 |
| range_4 | 40 | 6 |
| range_5 | 50 | 3 |
| range_6 | 60 | 0 → **Market sell** |

All TF values (tf1–tf6) start uniform. Tune per-TF after baseline observation.

---

## Config file

All profit target parameters live in:
```
config_6_profit_target_v1_0_0.json
```

| Key | Description |
|-----|-------------|
| `premarket_profit_target_offset` | Type 1 — spread below premarket resistance for limit order placement |
| `exit_counter_config` | Type 2 — 6 ranges per ticker defining `max_count` and `profit_limit_offset` |

> **Note:** `premarket_profit_target_offset` was originally in `config_5_strategy`. Once the strategy code is updated to read from `config_6_profit_target`, that key will be removed from `config_5_strategy`.

---

## Change Log

| Date | Version | Project | Change Summary |
|------|---------|---------|----------------|
| 2026-04-12 | v1.0.0 | 20260412_TWM_exit_optimization | Initial document — defined Type 1 and Type 2 profit target workflows, counter formula, range table, market order rule |
