Component:Signal Generation
Appearance
Signal Generation & Trade Management
Purpose
"Should we trade? Which direction? Where's entry/stop/target?"
This component is the decision maker. It receives graded setups from Layer 3 and decides:
- Should we take this trade? (passes all gate filters)
- Which direction? (LONG or SHORT based on MA21 slope)
- Where to enter, stop, and exit? (entry techniques)
Trading Market Principle
"Trade with the trend, not against it. MA21 slope is your compass."
- MA21 rising = LONG only → don't fight a rising market
- MA21 declining = SHORT only → don't catch falling knives
- MA21 flat = use MTF direction as tiebreaker
- Gate filters protect capital → blocked hours, wrong zone, weak confirmation = skip
- Only high-quality setups traded → A+, A, B grades only
Decision Flow
┌─────────────────────────────────┐
│ Setup Quality Result (Layer 3) │
│ Grade: A+/A/B/C/D/F │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ GATE 1: Grade Filter │
│ Only A+, A, B pass │
│ C, D, F → REJECTED │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ GATE 2: Hour Filter │
│ Blocked: 9 AM, 10 PM, 11 PM │
│ (Loss-making hours) │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ GATE 3: Direction Determination│
│ MA21 rising → LONG │
│ MA21 declining → SHORT │
│ Flat/unclear → NO TRADE │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ GATE 4: Probability Zone Filter│
│ Top third + LONG = OK │
│ Bottom third + LONG = BLOCKED │
│ (Trades with probability) │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ Calculate Entry/Stop/Target │
│ Generate Signal │
└─────────────────────────────────┘
Gate Filters
Gate 1: Grade Filter
Only high-quality setups are traded:
| Grade | Action |
|---|---|
| A+, A, B | PASS → Generate signal |
| C, D, F | BLOCKED → No signal generated |
Gate 2: Hour Filter
Block historically loss-making hours:
Blocked Hours: [9, 22, 23] // 9 AM, 10 PM, 11 PM IST
IF current_hour IN blocked_hours:
RETURN None // No signal
Gate 3: Direction Determination
Direction is determined by MA21 slope:
trend_direction = market_state.trend_direction
IF trend_direction == "up":
direction = LONG // MA21 rising → trade long
ELIF trend_direction == "down":
direction = SHORT // MA21 declining → trade short
ELSE:
// MA21 flat/unclear → use MTF fallback
IF mtf_direction == "up":
direction = LONG
ELIF mtf_direction == "down":
direction = SHORT
ELSE:
RETURN None // No clear direction → no trade
Gate 4: Probability Zone Filter
Filters trades based on price position within range:
| Zone | LONG | SHORT | Logic |
|---|---|---|---|
| Top Third | Blocked | Allowed | 80% continuation down, 15% up |
| Middle Half | Allowed | Allowed | Neutral zone |
| Bottom Third | Allowed | Blocked | 80% continuation up, 15% down |
Sideways Market Exception: In creeper/consolidation, uses mean reversion (opposite logic).
Stop Loss Calculation
Stop placement based on entry technique:
| Entry Technique | LONG Stop | SHORT Stop |
|---|---|---|
| GREEN_BAR_PULLBACK | Below pullback low | Above rally high |
| BREAKOUT_PULLBACK | min(last 3 lows) | max(last 3 highs) |
| MA_BOUNCE | MA21 - 5 points | MA21 + 5 points |
| BOS_ENTRY | min(last 10 lows) + buffer | max(last 10 highs) - buffer |
| DISCOUNT_ZONE | entry × 0.99 | entry × 1.01 |
| DEFAULT | entry - 40 points | entry + 40 points |
Minimum Stop Distance: 40 points (prevents tight stops hitting on noise)
Take Profit Calculation
Implements "cut the move in half" principle:
distance_to_ma = |entry_price - MA21|
target_distance = distance_to_ma × 0.5
IF LONG:
take_profit = entry_price + target_distance
ELSE:
take_profit = entry_price - target_distance
Minimum R:R: 1.5 (target must be at least 1.5× risk)
Cost Calculations
Commission (Dhan Broker)
commission = position_size × Rs 20 per lot
CTT (Commodity Transaction Tax)
contract_value = entry_price × lot_size × position_size tax = contract_value × 0.0001 // 0.01% total (0.005% per leg)
Configuration
| Parameter | Value | Description |
|---|---|---|
commission_per_lot |
20.00 | Rs per lot (Dhan) |
transaction_tax_rate |
0.00005 | 0.005% per leg × 2 = 0.01% CTT |
lot_size_multiplier |
100 | MCX lot size |
min_stop_distance |
40 | Minimum stop in points |
default_risk_reward |
1.5 | Minimum R:R ratio |
enable_hour_filter |
True | Block loss-making hours |
blocked_hours |
[9, 22, 23] | Hours to skip |
enable_ma_direction_filter |
True | MA21 alignment required |
enable_probability_zone_filter |
True | Zone probability check |
enable_color_change_filter |
False | Disabled (too restrictive) |
Output
Signal Object:
| Field | Type | Description |
|---|---|---|
direction |
Enum | LONG or SHORT |
entry_price |
float | Current close price |
stop_loss_price |
float | Calculated stop level |
take_profit_price |
float | Calculated target level |
position_size |
int | From setup quality (1 or 2 lots) |
setup_quality |
Enum | A+, A, or B |
entry_technique |
Enum | How stop was determined |
Dependencies
Upstream:
- multi_timeframe_analysis.py - MTF direction fallback
- market_state_analysis.py - Trend direction, market state
- setup_quality_detection.py - Grade, position size
Internal:
- probability_zone_analysis.py - Zone filtering logic
Downstream:
- trade_execution_engine.py - Executes the signal
Quick Reference
| Scenario | Result |
|---|---|
| Grade C, D, or F | No signal (rejected) |
| Hour = 9, 22, or 23 | No signal (blocked hour) |
| MA21 rising | LONG signal only |
| MA21 declining | SHORT signal only |
| MA21 flat, MTF unclear | No signal (no direction) |
| LONG in bottom third | Allowed (high prob) |
| LONG in top third | Blocked (low prob) |
| SHORT in top third | Allowed (high prob) |
| SHORT in bottom third | Blocked (low prob) |