AdaptiveMarketModePreset - Intelligent Multi-Strategy SystemΒΆ
DescriptionΒΆ
AdaptiveMarketModePreset is an intelligent trading system that automatically selects and executes the most appropriate orchestrator based on real-time market condition analysis. It combines ALL 5 orchestrators into one adaptive system that switches strategies dynamically every cycle.
Principle: "The right strategy for the right conditions" - the preset analyzes current market volatility, time, spread, and news schedule, then selects the optimal orchestrator (Grid, Scalping, Hedge, News Straddle, or Breakout) and executes it with preset-optimized parameters.
File: Examples\Presets\AdaptiveMarketModePreset.cs
ArchitectureΒΆ
ADAPTIVE MARKET MODE PRESET
β
Market Analysis Engine
β’ Volatility (spreadΓ10)
β’ Time-based news check
β’ Breakout detection
β
βββββββββββΌββββββββββ
β β β
Low Vol Medium High Vol
(<15 pts) (15-40) (>40 pts)
β β β
Grid Scalping Hedge
Trading Orchestr. Orchestr.
Orchestr.
β
βββββββΌββββββ
β β
News Spread>3
Time (Breakout)
β β
News Pending
Straddle Breakout
Orchestr. Orchestr.
DependenciesΒΆ
MT5Service: Service layer for MT5
- All 5 Orchestrators:
- GridTradingOrchestrator
- SimpleScalpingOrchestrator
- QuickHedgeOrchestrator
- NewsStraddleOrchestrator
- PendingBreakoutOrchestrator
- mt5_term_api: gRPC types
Market Conditions & Orchestrator SelectionΒΆ
π CONDITION 1: LOW VOLATILITY (< 15 points)ΒΆ
Selected Orchestrator: GridTradingOrchestrator
Why: Range-bound markets are perfect for grid strategies. When price moves in a narrow range, grid levels capture small movements in both directions.
Parameters:
GridLevels = 3 // Fewer levels for safety
GridSpacingPoints = 20 // 20 points between levels
VolumePerLevel = 0.01 // 0.01 lots per level
StopLossPoints = 30 // 30 points SL
TakeProfitPoints = 50 // 50 points TP
MaxRunMinutes = 5 // Run for 5 minutes
π CONDITION 2: MEDIUM VOLATILITY (15-40 points)ΒΆ
Selected Orchestrator: SimpleScalpingOrchestrator
Why: Normal market conditions suit quick scalping trades with tight stops and risk-based position sizing.
Parameters:
RiskAmount = BaseRiskAmount // Uses preset's base risk ($20)
StopLossPoints = 15 // 15 points SL
TakeProfitPoints = 25 // 25 points TP (R:R β 1:1.67)
IsBuy = Random // Randomly selected direction
MaxHoldSeconds = 60 // Hold maximum 60 seconds
π CONDITION 3: HIGH VOLATILITY (> 40 points)ΒΆ
Selected Orchestrator: QuickHedgeOrchestrator
Why: Volatile markets need hedging protection. Opens position, then hedges if price moves adversely, limiting downside risk.
Parameters:
RiskAmount = BaseRiskAmount Γ 0.7 // REDUCED RISK (70%)
StopLossPoints = 25 // 25 points SL
TakeProfitPoints = 40 // 40 points TP
HedgeTriggerPoints = 15 // Hedge after 15 points adverse move
OpenBuyFirst = Random // Randomly selected direction
Key Feature: Risk is reduced to 70% of base amount in high volatility for capital preservation.
π CONDITION 4: NEWS EVENT DETECTEDΒΆ
Selected Orchestrator: NewsStraddleOrchestrator
Why: Capture explosive volatility from high-impact news releases with symmetrical pending orders.
News Schedule (UTC):
- 08:30 - Economic data releases (NFP, CPI, Retail Sales)
- 12:30 - Midday economic reports
- 14:00 - FOMC statements, GDP releases
- 18:00 - ECB announcements
- 19:00 - Evening economic data
Detection: Preset checks if current time is within 5 minutes before any scheduled news event.
Parameters:
StraddleDistancePoints = 15 // 15 points from current price
Volume = 0.02 // 0.02 lots (fixed)
StopLossPoints = 20 // 20 points SL
TakeProfitPoints = 40 // 40 points TP
SecondsBeforeNews = 30 // 30 sec countdown (shortened for demo)
MaxWaitAfterNewsSeconds = 120 // Wait 2 minutes for breakout
π CONDITION 5: BREAKOUT SIGNAL (Spread > 3 points)ΒΆ
Selected Orchestrator: PendingBreakoutOrchestrator
Why: Large spread indicates potential breakout movement. Places BUY STOP above and SELL STOP below to catch momentum.
Detection: Current spread exceeds 3.0 points (unusual width suggests imminent volatility).
Parameters:
BreakoutDistancePoints = 20 // 20 points from current price
Volume = 0.01 // 0.01 lots (fixed)
StopLossPoints = 20 // 20 points SL
TakeProfitPoints = 40 // 40 points TP
MaxWaitMinutes = 3 // Wait 3 minutes for breakout
Configuration ParametersΒΆ
| Parameter | Type | Default | Description |
|---|---|---|---|
Symbol |
string | "EURUSD" |
Trading instrument |
BaseRiskAmount |
double | 20.0 |
Base risk per trade in dollars |
LowVolatilityThreshold |
double | 15.0 |
Max volatility for grid mode (points) |
HighVolatilityThreshold |
double | 40.0 |
Min volatility for hedge mode (points) |
EnableNewsMode |
bool | true |
Enable news event detection |
MinutesBeforeNews |
int | 5 |
Switch to news mode N minutes early |
Configuration ExampleΒΆ
var preset = new AdaptiveMarketModePreset(service)
{
Symbol = "GBPUSD",
BaseRiskAmount = 50.0, // Higher risk for larger account
LowVolatilityThreshold = 20.0, // More opportunities for grid
HighVolatilityThreshold = 30.0, // Trigger hedge earlier
EnableNewsMode = true
};
How to RunΒΆ
You can execute this preset using several command variations:
# Option 1: By number
dotnet run 14
# Option 2: By short name
dotnet run adaptive
# Option 3: By full name
dotnet run preset
All three commands will launch the AdaptiveMarketModePreset with the default configuration or the settings you specify in the code.
AlgorithmΒΆ
Main Cycle FlowchartΒΆ
START PRESET
β
Initialize (get starting balance, maxCycles=100)
β
βββββ CYCLE LOOP (repeat up to maxCycles)
β
β STEP 1: Analyze Market Conditions
β β Get current tick (bid/ask/spread)
β β Calculate volatility (spread Γ 10)
β β Check UTC time for news
β β Check spread for breakout signal
β β Determine MarketCondition enum
β
β STEP 2: Select Orchestrator
β β News mode? β NewsStraddleOrchestrator
β β Breakout? β PendingBreakoutOrchestrator
β β Low vol? β GridTradingOrchestrator
β β Med vol? β SimpleScalpingOrchestrator
β β High vol? β QuickHedgeOrchestrator
β
β STEP 3: Execute Selected Orchestrator
β β await selectedOrchestrator.ExecuteAsync()
β
β STEP 4: Track Results
β β Add cycle P/L to totalProfit
β β Increment cyclesRun counter
β β Display cycle summary
β
β STEP 5: Safety Check
β β If totalProfit < -BaseRiskAmount Γ 5
β β STOP (max loss exceeded)
β
β STEP 6: Pause Between Cycles
β β Wait 30 seconds (prevent overtrading)
β
β If cyclesRun < maxCycles β Loop back
ββββββββββββββββββββββββββββββββββββββββββββββ
β
Display Final Results & Exit
END PRESET
Market Analysis AlgorithmΒΆ
private async Task<MarketCondition> AnalyzeMarketConditions()
{
// STEP 1: Get current market data
var tick = await _service.SymbolInfoTickAsync(Symbol);
var point = await _service.GetPointAsync(Symbol);
// STEP 2: Calculate spread in points
var spreadPoints = (tick.Ask - tick.Bid) / point;
// STEP 3: Estimate volatility (simplified demo formula)
var avgRangePoints = spreadPoints * 10; // β οΈ Production: use ATR
// STEP 4: Priority 1 - Check news time
if (EnableNewsMode && IsNewsTime())
return MarketCondition.News;
// STEP 5: Priority 2 - Check breakout signal
if (spreadPoints > 3.0)
return MarketCondition.Breakout;
// STEP 6: Classify by volatility
if (avgRangePoints < LowVolatilityThreshold)
return MarketCondition.Grid;
else if (avgRangePoints < HighVolatilityThreshold)
return MarketCondition.Scalping;
else
return MarketCondition.HighVolatility;
}
Priority Order:
- News - Highest priority (scheduled events)
- Breakout - High priority (unusual spread)
- Volatility - Normal priority (Grid β Scalping β Hedge)
Strategy VisualizationΒΆ
Example Cycle 1: Medium Volatility β ScalpingΒΆ
T=0 Cycle #1 starts
β
Analyze Market:
Spread: 0.00015 (1.5 pts)
Volatility: 1.5 Γ 10 = 15 pts
Time: 10:00 UTC (no news)
Spread: 1.5 pts (no breakout)
β
Condition: MEDIUM VOLATILITY (15 pts)
β
Selected: SimpleScalpingOrchestrator
Parameters:
- RiskAmount: $20
- SL: 15 pts, TP: 25 pts
- Direction: BUY (random)
β
T=1 Execute SimpleScalpingOrchestrator
β Open BUY position
β Hold 60 seconds
β TP triggered at +17s
β Profit: +$25
β
T=2 Cycle complete
Total profit: $25
Cycles run: 1
β
Wait 30 seconds
β
T=3 Start Cycle #2...
Example Cycle 2: News Time β News StraddleΒΆ
T=0 Cycle #2 starts
β
Analyze Market:
Time: 08:28 UTC
News at 08:30 UTC (2 min away)
EnableNewsMode: true
MinutesBeforeNews: 5
β
Condition: NEWS EVENT
β
Selected: NewsStraddleOrchestrator
Parameters:
- StraddleDistance: 15 pts
- Volume: 0.02 lots
- SecondsBeforeNews: 30s
β
T=0.5 Execute NewsStraddleOrchestrator
β Wait 30 seconds countdown
β Place BUY STOP above
β Place SELL STOP below
β Wait for breakout
β BUY STOP triggered
β Cancel SELL STOP
β TP hit at +45s
β Profit: +$40
β
T=3 Cycle complete
Total profit: $65 ($25 + $40)
Cycles run: 2
β
Wait 30 seconds...
Example Cycle 3: High Volatility β HedgeΒΆ
T=0 Cycle #3 starts
β
Analyze Market:
Spread: 0.00045 (4.5 pts)
Volatility: 4.5 Γ 10 = 45 pts
β
Condition: HIGH VOLATILITY (45 pts > 40)
β
Selected: QuickHedgeOrchestrator
Parameters:
- RiskAmount: $20 Γ 0.7 = $14 (reduced!)
- SL: 25 pts, TP: 40 pts
- HedgeTrigger: 15 pts
β
T=0.5 Execute QuickHedgeOrchestrator
β Open SELL position
β Monitor price movement
β Price moves -18 pts (adverse)
β Hedge triggered! Open BUY
β Hold both 30 seconds
β Close all
β Profit: -$12 (limited loss)
β
T=2 Cycle complete
Total profit: $53 ($65 - $12)
Cycles run: 3
β
Continue...
Volatility Analysis MethodΒΆ
Demo Formula (Simplified)ΒΆ
var spreadPoints = (tick.Ask - tick.Bid) / point;
var avgRangePoints = spreadPoints * 10; // Simple multiplier
if (avgRangePoints < 15)
return "Low Volatility";
else if (avgRangePoints < 40)
return "Medium Volatility";
else
return "High Volatility";
Why this works:
- Spread widens when volatility increases.
- Multiplier (10Γ) amplifies small spread changes into meaningful volatility estimates.
- Fast calculation without historical data.
Limitations:
- β οΈ Not suitable for production - too simplistic.
- Doesn't account for recent price movements.
- Sensitive to broker spread changes.
Production RecommendationΒΆ
// Use ATR (Average True Range) for real volatility
var bars = await _service.GetBarsAsync(Symbol, Timeframe.M5, count: 20);
var atr = CalculateATR(bars, period: 14);
var avgRangePoints = atr / point;
// Now classify based on ATR
if (avgRangePoints < 15)
return MarketCondition.Grid;
// ...
News Detection ScheduleΒΆ
Default UTC ScheduleΒΆ
| Time (UTC) | Event Types |
|---|---|
| 08:30 | NFP, CPI, Retail Sales, Jobless Claims |
| 12:30 | Midday economic reports, Fed speeches |
| 14:00 | FOMC statements, GDP, Consumer Confidence |
| 18:00 | ECB press conferences, Eurozone data |
| 19:00 | Evening economic indicators |
Detection LogicΒΆ
private bool IsNewsTime()
{
var now = DateTime.UtcNow;
var currentHourMinute = now.Hour * 60 + now.Minute;
int[] newsMinutes = {
8 * 60 + 30, // 08:30
12 * 60 + 30, // 12:30
14 * 60, // 14:00
18 * 60, // 18:00
19 * 60 // 19:00
};
foreach (var newsMin in newsMinutes)
{
var diff = Math.Abs(currentHourMinute - newsMin);
if (diff <= MinutesBeforeNews) // Default: 5 minutes
return true;
}
return false;
}
Trigger Window: 5 minutes before scheduled time - Example: For 08:30 news, triggers from 08:25 to 08:30.
Safety FeaturesΒΆ
1. Stop-Loss ProtectionΒΆ
if (totalProfit < -BaseRiskAmount * 5)
{
Console.WriteLine($"π STOP: Total loss exceeds ${BaseRiskAmount * 5:F2}");
break; // Exit preset
}
Default: Halts if total loss exceeds $100 (5 Γ $20 base risk).
2. Cycle LimitΒΆ
int maxCycles = 100; // Maximum 100 cycles per run
for (int cycle = 1; cycle <= maxCycles; cycle++)
{
// Execute cycle...
}
Purpose: Prevents infinite loops, ensures eventual termination.
3. Inter-Cycle PauseΒΆ
Purpose:
- Prevents overtrading
- Gives market time to develop
- Reduces commission costs
4. Emergency CloseΒΆ
catch (Exception ex)
{
Console.WriteLine($"β Error in cycle {cycle}: {ex.Message}");
try {
await _service.CloseAll(Symbol); // Close all positions
} catch { }
// Continue to next cycle
}
Purpose: Closes all positions on unexpected error, then continues.
Risk ManagementΒΆ
Adaptive Risk SizingΒΆ
CONDITION BASE RISK ACTUAL RISK REASON
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Grid $20 $20 (100%) Normal risk
Scalping $20 $20 (100%) Normal risk
High Volatility $20 $14 (70%) β οΈ REDUCED!
News Straddle $20 $20 (100%) Controlled breakout
Breakout $20 $20 (100%) Pending orders
Key Feature: Risk automatically reduced in high volatility to preserve capital.
Maximum Loss CalculationΒΆ
Given:
- BaseRiskAmount: $20
- Safety multiplier: 5Γ
- Max cycles: 100
Worst-case scenarios:
1. All cycles lose (impossible but theoretical):
100 cycles Γ $20 = $2000 max loss
2. Safety stop triggers:
5 cycles Γ $20 = $100 max loss (realistic limit)
3. High volatility cycles:
5 cycles Γ $14 = $70 max loss (reduced risk)
Win Rate AnalysisΒΆ
Assume:
- 40% win rate
- 100 cycles
- Average win: +$30
- Average loss: -$20
Results:
Wins: 40 Γ $30 = +$1200
Losses: 60 Γ -$20 = -$1200
Net: $0 (break-even)
With 45% win rate:
Wins: 45 Γ $30 = +$1350
Losses: 55 Γ -$20 = -$1100
Net: +$250 profit
Conclusion: 45% win rate = profitable!
Console OutputΒΆ
Example Full Cycle OutputΒΆ
+============================================================+
| ADAPTIVE MARKET MODE PRESET |
+============================================================+
Symbol: EURUSD
Base Risk: $20.00 per cycle
Starting Balance: $10000.00
Running up to 100 cycles...
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
CYCLE #1
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Market Analysis:
Spread: 1.5 points
Estimated Volatility: 15 points
Condition: MEDIUM VOLATILITY β SimpleScalpingOrchestrator
+============================================================+
| SIMPLE SCALPING ORCHESTRATOR |
+============================================================+
Starting balance: $10000.00
Symbol: EURUSD
Direction: BUY
Risk: $20.00
SL: 15 pts | TP: 25 pts
Max hold: 60s
Opening position...
β Position opened: #123456789
Volume: 0.18 lots
β³ Holding for 60s...
β Position closed automatically (SL/TP hit)
Final balance: $10025.00
Profit/Loss: $25.00
+============================================================+
β
Cycle #1 complete
Cycle P/L: $25.00
Total Profit: $25.00
Cycles Run: 1 / 100
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βΈ Waiting 30 seconds before next cycle...
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
CYCLE #2
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Market Analysis:
Time: 08:28 UTC
News detected at 08:30 UTC
Condition: NEWS EVENT β NewsStraddleOrchestrator
[... NewsStraddleOrchestrator output ...]
β
Cycle #2 complete
Cycle P/L: $40.00
Total Profit: $65.00
Cycles Run: 2 / 100
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[... more cycles ...]
+============================================================+
| FINAL RESULTS |
+============================================================+
Initial Balance: $10000.00
Final Balance: $10350.00
Total Profit: $350.00
Cycles Run: 15 / 100
Stopped: User cancelled (or max cycles/safety stop)
+============================================================+
Usage ExamplesΒΆ
Example 1: Conservative ProfileΒΆ
var service = new MT5Service(account);
var conservative = new AdaptiveMarketModePreset(service)
{
Symbol = "EURUSD",
BaseRiskAmount = 10.0, // Lower risk
LowVolatilityThreshold = 12.0, // Stricter grid entry
HighVolatilityThreshold = 45.0, // Stay in scalping longer
EnableNewsMode = false // Disable news trading
};
await conservative.ExecuteAsync();
Example 2: Aggressive ProfileΒΆ
var aggressive = new AdaptiveMarketModePreset(service)
{
Symbol = "GBPUSD",
BaseRiskAmount = 50.0, // Higher risk
LowVolatilityThreshold = 20.0, // More grid opportunities
HighVolatilityThreshold = 30.0, // Trigger hedge earlier
EnableNewsMode = true
};
await aggressive.ExecuteAsync();
Example 3: News-Only ModeΒΆ
var newsOnly = new AdaptiveMarketModePreset(service)
{
Symbol = "EURUSD",
BaseRiskAmount = 30.0,
LowVolatilityThreshold = 1.0, // Never trigger grid
HighVolatilityThreshold = 1000.0, // Never trigger hedge
EnableNewsMode = true,
MinutesBeforeNews = 10 // Earlier news detection
};
// This will primarily trade NewsStraddleOrchestrator
await newsOnly.ExecuteAsync();
When to Use Adaptive PresetΒΆ
β Suitable ConditionsΒΆ
- 24/7 automated trading: Set and forget
- Uncertain market conditions: Don't know which strategy to use
- Learning/backtesting: Test multiple strategies simultaneously
- Diversification: Spread risk across different approaches
- Medium-term trading: Run for days/weeks
β Unsuitable ConditionsΒΆ
- Strong trending markets: Better to use single directional strategy
- Expert trader with clear setup: Use specific orchestrator directly
- Very small account: Risk management may be challenging
- Need for manual control: Preset is fully automated
OptimizationΒΆ
Possible ImprovementsΒΆ
-
Replace volatility formula with ATR:
-
Add machine learning for strategy selection:
-
Dynamic risk adjustment based on performance:
-
Add custom news calendar API:
Related StrategiesΒΆ
- GridTradingOrchestrator: Used in low volatility.
- SimpleScalpingOrchestrator: Used in medium volatility.
- QuickHedgeOrchestrator: Used in high volatility.
- NewsStraddleOrchestrator: Used during news events.
- PendingBreakoutOrchestrator: Used on breakout signals.
SummaryΒΆ
AdaptiveMarketModePreset is an intelligent multi-strategy system:
β Pros:
- Fully automated - no manual strategy selection needed
- Adaptive - automatically adjusts to market conditions
- Diversified - uses 5 different approaches
- Risk-managed - reduced risk in high volatility, safety stops
- 24/7 capable - designed for continuous operation
- Educational - demonstrates strategy coordination
β Cons:
- Simplified volatility - demo formula not production-ready
- Static news schedule - doesn't use real-time news feed
- No machine learning - simple rule-based logic
- Commission costs - frequent trading in some conditions
- Requires monitoring - safety stops may trigger unexpectedly
Recommendation:
Excellent for learning how to combine multiple strategies into one system. The preset demonstrates intelligent orchestrator coordination and adaptive strategy selection. For production use, replace volatility calculation with ATR and integrate real-time news API.
β οΈ DEMO WARNING:
This preset uses simplified market analysis (spread Γ 10) for educational purposes. For real trading, implement proper volatility measurement (ATR) and use live news calendar API.