Compare commits
23 Commits
ee4da1b607
...
cleanup/le
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b18df7f37 | |||
| e76180cf54 | |||
| c6833669ad | |||
| 2ad2ddf38b | |||
| f8587125c0 | |||
| de6e150655 | |||
| 4453ff552a | |||
| 1cd8df759f | |||
| 2be9c843e5 | |||
| a6ececaf73 | |||
| ce74f68e54 | |||
| 3ccd3a8bfd | |||
| e2ea45b58f | |||
| 9a28a49292 | |||
| d856f3949d | |||
| c9e8b35f15 | |||
| ae8ac05017 | |||
| 229f4e413e | |||
| c094e65b10 | |||
| a2af272d73 | |||
| 2f623dc2f8 | |||
| 3282254572 | |||
| 498f298975 |
BIN
.gitattributes
vendored
Normal file
BIN
.gitattributes
vendored
Normal file
Binary file not shown.
@@ -1,204 +1,35 @@
|
||||
ALWAYS verify with the live Ninjascript reference available on the web at https://developer.ninjatrader.com/docs/desktop before making final edits.
|
||||
|
||||
This third file confirms a recurrence of CS0246 compiler errors in VWAP_Pro.cs, specifically related to undefined types like ModeType and AnchorModeType.
|
||||
|
||||
🔍 CS0246 - "The type or namespace name 'X' could not be found"
|
||||
Line Column Missing Type
|
||||
371 28 ModeType
|
||||
371 43 AnchorModeType
|
||||
391 39 ModeType
|
||||
391 54 AnchorModeType
|
||||
376 51 ModeType
|
||||
✅ How to Fix It
|
||||
🔧 1. Define Missing Enums (Recommended Fix)
|
||||
|
||||
It seems that ModeType and AnchorModeType are custom enums that were expected to exist in the code but were never declared.
|
||||
|
||||
Create a separate file or include this in the same VWAP_Pro.cs file near the top:
|
||||
|
||||
public enum ModeType
|
||||
{
|
||||
Standard,
|
||||
Aggressive,
|
||||
Conservative
|
||||
}
|
||||
|
||||
public enum AnchorModeType
|
||||
{
|
||||
SessionStart,
|
||||
FixedTime,
|
||||
Custom
|
||||
}
|
||||
|
||||
|
||||
You’ll need to customize the enum values (Standard, Aggressive, etc.) to match your strategy logic. These names are placeholders based on typical VWAP settings.
|
||||
|
||||
🔧 2. OR Add Missing using Directives
|
||||
|
||||
If these types exist in another NinjaTrader script or add-on you've imported before, ensure you include the right namespace:
|
||||
|
||||
using NinjaTrader.NinjaScript.AddOns.MyVWAPEnums; // Example
|
||||
|
||||
|
||||
But since these errors suggest the types are completely undefined, it's much more likely they are simply not declared anywhere.
|
||||
|
||||
🔧 3. Verify File Inclusion in NinjaTrader
|
||||
|
||||
If these enums are defined in another file (e.g., VWAPTypes.cs), make sure:
|
||||
|
||||
It's saved in the same directory as VWAP_Pro.cs
|
||||
|
||||
It's added to the NinjaScript Editor, not just sitting in the file system
|
||||
|
||||
You can add it using:
|
||||
|
||||
Tools > NinjaScript Editor > Right-click > Add > Existing Item...
|
||||
|
||||
🧼 Suggested Organization
|
||||
|
||||
If these enums are used across multiple indicators/strategies, you should centralize them:
|
||||
|
||||
VWAP_Enums.cs
|
||||
|
||||
namespace NinjaTrader.NinjaScript.VWAP
|
||||
{
|
||||
public enum ModeType
|
||||
{
|
||||
Standard,
|
||||
Aggressive,
|
||||
Conservative
|
||||
}
|
||||
|
||||
public enum AnchorModeType
|
||||
{
|
||||
SessionStart,
|
||||
FixedTime,
|
||||
Custom
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Then use:
|
||||
|
||||
using NinjaTrader.NinjaScript.VWAP;
|
||||
|
||||
|
||||
|
||||
These are occurring in VWAP_Pro.cs, mainly on lines 132–134:
|
||||
|
||||
Line Error Message (Truncated)
|
||||
132 Argument 2: cannot convert from 'int' to 'NinjaTrader.Gui.Tools.SimpleFont'
|
||||
132 Argument 3: cannot convert from 'NinjaTrader.Gui.Tools.SimpleFont' to 'System.Windows.Media.Brush'
|
||||
133 Same as above
|
||||
134 Same as above
|
||||
🔧 Interpretation:
|
||||
|
||||
It looks like a method (likely a drawing method like Draw.Text() or Draw.TextFixed()) is being called with the wrong argument types — specifically:
|
||||
|
||||
An int is being passed where a SimpleFont is expected.
|
||||
|
||||
A SimpleFont is being passed where a Brush is expected.
|
||||
|
||||
This suggests that arguments are out of order or misassigned.
|
||||
|
||||
✅ Proper Fix
|
||||
|
||||
Let’s consider the proper usage of Draw.Text() in NinjaTrader 8:
|
||||
|
||||
Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, int barsAgo, double y, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, int outlineWidth);
|
||||
|
||||
|
||||
Or for simpler usage:
|
||||
|
||||
Draw.Text(this, "tag1", "Hello", 0, Close[0], Brushes.White);
|
||||
|
||||
|
||||
Your issue likely looks like this:
|
||||
|
||||
Draw.Text(this, "tag1", true, "Label", 0, 100, 12, someFont, ...);
|
||||
|
||||
|
||||
Where 12 (int) is mistakenly passed as a font or brush, causing the error.
|
||||
|
||||
🔧 Corrected Example:
|
||||
|
||||
Assuming you want to draw text with a specific font and color:
|
||||
|
||||
SimpleFont font = new SimpleFont("Arial", 12);
|
||||
Draw.Text(this, "tag1", true, "VWAP Label", 0, Close[0], Brushes.White, font, TextAlignment.Center, Brushes.Black, 1);
|
||||
|
||||
|
||||
Brushes.White → text color
|
||||
|
||||
font → SimpleFont object
|
||||
|
||||
TextAlignment.Center → alignment
|
||||
|
||||
Brushes.Black → outline brush
|
||||
|
||||
1 → outline width
|
||||
|
||||
✅ Steps to Fix:
|
||||
|
||||
Replace the integer (e.g., 12) with a SimpleFont:
|
||||
|
||||
new SimpleFont("Arial", 12)
|
||||
|
||||
|
||||
Ensure arguments are in the correct order:
|
||||
Double-check the method signature from the NinjaTrader 8 Help Guide
|
||||
.
|
||||
|
||||
Use Brushes for colors, not fonts.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Summary of Errors:
|
||||
Line Column Missing Type
|
||||
403 39 ModeType
|
||||
403 54 AnchorModeType
|
||||
408 63 ModeType
|
||||
408 78 AnchorModeType
|
||||
367 28 ModeType
|
||||
✅ Fixes:
|
||||
|
||||
These types — ModeType and AnchorModeType — are not recognized. Here are the likely causes and solutions:
|
||||
|
||||
🔍 1. Missing using Directive
|
||||
|
||||
These types might be defined in a different namespace. If they are from a custom or NinjaTrader add-on:
|
||||
|
||||
Fix: Add the appropriate using statement at the top of your file.
|
||||
|
||||
Example:
|
||||
|
||||
using NinjaTrader.NinjaScript.AddOns.VWAP;
|
||||
|
||||
|
||||
Adjust according to where ModeType and AnchorModeType are defined.
|
||||
|
||||
🔍 2. Missing Class Definitions
|
||||
|
||||
If they are not in any existing libraries, they might be custom enum types that should be defined in your project but are missing.
|
||||
|
||||
Fix: Add enum declarations like these (if applicable):
|
||||
|
||||
public enum ModeType
|
||||
{
|
||||
Standard,
|
||||
Aggressive,
|
||||
Conservative
|
||||
}
|
||||
|
||||
public enum AnchorModeType
|
||||
{
|
||||
SessionStart,
|
||||
FixedTime,
|
||||
Custom
|
||||
}
|
||||
|
||||
|
||||
Only do this if you know what the enum values should be. These names are placeholders — you should match them with how your indicator/strategy is designed.
|
||||
# Compile Error Guidance - Reusable Protocol
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
Apply this protocol for all compile issues.
|
||||
|
||||
## 1) Verify First
|
||||
- [ ] Verify exact NinjaScript/API signature against official NT8 docs before editing: `https://developer.ninjatrader.com/docs/desktop`.
|
||||
- [ ] Confirm file is in scope before making any change.
|
||||
|
||||
## 2) Classify the Error
|
||||
- [ ] Missing type/namespace (e.g., `CS0246`).
|
||||
- [ ] Invalid override/signature/access modifier (e.g., `CS0115`, `CS0507`).
|
||||
- [ ] Argument mismatch or wrong overload (e.g., `CS1503`).
|
||||
- [ ] C# language version incompatibility (C# 6+ syntax in C# 5 project).
|
||||
|
||||
## 3) Apply Smallest Safe Fix
|
||||
- [ ] Prefer minimal edits in scoped files only.
|
||||
- [ ] Fix root cause, not symptom chaining.
|
||||
- [ ] Preserve existing architecture/contracts unless task explicitly requires change.
|
||||
|
||||
## 4) Re-Run Verification
|
||||
- [ ] Run `.\verify-build.bat`.
|
||||
- [ ] Run required tests for changed area.
|
||||
- [ ] If NinjaScript touched, run NT8 compile check in NinjaScript Editor.
|
||||
|
||||
## 5) Capture Durable Learning
|
||||
For non-trivial compile failures, add concise entries to:
|
||||
- [ ] `docs/00-governance/common_failures.md` (error fingerprint + fix).
|
||||
- [ ] `docs/00-governance/compile_guardrails.md` (prevention rule).
|
||||
- [ ] `docs/00-governance/patterns_and_antipatterns.md` (good vs bad pattern).
|
||||
|
||||
## Do Not
|
||||
- [ ] Do not guess signatures, overloads, enums, or attributes.
|
||||
- [ ] Do not invent placeholder enums/types unless confirmed by domain/task requirements.
|
||||
- [ ] Do not keep one-off incident dumps in this rule file; store incidents in `common_failures.md`.
|
||||
|
||||
@@ -1,195 +1,33 @@
|
||||
# Coding Patterns — NT8 SDK Required Patterns
|
||||
# Coding Patterns - NT8 SDK Required Patterns
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
All code in the NT8 SDK MUST follow these patterns without exception.
|
||||
All production code must use these implementation patterns.
|
||||
|
||||
---
|
||||
## Scope Discipline and Minimum Diff
|
||||
- [ ] Edit only files in task scope.
|
||||
- [ ] Make the smallest safe change that resolves the issue.
|
||||
- [ ] Do not change interfaces/contracts unless explicitly required.
|
||||
- [ ] Do not mix functional changes with style-only cleanup.
|
||||
|
||||
## 1. Thread Safety — Lock Everything Shared
|
||||
## C# 5.0 and .NET 4.8 Compatibility
|
||||
- [ ] Use C# 5.0 syntax only.
|
||||
- [ ] Avoid C# 6+ features (`$""`, `?.`, `nameof`, expression-bodied members, `out var`, etc.).
|
||||
- [ ] Keep compatibility with .NET Framework 4.8.
|
||||
- [ ] Use `string.Format` style logging/messages.
|
||||
|
||||
Every class with shared state must have a lock object:
|
||||
```csharp
|
||||
private readonly object _lock = new object();
|
||||
```
|
||||
## Core Implementation Patterns
|
||||
- [ ] Validate inputs at method entry.
|
||||
- [ ] Wrap public method logic in `try/catch` with meaningful logging.
|
||||
- [ ] Protect shared mutable collections/state with `lock (_lock)`.
|
||||
- [ ] Do not raise events while holding locks.
|
||||
- [ ] Keep public members documented with XML comments where required by project standards.
|
||||
|
||||
Every access to shared `Dictionary`, `List`, `Queue`, or any field touched by multiple threads:
|
||||
```csharp
|
||||
// ❌ NEVER
|
||||
_activeOrders[orderId] = status;
|
||||
## NinjaScript Coding Patterns (When Applicable)
|
||||
- [ ] Keep `OnStateChange` responsibilities separated by state.
|
||||
- [ ] Guard `OnBarUpdate` by `BarsInProgress` and bar readiness.
|
||||
- [ ] In managed order flow, set stop/target before entry on the same bar.
|
||||
|
||||
// ✅ ALWAYS
|
||||
lock (_lock)
|
||||
{
|
||||
_activeOrders[orderId] = status;
|
||||
}
|
||||
```
|
||||
|
||||
### Read-then-write must be atomic
|
||||
```csharp
|
||||
// ❌ WRONG — race condition between check and write
|
||||
if (!_orders.ContainsKey(id))
|
||||
_orders[id] = newOrder;
|
||||
|
||||
// ✅ CORRECT
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_orders.ContainsKey(id))
|
||||
_orders[id] = newOrder;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Error Handling — Try-Catch on All Public Methods
|
||||
|
||||
```csharp
|
||||
public ReturnType MethodName(Type parameter)
|
||||
{
|
||||
// 1. Validate parameters first
|
||||
if (parameter == null)
|
||||
throw new ArgumentNullException("parameter");
|
||||
|
||||
// 2. Wrap the main logic
|
||||
try
|
||||
{
|
||||
// Implementation
|
||||
return result;
|
||||
}
|
||||
catch (SpecificException ex)
|
||||
{
|
||||
_logger.LogError("Specific failure in MethodName: {0}", ex.Message);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Unexpected failure in MethodName: {0}", ex.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Logging — Always string.Format, Never $""
|
||||
|
||||
```csharp
|
||||
// ❌ NEVER — C# 6 syntax, breaks NT8 compile
|
||||
_logger.LogInformation($"Order {orderId} filled");
|
||||
|
||||
// ✅ ALWAYS
|
||||
_logger.LogInformation("Order {0} filled", orderId);
|
||||
_logger.LogWarning("Risk check failed for {0}: {1}", symbol, reason);
|
||||
_logger.LogError("Exception in {0}: {1}", "MethodName", ex.Message);
|
||||
_logger.LogCritical("Emergency flatten triggered: {0}", reason);
|
||||
```
|
||||
|
||||
### Log level guide
|
||||
| Level | When to use |
|
||||
|---|---|
|
||||
| `LogTrace` | Entering/exiting methods, fine-grained flow |
|
||||
| `LogDebug` | State reads, normal data flow |
|
||||
| `LogInformation` | Important events: order submitted, filled, cancelled |
|
||||
| `LogWarning` | Recoverable issues: validation failed, limit approaching |
|
||||
| `LogError` | Failures: exceptions, unexpected states |
|
||||
| `LogCritical` | System integrity issues: emergency flatten, data corruption |
|
||||
|
||||
---
|
||||
|
||||
## 4. Events — Never Raise Inside Locks
|
||||
|
||||
Raising events inside a lock causes deadlocks when event handlers acquire other locks.
|
||||
|
||||
```csharp
|
||||
// ❌ DEADLOCK RISK
|
||||
lock (_lock)
|
||||
{
|
||||
_state = newState;
|
||||
OrderStateChanged?.Invoke(this, args); // handler may try to acquire _lock
|
||||
}
|
||||
|
||||
// ✅ CORRECT
|
||||
OrderState newState;
|
||||
lock (_lock)
|
||||
{
|
||||
newState = CalculateNewState();
|
||||
_state = newState;
|
||||
}
|
||||
// Raise AFTER releasing lock
|
||||
RaiseOrderStateChanged(orderId, previousState, newState);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Constructor — Validate All Dependencies
|
||||
|
||||
```csharp
|
||||
public MyClass(ILogger<MyClass> logger, ISomeDependency dep)
|
||||
{
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException("logger");
|
||||
if (dep == null)
|
||||
throw new ArgumentNullException("dep");
|
||||
|
||||
_logger = logger;
|
||||
_dep = dep;
|
||||
|
||||
// Initialize collections
|
||||
_activeOrders = new Dictionary<string, OrderStatus>();
|
||||
|
||||
_logger.LogInformation("MyClass initialized");
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. XML Documentation — Required on All Public Members
|
||||
|
||||
```csharp
|
||||
/// <summary>
|
||||
/// Brief one-line description of what this does.
|
||||
/// </summary>
|
||||
/// <param name="intent">The trading intent to validate.</param>
|
||||
/// <param name="context">Current strategy context with account state.</param>
|
||||
/// <returns>Risk decision indicating allow or reject.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown when intent or context is null.</exception>
|
||||
public RiskDecision ValidateOrder(StrategyIntent intent, StrategyContext context)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. NT8-Specific Patterns (NinjaScript)
|
||||
|
||||
When writing code that runs inside NinjaTrader (in `NT8.Adapters/`):
|
||||
|
||||
```csharp
|
||||
// Always guard OnBarUpdate
|
||||
protected override void OnBarUpdate()
|
||||
{
|
||||
if (BarsInProgress != 0) return;
|
||||
if (CurrentBar < BarsRequiredToTrade) return;
|
||||
// ...
|
||||
}
|
||||
|
||||
// Managed order pattern — set stops BEFORE entry
|
||||
SetStopLoss("SignalName", CalculationMode.Ticks, stopTicks, false);
|
||||
SetProfitTarget("SignalName", CalculationMode.Ticks, targetTicks);
|
||||
EnterLong(contracts, "SignalName");
|
||||
|
||||
// Use string.Format for Print() too
|
||||
Print(string.Format("Order submitted: {0} contracts at {1}", qty, price));
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Checklist Before Marking Any Method Complete
|
||||
|
||||
- [ ] Parameter null checks at the top
|
||||
- [ ] `try-catch` wrapping the body
|
||||
- [ ] All `Dictionary`/collection access inside `lock (_lock)`
|
||||
- [ ] All logging uses `string.Format()` (no `$""`)
|
||||
- [ ] XML `/// <summary>` on every public method, property, class
|
||||
- [ ] No C# 6+ syntax
|
||||
- [ ] Events raised outside lock blocks
|
||||
- [ ] `verify-build.bat` passes
|
||||
## Authoritative Rule References (Do Not Duplicate)
|
||||
- Syntax constraints: `.kilocode/rules/csharp_50_syntax.md`
|
||||
- NT8 compile and API constraints: `.kilocode/rules/nt8compilespec.md`
|
||||
- Verification commands and gates: `.kilocode/rules/verification_requirements.md`
|
||||
|
||||
@@ -124,3 +124,8 @@ Math.Floor(x); // (via standard using System;)
|
||||
- Search for `=>` — if on a property or method, rewrite as full block
|
||||
- Search for `nameof` — replace with string literal
|
||||
- Search for `out var` — split into declaration + assignment
|
||||
|
||||
## NinjaScript attributes
|
||||
|
||||
`[Optimizable]` does not exist in NinjaScript. Use `[NinjaScriptProperty]`
|
||||
and `[Range(min, max)]` instead. See nt8compilespec.md for full NT8 API rules.
|
||||
|
||||
@@ -1,243 +1,46 @@
|
||||
# NT8 Institutional SDK - Development Workflow
|
||||
# NT8-SDK Development Workflow
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
## Overview
|
||||
This document outlines the development workflow for the NT8 Institutional SDK, following the Archon workflow principles even in the absence of the Archon MCP server.
|
||||
Operational workflow for Kilo/Codex-assisted development.
|
||||
|
||||
## Archon Workflow Principles
|
||||
## Per-Task Workflow (Required)
|
||||
1. **Confirm scope**
|
||||
- [ ] Confirm exact files allowed by task spec.
|
||||
- [ ] Cross-check `.kilocode/rules/file_boundaries.md`.
|
||||
2. **Set task state**
|
||||
- [ ] Move task status from `todo` -> `doing` before edits.
|
||||
3. **Implement**
|
||||
- [ ] Make minimum-diff changes only in scoped files.
|
||||
4. **Verify**
|
||||
- [ ] Run `.\verify-build.bat` per `.kilocode/rules/verification_requirements.md`.
|
||||
- [ ] Run focused tests required by changed area.
|
||||
5. **NT8 compile/deploy checks** (when NinjaScript files are touched)
|
||||
- [ ] Validate signatures against official NT8 docs.
|
||||
- [ ] Run repo build/deploy sequence required for NT8.
|
||||
- [ ] Compile in NinjaScript Editor (authoritative NT8 check).
|
||||
6. **Documentation updates**
|
||||
- [ ] Update `docs/00-governance/active_work.md` when task status or blockers changed.
|
||||
- [ ] Update `docs/00-governance/roadmap.md` when sequencing/scope changed.
|
||||
- [ ] Update `docs/00-governance/decisions.md` when durable architecture decisions were made.
|
||||
- [ ] Update `CLEANUP_LOG.md` when cleanup debt status changed.
|
||||
7. **Compile-learning capture** (if any compile issue occurred)
|
||||
- [ ] Add prevention rule to `docs/00-governance/compile_guardrails.md`.
|
||||
- [ ] Add error fingerprint + fix to `docs/00-governance/common_failures.md`.
|
||||
- [ ] Add pattern/anti-pattern to `docs/00-governance/patterns_and_antipatterns.md`.
|
||||
8. **Set task state and report**
|
||||
- [ ] Move task status from `doing` -> `review`.
|
||||
- [ ] Report changed files and verification evidence.
|
||||
|
||||
The development process follows these core principles adapted from the Archon workflow:
|
||||
## Definition of Done (Required)
|
||||
- [ ] Only scoped files changed.
|
||||
- [ ] Verification gates passed.
|
||||
- [ ] NT8 compile checks completed when relevant.
|
||||
- [ ] Relevant governance docs updated.
|
||||
- [ ] Compile learnings captured when applicable.
|
||||
- [ ] Task moved to `review` with evidence.
|
||||
|
||||
### 1. Check Current Task
|
||||
Before beginning any work, clearly define what needs to be accomplished:
|
||||
- Review requirements and specifications
|
||||
- Understand success criteria
|
||||
- Identify dependencies and blockers
|
||||
|
||||
### 2. Research for Task
|
||||
Conduct thorough research before implementation:
|
||||
- Review existing code and documentation
|
||||
- Understand best practices and patterns
|
||||
- Identify potential challenges and solutions
|
||||
|
||||
### 3. Implement the Task
|
||||
Execute the implementation with focus and precision:
|
||||
- Follow established patterns and conventions
|
||||
- Write clean, maintainable code
|
||||
- Include comprehensive error handling
|
||||
- Add structured logging for observability
|
||||
|
||||
### 4. Update Task Status
|
||||
Track progress and document completion:
|
||||
- Mark tasks as completed in the todo list
|
||||
- Document any issues or deviations
|
||||
- Note lessons learned for future reference
|
||||
|
||||
### 5. Get Next Task
|
||||
Move systematically through the implementation:
|
||||
- Prioritize tasks based on dependencies
|
||||
- Focus on one task at a time
|
||||
- Ensure quality before moving forward
|
||||
|
||||
## Development Process
|
||||
|
||||
### Phase 1: Planning and Design
|
||||
1. Review specifications and requirements
|
||||
2. Create architecture diagrams and documentation
|
||||
3. Identify core components and their interactions
|
||||
4. Plan implementation approach and timeline
|
||||
|
||||
### Phase 2: Environment Setup
|
||||
1. Create project structure and configuration files
|
||||
2. Set up build and test infrastructure
|
||||
3. Configure CI/CD pipeline
|
||||
4. Verify development environment
|
||||
|
||||
### Phase 3: Core Implementation
|
||||
1. Implement core interfaces and models
|
||||
2. Develop risk management components
|
||||
3. Create position sizing algorithms
|
||||
4. Build supporting utilities and helpers
|
||||
|
||||
### Phase 4: Testing and Validation
|
||||
1. Create comprehensive unit tests
|
||||
2. Implement integration tests
|
||||
3. Run validation scripts
|
||||
4. Verify all success criteria
|
||||
|
||||
### Phase 5: Documentation and Delivery
|
||||
1. Create developer documentation
|
||||
2. Write user guides and examples
|
||||
3. Prepare release notes
|
||||
4. Conduct final validation
|
||||
|
||||
## Code Quality Standards
|
||||
|
||||
### 1. Code Structure
|
||||
- Follow established naming conventions
|
||||
- Use consistent formatting and style
|
||||
- Organize code into logical modules
|
||||
- Maintain clear separation of concerns
|
||||
|
||||
### 2. Error Handling
|
||||
- Validate all inputs and parameters
|
||||
- Provide meaningful error messages
|
||||
- Handle exceptions gracefully
|
||||
- Log errors for debugging
|
||||
|
||||
### 3. Testing
|
||||
- Write unit tests for all public methods
|
||||
- Include edge case testing
|
||||
- Validate error conditions
|
||||
- Maintain >90% code coverage
|
||||
|
||||
### 4. Documentation
|
||||
- Include XML documentation for all public APIs
|
||||
- Add inline comments for complex logic
|
||||
- Document configuration options
|
||||
- Provide usage examples
|
||||
|
||||
## Git Workflow
|
||||
|
||||
### Branching Strategy
|
||||
- Use feature branches for all development
|
||||
- Create branches from main for new features
|
||||
- Keep feature branches short-lived
|
||||
- Merge to main after review and testing
|
||||
|
||||
### Commit Guidelines
|
||||
- Write clear, descriptive commit messages
|
||||
- Make small, focused commits
|
||||
- Reference issues or tasks in commit messages
|
||||
- Squash related commits before merging
|
||||
|
||||
### Pull Request Process
|
||||
- Create PRs for all feature work
|
||||
- Include description of changes and testing
|
||||
- Request review from team members
|
||||
- Address feedback before merging
|
||||
|
||||
## Development Environment
|
||||
|
||||
### Required Tools
|
||||
- .NET 6.0 SDK
|
||||
- Visual Studio Code or Visual Studio
|
||||
- Git for version control
|
||||
- Docker Desktop (recommended)
|
||||
|
||||
### Recommended Extensions
|
||||
- C# for Visual Studio Code
|
||||
- EditorConfig for VS Code
|
||||
- GitLens for enhanced Git experience
|
||||
- Docker extension for container management
|
||||
|
||||
## Build and Test Process
|
||||
|
||||
### Local Development
|
||||
1. Restore NuGet packages: `dotnet restore`
|
||||
2. Build solution: `dotnet build`
|
||||
3. Run tests: `dotnet test`
|
||||
4. Run specific test categories if needed
|
||||
|
||||
### Continuous Integration
|
||||
- Automated builds on every commit
|
||||
- Run full test suite on each build
|
||||
- Generate code coverage reports
|
||||
- Deploy to test environments
|
||||
|
||||
## Debugging and Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
1. **Build Failures**
|
||||
- Check for missing NuGet packages
|
||||
- Verify .NET SDK version
|
||||
- Ensure all projects reference correct frameworks
|
||||
|
||||
2. **Test Failures**
|
||||
- Review test output for specific errors
|
||||
- Check test data and setup
|
||||
- Verify mock configurations
|
||||
|
||||
3. **Runtime Errors**
|
||||
- Check logs for error details
|
||||
- Validate configuration settings
|
||||
- Review dependency injection setup
|
||||
|
||||
### Debugging Tools
|
||||
- Visual Studio debugger
|
||||
- Console logging
|
||||
- Structured logging with correlation IDs
|
||||
- Performance profiling tools
|
||||
|
||||
## Release Process
|
||||
|
||||
### Versioning
|
||||
- Follow semantic versioning (MAJOR.MINOR.PATCH)
|
||||
- Increment version in Directory.Build.props
|
||||
- Update release notes with changes
|
||||
- Tag releases in Git
|
||||
|
||||
### Deployment
|
||||
- Create NuGet packages for SDK components
|
||||
- Publish to internal package repository
|
||||
- Update documentation with release notes
|
||||
- Notify stakeholders of new releases
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Code Reviews
|
||||
- Review all code before merging
|
||||
- Focus on correctness, maintainability, and performance
|
||||
- Provide constructive feedback
|
||||
- Ensure adherence to coding standards
|
||||
|
||||
### 2. Performance Considerations
|
||||
- Minimize allocations in hot paths
|
||||
- Use efficient data structures
|
||||
- Cache expensive operations
|
||||
- Profile performance regularly
|
||||
|
||||
### 3. Security
|
||||
- Validate all inputs
|
||||
- Sanitize user data
|
||||
- Protect sensitive configuration
|
||||
- Follow secure coding practices
|
||||
|
||||
### 4. Maintainability
|
||||
- Write self-documenting code
|
||||
- Use meaningful variable and method names
|
||||
- Keep methods small and focused
|
||||
- Refactor regularly to improve design
|
||||
|
||||
## Task Management Without Archon
|
||||
|
||||
Since we're not using the Archon MCP server, we'll manage tasks using:
|
||||
1. **Todo Lists**: Track progress using markdown checklists
|
||||
2. **Documentation**: Maintain detailed records of implementation decisions
|
||||
3. **Git**: Use commits and branches to track work progress
|
||||
4. **Issue Tracking**: Use GitHub Issues or similar for task management
|
||||
|
||||
### Task Status Tracking
|
||||
- **Todo**: Task identified but not started
|
||||
- **In Progress**: Actively working on task
|
||||
- **Review**: Task completed, awaiting validation
|
||||
- **Done**: Task validated and completed
|
||||
|
||||
## Communication and Collaboration
|
||||
|
||||
### Team Coordination
|
||||
- Hold regular standups to discuss progress
|
||||
- Use collaborative tools for communication
|
||||
- Document architectural decisions
|
||||
- Share knowledge and best practices
|
||||
|
||||
### Knowledge Sharing
|
||||
- Conduct code walkthroughs for complex features
|
||||
- Create technical documentation
|
||||
- Share lessons learned from issues
|
||||
- Mentor new team members
|
||||
|
||||
## Conclusion
|
||||
|
||||
This development workflow ensures consistent, high-quality implementation of the NT8 Institutional SDK. By following these principles and practices, we can deliver a robust, maintainable, and scalable trading platform that meets institutional requirements for risk management and performance.
|
||||
|
||||
The workflow emphasizes systematic progress, quality assurance, and continuous improvement. Each task should be approached with thorough research, careful implementation, and comprehensive validation to ensure the highest quality outcome.
|
||||
## Out-of-Scope Safety
|
||||
- [ ] Do not modify files outside task scope.
|
||||
- [ ] Do not apply opportunistic refactors.
|
||||
- [ ] Do not modify historical/contextual docs unless explicitly requested.
|
||||
- [ ] If a real fix needs out-of-scope changes, stop and report a scope blocker.
|
||||
|
||||
31
.kilocode/rules/docs_maintenance.md
Normal file
31
.kilocode/rules/docs_maintenance.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Governance Docs Maintenance Policy
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
## Canonical Source Rule
|
||||
- [ ] `docs/00-governance/*` is the canonical planning and execution source.
|
||||
- [ ] Prefer updating canonical governance docs over duplicating guidance elsewhere.
|
||||
|
||||
## Historical/Contextual Docs (Read-Only by Default)
|
||||
Treat these as non-canonical unless explicitly requested by task scope:
|
||||
- `README.md`
|
||||
- `docs/README.md`
|
||||
- `PROJECT_HANDOVER.md`
|
||||
- `DESIGNED_VS_IMPLEMENTED_GAP_ANALYSIS.md`
|
||||
- `docs/INDEX.md`
|
||||
|
||||
## Update Triggers
|
||||
- [ ] Update `docs/00-governance/active_work.md` when priorities, status, blockers, or ownership change.
|
||||
- [ ] Update `docs/00-governance/roadmap.md` when milestone sequence, scope, or timing changes.
|
||||
- [ ] Update `docs/00-governance/decisions.md` when durable architecture tradeoffs/decisions are made.
|
||||
- [ ] Update `CLEANUP_LOG.md` when cleanup debt is created, resolved, or deferred.
|
||||
|
||||
## Compile Knowledge Capture (Concise and Durable)
|
||||
When a compile issue occurs, record:
|
||||
- [ ] Prevention rule in `docs/00-governance/compile_guardrails.md`.
|
||||
- [ ] Error fingerprint + fix in `docs/00-governance/common_failures.md`.
|
||||
- [ ] Reusable pattern and anti-pattern in `docs/00-governance/patterns_and_antipatterns.md`.
|
||||
|
||||
## Documentation Hygiene
|
||||
- [ ] Keep entries concise, actionable, and dated.
|
||||
- [ ] Link to authoritative rules instead of duplicating long guidance.
|
||||
- [ ] Do not update unrelated docs in the same task.
|
||||
35
.kilocode/rules/ninjascript_guardrails.md
Normal file
35
.kilocode/rules/ninjascript_guardrails.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# NinjaScript Guardrails - Operational Checklist
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
Use this checklist whenever touching NinjaScript-facing files.
|
||||
|
||||
## Pre-Edit Checks (Required)
|
||||
- [ ] Confirm file is in task scope and allowed by `.kilocode/rules/file_boundaries.md`.
|
||||
- [ ] Verify exact API signatures against official NT8 docs: `https://developer.ninjatrader.com/docs/desktop`.
|
||||
- [ ] Confirm C# 5.0 compatibility using `.kilocode/rules/csharp_50_syntax.md`.
|
||||
|
||||
## Lifecycle and Safety Rules (Required)
|
||||
- [ ] Use `OnStateChange` correctly: `State.SetDefaults`, `State.Configure`, `State.DataLoaded`.
|
||||
- [ ] Keep runtime logic out of `SetDefaults` and `Configure`.
|
||||
- [ ] Guard `OnBarUpdate` for series and readiness (`BarsInProgress`, `CurrentBar`/`CurrentBars`).
|
||||
- [ ] In managed order flow, set stop/target before entry on the same bar.
|
||||
- [ ] Do not mix managed and unmanaged order models unless explicitly required.
|
||||
|
||||
## API Integrity Rules (Required)
|
||||
- [ ] Do not guess method signatures, enum members, attributes, or overloads.
|
||||
- [ ] Do not use unsupported attributes (example: `[Optimizable]`).
|
||||
- [ ] Do not invent placeholder types/enums unless explicitly confirmed by task/domain requirements.
|
||||
|
||||
## Compile Validation Sequence
|
||||
- [ ] Run `.\verify-build.bat`.
|
||||
- [ ] Run required deploy step when NinjaScript source must be synced to NT8.
|
||||
- [ ] Compile in NT8 NinjaScript Editor (authoritative NinjaScript compile gate).
|
||||
|
||||
## Compile Failure Learning Loop
|
||||
If any compile issue occurs:
|
||||
- [ ] Capture error fingerprint (`code`, file, line, root cause).
|
||||
- [ ] Apply the smallest safe fix and re-run validation sequence.
|
||||
- [ ] Record durable learning in:
|
||||
- `docs/00-governance/common_failures.md` (symptom + fix)
|
||||
- `docs/00-governance/compile_guardrails.md` (preventive rule)
|
||||
- `docs/00-governance/patterns_and_antipatterns.md` (good vs bad pattern)
|
||||
@@ -137,3 +137,126 @@ Compile Checklist (Preflight)
|
||||
""").format(date=datetime.date.today().isoformat())
|
||||
|
||||
files["NT8_Templates.md"] = textwrap.dedent("""
|
||||
|
||||
---
|
||||
|
||||
# NinjaScript Compiler Constraints
|
||||
|
||||
## CRITICAL: Two separate compilers exist in this project
|
||||
|
||||
This project uses TWO compilers with DIFFERENT rules:
|
||||
|
||||
1. **dotnet build** — compiles src/NT8.Core/ and src/NT8.Adapters/ as
|
||||
standard .NET Framework 4.8 assemblies. Errors here show up in the
|
||||
terminal.
|
||||
|
||||
2. **NinjaTrader NinjaScript compiler** — compiles the .cs files deployed
|
||||
to `C:\Users\billy\Documents\NinjaTrader 8\bin\Custom\Strategies\`.
|
||||
Errors here only show up inside the NT8 NinjaScript Editor, NOT in
|
||||
dotnet build output.
|
||||
|
||||
**dotnet build passing does NOT mean NT8 will compile successfully.**
|
||||
Always treat NT8 compilation as a required separate verification step.
|
||||
|
||||
---
|
||||
|
||||
## NinjaScript-specific API rules
|
||||
|
||||
### OnConnectionStatusUpdate — correct signature
|
||||
|
||||
```csharp
|
||||
// CORRECT — single ConnectionStatusEventArgs parameter
|
||||
protected override void OnConnectionStatusUpdate(
|
||||
ConnectionStatusEventArgs connectionStatusUpdate)
|
||||
{
|
||||
connectionStatusUpdate.Status // ConnectionStatus enum
|
||||
connectionStatusUpdate.PriceStatus // ConnectionStatus enum
|
||||
}
|
||||
|
||||
// WRONG — this signature does not exist in NT8
|
||||
protected override void OnConnectionStatusUpdate(
|
||||
Connection connection,
|
||||
ConnectionStatus status,
|
||||
DateTime time) // CS0115 — no suitable method found to override
|
||||
```
|
||||
|
||||
### [Optimizable] attribute — does not exist
|
||||
|
||||
```csharp
|
||||
// WRONG — OptimizableAttribute does not exist in NinjaScript
|
||||
[Optimizable]
|
||||
public int StopTicks { get; set; } // CS0246
|
||||
|
||||
// CORRECT — all [NinjaScriptProperty] params are optimizer-eligible
|
||||
// Use [Range] to set optimizer bounds
|
||||
[NinjaScriptProperty]
|
||||
[Range(1, 50)]
|
||||
public int StopTicks { get; set; }
|
||||
```
|
||||
|
||||
### Attributes that DO exist in NinjaScript
|
||||
|
||||
```csharp
|
||||
[NinjaScriptProperty] // exposes to UI and optimizer
|
||||
[Display(...)] // controls UI label, group, order
|
||||
[Range(min, max)] // sets optimizer/validation bounds
|
||||
[Browsable(false)] // hides from UI
|
||||
[XmlIgnore] // excludes from serialization
|
||||
```
|
||||
|
||||
### Attributes that do NOT exist in NinjaScript
|
||||
|
||||
```
|
||||
[Optimizable] — use [NinjaScriptProperty] + [Range] instead
|
||||
[JsonProperty] — not available
|
||||
[Required] — not available
|
||||
```
|
||||
|
||||
### Method overrides — always verify against NT8 documentation
|
||||
|
||||
Before adding any `protected override` method to NT8StrategyBase.cs or
|
||||
any NinjaScript file, verify the exact signature at:
|
||||
https://developer.ninjatrader.com/docs/desktop
|
||||
|
||||
Common signatures that differ from intuition:
|
||||
|
||||
| Method | Correct NT8 Signature |
|
||||
|---|---|
|
||||
| OnConnectionStatusUpdate | `(ConnectionStatusEventArgs e)` |
|
||||
| OnMarketData | `(MarketDataEventArgs e)` |
|
||||
| OnMarketDepth | `(MarketDepthEventArgs e)` |
|
||||
| OnOrderUpdate | `(Order order, double limitPrice, double stopPrice, int quantity, int filled, double avgFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)` |
|
||||
| OnExecutionUpdate | `(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)` |
|
||||
|
||||
---
|
||||
|
||||
## Deployment reminder
|
||||
|
||||
The full deployment sequence is always:
|
||||
|
||||
```
|
||||
1. dotnet build NT8-SDK.sln --configuration Release
|
||||
2. deployment\deploy-to-nt8.bat
|
||||
3. NT8: Tools → NinjaScript Editor → Compile All
|
||||
4. Reload any open Strategy Analyzer or chart instances
|
||||
```
|
||||
|
||||
Steps 1-2 passing does NOT mean step 3 will pass. NT8 compilation is
|
||||
the only authoritative check for NinjaScript-specific code.
|
||||
|
||||
---
|
||||
|
||||
## Files compiled by NT8 (not dotnet build)
|
||||
|
||||
These files are deployed as source and compiled by NT8's embedded
|
||||
compiler. Any NT8-specific API usage in these files is invisible to
|
||||
dotnet build:
|
||||
|
||||
- `src/NT8.Adapters/Strategies/NT8StrategyBase.cs`
|
||||
- `src/NT8.Adapters/Strategies/SimpleORBNT8.cs`
|
||||
- `src/NT8.Adapters/Wrappers/BaseNT8StrategyWrapper.cs`
|
||||
- `src/NT8.Adapters/Wrappers/SimpleORBNT8Wrapper.cs`
|
||||
|
||||
Any new strategy or wrapper file added to these locations inherits the
|
||||
same constraint. When modifying these files, the NT8 compiler is the
|
||||
only valid test.
|
||||
|
||||
@@ -1,96 +1,45 @@
|
||||
# Project Context — NT8 SDK (Production Hardening Phase)
|
||||
# Project Context - NT8 SDK
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
You are working on the **NT8 SDK** — an institutional-grade algorithmic trading framework for NinjaTrader 8.
|
||||
This is production trading software. Bugs cause real financial losses.
|
||||
Mission: ship safe, compile-stable NT8 trading software with strict scope control and durable governance documentation.
|
||||
|
||||
---
|
||||
## Session Start (Mandatory)
|
||||
- [ ] Read these files first, in order:
|
||||
1. `docs/00-governance/executive_summary.md`
|
||||
2. `docs/00-governance/current_status.md`
|
||||
3. `docs/00-governance/active_work.md`
|
||||
4. `docs/00-governance/architecture.md`
|
||||
5. `docs/00-governance/roadmap.md`
|
||||
- [ ] Treat `docs/00-governance/*` as canonical for current direction.
|
||||
|
||||
## What Is Already Built (Do Not Touch)
|
||||
## Historical/Contextual Sources (Non-Canonical)
|
||||
Use only for background unless explicitly requested in task scope:
|
||||
- `README.md`
|
||||
- `docs/README.md`
|
||||
- `PROJECT_HANDOVER.md`
|
||||
- `DESIGNED_VS_IMPLEMENTED_GAP_ANALYSIS.md`
|
||||
- `docs/INDEX.md`
|
||||
|
||||
All core trading logic is complete and has 240+ passing tests:
|
||||
## File Touch Scope (Hard Rules)
|
||||
- [ ] Modify only files explicitly listed in the task spec.
|
||||
- [ ] Do not edit adjacent files for cleanup or style-only changes.
|
||||
- [ ] If a required fix is outside scope, stop and record a scope blocker.
|
||||
- [ ] Respect `.kilocode/rules/file_boundaries.md` at all times.
|
||||
|
||||
| Layer | Status | Key Files |
|
||||
|---|---|---|
|
||||
| Risk (Tier 1-3) | ✅ Complete | `src/NT8.Core/Risk/` |
|
||||
| Position Sizing | ✅ Complete | `src/NT8.Core/Sizing/` |
|
||||
| OMS / Order Lifecycle | ✅ Complete | `src/NT8.Core/OMS/` |
|
||||
| Intelligence | ✅ Complete | `src/NT8.Core/Intelligence/` |
|
||||
| Analytics | ✅ Complete | `src/NT8.Core/Analytics/` |
|
||||
| Execution Utilities | ✅ Complete | `src/NT8.Core/Execution/` |
|
||||
| Market Data | ✅ Complete | `src/NT8.Core/MarketData/` |
|
||||
## Conversation Separation (Hard Rules)
|
||||
- [ ] Strategy conversations: entry/exit logic and behavior.
|
||||
- [ ] Architecture conversations: cross-component design and interfaces.
|
||||
- [ ] Coding conversations: implementation details and diffs.
|
||||
- [ ] Do not mix decisions across tracks; record architecture decisions in `docs/00-governance/decisions.md`.
|
||||
|
||||
**NT8 Order Execution is ALREADY WIRED.**
|
||||
`NT8StrategyBase.SubmitOrderToNT8()` calls `EnterLong`, `EnterShort`, `SetStopLoss`, and
|
||||
`SetProfitTarget` directly. The execution path works end-to-end. Do not re-implement it.
|
||||
## Documentation Sync Triggers
|
||||
Update governance docs as part of normal task completion:
|
||||
- [ ] Update `docs/00-governance/active_work.md` when current priorities, status, blockers, or ownership change.
|
||||
- [ ] Update `docs/00-governance/roadmap.md` when milestone sequence, scope, or timing changes.
|
||||
- [ ] Update `docs/00-governance/decisions.md` when a durable tradeoff or architecture decision is made.
|
||||
- [ ] Update `CLEANUP_LOG.md` when cleanup debt is created, resolved, or deferred.
|
||||
|
||||
---
|
||||
|
||||
## What You Are Fixing (The Active Task List)
|
||||
|
||||
### CRITICAL — `NT8StrategyBase.cs`
|
||||
|
||||
**Gap 1 — No kill switch**
|
||||
`NT8StrategyBase` has no `EnableKillSwitch` NinjaScript parameter and no early-exit in `OnBarUpdate()`.
|
||||
A runaway strategy cannot be stopped without killing NinjaTrader.
|
||||
**Fix:** Add `EnableKillSwitch` (bool NinjaScript property) and `EnableVerboseLogging` property.
|
||||
Add kill switch check as the FIRST thing in `OnBarUpdate()`.
|
||||
→ See `TASK-01-kill-switch.md`
|
||||
|
||||
**Gap 2 — `ExecutionCircuitBreaker` not wired**
|
||||
`src/NT8.Core/Execution/ExecutionCircuitBreaker.cs` is complete and tested.
|
||||
It is never instantiated. Orders submit regardless of latency or rejection conditions.
|
||||
**Fix:** Instantiate in `InitializeSdkComponents()`, gate orders in `SubmitOrderToNT8()`, wire rejections in `OnOrderUpdate()`.
|
||||
→ See `TASK-02-circuit-breaker.md`
|
||||
|
||||
### HIGH — `TrailingStopManager.cs`
|
||||
|
||||
**Gap 3 — Placeholder stop math returns zero**
|
||||
`CalculateNewStopPrice()` FixedTrailing branch: `marketPrice - (x - x)` = always zero movement.
|
||||
ATRTrailing and Chandelier also have meaningless placeholder formulas.
|
||||
**Fix:** Replace with real calculations using `TrailingStopConfig.TrailingAmountTicks` and `AtrMultiplier`.
|
||||
→ See `TASK-03-trailing-stop.md`
|
||||
|
||||
### HIGH — `BasicLogger.cs`
|
||||
|
||||
**Gap 4 — No log-level filter**
|
||||
Every log statement writes to console unconditionally. Cannot suppress debug noise in production.
|
||||
**Fix:** Add `MinimumLevel` property (defaults to `Information`). Suppress messages below threshold.
|
||||
→ See `TASK-04-log-level.md`
|
||||
|
||||
### MEDIUM — `SessionManager.cs`
|
||||
|
||||
**Gap 5 — No holiday awareness**
|
||||
`IsRegularTradingHours()` checks session times only. Will attempt to trade on Christmas, Thanksgiving, etc.
|
||||
**Fix:** Add static CME holiday set for 2025/2026. Return `false` on those dates.
|
||||
→ See `TASK-05-session-holidays.md`
|
||||
|
||||
---
|
||||
|
||||
## Architecture (Read Before Touching Anything)
|
||||
|
||||
```
|
||||
SimpleORBStrategy.OnBar()
|
||||
↓ returns StrategyIntent
|
||||
NT8StrategyBase.OnBarUpdate()
|
||||
↓ [TASK-01: kill switch check here, first]
|
||||
↓ calls ProcessStrategyIntent()
|
||||
↓ calls _riskManager.ValidateOrder()
|
||||
↓ calls _positionSizer.CalculateSize()
|
||||
↓ calls SubmitOrderToNT8()
|
||||
↓ [TASK-02: circuit breaker gate here]
|
||||
↓ calls EnterLong/EnterShort/SetStopLoss/SetProfitTarget (already works)
|
||||
NT8 callbacks → OnOrderUpdate / OnExecutionUpdate
|
||||
↓ [TASK-02: record rejections in circuit breaker here]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Technology Constraints
|
||||
|
||||
- **C# 5.0 only** — no `$""`, no `?.`, no `=>` on methods/properties, no `nameof()`, no `out var`
|
||||
- **.NET Framework 4.8** — not .NET Core/5+/6+
|
||||
- **NinjaScript managed orders** — `EnterLong`, `EnterShort`, `SetStopLoss`, `SetProfitTarget`
|
||||
- `string.Format()` everywhere, never string interpolation
|
||||
- All `Dictionary`, `HashSet` access inside `lock (_lock)` blocks
|
||||
- XML doc comments on all public members
|
||||
- `try/catch` on all public methods with `LogError` in the catch
|
||||
## Compatibility and Safety Baselines
|
||||
- [ ] C# 5.0 only (`.kilocode/rules/csharp_50_syntax.md`).
|
||||
- [ ] NT8 compile safety (`.kilocode/rules/ninjascript_guardrails.md`, `.kilocode/rules/nt8compilespec.md`).
|
||||
- [ ] Verification gates (`.kilocode/rules/verification_requirements.md`).
|
||||
|
||||
@@ -1,4 +1,75 @@
|
||||
# Designed vs. Implemented Features - Gap Analysis
|
||||
> ⚠️ HISTORICAL — see docs/00-governance/ for current state
|
||||
|
||||
This file may contain outdated or mixed historical information.
|
||||
Canonical current-state documentation lives in docs/00-governance/.
|
||||
This file is retained for history/reference only.
|
||||
|
||||
# NT8-SDK — Gap Analysis & Roadmap
|
||||
**Version:** 3.0 | **Date:** 2026-03-27 | Supersedes all previous gap analysis documents.
|
||||
|
||||
---
|
||||
|
||||
## Open Gaps
|
||||
|
||||
| ID | Description | Priority | Sprint |
|
||||
|---|---|---|---|
|
||||
| GAP-001 | Runner leg backtest validation (Qty=2 check). EntriesPerDirection=2 restored but not backtested. | CRITICAL | S2-05 |
|
||||
| GAP-002 | orbRangeTicks not wired in DailyBarContext (hardcoded 0.0 in SimpleORBNT8.OnBarUpdate) | LOW | Sprint 3 |
|
||||
| GAP-003 | Risk parameter consistency: RiskPerTrade can exceed MaxTradeRisk silently. No assertion. | HIGH | S2-06 |
|
||||
| GAP-004 | GetRiskStatus() returns hardcoded limit rather than registered strategy config value | LOW | Sprint 3 |
|
||||
| GAP-005 | No Gitea CI pipeline. Build and test are manual. | MEDIUM | Sprint 3 |
|
||||
| GAP-006 | No n8n webhook alerts for fills, risk events, connection loss | MEDIUM | Sprint 3 |
|
||||
| GAP-007 | No walk-forward / out-of-sample validation. All backtests are in-sample. | HIGH | S2-08 |
|
||||
| GAP-008 | Short-side profitable only in crash regimes. No regime filter. | MEDIUM | Sprint 3 |
|
||||
| GAP-009 | No tick replay backtest. OnBarClose simulation compresses trade duration. | LOW | Sprint 3 |
|
||||
|
||||
---
|
||||
|
||||
## Sprint Roadmap
|
||||
|
||||
### Sprint 2 (ACTIVE) — SIM Validation
|
||||
Goal: 2+ weeks unattended SIM with dual-leg execution confirmed.
|
||||
Key pending: GAP-001 (runner validation), GAP-003 (risk consistency), GAP-007 (walk-forward).
|
||||
|
||||
### Sprint 3 — Production Hardening
|
||||
Goal: 30-day SIM clean. CI and alerts wired.
|
||||
Key work: GAP-004 through GAP-009, VWAPMeanReversion skeleton.
|
||||
|
||||
### Sprint 4 — Live Capital
|
||||
Gate: 30-day SIM PF > 2.0, max DD < $500.
|
||||
Key work: Go live 1 NQ contract, OvernightGap strategies, ops runbook.
|
||||
|
||||
### Sprint 5 — ML Inference
|
||||
Prerequisite: 60 days live data.
|
||||
Key work: FastAPI /predict, MLSignalFactorCalculator as 11th factor.
|
||||
|
||||
---
|
||||
|
||||
## Strategy Backlog
|
||||
|
||||
| ID | Strategy | Priority | Notes |
|
||||
|---|---|---|---|
|
||||
| STRAT_079 | Liquidity Sweep Reversal | Medium | Potential short-trade improvement |
|
||||
| STRAT_154 | Overnight Gap Continuation | High | Leverages existing SessionManager |
|
||||
| STRAT_214 | Overnight Gap Reversion | High | Counter to STRAT_154 |
|
||||
| LondonORB | London ORB (3:00 AM ET) | Medium | Separate LondonORBNT8 strategy file |
|
||||
| VWAP-MR | VWAP Mean Reversion | High | Sprint 3 build target |
|
||||
|
||||
---
|
||||
|
||||
## Backtest Performance Reference
|
||||
|
||||
| Date | Period | Trades | Win% | PF | Net | Config |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 2026-03-27 | Jan–Mar 2026 | 20 | 75% | **7.00** | $1,200 | trail=20 ✅ Production config |
|
||||
| 2026-03-27 | Jan–Mar 2026 | 40 | 75% | 3.69 | $1,075 | trail=12 |
|
||||
| 2026-03-27 | Mar 2025–Mar 2026 | 148 | 51% | 3.15 | $71,303 | 9 cts experimental |
|
||||
|
||||
Note: 148-trade run used RiskPerTrade=$500 + EntriesPerDirection=1 (runner blocked). Not a production reference.
|
||||
|
||||
---
|
||||
|
||||
# ARCHIVED BELOW — Original Gap Analysis (2026-02-17, superseded)
|
||||
|
||||
**Date:** February 17, 2026
|
||||
**Status:** Post Phase A-B-C NT8 Integration
|
||||
|
||||
@@ -1,9 +1,140 @@
|
||||
# NT8 SDK Project - Comprehensive Recap & Handover
|
||||
> ⚠️ HISTORICAL — see docs/00-governance/ for current state
|
||||
|
||||
**Document Version:** 2.0
|
||||
**Date:** February 16, 2026
|
||||
**Current Phase:** Phase 5 Complete
|
||||
**Project Completion:** ~85%
|
||||
This file may contain outdated or mixed historical information.
|
||||
Canonical current-state documentation lives in docs/00-governance/.
|
||||
This file is retained for history/reference only.
|
||||
|
||||
# NT8-SDK — Project Context & Current State
|
||||
**Version:** 3.0 | **Date:** 2026-03-27 | **Status:** Sprint 2 Active — SIM Validation
|
||||
|
||||
> This file supersedes the previous PROJECT_HANDOVER.md and is the live source of truth.
|
||||
> See also: `SPRINT_BOARD.md`, `GAP_ANALYSIS_AND_ROADMAP.md`, `CODING_PATTERNS.md`, `KILOCODE_WORKFLOW.md`
|
||||
> Full formatted handover: `NT8_SDK_Handover_Package.docx`
|
||||
|
||||
---
|
||||
|
||||
## 1. What This Is
|
||||
|
||||
NT8-SDK is an institutional-grade algorithmic futures trading system built on NinjaTrader 8. It is not a research prototype — it is production trading software where bugs equal real financial losses.
|
||||
|
||||
The system trades NQ (Nasdaq 100 E-mini futures) using a 30-minute Opening Range Breakout strategy (SimpleORB) with a 10-factor confluence scoring engine that grades each signal A+ through F before allowing execution. A scaler/runner dual-leg architecture captures quick targets on the scaler while the runner trails for extended moves.
|
||||
|
||||
**Division of labor:** Claude handles architecture, diagnosis, and Kilocode prompt design. Kilocode executes ALL code changes. Mo owns strategy direction and go/no-go decisions.
|
||||
|
||||
---
|
||||
|
||||
## 2. Technology Stack
|
||||
|
||||
| Layer | Technology | Constraint |
|
||||
|---|---|---|
|
||||
| Language | C# 5.0 | Hard — NinjaScript compiler limit |
|
||||
| Framework | .NET Framework 4.8 | Hard — NT8 requirement |
|
||||
| Platform | NinjaTrader 8 | Hard — execution environment |
|
||||
| Local repo | `C:\dev\nt8-sdk` | Windows path |
|
||||
| NT8 deploy | `C:\Users\billy\Documents\NinjaTrader 8\bin\Custom\Strategies\` | Must match source |
|
||||
| Deploy script | `deployment\deploy-to-nt8.bat` | Creates timestamped backups |
|
||||
| VCS | Gitea (self-hosted) | `https://git.thehussains.org/mo/nt8-sdk` |
|
||||
| AI coding | Kilocode | Executes ALL code changes |
|
||||
| Automation | n8n (self-hosted) | Deferred to Sprint 4 |
|
||||
| ML inference | Ollama (local) | Deferred to Sprint 5 |
|
||||
|
||||
**Critical C# constraint:** No `$""`, no `?.`, no `=>`, no async/await. Use `string.Format()`, explicit null checks, full method bodies.
|
||||
|
||||
---
|
||||
|
||||
## 3. Architecture (Top to Bottom)
|
||||
|
||||
```
|
||||
SimpleORBNT8.cs NT8 entry point — sets defaults, adds daily bar series, builds DailyBarContext
|
||||
↓
|
||||
NT8StrategyBase.cs Abstract base — bar routing, session management, kill switch, breakeven, runner
|
||||
↓
|
||||
SimpleORBStrategy.cs Core signal — ORB detection, 10-factor confluence, _tradeTaken session lock
|
||||
↓
|
||||
NT8OrderAdapter.cs INT8ExecutionBridge — calls EnterLong/EnterShort/SetStopLoss
|
||||
↓
|
||||
PortfolioRiskManager.cs Singleton — cross-strategy daily loss + contract cap
|
||||
↓
|
||||
NinjaTrader 8 Execution, fills, order management
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Current Production Parameters (SIM: SimSimple ORB)
|
||||
|
||||
| Parameter | Value | Notes |
|
||||
|---|---|---|
|
||||
| Instrument | NQ JUN26 | Primary instrument |
|
||||
| Bar type | 13-Range bars | |
|
||||
| BarsRequiredToTrade | 50 | Warm-up guard |
|
||||
| DailyLossLimit | $1,000 | |
|
||||
| MaxTradeRisk | $200 | |
|
||||
| RiskPerTrade | $100 | 2 contracts at current NQ prices |
|
||||
| MinContracts | 1 | |
|
||||
| MaxContracts | 3 | |
|
||||
| MaxOpenPositions | 2 | Scaler + runner |
|
||||
| EntriesPerDirection | 2 | Scaler slot 1, runner slot 2 |
|
||||
| MinTradeGrade | 5 (A) | 4 (B) for broad SIM testing |
|
||||
| EnableShortTrades | False | Long-only until short backtest done |
|
||||
| BreakevenTriggerTicks | 20 | Tuned from 12 |
|
||||
| RunnerTrailTicks | 20 | Tuned from 12 |
|
||||
| OpeningRangeMinutes | 30 | 9:30–10:00 ET |
|
||||
| StopTicks | 8 | $40 per contract NQ |
|
||||
| TargetTicks | 16 (dynamic) | Scales with ORB/ATR ratio |
|
||||
|
||||
---
|
||||
|
||||
## 5. Two-Path Deployment Rule
|
||||
|
||||
Every code change MUST be applied to both:
|
||||
1. `C:\dev\nt8-sdk\src\NT8.Adapters\Strategies\` (repo source)
|
||||
2. `C:\Users\billy\Documents\NinjaTrader 8\bin\Custom\Strategies\` (NT8 runtime)
|
||||
|
||||
After deployment, NT8 must recompile: Tools → Edit NinjaScript → open `NT8StrategyBase.cs` → save.
|
||||
|
||||
---
|
||||
|
||||
## 6. Key Learnings (Hard-Won)
|
||||
|
||||
1. **`State.Historical` guard** — `if (State == State.Realtime && !_realtimeBarSeen) return;` in `ProcessStrategyIntent` allows backtest (Historical), blocks replay burst in live.
|
||||
|
||||
2. **`_realtimeBarSeen` flag** — reset to `false` in `State.Realtime`, set `true` on first bar. Skips catch-up bar on live load to prevent replay burst.
|
||||
|
||||
3. **`EntriesPerDirection = 2`** — required for scaler + runner. Setting to 1 silently blocks the runner with no error.
|
||||
|
||||
4. **`SetStopLoss`/`SetProfitTarget` before `EnterLong`/`EnterShort`** — calling after entry is silently ignored in backtest.
|
||||
|
||||
5. **`Calculate.OnBarClose` backtest** — trades appear to close in under 1 second in logs. NT8 simulation artifact, not a bug. Live trades hold 2–20 minutes.
|
||||
|
||||
6. **NR7 warm-up** — requires 7 daily bars. Warm-up messages (0/7 bars) are correct behavior.
|
||||
|
||||
7. **NT8 never auto-recompiles** — always force recompile after file changes via NinjaScript Editor.
|
||||
|
||||
8. **Dual-path deployment mandatory** — stale deployed files cause phantom bugs where code looks right but behaves wrong.
|
||||
|
||||
9. **`PortfolioRiskManager` is a singleton** — fully implemented, no changes required. Kill switch, daily loss, contract cap all working.
|
||||
|
||||
10. **Sizing formula** — `floor(RiskPerTrade / (StopTicks × $5.00))`. NQ: `$100 / (8 × $5) = 2 contracts`. Always verify `RiskPerTrade <= MaxTradeRisk`.
|
||||
|
||||
---
|
||||
|
||||
## 7. Validated Backtest Results
|
||||
|
||||
| Date | Period | Trades | Win% | PF | Net | Config |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 2026-03-27 | Jan–Mar 2026 | 20 | 75% | **7.00** | $1,200 | 1 ct, trail=20 ✅ Best |
|
||||
| 2026-03-27 | Jan–Mar 2026 | 40 | 75% | 3.69 | $1,075 | 1 ct, trail=12 |
|
||||
| 2026-03-27 | Mar 2025–Mar 2026 | 148 | 51% | 3.15 | $71,303 | 9 cts (experimental) |
|
||||
|
||||
The 9-contract run used `RiskPerTrade=$500` — not a production configuration. Runner leg was also blocked (`EntriesPerDirection=1`) for that run. Re-run required after Sprint 2 fixes.
|
||||
|
||||
---
|
||||
|
||||
## 8. Immediate Next Actions Before Market Open
|
||||
|
||||
1. Run Strategy Analyzer (NQ JUN26, Jan 1 2026 → Mar 27 2026) and confirm `PNL_UPDATE Position=Short Qty=2` in session log — validates runner leg
|
||||
2. Verify SIM account settings: `RiskPerTrade=$100`, `BreakevenTriggerTicks=20`, `RunnerTrailTicks=20`
|
||||
3. Only re-enable BX68915-15 after runner validation passes — long-only, `MaxContracts=2`, `DailyLossLimit=$500`
|
||||
|
||||
---
|
||||
|
||||
|
||||
15
README.md
15
README.md
@@ -1,3 +1,18 @@
|
||||
> 📋 NOTE — This README is under revision. See docs/00-governance/ for current architecture and status.
|
||||
|
||||
# NT8-SDK — Institutional Algorithmic Futures Trading System
|
||||
|
||||
**See `NT8_SDK_Handover_Package.docx` for the complete milestone handover document.**
|
||||
|
||||
Quick reference docs in repo root:
|
||||
- `PROJECT_CONTEXT.md` — current state, parameters, key learnings
|
||||
- `SPRINT_BOARD.md` — active sprint tasks
|
||||
- `GAP_ANALYSIS_AND_ROADMAP.md` — open gaps and sprint plan
|
||||
- `CODING_PATTERNS.md` — C# 5.0 rules, NT8 quirks
|
||||
- `KILOCODE_WORKFLOW.md` — Kilocode prompt template and guardrails
|
||||
|
||||
---
|
||||
|
||||
# NT8 Institutional Trading SDK
|
||||
|
||||
**Version:** 0.2.0
|
||||
|
||||
130
cleanup-repo.ps1
Normal file
130
cleanup-repo.ps1
Normal file
@@ -0,0 +1,130 @@
|
||||
# cleanup-repo.ps1
|
||||
# Removes stale, superseded, and AI-process artifacts from the repo root
|
||||
# Run from: C:\dev\nt8-sdk
|
||||
|
||||
Set-Location "C:\dev\nt8-sdk"
|
||||
|
||||
$filesToDelete = @(
|
||||
# Archon planning docs (tool was never used)
|
||||
"archon_task_mapping.md",
|
||||
"archon_update_plan.md",
|
||||
|
||||
# AI team/agent process docs (internal scaffolding, no ongoing value)
|
||||
"ai_agent_tasks.md",
|
||||
"ai_success_metrics.md",
|
||||
"AI_DEVELOPMENT_GUIDELINES.md",
|
||||
"AI_TEAM_SETUP_DOCUMENTATION.md",
|
||||
"ai_workflow_templates.md",
|
||||
|
||||
# Phase A/B/C planning docs (all phases complete, superseded by PROJECT_HANDOVER)
|
||||
"PHASE_A_READY_FOR_KILOCODE.md",
|
||||
"PHASE_A_SPECIFICATION.md",
|
||||
"PHASE_B_SPECIFICATION.md",
|
||||
"PHASE_C_SPECIFICATION.md",
|
||||
"PHASES_ABC_COMPLETION_REPORT.md",
|
||||
|
||||
# Old TASK- files superseded by TASK_ files (better versions exist)
|
||||
"TASK-01-kill-switch.md",
|
||||
"TASK-02-circuit-breaker.md",
|
||||
"TASK-03-trailing-stop.md",
|
||||
"TASK-04-log-level.md",
|
||||
"TASK-05-session-holidays.md",
|
||||
|
||||
# Fix specs already applied to codebase
|
||||
"COMPILE_FIX_SPECIFICATION.md",
|
||||
"DROPDOWN_FIX_SPECIFICATION.md",
|
||||
"STRATEGY_DROPDOWN_COMPLETE_FIX.md",
|
||||
|
||||
# One-time historical docs
|
||||
"NET_FRAMEWORK_CONVERSION.md",
|
||||
"FIX_GIT_AUTH.md",
|
||||
"GIT_COMMIT_INSTRUCTIONS.md",
|
||||
|
||||
# Superseded implementation docs
|
||||
"implementation_guide.md",
|
||||
"implementation_guide_summary.md",
|
||||
"implementation_attention_points.md",
|
||||
"OMS_IMPLEMENTATION_START.md",
|
||||
"nt8_sdk_phase0_completion.md",
|
||||
"NT8_INTEGRATION_COMPLETE_SPECS.md",
|
||||
"nt8_integration_guidelines.md",
|
||||
"POST_INTEGRATION_ROADMAP.md",
|
||||
|
||||
# Superseded project planning (PROJECT_HANDOVER.md is canonical now)
|
||||
"project_plan.md",
|
||||
"project_summary.md",
|
||||
"architecture_summary.md",
|
||||
"development_workflow.md",
|
||||
|
||||
# Kilocode setup (already done, no ongoing value)
|
||||
"KILOCODE_SETUP_COMPLETE.md",
|
||||
"setup-kilocode-files.ps1",
|
||||
|
||||
# Utility scripts (one-time use)
|
||||
"commit-now.ps1",
|
||||
"cleanup-repo.ps1" # self-delete at end
|
||||
)
|
||||
|
||||
$dirsToDelete = @(
|
||||
"plans", # single stale analysis report
|
||||
"Specs" # original spec packages, all implemented
|
||||
)
|
||||
|
||||
Write-Host "`n=== NT8-SDK Repository Cleanup ===" -ForegroundColor Cyan
|
||||
Write-Host "Removing stale and superseded files...`n"
|
||||
|
||||
$deleted = 0
|
||||
$notFound = 0
|
||||
|
||||
foreach ($file in $filesToDelete) {
|
||||
$path = Join-Path (Get-Location) $file
|
||||
if (Test-Path $path) {
|
||||
Remove-Item $path -Force
|
||||
Write-Host " DELETED: $file" -ForegroundColor Green
|
||||
$deleted++
|
||||
} else {
|
||||
Write-Host " SKIP (not found): $file" -ForegroundColor DarkGray
|
||||
$notFound++
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($dir in $dirsToDelete) {
|
||||
$path = Join-Path (Get-Location) $dir
|
||||
if (Test-Path $path) {
|
||||
Remove-Item $path -Recurse -Force
|
||||
Write-Host " DELETED DIR: $dir\" -ForegroundColor Green
|
||||
$deleted++
|
||||
} else {
|
||||
Write-Host " SKIP DIR (not found): $dir\" -ForegroundColor DarkGray
|
||||
$notFound++
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "`n=== Staging changes ===" -ForegroundColor Cyan
|
||||
git add -A
|
||||
|
||||
Write-Host "`n=== Committing ===" -ForegroundColor Cyan
|
||||
git commit -m "chore: repo housekeeping - remove stale and superseded files
|
||||
|
||||
Removed categories:
|
||||
- Archon planning docs (tool never used)
|
||||
- AI team/agent scaffolding docs
|
||||
- Phase A/B/C specs (complete, superseded by PROJECT_HANDOVER)
|
||||
- Old TASK-0x files (superseded by TASK_0x versions)
|
||||
- Applied fix specs (COMPILE, DROPDOWN, STRATEGY_DROPDOWN)
|
||||
- One-time historical docs (NET_FRAMEWORK_CONVERSION, FIX_GIT_AUTH)
|
||||
- Superseded implementation guides and planning docs
|
||||
- plans/ and Specs/ directories (all implemented)
|
||||
|
||||
Kept:
|
||||
- All active TASK_0x work items (TASK_01/02/03 execution wiring)
|
||||
- PROJECT_HANDOVER, NEXT_STEPS_RECOMMENDED, GAP_ANALYSIS
|
||||
- Phase3/4/5 Implementation Guides
|
||||
- KILOCODE_RUNBOOK, OPTIMIZATION_GUIDE
|
||||
- All spec files for pending work (RTH, CONFIG_EXPORT, DIAGNOSTIC_LOGGING)
|
||||
- src/, tests/, docs/, deployment/, rules/, .kilocode/ unchanged"
|
||||
|
||||
Write-Host "`nDeleted: $deleted items" -ForegroundColor Green
|
||||
Write-Host "Skipped: $notFound items (already gone)" -ForegroundColor DarkGray
|
||||
Write-Host "`n=== Done! Current root files: ===" -ForegroundColor Cyan
|
||||
Get-ChildItem -File | Where-Object { $_.Name -notlike ".*" } | Select-Object Name | Format-Table -HideTableHeaders
|
||||
@@ -173,7 +173,7 @@ else {
|
||||
}
|
||||
|
||||
if (-not $SkipVerification) {
|
||||
Write-Step "6/6" "Verifying deployment"
|
||||
Write-Step "6/7" "Verifying deployment"
|
||||
$ok = $true
|
||||
|
||||
if (-not (Test-Path "$nt8Custom\NT8.Core.dll")) {
|
||||
@@ -195,13 +195,43 @@ if (-not $SkipVerification) {
|
||||
Write-Success "Deployment verification passed"
|
||||
}
|
||||
else {
|
||||
Write-Step "6/6" "Skipping verification"
|
||||
Write-Step "6/7" "Skipping verification"
|
||||
}
|
||||
|
||||
# Step 7: Delete the compiled NinjaScript assembly so NT8 is forced to do a
|
||||
# full recompile from source on next startup. Without this, NT8 may load a
|
||||
# stale cached assembly and silently ignore updated .cs files.
|
||||
Write-Step "7/7" "Invalidating NinjaScript compiled assembly"
|
||||
$compiledDll = Join-Path $nt8Custom "NinjaTrader.Custom.dll"
|
||||
$compiledPdb = Join-Path $nt8Custom "NinjaTrader.Custom.pdb"
|
||||
$compiledXml = Join-Path $nt8Custom "NinjaTrader.Custom.xml"
|
||||
|
||||
$invalidated = 0
|
||||
foreach ($artifact in @($compiledDll, $compiledPdb)) {
|
||||
if (Test-Path $artifact) {
|
||||
Remove-Item $artifact -Force
|
||||
Write-Success ("Deleted {0}" -f (Split-Path $artifact -Leaf))
|
||||
$invalidated++
|
||||
}
|
||||
}
|
||||
|
||||
if ($invalidated -gt 0) {
|
||||
Write-Host " NT8 will perform a full recompile on next startup." -ForegroundColor Green
|
||||
Write-Host " The .xml file is documentation only and was left in place." -ForegroundColor Gray
|
||||
} else {
|
||||
Write-Warn "No compiled assembly found to delete (NT8 may not have been run yet)"
|
||||
}
|
||||
|
||||
$duration = (Get-Date) - $startTime
|
||||
Write-Header "Deployment Complete"
|
||||
Write-Host ("Duration: {0:F1} seconds" -f $duration.TotalSeconds)
|
||||
Write-Host "Next: Open NinjaTrader 8 -> NinjaScript Editor -> Compile All"
|
||||
Write-Host ""
|
||||
Write-Host "NEXT STEPS:" -ForegroundColor Cyan
|
||||
Write-Host " 1. Start NinjaTrader 8 (full recompile will happen automatically)" -ForegroundColor White
|
||||
Write-Host " 2. Wait for compilation to complete in the Output window" -ForegroundColor White
|
||||
Write-Host " 3. Remove and re-add the strategy to the chart" -ForegroundColor White
|
||||
Write-Host " 4. Verify defaults: BreakevenTriggerTicks=20 RunnerTrailTicks=20 MaxContracts=3" -ForegroundColor White
|
||||
Write-Host " 5. Confirm NT8 Output shows: StartBehavior=AdoptAccountPosition EntriesPerDirection=2" -ForegroundColor White
|
||||
|
||||
exit 0
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ set "NT8_CUSTOM=%USERPROFILE%\Documents\NinjaTrader 8\bin\Custom"
|
||||
set "NT8_STRATEGIES=%NT8_CUSTOM%\Strategies"
|
||||
set "CORE_BIN=%PROJECT_ROOT%\src\NT8.Core\bin\Release\net48"
|
||||
set "ADAPTERS_BIN=%PROJECT_ROOT%\src\NT8.Adapters\bin\Release\net48"
|
||||
set "STRATEGIES_BIN=%PROJECT_ROOT%\src\NT8.Strategies\bin\Release\net48"
|
||||
set "WRAPPERS_SRC=%PROJECT_ROOT%\src\NT8.Adapters\Wrappers"
|
||||
set "BACKUP_ROOT=%SCRIPT_DIR%backups"
|
||||
|
||||
@@ -40,6 +41,13 @@ if not exist "%ADAPTERS_BIN%\NT8.Adapters.dll" (
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
if not exist "%STRATEGIES_BIN%\NT8.Strategies.dll" (
|
||||
echo ERROR: Strategies DLL not found: %STRATEGIES_BIN%\NT8.Strategies.dll
|
||||
echo Build release artifacts first:
|
||||
echo dotnet build NT8-SDK.sln --configuration Release
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
if not exist "%NT8_STRATEGIES%" (
|
||||
mkdir "%NT8_STRATEGIES%"
|
||||
)
|
||||
@@ -52,6 +60,7 @@ mkdir "%BACKUP_ROOT%\%STAMP%" >nul 2>&1
|
||||
echo Backing up existing NT8 SDK files...
|
||||
if exist "%NT8_CUSTOM%\NT8.Core.dll" copy /Y "%NT8_CUSTOM%\NT8.Core.dll" "%BACKUP_DIR%\NT8.Core.dll" >nul
|
||||
if exist "%NT8_CUSTOM%\NT8.Adapters.dll" copy /Y "%NT8_CUSTOM%\NT8.Adapters.dll" "%BACKUP_DIR%\NT8.Adapters.dll" >nul
|
||||
if exist "%NT8_CUSTOM%\NT8.Strategies.dll" copy /Y "%NT8_CUSTOM%\NT8.Strategies.dll" "%BACKUP_DIR%\NT8.Strategies.dll" >nul
|
||||
if exist "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" copy /Y "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" "%BACKUP_DIR%\BaseNT8StrategyWrapper.cs" >nul
|
||||
if exist "%NT8_STRATEGIES%\SimpleORBNT8Wrapper.cs" copy /Y "%NT8_STRATEGIES%\SimpleORBNT8Wrapper.cs" "%BACKUP_DIR%\SimpleORBNT8Wrapper.cs" >nul
|
||||
|
||||
@@ -59,6 +68,7 @@ echo Deployment manifest > "%MANIFEST_FILE%"
|
||||
echo Timestamp: %STAMP%>> "%MANIFEST_FILE%"
|
||||
echo Source Core DLL: %CORE_BIN%\NT8.Core.dll>> "%MANIFEST_FILE%"
|
||||
echo Source Adapters DLL: %ADAPTERS_BIN%\NT8.Adapters.dll>> "%MANIFEST_FILE%"
|
||||
echo Source Strategies DLL: %STRATEGIES_BIN%\NT8.Strategies.dll>> "%MANIFEST_FILE%"
|
||||
echo Destination Custom Folder: %NT8_CUSTOM%>> "%MANIFEST_FILE%"
|
||||
echo Destination Strategies Folder: %NT8_STRATEGIES%>> "%MANIFEST_FILE%"
|
||||
|
||||
@@ -75,6 +85,12 @@ if errorlevel 1 (
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
copy /Y "%STRATEGIES_BIN%\NT8.Strategies.dll" "%NT8_CUSTOM%\NT8.Strategies.dll" >nul
|
||||
if errorlevel 1 (
|
||||
echo ERROR: Failed to copy NT8.Strategies.dll
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo Deploying wrapper sources...
|
||||
copy /Y "%WRAPPERS_SRC%\BaseNT8StrategyWrapper.cs" "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" >nul
|
||||
if errorlevel 1 (
|
||||
@@ -112,6 +128,11 @@ if not exist "%NT8_CUSTOM%\NT8.Adapters.dll" (
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
if not exist "%NT8_CUSTOM%\NT8.Strategies.dll" (
|
||||
echo ERROR: Verification failed for NT8.Strategies.dll
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
if not exist "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" (
|
||||
echo ERROR: Verification failed for BaseNT8StrategyWrapper.cs
|
||||
exit /b 1
|
||||
@@ -126,11 +147,32 @@ echo.
|
||||
echo Deployment complete.
|
||||
echo Backup location: %BACKUP_DIR%
|
||||
echo Manifest file : %MANIFEST_FILE%
|
||||
|
||||
echo.
|
||||
echo Next steps:
|
||||
echo 1. Open NinjaTrader 8.
|
||||
echo 2. Open NinjaScript Editor and press F5 (Compile).
|
||||
echo 3. Verify strategies appear in the Strategies list.
|
||||
echo Invalidating NinjaScript compiled assembly...
|
||||
set "COMPILED_DLL=%NT8_CUSTOM%\NinjaTrader.Custom.dll"
|
||||
set "COMPILED_PDB=%NT8_CUSTOM%\NinjaTrader.Custom.pdb"
|
||||
|
||||
if exist "%COMPILED_DLL%" (
|
||||
del /F /Q "%COMPILED_DLL%"
|
||||
echo [OK] Deleted NinjaTrader.Custom.dll
|
||||
) else (
|
||||
echo [WARN] NinjaTrader.Custom.dll not found - NT8 may not have been run yet
|
||||
)
|
||||
|
||||
if exist "%COMPILED_PDB%" (
|
||||
del /F /Q "%COMPILED_PDB%"
|
||||
echo [OK] Deleted NinjaTrader.Custom.pdb
|
||||
)
|
||||
|
||||
echo.
|
||||
echo ============================================================
|
||||
echo NEXT STEPS:
|
||||
echo 1. Start NinjaTrader 8 (full recompile happens automatically)
|
||||
echo 2. Wait for compilation to finish in the Output window
|
||||
echo 3. Remove and re-add the strategy to the chart
|
||||
echo 4. Verify defaults: BreakevenTriggerTicks=20 RunnerTrailTicks=20 MaxContracts=3
|
||||
echo 5. NT8 Output must show: StartBehavior=AdoptAccountPosition EntriesPerDirection=2
|
||||
echo ============================================================
|
||||
|
||||
exit /b 0
|
||||
|
||||
|
||||
165
docs/00-governance/CLEANUP_LOG.md
Normal file
165
docs/00-governance/CLEANUP_LOG.md
Normal file
@@ -0,0 +1,165 @@
|
||||
# Governance Cleanup Log
|
||||
|
||||
## 2026-04-05
|
||||
- Established `docs/00-governance/` as canonical governance entry point.
|
||||
- Added concise governance baseline documents:
|
||||
- `executive_summary.md`
|
||||
- `architecture.md`
|
||||
- `current_status.md`
|
||||
- `roadmap.md`
|
||||
- `active_work.md`
|
||||
- Updated onboarding guidance in `.kilocode/rules/project_context.md` to point new sessions to governance docs first.
|
||||
- Reclassified `PROJECT_HANDOVER.md` and `DESIGNED_VS_IMPLEMENTED_GAP_ANALYSIS.md` as historical/contextual references, not primary truth.
|
||||
- No code files modified and no file moves performed.
|
||||
- Moved `docs/PHASE2_COMPLETION_REPORT.md` to `docs/archive/phase-history/PHASE2_COMPLETION_REPORT.md`.
|
||||
- Added historical header + archival note block to:
|
||||
- `docs/README.md`
|
||||
- `PROJECT_HANDOVER.md`
|
||||
- `DESIGNED_VS_IMPLEMENTED_GAP_ANALYSIS.md`
|
||||
- `docs/INDEX.md`
|
||||
- `docs/archive/phase-history/PHASE2_COMPLETION_REPORT.md`
|
||||
- Added a softer revision note to `README.md` instead of a historical warning.
|
||||
- Marked `docs/INDEX.md` historical in place because it currently misdirects navigation.
|
||||
- Added follow-up debt: shared metrics vocabulary currently duplicated in `docs/02-runbooks/backtest_review_workflow.md` and `docs/02-runbooks/live_test_review_workflow.md`; later extract to shared governance doc `docs/00-governance/metrics_vocabulary.md`.
|
||||
|
||||
|
||||
## Legacy Source Cleanup — Pass 1 (Non-Destructive)
|
||||
|
||||
**Date:** 2026-04-05
|
||||
**Branch:** cleanup/legacy-source
|
||||
**Type:** Non-destructive archive + tombstone cleanup
|
||||
|
||||
### Summary
|
||||
Performed a conservative source cleanup to reduce ambiguity for AI agents and developers.
|
||||
Focused on isolating legacy code paths and removing confirmed placeholder/tombstone artifacts without impacting the active runtime path.
|
||||
|
||||
---
|
||||
|
||||
### Actions Taken
|
||||
|
||||
#### 1. Legacy Orders System Archived
|
||||
The `NT8.Core.Orders` namespace was explicitly marked as archived and superseded by `NT8.Core.OMS`.
|
||||
|
||||
Moved to:
|
||||
- `src/_archive/legacy-orders/`
|
||||
- `IOrderManager.cs`
|
||||
- `OrderManager.cs`
|
||||
- `OrderModels.cs`
|
||||
|
||||
Paired test moved with source:
|
||||
- `tests/_archive/legacy-orders/OrderManagerTests.cs`
|
||||
|
||||
Rationale:
|
||||
- Prevents compile/runtime confusion between legacy Orders and active OMS
|
||||
- Prevents tests from referencing removed implementation paths
|
||||
- Ensures AI agents do not target deprecated order management logic
|
||||
|
||||
---
|
||||
|
||||
#### 2. Placeholder Source Files Archived
|
||||
Moved to:
|
||||
- `src/_archive/placeholders/`
|
||||
- `PlaceholderAdapter.cs`
|
||||
- `PlaceholderContract.cs`
|
||||
- `PlaceholderStrategy.cs`
|
||||
|
||||
Rationale:
|
||||
- These files contain no meaningful implementation
|
||||
- They create false signal for AI-assisted development
|
||||
|
||||
---
|
||||
|
||||
#### 3. Placeholder Test Files Archived
|
||||
Moved to:
|
||||
- `tests/_archive/placeholders/`
|
||||
- `Integration_PlaceholderTests.cs`
|
||||
- `Performance_PlaceholderTests.cs`
|
||||
|
||||
Deleted:
|
||||
- `tests/NT8.Integration.Tests/UnitTest1.cs`
|
||||
- `tests/NT8.Performance.Tests/UnitTest1.cs`
|
||||
|
||||
Rationale:
|
||||
- Placeholder tests (`Assert.IsTrue(true)`) provide no coverage
|
||||
- They distort test signal and can mislead analysis workflows
|
||||
|
||||
---
|
||||
|
||||
#### 4. Tombstone / Dead Files Removed
|
||||
Deleted (confirmed comment-only or empty artifacts):
|
||||
- `src/NT8.Adapters/Class1.cs.bak`
|
||||
- `src/NT8.Contracts/Class1.cs`
|
||||
- `src/NT8.Strategies/Class1.cs`
|
||||
|
||||
Rationale:
|
||||
- Files contained no executable logic
|
||||
- Safe removal reduces noise and ambiguity
|
||||
|
||||
---
|
||||
|
||||
### Deferred / Blocked Items
|
||||
|
||||
These files were intentionally **not modified** in this pass due to active references or ambiguity:
|
||||
|
||||
- `src/NT8.Adapters/Wrappers/BaseNT8StrategyWrapper.cs`
|
||||
- `src/NT8.Adapters/Wrappers/SimpleORBNT8Wrapper.cs`
|
||||
- `src/NT8.Core/Risk/RiskManager.cs`
|
||||
- `src/NT8.Adapters/NinjaTrader/NT8Adapter.cs`
|
||||
- `src/NT8.Core/Class1.cs.bak` (contains real code, not a tombstone)
|
||||
|
||||
Status:
|
||||
- Blocked pending second cleanup pass
|
||||
|
||||
Reason:
|
||||
- Wrapper layer has test dependencies that must be migrated or rewritten before archiving
|
||||
- Some files contain real logic and require classification (active vs legacy) before removal
|
||||
|
||||
---
|
||||
|
||||
### Verification
|
||||
|
||||
- `verify-build.bat`: **PASS**
|
||||
- All tests passing:
|
||||
- Core: 393/393
|
||||
- Integration: 78/78
|
||||
- Performance: 10/10
|
||||
|
||||
No compile errors or regressions introduced.
|
||||
|
||||
---
|
||||
|
||||
### Impact
|
||||
|
||||
- Eliminated legacy order management ambiguity
|
||||
- Reduced AI hallucination risk in source selection
|
||||
- Removed non-functional placeholder code and tests
|
||||
- Preserved full build and test integrity
|
||||
|
||||
---
|
||||
|
||||
### Follow-Up Actions
|
||||
|
||||
1. Second cleanup pass:
|
||||
- Wrapper layer isolation or migration
|
||||
- RiskManager classification
|
||||
- NT8Adapter classification
|
||||
- Resolve `Class1.cs.bak` ambiguity
|
||||
|
||||
2. Metrics vocabulary extraction:
|
||||
- Duplicate metrics definitions exist in:
|
||||
- `backtest_review_workflow.md`
|
||||
- `live_test_review_workflow.md`
|
||||
- Planned extraction target:
|
||||
- `docs/00-governance/metrics_vocabulary.md`
|
||||
|
||||
3. Optional:
|
||||
- Introduce `src/_archive/experimental/` if needed for future isolation
|
||||
|
||||
---
|
||||
|
||||
### Notes
|
||||
|
||||
- No protected runtime paths were modified
|
||||
- No namespaces or interfaces were changed
|
||||
- No `.csproj` files were modified
|
||||
- All changes were reversible via archive structure
|
||||
17
docs/00-governance/active_work.md
Normal file
17
docs/00-governance/active_work.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Active Work
|
||||
|
||||
## Current Execution Focus
|
||||
- Validate runner-leg dual-fill behavior in Strategy Analyzer/session logs.
|
||||
- Close remaining Sprint 2 hardening items already identified in task specs.
|
||||
- Preserve strict compile and deployment verification sequence for every change.
|
||||
|
||||
## In-Scope Hardening Themes
|
||||
- Strategy safety controls and execution circuit-breaker wiring.
|
||||
- Trailing stop calculation correctness.
|
||||
- Logging verbosity controls for production noise reduction.
|
||||
- Session/holiday awareness to avoid invalid trading windows.
|
||||
|
||||
## Working Rules
|
||||
- Apply changes only to explicitly scoped files per task.
|
||||
- Keep NT8 API signatures and managed-order sequencing exact.
|
||||
- Maintain C# 5.0 compatibility and existing interface boundaries.
|
||||
28
docs/00-governance/architecture.md
Normal file
28
docs/00-governance/architecture.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Architecture Governance
|
||||
|
||||
## Runtime Flow (Authoritative)
|
||||
```
|
||||
SimpleORBNT8.cs
|
||||
-> NT8StrategyBase.cs
|
||||
-> SimpleORBStrategy.cs
|
||||
-> NT8OrderAdapter.cs
|
||||
-> PortfolioRiskManager.cs
|
||||
-> NinjaTrader 8
|
||||
```
|
||||
|
||||
## Responsibilities
|
||||
- `SimpleORBNT8.cs`: NT8 entry point and platform lifecycle bridge.
|
||||
- `NT8StrategyBase.cs`: orchestration, risk gate sequencing, execution handoff, platform callbacks.
|
||||
- `SimpleORBStrategy.cs`: signal generation and confluence grading only.
|
||||
- `NT8OrderAdapter.cs`: execution bridge to NT8 managed order APIs.
|
||||
- `PortfolioRiskManager.cs`: cross-strategy risk controls and account-level enforcement.
|
||||
|
||||
## Architectural Constraints
|
||||
- Risk-first flow is mandatory; no strategy-level bypass of risk validation.
|
||||
- Managed-order sequence remains required (`SetStopLoss` / `SetProfitTarget` before entry).
|
||||
- C# 5.0 syntax only, .NET Framework 4.8 only.
|
||||
- NT8 signatures must be verified against official NinjaTrader docs before API-touching edits.
|
||||
|
||||
## Governance Notes
|
||||
- Core Risk/Sizing/OMS/Intelligence/Analytics layers are treated as complete and stable unless explicitly re-opened by approved work.
|
||||
- Hardening changes are concentrated in targeted adapter/utility components per active-work scope.
|
||||
33
docs/00-governance/common_failures.md
Normal file
33
docs/00-governance/common_failures.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Common Compile Failures
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
Record concise error fingerprints and verified fixes.
|
||||
|
||||
## Entry Template
|
||||
- **Date:** YYYY-MM-DD
|
||||
- **Error:** `CSXXXX`
|
||||
- **Fingerprint:** file + line + short symptom
|
||||
- **Root cause:** one sentence
|
||||
- **Fix:** one to three concrete steps
|
||||
- **Prevention link:** guardrail/pattern doc entry added
|
||||
|
||||
## Starter Examples
|
||||
1. **`CS0115` no suitable method found to override**
|
||||
- Root cause: incorrect NT8 override signature.
|
||||
- Fix: replace with exact official signature; recompile.
|
||||
|
||||
2. **`CS0507` cannot change access modifiers when overriding**
|
||||
- Root cause: used `public override`/`private override` instead of `protected override`.
|
||||
- Fix: change to `protected override`; recompile.
|
||||
|
||||
3. **`CS0246` type or namespace not found**
|
||||
- Root cause: missing using/namespace or undefined domain type.
|
||||
- Fix: add correct namespace reference or confirmed type definition.
|
||||
|
||||
4. **`CS1503` argument type mismatch**
|
||||
- Root cause: wrong overload or argument ordering.
|
||||
- Fix: align call with exact method signature and parameter types.
|
||||
|
||||
5. **C# 6+ syntax in C# 5 project**
|
||||
- Root cause: use of `$""`, `?.`, `nameof`, expression-bodied members, etc.
|
||||
- Fix: rewrite using C# 5 compatible constructs.
|
||||
20
docs/00-governance/compile_guardrails.md
Normal file
20
docs/00-governance/compile_guardrails.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Compile Guardrails
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
Preventive rules to reduce repeat compile failures.
|
||||
|
||||
## Core Prevention Rules
|
||||
- [ ] Verify NT8 signatures in official docs before adding/changing any `protected override`.
|
||||
- [ ] Keep NinjaScript edits within task scope and allowed file boundaries.
|
||||
- [ ] Enforce C# 5.0 syntax only; reject C# 6+ constructs.
|
||||
- [ ] Keep managed order sequence correct (stop/target before entry on same bar).
|
||||
- [ ] Guard `OnBarUpdate` by `BarsInProgress` and bar readiness.
|
||||
- [ ] Do not use unsupported attributes/types/enums without confirmation.
|
||||
|
||||
## Verification Gates
|
||||
- [ ] Run `.\verify-build.bat` after changes.
|
||||
- [ ] Run focused tests for changed area.
|
||||
- [ ] If NinjaScript touched, compile in NT8 NinjaScript Editor.
|
||||
|
||||
## Update Rule
|
||||
- [ ] Add a new guardrail entry whenever a compile issue reveals a missing prevention rule.
|
||||
20
docs/00-governance/current_status.md
Normal file
20
docs/00-governance/current_status.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Current Status
|
||||
|
||||
## Snapshot (2026-04-05)
|
||||
- Program phase: Sprint 2 SIM validation with production-hardening follow-through.
|
||||
- Core implementation: complete across major layers with 240+ passing tests.
|
||||
- Live focus: execution safety, validation depth, and operational reliability.
|
||||
|
||||
## Confirmed Working Areas
|
||||
- End-to-end strategy pipeline from signal -> risk -> sizing -> NT8 managed execution.
|
||||
- Session handling, portfolio risk controls, and base strategy orchestration.
|
||||
- Existing analytics/risk/sizing/OMS foundations remain stable.
|
||||
|
||||
## Open Findings Driving Work
|
||||
- Runner leg behavior requires explicit validation evidence (`Qty=2` path confirmation).
|
||||
- Risk/config consistency checks need tightening in runtime safeguards.
|
||||
- Operational controls (CI automation, alerting, and out-of-sample validation) remain pending.
|
||||
|
||||
## Operational Reality
|
||||
- `dotnet build` success is necessary but not sufficient; NT8 NinjaScript compile remains a separate required validation step.
|
||||
- Deployment integrity requires keeping repo and NT8 runtime strategy copies synchronized.
|
||||
15
docs/00-governance/decisions.md
Normal file
15
docs/00-governance/decisions.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Architecture Decisions
|
||||
|
||||
Use this file to record durable project decisions that affect implementation patterns, interfaces, or workflow.
|
||||
|
||||
## Decision Template
|
||||
- ID: DEC-YYYYMMDD-XX
|
||||
- Date:
|
||||
- Status: proposed | accepted | superseded
|
||||
- Context:
|
||||
- Decision:
|
||||
- Consequences:
|
||||
- Related Files:
|
||||
|
||||
## Decision Log
|
||||
- No decisions recorded yet.
|
||||
20
docs/00-governance/executive_summary.md
Normal file
20
docs/00-governance/executive_summary.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Executive Summary
|
||||
|
||||
## Scope
|
||||
- This folder (`docs/00-governance/`) is the canonical governance source for project state, priorities, architecture intent, and active execution guidance.
|
||||
- Governance is aligned to Sprint 2 (SIM validation) with production hardening gaps tracked and prioritized.
|
||||
|
||||
## Current Position (2026-04-05)
|
||||
- Core engine is implemented with 240+ passing tests across core components.
|
||||
- NT8 execution path is wired and validated in SIM for baseline operation.
|
||||
- Remaining work is focused on closing operational hardening gaps and validating runner behavior under production-like conditions.
|
||||
|
||||
## Approved Direction
|
||||
- Keep execution in managed-order NT8 patterns and C# 5.0 / .NET Framework 4.8 constraints.
|
||||
- Complete critical and high-priority hardening tasks before expanding strategy scope.
|
||||
- Treat historical handover artifacts as context only; governance decisions flow from this folder.
|
||||
|
||||
## Immediate Priorities
|
||||
- Confirm runner-leg dual-fill behavior in analyzer/session logs.
|
||||
- Close safety and observability items already identified in Sprint 2/3 gap tracking.
|
||||
- Maintain strict file-boundary and compile-guardrail discipline for all changes.
|
||||
36
docs/00-governance/patterns_and_antipatterns.md
Normal file
36
docs/00-governance/patterns_and_antipatterns.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Patterns and Anti-Patterns
|
||||
**Last Updated:** 2026-04-05
|
||||
|
||||
Capture recurring implementation patterns that impact compile stability.
|
||||
|
||||
## Pattern Entry Template
|
||||
- **Context:** where this applies
|
||||
- **Good pattern:** concise example/description
|
||||
- **Anti-pattern:** concise example/description
|
||||
- **Why it matters:** one sentence
|
||||
|
||||
## Starter Patterns
|
||||
1. **NT8 override signatures**
|
||||
- Good pattern: copy exact signature from official NT8 docs before coding.
|
||||
- Anti-pattern: infer or copy from outdated examples.
|
||||
- Why it matters: prevents `CS0115` and runtime integration drift.
|
||||
|
||||
2. **Access modifier on overrides**
|
||||
- Good pattern: use `protected override` for NT8 lifecycle/event methods.
|
||||
- Anti-pattern: `public override` or `private override`.
|
||||
- Why it matters: prevents `CS0507`.
|
||||
|
||||
3. **C# 5 compatibility discipline**
|
||||
- Good pattern: `string.Format`, explicit null checks, block-bodied members.
|
||||
- Anti-pattern: string interpolation, null-conditional, expression-bodied members.
|
||||
- Why it matters: prevents avoidable language-version compile failures.
|
||||
|
||||
4. **Managed order sequencing**
|
||||
- Good pattern: set stop/target before entry on the same bar.
|
||||
- Anti-pattern: entry first, then stop/target.
|
||||
- Why it matters: avoids silent behavior defects and rejected assumptions.
|
||||
|
||||
5. **Scope-first fixes**
|
||||
- Good pattern: smallest safe change in scoped files only.
|
||||
- Anti-pattern: broad cleanup or adjacent-file edits during bugfix.
|
||||
- Why it matters: reduces regression risk and review churn.
|
||||
21
docs/00-governance/roadmap.md
Normal file
21
docs/00-governance/roadmap.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Roadmap
|
||||
|
||||
## Sprint 2 (Active): SIM Validation
|
||||
- Validate dual-leg execution behavior with log evidence.
|
||||
- Complete remaining safety/consistency fixes tied to active gap list.
|
||||
- Exit criteria: stable SIM behavior with no unresolved critical gaps.
|
||||
|
||||
## Sprint 3: Production Hardening
|
||||
- Implement CI build/test automation and operational alerting.
|
||||
- Complete lower-priority runtime consistency and observability improvements.
|
||||
- Add walk-forward and broader validation coverage beyond in-sample checks.
|
||||
|
||||
## Sprint 4: Live Capital Readiness
|
||||
- Gate on sustained SIM metrics and drawdown controls.
|
||||
- Introduce go-live runbook and operational controls for controlled capital exposure.
|
||||
|
||||
## Sprint 5: ML Extension (Deferred)
|
||||
- Add inference integration only after sufficient live/sim data and stable production operations.
|
||||
|
||||
## Sequencing Rule
|
||||
- No feature expansion ahead of unresolved safety and validation gates.
|
||||
36
docs/02-runbooks/backtest_review_workflow.md
Normal file
36
docs/02-runbooks/backtest_review_workflow.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Backtest Review Workflow
|
||||
|
||||
## Purpose
|
||||
Provide a consistent pass/fail review process for strategy backtest output.
|
||||
|
||||
## Inputs
|
||||
- Backtest report (date range, instrument, timeframe, settings).
|
||||
- Strategy version/commit reference.
|
||||
- Risk and execution assumptions used for the run.
|
||||
|
||||
## Review Steps
|
||||
1. Confirm run metadata is complete and reproducible.
|
||||
2. Validate data window and session settings match test intent.
|
||||
3. Review shared metrics vocabulary:
|
||||
- Net Profit
|
||||
- Profit Factor
|
||||
- Max Drawdown
|
||||
- Win Rate
|
||||
- Average Trade
|
||||
- Expectancy
|
||||
- Trade Count
|
||||
4. Check risk behavior consistency (drawdown shape, loss clustering, streak behavior).
|
||||
5. Compare against prior baseline and flag material regressions.
|
||||
6. Record decision: `pass`, `pass_with_conditions`, or `fail`.
|
||||
|
||||
## Required Output
|
||||
- Backtest Review Note with:
|
||||
- metadata,
|
||||
- metrics snapshot,
|
||||
- baseline delta,
|
||||
- decision,
|
||||
- required follow-ups.
|
||||
|
||||
## Exit Criteria
|
||||
- Review note is stored with clear decision and rationale.
|
||||
- Any follow-up actions are captured as tasks.
|
||||
30
docs/02-runbooks/compile_error_triage_workflow.md
Normal file
30
docs/02-runbooks/compile_error_triage_workflow.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Compile Error Triage Workflow
|
||||
|
||||
## Purpose
|
||||
Resolve compile errors quickly using smallest-safe changes and durable prevention notes.
|
||||
|
||||
## Inputs
|
||||
- Failing command output (`./verify-build.bat` or `dotnet test`).
|
||||
- File scope constraints for current task.
|
||||
|
||||
## Steps
|
||||
1. Reproduce with the same command to confirm current error set.
|
||||
2. Classify each error:
|
||||
- missing type/namespace,
|
||||
- bad override/signature,
|
||||
- wrong overload/argument,
|
||||
- C# 5.0 compatibility violation.
|
||||
3. Verify any NinjaScript/API signatures against NT8 docs before editing.
|
||||
4. Apply minimum-diff fix in scoped files only.
|
||||
5. Re-run `./verify-build.bat`.
|
||||
6. Run focused tests for the impacted area.
|
||||
7. If issue is non-trivial, add concise prevention notes to governance docs.
|
||||
|
||||
## Required Output
|
||||
- Error-to-fix mapping (error code, root cause, fix).
|
||||
- Verification evidence (build/test commands and results).
|
||||
|
||||
## Exit Criteria
|
||||
- Build passes.
|
||||
- Relevant tests pass.
|
||||
- Non-trivial learnings are captured.
|
||||
36
docs/02-runbooks/live_test_review_workflow.md
Normal file
36
docs/02-runbooks/live_test_review_workflow.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Live Test Review Workflow
|
||||
|
||||
## Purpose
|
||||
Review paper/live-forward test behavior using the same decision discipline as backtest review.
|
||||
|
||||
## Inputs
|
||||
- Live test window and environment details (paper/live, broker feed, session template).
|
||||
- Strategy version/commit reference.
|
||||
- Execution logs and order/rejection events.
|
||||
|
||||
## Review Steps
|
||||
1. Confirm environment and deployment metadata.
|
||||
2. Validate runtime stability (disconnects, errors, unexpected restarts).
|
||||
3. Review shared metrics vocabulary:
|
||||
- Net Profit
|
||||
- Profit Factor
|
||||
- Max Drawdown
|
||||
- Win Rate
|
||||
- Average Trade
|
||||
- Expectancy
|
||||
- Trade Count
|
||||
4. Inspect execution quality (slippage, missed fills, rejection frequency, latency anomalies).
|
||||
5. Compare live metrics to backtest expectations and tolerance bands.
|
||||
6. Record decision: `promote`, `extend_test`, or `hold`.
|
||||
|
||||
## Required Output
|
||||
- Live Test Review Note with:
|
||||
- environment details,
|
||||
- key metrics,
|
||||
- execution anomalies,
|
||||
- decision,
|
||||
- next actions.
|
||||
|
||||
## Exit Criteria
|
||||
- Review note is complete and decision is explicit.
|
||||
- Any blockers or promotion prerequisites are tracked as tasks.
|
||||
25
docs/02-runbooks/repo_cleanup_workflow.md
Normal file
25
docs/02-runbooks/repo_cleanup_workflow.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Repo Cleanup Workflow
|
||||
|
||||
## Purpose
|
||||
Keep repository documentation and task metadata clean without changing functional behavior.
|
||||
|
||||
## Inputs
|
||||
- Cleanup trigger (stale docs, duplicate guidance, archival need, naming drift).
|
||||
- Current governance priorities from `docs/00-governance/active_work.md`.
|
||||
|
||||
## Steps
|
||||
1. Confirm cleanup scope and impacted files.
|
||||
2. Ensure no production code changes are included.
|
||||
3. Remove stale or duplicate sections that are no longer authoritative.
|
||||
4. Consolidate references so canonical docs remain in `docs/00-governance/`.
|
||||
5. Log cleanup action and any remaining debt in `docs/00-governance/CLEANUP_LOG.md`.
|
||||
6. Verify links/paths and runbook references after edits.
|
||||
|
||||
## Required Output
|
||||
- List of files cleaned and reason for each.
|
||||
- Cleanup debt notes for deferred follow-ups.
|
||||
|
||||
## Exit Criteria
|
||||
- Cleanup scope is complete.
|
||||
- `CLEANUP_LOG.md` is updated.
|
||||
- No code files were modified.
|
||||
26
docs/02-runbooks/session_start_workflow.md
Normal file
26
docs/02-runbooks/session_start_workflow.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Session Start Workflow
|
||||
|
||||
## Purpose
|
||||
Start each implementation session with correct scope, context, and task state before editing files.
|
||||
|
||||
## Inputs
|
||||
- Current priority task from Archon.
|
||||
- Governance docs in `docs/00-governance/`.
|
||||
|
||||
## Steps
|
||||
1. Open and read in order:
|
||||
- `docs/00-governance/executive_summary.md`
|
||||
- `docs/00-governance/current_status.md`
|
||||
- `docs/00-governance/active_work.md`
|
||||
- `docs/00-governance/architecture.md`
|
||||
- `docs/00-governance/roadmap.md`
|
||||
2. Pull current task details from Archon and confirm acceptance criteria.
|
||||
3. Confirm allowed file scope from task spec and `.kilocode/rules/file_boundaries.md`.
|
||||
4. Move task status in Archon from `todo` to `doing`.
|
||||
5. Capture a short execution plan (3-6 bullets) tied to acceptance criteria.
|
||||
6. Begin implementation with minimum-diff edits only in scoped files.
|
||||
|
||||
## Exit Criteria
|
||||
- Task is set to `doing`.
|
||||
- Scope boundaries are explicitly confirmed.
|
||||
- Work plan exists and maps to task acceptance criteria.
|
||||
49
docs/02-runbooks/spec_to_build_workflow.md
Normal file
49
docs/02-runbooks/spec_to_build_workflow.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# Spec-to-Build Workflow
|
||||
|
||||
## Purpose
|
||||
Convert an approved spec into implementation-ready work with explicit tool handoffs and verifiable artifacts.
|
||||
|
||||
## Roles
|
||||
- Planner: decomposes spec into implementable tasks.
|
||||
- Implementer: edits code within scope.
|
||||
- Verifier: runs required build and tests.
|
||||
|
||||
## Tool Handoffs and Artifact Passing
|
||||
1. **Spec Intake (Planner)**
|
||||
- Tool: Archon task/project view.
|
||||
- Input artifact: approved spec text.
|
||||
- Output artifact: `Task Brief` containing objective, scope, constraints, acceptance criteria.
|
||||
2. **Task Breakdown (Planner)**
|
||||
- Tool: Archon task board.
|
||||
- Input artifact: `Task Brief`.
|
||||
- Output artifact: ordered subtasks with file scope and done criteria.
|
||||
3. **Implementation (Implementer)**
|
||||
- Tool: repository editor + scoped files.
|
||||
- Input artifact: ordered subtasks.
|
||||
- Output artifact: minimal code/doc diffs limited to allowed files.
|
||||
4. **Build Verification (Verifier)**
|
||||
- Tool: `./verify-build.bat`.
|
||||
- Input artifact: working tree changes.
|
||||
- Output artifact: pass/fail result with error details if failed.
|
||||
5. **Focused Tests (Verifier)**
|
||||
- Tool: `dotnet test` with area filter.
|
||||
- Input artifact: verified build output.
|
||||
- Output artifact: test evidence (command + pass/fail + failing test IDs).
|
||||
6. **Task State Update (Planner/Implementer)**
|
||||
- Tool: Archon task board.
|
||||
- Input artifact: verification evidence.
|
||||
- Output artifact: task moved `doing` -> `review` with implementation notes and evidence links.
|
||||
|
||||
## Required Artifact Bundle for Review
|
||||
- `Task Brief` (objective/scope/constraints/acceptance criteria).
|
||||
- Subtask list with scoped files.
|
||||
- Diff summary (files changed + why).
|
||||
- Verification evidence:
|
||||
- `./verify-build.bat` result.
|
||||
- Relevant `dotnet test` result.
|
||||
- Known limitations or follow-up items.
|
||||
|
||||
## Exit Criteria
|
||||
- Acceptance criteria are satisfied.
|
||||
- Evidence bundle is complete.
|
||||
- Task status is `review`.
|
||||
@@ -1,3 +1,10 @@
|
||||
> ⚠️ HISTORICAL — see docs/00-governance/ for current state
|
||||
|
||||
This file may contain outdated or mixed historical information.
|
||||
Canonical current-state documentation lives in docs/00-governance/.
|
||||
This file is retained for history/reference only.
|
||||
Navigation links in this file may be stale or broken.
|
||||
|
||||
# NT8 SDK - Documentation Index
|
||||
|
||||
**Complete documentation for the NT8 Institutional Trading SDK**
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
> ⚠️ HISTORICAL — see docs/00-governance/ for current state
|
||||
|
||||
This file may contain outdated or mixed historical information.
|
||||
Canonical current-state documentation lives in docs/00-governance/.
|
||||
This file is retained for history/reference only.
|
||||
|
||||
# NT8 Institutional Trading SDK
|
||||
|
||||
**Version:** 0.2.0
|
||||
|
||||
@@ -1,7 +1,99 @@
|
||||
# NT8 Institutional SDK - Phase 1 Sprint Plan
|
||||
# NT8-SDK — Sprint Board
|
||||
**Last Updated:** 2026-03-27 | **Active Sprint:** Sprint 2
|
||||
|
||||
## Overview
|
||||
This document outlines the sprint plan for Phase 1 development of the NT8 Institutional SDK. Phase 1 builds upon the completed Phase 0 foundation to deliver a more complete trading system with Order Management System (OMS), NinjaTrader 8 integration, enhanced risk controls, and market data handling.
|
||||
---
|
||||
|
||||
## Sprint 2 — SIM Validation (ACTIVE)
|
||||
**Goal:** Strategy runs unattended 2+ weeks in SIM with correct dual-leg execution.
|
||||
**Gate:** Zero unhandled exceptions, both scaler and runner filling (Qty=2 in log), daily loss limit never accidentally triggered.
|
||||
|
||||
| # | Task | Status | Notes |
|
||||
|---|---|---|---|
|
||||
| S2-01 | Restore `EntriesPerDirection=2` | ✅ Done | Deployed 2026-03-27 |
|
||||
| S2-02 | `MaxOpenPositions=2` in SimpleORBNT8 | ✅ Done | Deployed 2026-03-27 |
|
||||
| S2-03 | `_realtimeBarSeen` replay guard | ✅ Done | Blocks live replay burst, allows backtest |
|
||||
| S2-04 | `State.Historical` guard in ProcessStrategyIntent | ✅ Done | Restores backtest execution |
|
||||
| S2-05 | Validate runner leg — Qty=2 in session log | ⬜ Pending | Run backtest after S2-01 |
|
||||
| S2-06 | Risk parameter consistency validation | ⬜ Pending | Assert RiskPerTrade ≤ MaxTradeRisk |
|
||||
| S2-07 | SIM unattended run 2+ weeks | ▶ In progress | Started 2026-03-27 |
|
||||
| S2-08 | Walk-forward backtest split | ⬜ Pending | Mar–Sep 2025 train / Oct–Mar 2026 test |
|
||||
| S2-09 | BX68915-15 prop firm re-enable | 🔴 Blocked | Gate: S2-05 + S2-07 pass |
|
||||
|
||||
---
|
||||
|
||||
## Sprint 3 — Production Hardening
|
||||
**Goal:** 30-day SIM run clean. CI and alerts live.
|
||||
|
||||
| # | Task | Status | Notes |
|
||||
|---|---|---|---|
|
||||
| S3-01 | Gitea CI — build + test on push | ⬜ Pending | `.gitea/workflows/build.yml` |
|
||||
| S3-02 | n8n webhook — fills + risk events | ⬜ Pending | HTTP POST from NT8StrategyBase |
|
||||
| S3-03 | Fix `GetRiskStatus()` hardcoded limit | ⬜ Pending | Read from registered strategy config |
|
||||
| S3-04 | `orbRangeTicks` wiring in DailyBarContext | ⬜ Pending | Low priority |
|
||||
| S3-05 | Short-side regime filter | ⬜ Pending | 20-day MA or VIX-based gate |
|
||||
| S3-06 | Tick replay validation | ⬜ Pending | 30-day window |
|
||||
| S3-07 | VWAPMeanReversion strategy skeleton | ⬜ Pending | New IStrategy implementation |
|
||||
|
||||
---
|
||||
|
||||
## Sprint 4 — Live Capital
|
||||
**Gate:** 30-day SIM pass rate > 70%, PF > 2.0, max drawdown < $500
|
||||
|
||||
| # | Task | Status |
|
||||
|---|---|---|
|
||||
| S4-01 | Go live 1 NQ contract | ⬜ Pending gate |
|
||||
| S4-02 | OvernightGapContinuation strategy | ⬜ Pending |
|
||||
| S4-03 | OvernightGapReversion strategy | ⬜ Pending |
|
||||
| S4-04 | Ops runbook and emergency procedures | ⬜ Pending |
|
||||
| S4-05 | Connection loss recovery testing | ⬜ Pending |
|
||||
| S4-06 | CME holiday filter | ⬜ Pending |
|
||||
|
||||
---
|
||||
|
||||
## Sprint 5 — ML Inference
|
||||
**Prerequisite:** 60 days of live trading data.
|
||||
|
||||
| # | Task |
|
||||
|---|---|
|
||||
| S5-01 | FastAPI /predict endpoint on Ollama workstation |
|
||||
| S5-02 | HTTP client in SimpleORBStrategy |
|
||||
| S5-03 | MLSignalFactorCalculator as IFactorCalculator |
|
||||
| S5-04 | Feature engineering from live trade history |
|
||||
| S5-05 | A/B test: with vs without ML factor |
|
||||
|
||||
---
|
||||
|
||||
## Completed (Historical)
|
||||
|
||||
| Task | Sprint | Completed |
|
||||
|---|---|---|
|
||||
| Execute trades in NT8 SIM (execution bridge wired) | Sprint 1 | 2026-03-27 |
|
||||
| Historical replay burst fix | Sprint 1 | 2026-03-27 |
|
||||
| NR7 warm-up guard | Phase 4 | 2026-02 |
|
||||
| 10-factor confluence scoring engine | Phase 4 | 2026-02 |
|
||||
| PortfolioRiskManager singleton | Phase 4 | 2026-02 |
|
||||
| Analytics layer (240+ tests) | Phase 5 | 2026-02-16 |
|
||||
| Breakeven + runner trail logic | Sprint 1 | 2026-03 |
|
||||
| Connection loss detection | Sprint 1 | 2026-03 |
|
||||
| File logging + settings export | Sprint 1 | 2026-03 |
|
||||
|
||||
---
|
||||
|
||||
## Backtest Results History
|
||||
|
||||
| Date | Period | Trades | Win% | PF | Net | Config |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 2026-03-27 | Jan–Mar 2026 | 20 | 75% | 7.00 | $1,200 | trail=20, grade=B ✅ Use this |
|
||||
| 2026-03-27 | Jan–Mar 2026 | 40 | 75% | 3.69 | $1,075 | trail=12, grade=B |
|
||||
| 2026-03-27 | Mar 2025–Mar 2026 | 148 | 51% | 3.15 | $71,303 | 9 cts experimental |
|
||||
|
||||
**Note:** The 148-trade run used `RiskPerTrade=$500` producing 9 contracts AND had `EntriesPerDirection=1` blocking the runner. Not a production reference. Re-run required post Sprint 2.
|
||||
|
||||
---
|
||||
# ARCHIVED BELOW — Phase 1 Sprint Plan (2025-09-15, superseded)
|
||||
|
||||
## Overview (ARCHIVED)
|
||||
This document originally outlined Phase 1 sprint planning from September 2025.
|
||||
|
||||
## Sprint Goals
|
||||
1. Implement Order Management System (OMS) with smart order routing
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
> ⚠️ HISTORICAL — see docs/00-governance/ for current state
|
||||
|
||||
This file may contain outdated or mixed historical information.
|
||||
Canonical current-state documentation lives in docs/00-governance/.
|
||||
This file is retained for history/reference only.
|
||||
|
||||
# Phase 2 Completion Report
|
||||
|
||||
**Project:** NT8 Institutional Trading SDK
|
||||
@@ -1 +0,0 @@
|
||||
// Removed - replaced with PlaceholderAdapter.cs
|
||||
26
src/NT8.Adapters/NinjaTrader/INT8ExecutionBridge.cs
Normal file
26
src/NT8.Adapters/NinjaTrader/INT8ExecutionBridge.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace NT8.Adapters.NinjaTrader
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides NT8OrderAdapter access to NinjaScript execution methods.
|
||||
/// Implemented by NT8StrategyBase.
|
||||
/// </summary>
|
||||
public interface INT8ExecutionBridge
|
||||
{
|
||||
/// <summary>Submit a long entry with stop and target.</summary>
|
||||
void EnterLongManaged(int quantity, string signalName, int stopTicks, int targetTicks, double tickSize);
|
||||
|
||||
/// <summary>Submit a short entry with stop and target.</summary>
|
||||
void EnterShortManaged(int quantity, string signalName, int stopTicks, int targetTicks, double tickSize);
|
||||
|
||||
/// <summary>Exit all long positions.</summary>
|
||||
void ExitLongManaged(string signalName);
|
||||
|
||||
/// <summary>Exit all short positions.</summary>
|
||||
void ExitShortManaged(string signalName);
|
||||
|
||||
/// <summary>Flatten the full position immediately.</summary>
|
||||
void FlattenAll();
|
||||
}
|
||||
}
|
||||
@@ -27,11 +27,34 @@ namespace NT8.Adapters.NinjaTrader
|
||||
public NT8Adapter()
|
||||
{
|
||||
_dataAdapter = new NT8DataAdapter();
|
||||
_orderAdapter = new NT8OrderAdapter();
|
||||
_orderAdapter = new NT8OrderAdapter(new NullExecutionBridge());
|
||||
_loggingAdapter = new NT8LoggingAdapter();
|
||||
_executionHistory = new List<NT8OrderExecutionRecord>();
|
||||
}
|
||||
|
||||
private class NullExecutionBridge : INT8ExecutionBridge
|
||||
{
|
||||
public void EnterLongManaged(int quantity, string signalName, int stopTicks, int targetTicks, double tickSize)
|
||||
{
|
||||
}
|
||||
|
||||
public void EnterShortManaged(int quantity, string signalName, int stopTicks, int targetTicks, double tickSize)
|
||||
{
|
||||
}
|
||||
|
||||
public void ExitLongManaged(string signalName)
|
||||
{
|
||||
}
|
||||
|
||||
public void ExitShortManaged(string signalName)
|
||||
{
|
||||
}
|
||||
|
||||
public void FlattenAll()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the adapter with required components
|
||||
/// </summary>
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace NT8.Adapters.NinjaTrader
|
||||
public class NT8OrderAdapter
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
private readonly INT8ExecutionBridge _bridge;
|
||||
private IRiskManager _riskManager;
|
||||
private IPositionSizer _positionSizer;
|
||||
private readonly List<NT8OrderExecutionRecord> _executionHistory;
|
||||
@@ -19,8 +20,11 @@ namespace NT8.Adapters.NinjaTrader
|
||||
/// <summary>
|
||||
/// Constructor for NT8OrderAdapter.
|
||||
/// </summary>
|
||||
public NT8OrderAdapter()
|
||||
public NT8OrderAdapter(INT8ExecutionBridge bridge)
|
||||
{
|
||||
if (bridge == null)
|
||||
throw new ArgumentNullException("bridge");
|
||||
_bridge = bridge;
|
||||
_executionHistory = new List<NT8OrderExecutionRecord>();
|
||||
}
|
||||
|
||||
@@ -127,18 +131,30 @@ namespace NT8.Adapters.NinjaTrader
|
||||
private void ExecuteInNT8(StrategyIntent intent, SizingResult sizing)
|
||||
{
|
||||
if (intent == null)
|
||||
{
|
||||
throw new ArgumentNullException("intent");
|
||||
}
|
||||
|
||||
if (sizing == null)
|
||||
{
|
||||
throw new ArgumentNullException("sizing");
|
||||
}
|
||||
|
||||
// This is where the actual NT8 order execution would happen
|
||||
// In a real implementation, this would call NT8's EnterLong/EnterShort methods
|
||||
// along with SetStopLoss, SetProfitTarget, etc.
|
||||
var signalName = string.Format("SDK_{0}_{1}", intent.Symbol, intent.Side);
|
||||
|
||||
if (intent.Side == OrderSide.Buy)
|
||||
{
|
||||
_bridge.EnterLongManaged(
|
||||
sizing.Contracts,
|
||||
signalName,
|
||||
intent.StopTicks,
|
||||
intent.TargetTicks.HasValue ? intent.TargetTicks.Value : 0,
|
||||
0.25);
|
||||
}
|
||||
else if (intent.Side == OrderSide.Sell)
|
||||
{
|
||||
_bridge.EnterShortManaged(
|
||||
sizing.Contracts,
|
||||
signalName,
|
||||
intent.StopTicks,
|
||||
intent.TargetTicks.HasValue ? intent.TargetTicks.Value : 0,
|
||||
0.25);
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -151,28 +167,6 @@ namespace NT8.Adapters.NinjaTrader
|
||||
intent.TargetTicks,
|
||||
DateTime.UtcNow));
|
||||
}
|
||||
|
||||
// Example of what this might look like in NT8:
|
||||
/*
|
||||
if (intent.Side == OrderSide.Buy)
|
||||
{
|
||||
EnterLong(sizing.Contracts, "SDK_Entry");
|
||||
SetStopLoss("SDK_Entry", CalculationMode.Ticks, intent.StopTicks);
|
||||
if (intent.TargetTicks.HasValue)
|
||||
{
|
||||
SetProfitTarget("SDK_Entry", CalculationMode.Ticks, intent.TargetTicks.Value);
|
||||
}
|
||||
}
|
||||
else if (intent.Side == OrderSide.Sell)
|
||||
{
|
||||
EnterShort(sizing.Contracts, "SDK_Entry");
|
||||
SetStopLoss("SDK_Entry", CalculationMode.Ticks, intent.StopTicks);
|
||||
if (intent.TargetTicks.HasValue)
|
||||
{
|
||||
SetProfitTarget("SDK_Entry", CalculationMode.Ticks, intent.TargetTicks.Value);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
/// <summary>
|
||||
/// Base class for strategies that integrate NT8 SDK components.
|
||||
/// </summary>
|
||||
public abstract class NT8StrategyBase : Strategy
|
||||
public abstract class NT8StrategyBase : Strategy, INT8ExecutionBridge
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
|
||||
@@ -53,7 +53,15 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
private int _ordersSubmittedToday;
|
||||
private DateTime _lastBarTime;
|
||||
private bool _killSwitchTriggered;
|
||||
private bool _connectionLost;
|
||||
private bool _realtimeBarSeen;
|
||||
private bool _breakevenMoved;
|
||||
private string _scalerSignalName;
|
||||
private string _runnerSignalName;
|
||||
private bool _runnerActive;
|
||||
private ExecutionCircuitBreaker _circuitBreaker;
|
||||
private System.IO.StreamWriter _fileLog;
|
||||
private readonly object _fileLock = new object();
|
||||
|
||||
#region User-Configurable Properties
|
||||
|
||||
@@ -93,8 +101,97 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
[Display(Name = "Verbose Logging", GroupName = "Debug", Order = 1)]
|
||||
public bool EnableVerboseLogging { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Min Trade Grade (1=F,2=D,3=C,4=B,5=A,6=A+)", GroupName = "Confluence", Order = 1)]
|
||||
[Range(0, 6)]
|
||||
public int MinTradeGrade { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Enable File Logging", GroupName = "Diagnostics", Order = 10)]
|
||||
public bool EnableFileLogging { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Log Directory", GroupName = "Diagnostics", Order = 11)]
|
||||
public string LogDirectory { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Enable Long Trades", GroupName = "Trade Direction", Order = 1)]
|
||||
public bool EnableLongTrades { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Enable Short Trades", GroupName = "Trade Direction", Order = 2)]
|
||||
public bool EnableShortTrades { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Enable Auto Breakeven", GroupName = "Exit Management", Order = 1)]
|
||||
public bool EnableAutoBreakeven { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Breakeven Trigger Ticks", GroupName = "Exit Management", Order = 2)]
|
||||
[Range(1, 100)]
|
||||
public int BreakevenTriggerTicks { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Breakeven Offset Ticks", GroupName = "Exit Management", Order = 3)]
|
||||
[Range(0, 20)]
|
||||
public int BreakevenOffsetTicks { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Enable Runner", GroupName = "Exit Management", Order = 4)]
|
||||
public bool EnableRunner { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Runner Trail Ticks", GroupName = "Exit Management", Order = 5)]
|
||||
[Range(4, 100)]
|
||||
public int RunnerTrailTicks { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
// INT8ExecutionBridge implementation
|
||||
public void EnterLongManaged(int quantity, string signalName, int stopTicks, int targetTicks, double tickSize)
|
||||
{
|
||||
if (stopTicks > 0)
|
||||
SetStopLoss(signalName, CalculationMode.Ticks, stopTicks, false);
|
||||
if (targetTicks > 0)
|
||||
SetProfitTarget(signalName, CalculationMode.Ticks, targetTicks);
|
||||
EnterLong(quantity, signalName);
|
||||
}
|
||||
|
||||
public void EnterShortManaged(int quantity, string signalName, int stopTicks, int targetTicks, double tickSize)
|
||||
{
|
||||
if (stopTicks > 0)
|
||||
SetStopLoss(signalName, CalculationMode.Ticks, stopTicks, false);
|
||||
if (targetTicks > 0)
|
||||
SetProfitTarget(signalName, CalculationMode.Ticks, targetTicks);
|
||||
EnterShort(quantity, signalName);
|
||||
}
|
||||
|
||||
public void ExitLongManaged(string signalName)
|
||||
{
|
||||
ExitLong(signalName);
|
||||
}
|
||||
|
||||
public void ExitShortManaged(string signalName)
|
||||
{
|
||||
ExitShort(signalName);
|
||||
}
|
||||
|
||||
public void FlattenAll()
|
||||
{
|
||||
ExitLong("EmergencyFlatten");
|
||||
ExitShort("EmergencyFlatten");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the concrete strategy has ForceSessionReset enabled.
|
||||
/// Override in subclass to expose the NinjaScript parameter value.
|
||||
/// Default returns false so base class never forces a reset unless overridden.
|
||||
/// </summary>
|
||||
protected virtual bool GetForceSessionReset()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the SDK strategy instance.
|
||||
/// </summary>
|
||||
@@ -112,7 +209,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
Description = "SDK-integrated strategy base";
|
||||
// Name intentionally not set - this is an abstract base class
|
||||
Calculate = Calculate.OnBarClose;
|
||||
EntriesPerDirection = 1;
|
||||
EntriesPerDirection = 2;
|
||||
EntryHandling = EntryHandling.AllEntries;
|
||||
IsExitOnSessionCloseStrategy = true;
|
||||
ExitOnSessionCloseSeconds = 30;
|
||||
@@ -120,12 +217,12 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
|
||||
OrderFillResolution = OrderFillResolution.Standard;
|
||||
Slippage = 0;
|
||||
StartBehavior = StartBehavior.WaitUntilFlat;
|
||||
StartBehavior = StartBehavior.AdoptAccountPosition;
|
||||
TimeInForce = TimeInForce.Gtc;
|
||||
TraceOrders = false;
|
||||
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
|
||||
StopTargetHandling = StopTargetHandling.PerEntryExecution;
|
||||
BarsRequiredToTrade = 20;
|
||||
BarsRequiredToTrade = 1;
|
||||
|
||||
EnableSDK = true;
|
||||
DailyLossLimit = 1000.0;
|
||||
@@ -133,10 +230,21 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
MaxOpenPositions = 3;
|
||||
RiskPerTrade = 100.0;
|
||||
MinContracts = 1;
|
||||
MaxContracts = 10;
|
||||
MaxContracts = 3;
|
||||
EnableKillSwitch = false;
|
||||
EnableVerboseLogging = false;
|
||||
MinTradeGrade = 4;
|
||||
EnableFileLogging = true;
|
||||
LogDirectory = string.Empty;
|
||||
EnableLongTrades = true;
|
||||
EnableShortTrades = true;
|
||||
EnableAutoBreakeven = true;
|
||||
BreakevenTriggerTicks = 20;
|
||||
BreakevenOffsetTicks = 1;
|
||||
EnableRunner = true;
|
||||
RunnerTrailTicks = 20;
|
||||
_killSwitchTriggered = false;
|
||||
_connectionLost = false;
|
||||
}
|
||||
else if (State == State.DataLoaded)
|
||||
{
|
||||
@@ -144,9 +252,20 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
{
|
||||
try
|
||||
{
|
||||
// DIAGNOSTIC: Print actual runtime property values to confirm
|
||||
// what NT8 loaded vs what SetDefaults specified.
|
||||
Print(string.Format("[SDK-DIAG] SetDefaults check: BE={0} Trail={1} MaxC={2} SB={3} EPD={4}",
|
||||
BreakevenTriggerTicks,
|
||||
RunnerTrailTicks,
|
||||
MaxContracts,
|
||||
StartBehavior,
|
||||
EntriesPerDirection));
|
||||
InitFileLog();
|
||||
InitializeSdkComponents();
|
||||
_sdkInitialized = true;
|
||||
Print(string.Format("[SDK] {0} initialized successfully", Name));
|
||||
WriteSettingsFile();
|
||||
WriteSessionHeader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -156,11 +275,90 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (State == State.Realtime)
|
||||
{
|
||||
_realtimeBarSeen = false;
|
||||
|
||||
// If ForceSessionReset is enabled, push a reset signal into the SDK strategy
|
||||
// so _tradeTaken is cleared before any live bar is processed.
|
||||
// This recovers from replay-burst scenarios where historical bars set _tradeTaken.
|
||||
if (_sdkStrategy != null && GetForceSessionReset())
|
||||
{
|
||||
var resetParams = new Dictionary<string, object>();
|
||||
resetParams.Add("force_session_reset", true);
|
||||
_sdkStrategy.SetParameters(resetParams);
|
||||
Print(string.Format("[SDK] ForceSessionReset: _tradeTaken cleared on live start at {0}", DateTime.Now.ToString("HH:mm:ss")));
|
||||
}
|
||||
|
||||
WriteSettingsFile();
|
||||
}
|
||||
else if (State == State.Terminated)
|
||||
{
|
||||
PortfolioRiskManager.Instance.UnregisterStrategy(Name);
|
||||
WriteSessionFooter();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnBarUpdate()
|
||||
{
|
||||
// Kill switch check — must be first
|
||||
// Only process primary bar series — ignore secondary data series updates.
|
||||
// Secondary series (e.g. daily bars for confluence) trigger OnBarUpdate separately
|
||||
// and must never generate strategy signals.
|
||||
if (BarsInProgress != 0)
|
||||
return;
|
||||
|
||||
// Require 7 completed daily bars before allowing any signal.
|
||||
// NarrowRangeFactorCalculator needs 7 daily bars for NR7 scoring.
|
||||
// Without this guard, NR7 silently returns its floor score (0.3)
|
||||
// which may suppress trades via the MinTradeGrade confluence gate.
|
||||
if (BarsArray != null && BarsArray.Length > 1
|
||||
&& CurrentBars != null && CurrentBars.Length > 1
|
||||
&& CurrentBars[1] < 7)
|
||||
{
|
||||
// Always print warm-up status — visible in Strategy Analyzer output
|
||||
// to confirm how many daily bars are available on a given backtest range.
|
||||
if (CurrentBar % 20 == 0)
|
||||
Print(String.Format("[SDK] Daily warm-up: {0}/7 bars — waiting for NR7 history. Extend backtest start date or add pre-load days.",
|
||||
CurrentBars[1] + 1));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_sdkInitialized || _sdkStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (CurrentBar < BarsRequiredToTrade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Time[0] == _lastBarTime)
|
||||
return;
|
||||
|
||||
if (Time[0].Date != _lastBarTime.Date && _lastBarTime != DateTime.MinValue)
|
||||
{
|
||||
_runnerActive = false;
|
||||
_breakevenMoved = false;
|
||||
_scalerSignalName = null;
|
||||
_runnerSignalName = null;
|
||||
}
|
||||
|
||||
_lastBarTime = Time[0];
|
||||
|
||||
// Mark first bar seen after going realtime. Until this fires, we're
|
||||
// processing catch-up replay bars and must not submit orders.
|
||||
if (State == State.Realtime && !_realtimeBarSeen)
|
||||
{
|
||||
_realtimeBarSeen = true;
|
||||
Print(string.Format("[SDK] First realtime bar seen: {0}", Time[0]));
|
||||
return;
|
||||
}
|
||||
|
||||
// Sync actual open position to portfolio manager on every bar
|
||||
PortfolioRiskManager.Instance.UpdateOpenContracts(Name, Math.Abs(Position.Quantity));
|
||||
|
||||
// Kill switch — checked AFTER bar guards so ExitLong/ExitShort are valid
|
||||
if (EnableKillSwitch)
|
||||
{
|
||||
if (!_killSwitchTriggered)
|
||||
@@ -177,28 +375,44 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
Print(string.Format("[SDK] Kill switch flatten error: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_sdkInitialized || _sdkStrategy == null)
|
||||
// Connection loss guard — do not submit new orders if broker is disconnected
|
||||
if (_connectionLost)
|
||||
{
|
||||
if (CurrentBar == 0)
|
||||
Print(string.Format("[SDK] Not initialized: sdkInit={0}, strategy={1}", _sdkInitialized, _sdkStrategy != null));
|
||||
if (EnableVerboseLogging)
|
||||
Print(string.Format("[NT8-SDK] Bar skipped — connection lost: {0}", Time[0]));
|
||||
return;
|
||||
}
|
||||
|
||||
if (CurrentBar < BarsRequiredToTrade)
|
||||
// Hard RTH guard using NT8 bar time converted from CT to ET.
|
||||
// Belt-and-suspenders against SDK session timezone issues.
|
||||
DateTime ntBarTimeEt;
|
||||
try
|
||||
{
|
||||
if (CurrentBar == 0)
|
||||
Print(string.Format("[SDK] Waiting for bars: current={0}, required={1}", CurrentBar, BarsRequiredToTrade));
|
||||
return;
|
||||
var centralZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
|
||||
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
|
||||
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(
|
||||
DateTime.SpecifyKind(Time[0], DateTimeKind.Unspecified),
|
||||
centralZone);
|
||||
ntBarTimeEt = TimeZoneInfo.ConvertTimeFromUtc(utcTime, easternZone);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ntBarTimeEt = Time[0];
|
||||
}
|
||||
|
||||
if (Time[0] == _lastBarTime)
|
||||
return;
|
||||
bool isRthBar = ntBarTimeEt.TimeOfDay >= new TimeSpan(9, 30, 0)
|
||||
&& ntBarTimeEt.TimeOfDay < new TimeSpan(16, 0, 0);
|
||||
|
||||
_lastBarTime = Time[0];
|
||||
if (!isRthBar)
|
||||
{
|
||||
if (EnableVerboseLogging && CurrentBar % 500 == 0)
|
||||
Print(string.Format("[SDK] Skipping ETH bar {0} at {1:HH:mm} ET",
|
||||
CurrentBar, ntBarTimeEt));
|
||||
return;
|
||||
}
|
||||
|
||||
// Log first processable bar and every 100th bar.
|
||||
if (CurrentBar == BarsRequiredToTrade || CurrentBar % 100 == 0)
|
||||
@@ -212,6 +426,50 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
Close[0]));
|
||||
}
|
||||
|
||||
// --- Breakeven and runner trailing monitor ---
|
||||
if (_runnerActive && !string.IsNullOrEmpty(_runnerSignalName) && Position.Quantity != 0)
|
||||
{
|
||||
double entryPrice = Position.AveragePrice;
|
||||
double currentClose = Close[0];
|
||||
bool isLong = Position.MarketPosition == MarketPosition.Long;
|
||||
|
||||
double profitTicks = isLong
|
||||
? (currentClose - entryPrice) / TickSize
|
||||
: (entryPrice - currentClose) / TickSize;
|
||||
|
||||
// Move runner stop to breakeven + offset once trigger is reached
|
||||
if (EnableAutoBreakeven && !_breakevenMoved && profitTicks >= BreakevenTriggerTicks)
|
||||
{
|
||||
double bePrice = isLong
|
||||
? entryPrice + (BreakevenOffsetTicks * TickSize)
|
||||
: entryPrice - (BreakevenOffsetTicks * TickSize);
|
||||
|
||||
SetStopLoss(_runnerSignalName, CalculationMode.Price, bePrice, false);
|
||||
_breakevenMoved = true;
|
||||
|
||||
Print(String.Format("[SDK] Runner breakeven set at {0:F2} (profit={1:F0} ticks)",
|
||||
bePrice, profitTicks));
|
||||
if (EnableFileLogging)
|
||||
FileLog(String.Format("BREAKEVEN runner stop -> {0:F2} profit={1:F0}ticks",
|
||||
bePrice, profitTicks));
|
||||
}
|
||||
|
||||
// Activate trailing stop on runner once breakeven is secured
|
||||
if (_breakevenMoved && RunnerTrailTicks > 0)
|
||||
{
|
||||
SetTrailStop(_runnerSignalName, CalculationMode.Ticks, RunnerTrailTicks, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear runner state when flat
|
||||
if (Position.Quantity == 0 && _runnerActive)
|
||||
{
|
||||
_runnerActive = false;
|
||||
_breakevenMoved = false;
|
||||
if (EnableFileLogging && !string.IsNullOrEmpty(_runnerSignalName))
|
||||
FileLog("RUNNER closed — position flat");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var barData = ConvertCurrentBar();
|
||||
@@ -290,9 +548,186 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
if (string.IsNullOrEmpty(execution.Order.Name) || !execution.Order.Name.StartsWith("SDK_"))
|
||||
return;
|
||||
|
||||
FileLog(string.Format("FILL {0} {1} @ {2:F2} | OrderId={3}",
|
||||
execution.MarketPosition,
|
||||
execution.Quantity,
|
||||
execution.Price,
|
||||
execution.OrderId));
|
||||
|
||||
var fill = new NT8.Core.Common.Models.OrderFill(
|
||||
orderId,
|
||||
execution.Order != null ? execution.Order.Instrument.MasterInstrument.Name : string.Empty,
|
||||
execution.Quantity,
|
||||
execution.Price,
|
||||
time,
|
||||
0.0,
|
||||
executionId);
|
||||
PortfolioRiskManager.Instance.ReportFill(Name, fill);
|
||||
|
||||
_executionAdapter.ProcessExecution(orderId, executionId, price, quantity, time);
|
||||
}
|
||||
|
||||
protected override void OnPositionUpdate(
|
||||
NinjaTrader.Cbi.Position position,
|
||||
double averagePrice,
|
||||
int quantity,
|
||||
MarketPosition marketPosition)
|
||||
{
|
||||
if (!_sdkInitialized || _riskManager == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
double dayPnL = 0.0;
|
||||
if (Account != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
dayPnL = Account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar);
|
||||
}
|
||||
catch
|
||||
{
|
||||
dayPnL = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
_riskManager.OnPnLUpdate(dayPnL, dayPnL);
|
||||
PortfolioRiskManager.Instance.ReportPnL(Name, dayPnL);
|
||||
|
||||
if (EnableVerboseLogging)
|
||||
Print(string.Format("[SDK] P&L update: DayPnL={0:C} Position={1} Qty={2}",
|
||||
dayPnL, marketPosition, quantity));
|
||||
|
||||
FileLog(string.Format("PNL_UPDATE DayPnL={0:C} Position={1} Qty={2}",
|
||||
dayPnL, marketPosition, quantity));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Print(string.Format("[SDK] OnPositionUpdate error: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles broker connection status changes. Halts new orders on disconnect,
|
||||
/// logs reconnect, and resets the connection flag when restored.
|
||||
/// NinjaScript signature: single ConnectionStatusEventArgs parameter.
|
||||
/// </summary>
|
||||
protected override void OnConnectionStatusUpdate(
|
||||
ConnectionStatusEventArgs connectionStatusUpdate)
|
||||
{
|
||||
if (connectionStatusUpdate == null) return;
|
||||
|
||||
if (connectionStatusUpdate.Status == ConnectionStatus.Connected)
|
||||
{
|
||||
if (_connectionLost)
|
||||
{
|
||||
_connectionLost = false;
|
||||
Print(string.Format("[NT8-SDK] Connection RESTORED at {0} — trading resumed.",
|
||||
DateTime.Now.ToString("HH:mm:ss")));
|
||||
FileLog(string.Format("CONNECTION RESTORED at {0}", DateTime.Now.ToString("HH:mm:ss")));
|
||||
}
|
||||
}
|
||||
else if (connectionStatusUpdate.Status == ConnectionStatus.Disconnected ||
|
||||
connectionStatusUpdate.Status == ConnectionStatus.ConnectionLost)
|
||||
{
|
||||
if (!_connectionLost)
|
||||
{
|
||||
_connectionLost = true;
|
||||
Print(string.Format("[NT8-SDK] Connection LOST at {0} — halting new orders. Status={1}",
|
||||
DateTime.Now.ToString("HH:mm:ss"),
|
||||
connectionStatusUpdate.Status));
|
||||
FileLog(string.Format("CONNECTION LOST at {0} Status={1}",
|
||||
DateTime.Now.ToString("HH:mm:ss"),
|
||||
connectionStatusUpdate.Status));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitFileLog()
|
||||
{
|
||||
if (!EnableFileLogging)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
string dir = string.IsNullOrEmpty(LogDirectory)
|
||||
? System.IO.Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
||||
"NinjaTrader 8", "log", "nt8-sdk")
|
||||
: LogDirectory;
|
||||
|
||||
System.IO.Directory.CreateDirectory(dir);
|
||||
|
||||
string path = System.IO.Path.Combine(
|
||||
dir,
|
||||
string.Format("session_{0}.log", DateTime.Now.ToString("yyyyMMdd_HHmmss")));
|
||||
|
||||
_fileLog = new System.IO.StreamWriter(path, false);
|
||||
_fileLog.AutoFlush = true;
|
||||
Print(string.Format("[NT8-SDK] File log started: {0}", path));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Print(string.Format("[NT8-SDK] Failed to open file log: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
private void FileLog(string message)
|
||||
{
|
||||
if (_fileLog == null)
|
||||
return;
|
||||
|
||||
lock (_fileLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
_fileLog.WriteLine(string.Format("[{0:HH:mm:ss.fff}] {1}", DateTime.Now, message));
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteSessionHeader()
|
||||
{
|
||||
FileLog("=== SESSION START " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ===");
|
||||
FileLog(string.Format("Mode : {0}", State == State.Historical ? "BACKTEST" : "LIVE/SIM"));
|
||||
FileLog(string.Format("Strategy : {0}", Name));
|
||||
FileLog(string.Format("Account : {0}", Account != null ? Account.Name : "N/A"));
|
||||
FileLog(string.Format("Symbol : {0}", Instrument != null ? Instrument.FullName : "N/A"));
|
||||
FileLog(string.Format("Risk : DailyLimit=${0} MaxTradeRisk=${1} RiskPerTrade=${2}",
|
||||
DailyLossLimit,
|
||||
MaxTradeRisk,
|
||||
RiskPerTrade));
|
||||
FileLog(string.Format("Sizing : MinContracts={0} MaxContracts={1}", MinContracts, MaxContracts));
|
||||
FileLog(string.Format("VerboseLog : {0} FileLog: {1}", EnableVerboseLogging, EnableFileLogging));
|
||||
FileLog(string.Format("ConnectionLost : {0}", _connectionLost));
|
||||
FileLog("---");
|
||||
}
|
||||
|
||||
private void WriteSessionFooter()
|
||||
{
|
||||
FileLog("---");
|
||||
FileLog("=== SESSION END " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ===");
|
||||
|
||||
if (_fileLog != null)
|
||||
{
|
||||
lock (_fileLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
_fileLog.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
_fileLog = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeSdkComponents()
|
||||
{
|
||||
_logger = new BasicLogger(Name);
|
||||
@@ -317,7 +752,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
_riskConfig,
|
||||
_sizingConfig);
|
||||
|
||||
_riskManager = new BasicRiskManager(_logger);
|
||||
_riskManager = new BasicRiskManager(_logger, DailyLossLimit);
|
||||
_positionSizer = new BasicPositionSizer(_logger);
|
||||
_circuitBreaker = new ExecutionCircuitBreaker(
|
||||
_logger,
|
||||
@@ -331,6 +766,8 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
_sdkStrategy.Initialize(_strategyConfig, null, _logger);
|
||||
ConfigureStrategyParameters();
|
||||
PortfolioRiskManager.Instance.RegisterStrategy(Name, _riskConfig);
|
||||
Print(string.Format("[NT8-SDK] Registered with PortfolioRiskManager: {0}", PortfolioRiskManager.Instance.GetStatusSnapshot()));
|
||||
|
||||
_ordersSubmittedToday = 0;
|
||||
_lastBarTime = DateTime.MinValue;
|
||||
@@ -341,9 +778,24 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
private BarData ConvertCurrentBar()
|
||||
{
|
||||
DateTime barTimeEt;
|
||||
try
|
||||
{
|
||||
var centralZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
|
||||
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
|
||||
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(
|
||||
DateTime.SpecifyKind(Time[0], DateTimeKind.Unspecified),
|
||||
centralZone);
|
||||
barTimeEt = TimeZoneInfo.ConvertTimeFromUtc(utcTime, easternZone);
|
||||
}
|
||||
catch
|
||||
{
|
||||
barTimeEt = Time[0];
|
||||
}
|
||||
|
||||
return NT8DataConverter.ConvertBar(
|
||||
Instrument.MasterInstrument.Name,
|
||||
Time[0],
|
||||
barTimeEt,
|
||||
Open[0],
|
||||
High[0],
|
||||
Low[0],
|
||||
@@ -354,6 +806,21 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
private StrategyContext BuildStrategyContext()
|
||||
{
|
||||
DateTime etTime;
|
||||
try
|
||||
{
|
||||
var centralZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
|
||||
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
|
||||
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(
|
||||
DateTime.SpecifyKind(Time[0], DateTimeKind.Unspecified),
|
||||
centralZone);
|
||||
etTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, easternZone);
|
||||
}
|
||||
catch
|
||||
{
|
||||
etTime = Time[0];
|
||||
}
|
||||
|
||||
var customData = new Dictionary<string, object>();
|
||||
customData.Add("CurrentBar", CurrentBar);
|
||||
customData.Add("BarsRequiredToTrade", BarsRequiredToTrade);
|
||||
@@ -361,7 +828,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
return NT8DataConverter.ConvertContext(
|
||||
Instrument.MasterInstrument.Name,
|
||||
Time[0],
|
||||
etTime,
|
||||
BuildPositionInfo(),
|
||||
BuildAccountInfo(),
|
||||
BuildSessionInfo(),
|
||||
@@ -370,7 +837,23 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
private AccountInfo BuildAccountInfo()
|
||||
{
|
||||
var accountInfo = NT8DataConverter.ConvertAccount(100000.0, 250000.0, 0.0, 0.0, DateTime.UtcNow);
|
||||
double cashValue = 100000.0;
|
||||
double buyingPower = 250000.0;
|
||||
|
||||
try
|
||||
{
|
||||
if (Account != null)
|
||||
{
|
||||
cashValue = Account.Get(AccountItem.CashValue, Currency.UsDollar);
|
||||
buyingPower = Account.Get(AccountItem.BuyingPower, Currency.UsDollar);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Print(string.Format("[NT8-SDK] WARNING: Could not read live account balance, using defaults: {0}", ex.Message));
|
||||
}
|
||||
|
||||
var accountInfo = NT8DataConverter.ConvertAccount(cashValue, buyingPower, 0.0, 0.0, DateTime.UtcNow);
|
||||
_lastAccountInfo = accountInfo;
|
||||
return accountInfo;
|
||||
}
|
||||
@@ -391,20 +874,78 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
private MarketSession BuildSessionInfo()
|
||||
{
|
||||
if (_currentSession != null && _currentSession.SessionStart.Date == Time[0].Date)
|
||||
return _currentSession;
|
||||
DateTime etTime;
|
||||
try
|
||||
{
|
||||
var centralZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
|
||||
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
|
||||
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(
|
||||
DateTime.SpecifyKind(Time[0], DateTimeKind.Unspecified),
|
||||
centralZone);
|
||||
etTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, easternZone);
|
||||
}
|
||||
catch
|
||||
{
|
||||
etTime = Time[0];
|
||||
}
|
||||
|
||||
var sessionStart = Time[0].Date.AddHours(9).AddMinutes(30);
|
||||
var sessionEnd = Time[0].Date.AddHours(16);
|
||||
var isRth = Time[0].Hour >= 9 && Time[0].Hour < 16;
|
||||
var sessionName = isRth ? "RTH" : "ETH";
|
||||
// Futures trade nearly 24 hours. Bars at/after 17:00 ET belong to the next
|
||||
// calendar day's RTH trading session.
|
||||
DateTime tradingDate;
|
||||
if (etTime.TimeOfDay >= new TimeSpan(17, 0, 0))
|
||||
tradingDate = etTime.Date.AddDays(1);
|
||||
else
|
||||
tradingDate = etTime.Date;
|
||||
|
||||
_currentSession = NT8DataConverter.ConvertSession(sessionStart, sessionEnd, isRth, sessionName);
|
||||
if (EnableVerboseLogging && (CurrentBar == BarsRequiredToTrade || CurrentBar % 500 == 0))
|
||||
{
|
||||
Print(string.Format("[SDK-TZ] Bar {0}: NT8 Time[0]={1:yyyy-MM-dd HH:mm:ss} | etTime={2:yyyy-MM-dd HH:mm:ss} | isRth={3}",
|
||||
CurrentBar,
|
||||
Time[0],
|
||||
etTime,
|
||||
etTime.TimeOfDay >= TimeSpan.FromHours(9.5) && etTime.TimeOfDay < TimeSpan.FromHours(16.0)));
|
||||
}
|
||||
|
||||
var sessionStart = tradingDate.AddHours(9).AddMinutes(30);
|
||||
var sessionEnd = tradingDate.AddHours(16);
|
||||
var isRth = etTime.TimeOfDay >= TimeSpan.FromHours(9.5)
|
||||
&& etTime.TimeOfDay < TimeSpan.FromHours(16.0);
|
||||
|
||||
_currentSession = NT8DataConverter.ConvertSession(sessionStart, sessionEnd, isRth, isRth ? "RTH" : "ETH");
|
||||
return _currentSession;
|
||||
}
|
||||
|
||||
private void ProcessStrategyIntent(StrategyIntent intent, StrategyContext context)
|
||||
{
|
||||
// In live/SIM: block if we haven't seen a genuine realtime bar yet (replay guard).
|
||||
// In Strategy Analyzer (State.Historical): always allow — backtest must execute normally.
|
||||
if (State == State.Realtime && !_realtimeBarSeen)
|
||||
return;
|
||||
|
||||
// Portfolio-level risk check — runs before per-strategy risk validation
|
||||
var portfolioDecision = PortfolioRiskManager.Instance.ValidatePortfolioRisk(Name, intent);
|
||||
if (!portfolioDecision.Allow)
|
||||
{
|
||||
Print(string.Format("[SDK] Portfolio blocked: {0}", portfolioDecision.RejectReason));
|
||||
if (_logger != null)
|
||||
_logger.LogWarning("Portfolio risk blocked order: {0}", portfolioDecision.RejectReason);
|
||||
return;
|
||||
}
|
||||
|
||||
// Direction filter — checked before risk to avoid unnecessary processing
|
||||
if (intent.Side == SdkOrderSide.Buy && !EnableLongTrades)
|
||||
{
|
||||
if (EnableVerboseLogging)
|
||||
Print(string.Format("[SDK] Long trade filtered by direction setting: {0}", intent.Symbol));
|
||||
return;
|
||||
}
|
||||
if (intent.Side == SdkOrderSide.Sell && !EnableShortTrades)
|
||||
{
|
||||
if (EnableVerboseLogging)
|
||||
Print(string.Format("[SDK] Short trade filtered by direction setting: {0}", intent.Symbol));
|
||||
return;
|
||||
}
|
||||
|
||||
if (EnableVerboseLogging)
|
||||
Print(string.Format("[SDK] Validating intent: {0} {1}", intent.Side, intent.Symbol));
|
||||
|
||||
@@ -460,45 +1001,82 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
private void SubmitOrderToNT8(OmsOrderRequest request, StrategyIntent intent)
|
||||
{
|
||||
// Circuit breaker gate
|
||||
if (State != State.Historical)
|
||||
{
|
||||
if (_circuitBreaker != null && !_circuitBreaker.ShouldAllowOrder())
|
||||
{
|
||||
var state = _circuitBreaker.GetState();
|
||||
Print(string.Format("[SDK] Circuit breaker OPEN — order blocked: {0}", state.Reason));
|
||||
var cbState = _circuitBreaker.GetState();
|
||||
Print(String.Format("[SDK] Circuit breaker OPEN — order blocked: {0}", cbState.Reason));
|
||||
if (_logger != null)
|
||||
_logger.LogWarning("Circuit breaker blocked order: {0}", state.Reason);
|
||||
_logger.LogWarning("Circuit breaker blocked order: {0}", cbState.Reason);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var orderName = string.Format("SDK_{0}_{1}", intent.Symbol, DateTime.Now.Ticks);
|
||||
_executionAdapter.SubmitOrder(request, orderName);
|
||||
bool useRunner = EnableRunner && request.Quantity >= 2;
|
||||
|
||||
int scalerQty = useRunner ? request.Quantity - 1 : request.Quantity;
|
||||
int runnerQty = useRunner ? 1 : 0;
|
||||
|
||||
string baseId = Guid.NewGuid().ToString("N").Substring(0, 12);
|
||||
_scalerSignalName = String.Format("SDK_{0}_S_{1}", intent.Symbol, baseId);
|
||||
_runnerSignalName = useRunner ? String.Format("SDK_{0}_R_{1}", intent.Symbol, baseId) : null;
|
||||
_breakevenMoved = false;
|
||||
_runnerActive = useRunner;
|
||||
|
||||
if (EnableFileLogging)
|
||||
{
|
||||
string grade = "N/A";
|
||||
string score = "N/A";
|
||||
string factors = string.Empty;
|
||||
if (intent.Metadata != null && intent.Metadata.ContainsKey("confluence_score"))
|
||||
{
|
||||
var cs = intent.Metadata["confluence_score"] as NT8.Core.Intelligence.ConfluenceScore;
|
||||
if (cs != null)
|
||||
{
|
||||
grade = cs.Grade.ToString();
|
||||
score = cs.WeightedScore.ToString("F3");
|
||||
var sb = new System.Text.StringBuilder();
|
||||
foreach (var f in cs.Factors)
|
||||
sb.Append(String.Format("{0}={1:F2} ", f.Type, f.Score));
|
||||
factors = sb.ToString().TrimEnd();
|
||||
}
|
||||
}
|
||||
FileLog(String.Format("SIGNAL {0} | Grade={1} | Score={2}", intent.Side, grade, score));
|
||||
if (!string.IsNullOrEmpty(factors))
|
||||
FileLog(String.Format(" Factors: {0}", factors));
|
||||
FileLog(String.Format("SUBMIT Scaler={0} Runner={1} Stop={2} Target={3}",
|
||||
scalerQty, runnerQty, intent.StopTicks,
|
||||
intent.TargetTicks.HasValue ? intent.TargetTicks.Value.ToString() : "none"));
|
||||
}
|
||||
|
||||
// --- Submit scaler leg ---
|
||||
if (intent.StopTicks > 0)
|
||||
SetStopLoss(_scalerSignalName, CalculationMode.Ticks, (int)intent.StopTicks, false);
|
||||
if (intent.TargetTicks.HasValue && intent.TargetTicks.Value > 0)
|
||||
SetProfitTarget(_scalerSignalName, CalculationMode.Ticks, (int)intent.TargetTicks.Value);
|
||||
|
||||
if (request.Side == OmsOrderSide.Buy)
|
||||
{
|
||||
if (request.Type == OmsOrderType.Market)
|
||||
EnterLong(request.Quantity, orderName);
|
||||
else if (request.Type == OmsOrderType.Limit && request.LimitPrice.HasValue)
|
||||
EnterLongLimit(request.Quantity, (double)request.LimitPrice.Value, orderName);
|
||||
else if (request.Type == OmsOrderType.StopMarket && request.StopPrice.HasValue)
|
||||
EnterLongStopMarket(request.Quantity, (double)request.StopPrice.Value, orderName);
|
||||
}
|
||||
else if (request.Side == OmsOrderSide.Sell)
|
||||
{
|
||||
if (request.Type == OmsOrderType.Market)
|
||||
EnterShort(request.Quantity, orderName);
|
||||
else if (request.Type == OmsOrderType.Limit && request.LimitPrice.HasValue)
|
||||
EnterShortLimit(request.Quantity, (double)request.LimitPrice.Value, orderName);
|
||||
else if (request.Type == OmsOrderType.StopMarket && request.StopPrice.HasValue)
|
||||
EnterShortStopMarket(request.Quantity, (double)request.StopPrice.Value, orderName);
|
||||
}
|
||||
EnterLong(scalerQty, _scalerSignalName);
|
||||
else
|
||||
EnterShort(scalerQty, _scalerSignalName);
|
||||
|
||||
// --- Submit runner leg (no fixed target — exits via trailing stop) ---
|
||||
if (useRunner)
|
||||
{
|
||||
if (intent.StopTicks > 0)
|
||||
SetStopLoss(orderName, CalculationMode.Ticks, (int)intent.StopTicks, false);
|
||||
SetStopLoss(_runnerSignalName, CalculationMode.Ticks, (int)intent.StopTicks, false);
|
||||
// No SetProfitTarget on runner — trail stop will manage exit
|
||||
|
||||
if (intent.TargetTicks.HasValue && intent.TargetTicks.Value > 0)
|
||||
SetProfitTarget(orderName, CalculationMode.Ticks, (int)intent.TargetTicks.Value);
|
||||
if (request.Side == OmsOrderSide.Buy)
|
||||
EnterLong(runnerQty, _runnerSignalName);
|
||||
else
|
||||
EnterShort(runnerQty, _runnerSignalName);
|
||||
}
|
||||
|
||||
_executionAdapter.SubmitOrder(request, _scalerSignalName);
|
||||
|
||||
if (_circuitBreaker != null)
|
||||
_circuitBreaker.OnSuccess();
|
||||
@@ -507,8 +1085,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
{
|
||||
if (_circuitBreaker != null)
|
||||
_circuitBreaker.OnFailure();
|
||||
|
||||
Print(string.Format("[SDK] SubmitOrderToNT8 failed: {0}", ex.Message));
|
||||
Print(String.Format("[SDK] SubmitOrderToNT8 failed: {0}", ex.Message));
|
||||
if (_logger != null)
|
||||
_logger.LogError("SubmitOrderToNT8 failed: {0}", ex.Message);
|
||||
throw;
|
||||
@@ -539,6 +1116,83 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
return null;
|
||||
return _executionAdapter.GetOrderStatus(orderName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all strategy parameter lines for the settings export file.
|
||||
/// Override in subclasses to append strategy-specific parameters.
|
||||
/// Call base.GetStrategySettingsLines() first then add to the list.
|
||||
/// </summary>
|
||||
protected virtual List<string> GetStrategySettingsLines()
|
||||
{
|
||||
var lines = new List<string>();
|
||||
lines.Add("=== STRATEGY SETTINGS EXPORT ===");
|
||||
lines.Add(string.Format("ExportTime : {0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
|
||||
lines.Add(string.Format("StrategyName : {0}", Name));
|
||||
lines.Add(string.Format("Description : {0}", Description));
|
||||
lines.Add(string.Format("Account : {0}", Account != null ? Account.Name : "N/A"));
|
||||
lines.Add(string.Format("Instrument : {0}", Instrument != null ? Instrument.FullName : "N/A"));
|
||||
lines.Add(string.Format("BarsPeriod : {0} {1}", BarsPeriod != null ? BarsPeriod.Value.ToString() : "N/A", BarsPeriod != null ? BarsPeriod.BarsPeriodType.ToString() : string.Empty));
|
||||
lines.Add(string.Format("BarsRequiredToTrade: {0}", BarsRequiredToTrade));
|
||||
lines.Add(string.Format("Calculate : {0}", Calculate));
|
||||
lines.Add("--- Risk ---");
|
||||
lines.Add(string.Format("DailyLossLimit : {0:C}", DailyLossLimit));
|
||||
lines.Add(string.Format("MaxTradeRisk : {0:C}", MaxTradeRisk));
|
||||
lines.Add(string.Format("MaxOpenPositions : {0}", MaxOpenPositions));
|
||||
lines.Add(string.Format("RiskPerTrade : {0:C}", RiskPerTrade));
|
||||
lines.Add("--- Sizing ---");
|
||||
lines.Add(string.Format("MinContracts : {0}", MinContracts));
|
||||
lines.Add(string.Format("MaxContracts : {0}", MaxContracts));
|
||||
lines.Add("--- Direction ---");
|
||||
lines.Add(string.Format("EnableLongTrades : {0}", EnableLongTrades));
|
||||
lines.Add(string.Format("EnableShortTrades : {0}", EnableShortTrades));
|
||||
lines.Add("--- Controls ---");
|
||||
lines.Add(string.Format("EnableKillSwitch : {0}", EnableKillSwitch));
|
||||
lines.Add(string.Format("EnableVerboseLogging: {0}", EnableVerboseLogging));
|
||||
lines.Add(string.Format("MinTradeGrade : {0}", MinTradeGrade));
|
||||
lines.Add(string.Format("EnableFileLogging : {0}", EnableFileLogging));
|
||||
lines.Add(string.Format("LogDirectory : {0}", string.IsNullOrEmpty(LogDirectory) ? "(default)" : LogDirectory));
|
||||
lines.Add("--- Portfolio ---");
|
||||
lines.Add(string.Format("PortfolioStatus : {0}", PortfolioRiskManager.Instance.GetStatusSnapshot()));
|
||||
lines.Add("=== END SETTINGS ===");
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a settings export file to the same directory as the session log.
|
||||
/// File is named settings_STRATEGYNAME_YYYYMMDD_HHmmss.txt.
|
||||
/// Only writes when EnableVerboseLogging is true.
|
||||
/// </summary>
|
||||
private void WriteSettingsFile()
|
||||
{
|
||||
if (!EnableVerboseLogging) return;
|
||||
|
||||
try
|
||||
{
|
||||
string dir = string.IsNullOrEmpty(LogDirectory)
|
||||
? System.IO.Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
||||
"NinjaTrader 8", "log", "nt8-sdk")
|
||||
: LogDirectory;
|
||||
|
||||
System.IO.Directory.CreateDirectory(dir);
|
||||
|
||||
string safeName = Name.Replace(" ", "_").Replace("/", "_").Replace("\\", "_");
|
||||
string path = System.IO.Path.Combine(dir,
|
||||
string.Format("settings_{0}_{1}.txt",
|
||||
safeName,
|
||||
DateTime.Now.ToString("yyyyMMdd_HHmmss")));
|
||||
|
||||
var lines = GetStrategySettingsLines();
|
||||
|
||||
System.IO.File.WriteAllLines(path, lines.ToArray());
|
||||
|
||||
Print(string.Format("[NT8-SDK] Settings exported: {0}", path));
|
||||
FileLog(string.Format("SETTINGS FILE: {0}", path));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Print(string.Format("[NT8-SDK] WARNING: Could not write settings file: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ using NinjaTrader.NinjaScript;
|
||||
using NinjaTrader.NinjaScript.Indicators;
|
||||
using NinjaTrader.NinjaScript.Strategies;
|
||||
using NT8.Core.Common.Interfaces;
|
||||
using NT8.Core.Intelligence;
|
||||
using NT8.Strategies.Examples;
|
||||
using SdkSimpleORB = NT8.Strategies.Examples.SimpleORBStrategy;
|
||||
|
||||
@@ -22,6 +23,8 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
/// </summary>
|
||||
public class SimpleORBNT8 : NT8StrategyBase
|
||||
{
|
||||
private int _lastSignalDirection;
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Opening Range Minutes", GroupName = "ORB Strategy", Order = 1)]
|
||||
[Range(5, 120)]
|
||||
@@ -37,6 +40,10 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
[Range(1, 50)]
|
||||
public int StopTicks { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Force Session Reset On Start", GroupName = "ORB Strategy", Order = 10)]
|
||||
public bool ForceSessionReset { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Profit Target Ticks", GroupName = "ORB Risk", Order = 2)]
|
||||
[Range(1, 100)]
|
||||
@@ -47,7 +54,9 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
if (State == State.SetDefaults)
|
||||
{
|
||||
Name = "Simple ORB NT8";
|
||||
Description = "Opening Range Breakout with NT8 SDK integration";
|
||||
Description = "v0.4.0 | 2026-03-19 | NR7+ORB factors, PortfolioRiskManager, connection recovery, live account balance";
|
||||
|
||||
// Daily bar series is added automatically via AddDataSeries in Configure.
|
||||
|
||||
OpeningRangeMinutes = 30;
|
||||
StdDevMultiplier = 1.0;
|
||||
@@ -56,18 +65,57 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
DailyLossLimit = 1000.0;
|
||||
MaxTradeRisk = 200.0;
|
||||
MaxOpenPositions = 1;
|
||||
MaxOpenPositions = 2;
|
||||
RiskPerTrade = 100.0;
|
||||
MinContracts = 1;
|
||||
MaxContracts = 3;
|
||||
|
||||
Calculate = Calculate.OnBarClose;
|
||||
BarsRequiredToTrade = 50;
|
||||
MinTradeGrade = 5;
|
||||
EnableLongTrades = true;
|
||||
// Long-only: short trades permanently disabled pending backtest confirmation
|
||||
EnableShortTrades = false;
|
||||
EnableAutoBreakeven = true;
|
||||
BreakevenTriggerTicks = 20;
|
||||
BreakevenOffsetTicks = 1;
|
||||
EnableRunner = true;
|
||||
RunnerTrailTicks = 20;
|
||||
ForceSessionReset = false;
|
||||
StartBehavior = StartBehavior.AdoptAccountPosition;
|
||||
}
|
||||
else if (State == State.Configure)
|
||||
{
|
||||
AddDataSeries(BarsPeriodType.Day, 1);
|
||||
}
|
||||
|
||||
base.OnStateChange();
|
||||
}
|
||||
|
||||
protected override void OnBarUpdate()
|
||||
{
|
||||
if (_strategyConfig != null && BarsArray != null && BarsArray.Length > 1)
|
||||
{
|
||||
DailyBarContext dailyContext = BuildDailyBarContext(_lastSignalDirection, 0.0, (double)Volume[0]);
|
||||
_strategyConfig.Parameters["daily_bars"] = dailyContext;
|
||||
}
|
||||
|
||||
base.OnBarUpdate();
|
||||
|
||||
if (Position != null)
|
||||
{
|
||||
if (Position.MarketPosition == MarketPosition.Long)
|
||||
_lastSignalDirection = 1;
|
||||
else if (Position.MarketPosition == MarketPosition.Short)
|
||||
_lastSignalDirection = -1;
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool GetForceSessionReset()
|
||||
{
|
||||
return ForceSessionReset;
|
||||
}
|
||||
|
||||
protected override IStrategy CreateSdkStrategy()
|
||||
{
|
||||
return new SdkSimpleORB(OpeningRangeMinutes, StdDevMultiplier);
|
||||
@@ -97,16 +145,114 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
_strategyConfig.Parameters["StopTicks"] = StopTicks;
|
||||
_strategyConfig.Parameters["TargetTicks"] = TargetTicks;
|
||||
_strategyConfig.Parameters["OpeningRangeMinutes"] = OpeningRangeMinutes;
|
||||
_strategyConfig.Parameters["MinTradeGrade"] = MinTradeGrade;
|
||||
|
||||
if (Instrument != null && Instrument.MasterInstrument != null)
|
||||
{
|
||||
_strategyConfig.Parameters["TickSize"] = Instrument.MasterInstrument.TickSize;
|
||||
}
|
||||
|
||||
if (_logger != null)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Simple ORB configured: OR={0}min, Stop={1}ticks, Target={2}ticks",
|
||||
"Simple ORB configured: OR={0}min, Stop={1}ticks, Target={2}ticks, Long={3}, Short={4}",
|
||||
OpeningRangeMinutes,
|
||||
StopTicks,
|
||||
TargetTicks);
|
||||
TargetTicks,
|
||||
EnableLongTrades,
|
||||
EnableShortTrades);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends ORB-specific parameters to the base settings export.
|
||||
/// </summary>
|
||||
protected override List<string> GetStrategySettingsLines()
|
||||
{
|
||||
var lines = base.GetStrategySettingsLines();
|
||||
|
||||
// Insert ORB section before the final === END SETTINGS === line
|
||||
int endIdx = lines.Count - 1;
|
||||
lines.Insert(endIdx, "--- ORB Strategy ---");
|
||||
lines.Insert(endIdx + 1, string.Format("OpeningRangeMinutes: {0}", OpeningRangeMinutes));
|
||||
lines.Insert(endIdx + 2, string.Format("StdDevMultiplier : {0:F2}", StdDevMultiplier));
|
||||
lines.Insert(endIdx + 3, string.Format("StopTicks : {0}", StopTicks));
|
||||
lines.Insert(endIdx + 4, string.Format("TargetTicks : {0}", TargetTicks));
|
||||
lines.Insert(endIdx + 5, string.Format("MinTradeGrade : {0}", MinTradeGrade));
|
||||
|
||||
double tickDollarValue = 0.25 * 50.0;
|
||||
if (Instrument != null && Instrument.MasterInstrument != null)
|
||||
tickDollarValue = Instrument.MasterInstrument.TickSize * Instrument.MasterInstrument.PointValue;
|
||||
|
||||
lines.Insert(endIdx + 6, string.Format("StopDollars : {0:C}", StopTicks * tickDollarValue));
|
||||
lines.Insert(endIdx + 7, string.Format("TargetDollars : {0:C}", TargetTicks * tickDollarValue));
|
||||
lines.Insert(endIdx + 8, string.Format("RR_Ratio : {0:F2}:1", (double)TargetTicks / StopTicks));
|
||||
lines.Insert(endIdx + 9, String.Format("AutoBreakeven : {0} @ {1}ticks + {2}tick offset",
|
||||
EnableAutoBreakeven, BreakevenTriggerTicks, BreakevenOffsetTicks));
|
||||
lines.Insert(endIdx + 10, String.Format("Runner : {0} | Trail={1}ticks",
|
||||
EnableRunner, RunnerTrailTicks));
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a DailyBarContext from the secondary daily bar series.
|
||||
/// Returns a context with Count=0 if fewer than 2 daily bars are available.
|
||||
/// </summary>
|
||||
/// <param name="tradeDirection">1 for long, -1 for short.</param>
|
||||
/// <param name="orbRangeTicks">ORB range in ticks for ORB range factor.</param>
|
||||
/// <param name="breakoutBarVolume">Volume of the current breakout bar.</param>
|
||||
/// <returns>Populated daily context for confluence scoring.</returns>
|
||||
private DailyBarContext BuildDailyBarContext(int tradeDirection, double orbRangeTicks, double breakoutBarVolume)
|
||||
{
|
||||
DailyBarContext ctx = new DailyBarContext();
|
||||
ctx.TradeDirection = tradeDirection;
|
||||
ctx.BreakoutBarVolume = breakoutBarVolume;
|
||||
ctx.TodayOpen = Open[0];
|
||||
|
||||
if (BarsArray == null || BarsArray.Length < 2 || CurrentBars == null || CurrentBars.Length < 2)
|
||||
{
|
||||
ctx.Count = 0;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
int dailyBarsAvailable = CurrentBars[1] + 1;
|
||||
int lookback = Math.Min(10, dailyBarsAvailable);
|
||||
|
||||
if (lookback < 2)
|
||||
{
|
||||
ctx.Count = 0;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
ctx.Highs = new double[lookback];
|
||||
ctx.Lows = new double[lookback];
|
||||
ctx.Closes = new double[lookback];
|
||||
ctx.Opens = new double[lookback];
|
||||
ctx.Volumes = new long[lookback];
|
||||
ctx.Count = lookback;
|
||||
|
||||
for (int i = 0; i < lookback; i++)
|
||||
{
|
||||
int barsAgo = lookback - 1 - i;
|
||||
ctx.Highs[i] = Highs[1][barsAgo];
|
||||
ctx.Lows[i] = Lows[1][barsAgo];
|
||||
ctx.Closes[i] = Closes[1][barsAgo];
|
||||
ctx.Opens[i] = Opens[1][barsAgo];
|
||||
ctx.Volumes[i] = (long)Volumes[1][barsAgo];
|
||||
}
|
||||
|
||||
double sumVol = 0.0;
|
||||
int intradayCount = 0;
|
||||
int maxBars = Math.Min(78, CurrentBar + 1);
|
||||
for (int i = 0; i < maxBars; i++)
|
||||
{
|
||||
sumVol += Volume[i];
|
||||
intradayCount++;
|
||||
}
|
||||
|
||||
ctx.AvgIntradayBarVolume = intradayCount > 0 ? sumVol / intradayCount : Volume[0];
|
||||
return ctx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
// Removed - replaced with PlaceholderContract.cs
|
||||
@@ -43,6 +43,31 @@ namespace NT8.Core.Intelligence
|
||||
/// </summary>
|
||||
Risk = 6,
|
||||
|
||||
/// <summary>
|
||||
/// Narrow range contraction quality (NR4/NR7 concepts).
|
||||
/// </summary>
|
||||
NarrowRange = 7,
|
||||
|
||||
/// <summary>
|
||||
/// Opening range size relative to average daily ATR/range.
|
||||
/// </summary>
|
||||
OrbRangeVsAtr = 8,
|
||||
|
||||
/// <summary>
|
||||
/// Alignment between overnight gap direction and trade direction.
|
||||
/// </summary>
|
||||
GapDirectionAlignment = 9,
|
||||
|
||||
/// <summary>
|
||||
/// Breakout bar volume strength relative to intraday average volume.
|
||||
/// </summary>
|
||||
BreakoutVolumeStrength = 10,
|
||||
|
||||
/// <summary>
|
||||
/// Prior day close location strength in prior day range.
|
||||
/// </summary>
|
||||
PriorDayCloseStrength = 11,
|
||||
|
||||
/// <summary>
|
||||
/// Additional custom factor.
|
||||
/// </summary>
|
||||
|
||||
@@ -110,6 +110,11 @@ namespace NT8.Core.Intelligence
|
||||
_factorWeights.Add(FactorType.Volatility, 1.0);
|
||||
_factorWeights.Add(FactorType.Timing, 1.0);
|
||||
_factorWeights.Add(FactorType.ExecutionQuality, 1.0);
|
||||
_factorWeights.Add(FactorType.NarrowRange, 1.0);
|
||||
_factorWeights.Add(FactorType.OrbRangeVsAtr, 1.0);
|
||||
_factorWeights.Add(FactorType.GapDirectionAlignment, 1.0);
|
||||
_factorWeights.Add(FactorType.BreakoutVolumeStrength, 1.0);
|
||||
_factorWeights.Add(FactorType.PriorDayCloseStrength, 1.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NT8.Core.Common.Models;
|
||||
using NT8.Core.Logging;
|
||||
|
||||
namespace NT8.Core.Intelligence
|
||||
{
|
||||
@@ -398,4 +399,625 @@ namespace NT8.Core.Intelligence
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Daily bar data passed to ORB-specific factor calculators.
|
||||
/// Contains a lookback window of recent daily bars in chronological order,
|
||||
/// oldest first, with index [Count-1] being the most recent completed day.
|
||||
/// </summary>
|
||||
public struct DailyBarContext
|
||||
{
|
||||
/// <summary>Daily high prices, oldest first.</summary>
|
||||
public double[] Highs;
|
||||
|
||||
/// <summary>Daily low prices, oldest first.</summary>
|
||||
public double[] Lows;
|
||||
|
||||
/// <summary>Daily close prices, oldest first.</summary>
|
||||
public double[] Closes;
|
||||
|
||||
/// <summary>Daily open prices, oldest first.</summary>
|
||||
public double[] Opens;
|
||||
|
||||
/// <summary>Daily volume values, oldest first.</summary>
|
||||
public long[] Volumes;
|
||||
|
||||
/// <summary>Number of valid bars populated.</summary>
|
||||
public int Count;
|
||||
|
||||
/// <summary>Today's RTH open price.</summary>
|
||||
public double TodayOpen;
|
||||
|
||||
/// <summary>Volume of the breakout bar (current intraday bar).</summary>
|
||||
public double BreakoutBarVolume;
|
||||
|
||||
/// <summary>Average intraday volume per bar for today's session so far.</summary>
|
||||
public double AvgIntradayBarVolume;
|
||||
|
||||
/// <summary>Trade direction: 1 for long, -1 for short.</summary>
|
||||
public int TradeDirection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scores the setup based on narrow range day concepts.
|
||||
/// An NR7 (range is the narrowest of the last 7 days) scores highest,
|
||||
/// indicating volatility contraction and likely expansion on breakout.
|
||||
/// Requires at least 7 completed daily bars in DailyBarContext.
|
||||
/// </summary>
|
||||
public class NarrowRangeFactorCalculator : IFactorCalculator
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the NarrowRangeFactorCalculator class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
public NarrowRangeFactorCalculator(ILogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException("logger");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factor type identifier.
|
||||
/// </summary>
|
||||
public FactorType Type
|
||||
{
|
||||
get { return FactorType.NarrowRange; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates narrow range score. Expects DailyBarContext in
|
||||
/// intent.Metadata["daily_bars"]. Returns 0.3 if context is missing.
|
||||
/// </summary>
|
||||
/// <param name="intent">Current strategy intent.</param>
|
||||
/// <param name="context">Current strategy context.</param>
|
||||
/// <param name="bar">Current bar data.</param>
|
||||
/// <returns>Calculated confluence factor.</returns>
|
||||
public ConfluenceFactor Calculate(StrategyIntent intent, StrategyContext context, BarData bar)
|
||||
{
|
||||
double score = 0.3;
|
||||
string reason = "No daily bar context available";
|
||||
|
||||
if (intent != null && intent.Metadata != null && intent.Metadata.ContainsKey("daily_bars"))
|
||||
{
|
||||
DailyBarContext daily = (DailyBarContext)intent.Metadata["daily_bars"];
|
||||
|
||||
if (daily.Count >= 7 && daily.Highs != null && daily.Lows != null)
|
||||
{
|
||||
double todayRange = daily.Highs[daily.Count - 1] - daily.Lows[daily.Count - 1];
|
||||
|
||||
bool isNR4 = true;
|
||||
int start4 = daily.Count - 4;
|
||||
int end = daily.Count - 2;
|
||||
for (int i = start4; i <= end; i++)
|
||||
{
|
||||
double r = daily.Highs[i] - daily.Lows[i];
|
||||
if (todayRange >= r)
|
||||
{
|
||||
isNR4 = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool isNR7 = true;
|
||||
int start7 = daily.Count - 7;
|
||||
for (int i = start7; i <= end; i++)
|
||||
{
|
||||
double r = daily.Highs[i] - daily.Lows[i];
|
||||
if (todayRange >= r)
|
||||
{
|
||||
isNR7 = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isNR7)
|
||||
{
|
||||
score = 1.0;
|
||||
reason = "NR7: Narrowest range in 7 days — strong volatility contraction";
|
||||
}
|
||||
else if (isNR4)
|
||||
{
|
||||
score = 0.75;
|
||||
reason = "NR4: Narrowest range in 4 days — moderate volatility contraction";
|
||||
}
|
||||
else
|
||||
{
|
||||
double sumRanges = 0.0;
|
||||
int lookback = Math.Min(7, daily.Count - 1);
|
||||
int start = daily.Count - 1 - lookback;
|
||||
int finish = daily.Count - 2;
|
||||
for (int i = start; i <= finish; i++)
|
||||
sumRanges += daily.Highs[i] - daily.Lows[i];
|
||||
|
||||
double avgRange = lookback > 0 ? sumRanges / lookback : todayRange;
|
||||
double ratio = avgRange > 0.0 ? todayRange / avgRange : 1.0;
|
||||
|
||||
if (ratio <= 0.7)
|
||||
{
|
||||
score = 0.6;
|
||||
reason = "Range below 70% of avg — mild contraction";
|
||||
}
|
||||
else if (ratio <= 0.9)
|
||||
{
|
||||
score = 0.45;
|
||||
reason = "Range near avg — no significant contraction";
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.2;
|
||||
reason = "Range above avg — expansion day, low NR score";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = String.Format("Insufficient daily bars: {0} of 7 required", daily.Count);
|
||||
}
|
||||
}
|
||||
|
||||
return new ConfluenceFactor(
|
||||
FactorType.NarrowRange,
|
||||
"Narrow Range (NR4/NR7)",
|
||||
score,
|
||||
0.20,
|
||||
reason,
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scores the ORB range relative to average daily range.
|
||||
/// Prevents trading when the ORB has already consumed most of the
|
||||
/// day's expected range, leaving little room for continuation.
|
||||
/// </summary>
|
||||
public class OrbRangeVsAtrFactorCalculator : IFactorCalculator
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the OrbRangeVsAtrFactorCalculator class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
public OrbRangeVsAtrFactorCalculator(ILogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException("logger");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factor type identifier.
|
||||
/// </summary>
|
||||
public FactorType Type
|
||||
{
|
||||
get { return FactorType.OrbRangeVsAtr; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates ORB range vs ATR score. Expects DailyBarContext in
|
||||
/// intent.Metadata["daily_bars"] and double in intent.Metadata["orb_range_ticks"].
|
||||
/// </summary>
|
||||
/// <param name="intent">Current strategy intent.</param>
|
||||
/// <param name="context">Current strategy context.</param>
|
||||
/// <param name="bar">Current bar data.</param>
|
||||
/// <returns>Calculated confluence factor.</returns>
|
||||
public ConfluenceFactor Calculate(StrategyIntent intent, StrategyContext context, BarData bar)
|
||||
{
|
||||
double score = 0.5;
|
||||
string reason = "No daily bar context available";
|
||||
|
||||
if (intent != null && intent.Metadata != null &&
|
||||
intent.Metadata.ContainsKey("daily_bars") &&
|
||||
intent.Metadata.ContainsKey("orb_range_ticks"))
|
||||
{
|
||||
DailyBarContext daily = (DailyBarContext)intent.Metadata["daily_bars"];
|
||||
double orbRangeTicks = ToDouble(intent.Metadata["orb_range_ticks"], 0.0);
|
||||
|
||||
if (daily.Count >= 5 && daily.Highs != null && daily.Lows != null)
|
||||
{
|
||||
double sumAtr = 0.0;
|
||||
int lookback = Math.Min(10, daily.Count - 1);
|
||||
int start = daily.Count - 1 - lookback;
|
||||
int end = daily.Count - 2;
|
||||
|
||||
for (int i = start; i <= end; i++)
|
||||
sumAtr += daily.Highs[i] - daily.Lows[i];
|
||||
|
||||
double avgDailyRange = lookback > 0 ? sumAtr / lookback : 0.0;
|
||||
double orbRangePoints = orbRangeTicks / 4.0;
|
||||
double ratio = avgDailyRange > 0.0 ? orbRangePoints / avgDailyRange : 0.5;
|
||||
|
||||
if (ratio <= 0.20)
|
||||
{
|
||||
score = 1.0;
|
||||
reason = String.Format("ORB is {0:P0} of daily ATR — tight range, high expansion potential", ratio);
|
||||
}
|
||||
else if (ratio <= 0.35)
|
||||
{
|
||||
score = 0.80;
|
||||
reason = String.Format("ORB is {0:P0} of daily ATR — good room to run", ratio);
|
||||
}
|
||||
else if (ratio <= 0.50)
|
||||
{
|
||||
score = 0.60;
|
||||
reason = String.Format("ORB is {0:P0} of daily ATR — moderate room remaining", ratio);
|
||||
}
|
||||
else if (ratio <= 0.70)
|
||||
{
|
||||
score = 0.35;
|
||||
reason = String.Format("ORB is {0:P0} of daily ATR — limited room, caution", ratio);
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.10;
|
||||
reason = String.Format("ORB is {0:P0} of daily ATR — range nearly exhausted", ratio);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ConfluenceFactor(
|
||||
FactorType.OrbRangeVsAtr,
|
||||
"ORB Range vs ATR",
|
||||
score,
|
||||
0.15,
|
||||
reason,
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
|
||||
private static double ToDouble(object value, double defaultValue)
|
||||
{
|
||||
if (value == null)
|
||||
return defaultValue;
|
||||
|
||||
if (value is double)
|
||||
return (double)value;
|
||||
if (value is float)
|
||||
return (double)(float)value;
|
||||
if (value is int)
|
||||
return (double)(int)value;
|
||||
if (value is long)
|
||||
return (double)(long)value;
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scores alignment between today's overnight gap direction and the
|
||||
/// trade direction. A gap-and-go setup (gap up + long trade) scores
|
||||
/// highest. A gap fade setup penalizes the score.
|
||||
/// </summary>
|
||||
public class GapDirectionAlignmentCalculator : IFactorCalculator
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the GapDirectionAlignmentCalculator class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
public GapDirectionAlignmentCalculator(ILogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException("logger");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factor type identifier.
|
||||
/// </summary>
|
||||
public FactorType Type
|
||||
{
|
||||
get { return FactorType.GapDirectionAlignment; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates gap alignment score. Expects DailyBarContext in
|
||||
/// intent.Metadata["daily_bars"] with TodayOpen and TradeDirection populated.
|
||||
/// </summary>
|
||||
/// <param name="intent">Current strategy intent.</param>
|
||||
/// <param name="context">Current strategy context.</param>
|
||||
/// <param name="bar">Current bar data.</param>
|
||||
/// <returns>Calculated confluence factor.</returns>
|
||||
public ConfluenceFactor Calculate(StrategyIntent intent, StrategyContext context, BarData bar)
|
||||
{
|
||||
double score = 0.5;
|
||||
string reason = "No daily bar context available";
|
||||
|
||||
if (intent != null && intent.Metadata != null && intent.Metadata.ContainsKey("daily_bars"))
|
||||
{
|
||||
DailyBarContext daily = (DailyBarContext)intent.Metadata["daily_bars"];
|
||||
|
||||
if (daily.Count >= 2 && daily.Closes != null)
|
||||
{
|
||||
double prevClose = daily.Closes[daily.Count - 2];
|
||||
double todayOpen = daily.TodayOpen;
|
||||
double gapPoints = todayOpen - prevClose;
|
||||
int gapDirection = gapPoints > 0.25 ? 1 : (gapPoints < -0.25 ? -1 : 0);
|
||||
int tradeDir = daily.TradeDirection;
|
||||
|
||||
if (gapDirection == 0)
|
||||
{
|
||||
score = 0.55;
|
||||
reason = "Flat open — no gap bias, neutral score";
|
||||
}
|
||||
else if (gapDirection == tradeDir)
|
||||
{
|
||||
double gapSize = Math.Abs(gapPoints);
|
||||
if (gapSize >= 5.0)
|
||||
{
|
||||
score = 1.0;
|
||||
reason = String.Format("Large gap {0:+0.00;-0.00} aligns with trade — strong gap-and-go", gapPoints);
|
||||
}
|
||||
else if (gapSize >= 2.0)
|
||||
{
|
||||
score = 0.85;
|
||||
reason = String.Format("Moderate gap {0:+0.00;-0.00} aligns with trade", gapPoints);
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.65;
|
||||
reason = String.Format("Small gap {0:+0.00;-0.00} aligns with trade", gapPoints);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
double gapSize = Math.Abs(gapPoints);
|
||||
if (gapSize >= 5.0)
|
||||
{
|
||||
score = 0.10;
|
||||
reason = String.Format("Large gap {0:+0.00;-0.00} opposes trade — high fade risk", gapPoints);
|
||||
}
|
||||
else if (gapSize >= 2.0)
|
||||
{
|
||||
score = 0.25;
|
||||
reason = String.Format("Moderate gap {0:+0.00;-0.00} opposes trade", gapPoints);
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.40;
|
||||
reason = String.Format("Small gap {0:+0.00;-0.00} opposes trade — minor headwind", gapPoints);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ConfluenceFactor(
|
||||
FactorType.GapDirectionAlignment,
|
||||
"Gap Direction Alignment",
|
||||
score,
|
||||
0.15,
|
||||
reason,
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scores the volume of the breakout bar relative to the average
|
||||
/// volume of bars seen so far in today's session.
|
||||
/// A volume surge on the breakout bar strongly confirms the move.
|
||||
/// </summary>
|
||||
public class BreakoutVolumeStrengthCalculator : IFactorCalculator
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the BreakoutVolumeStrengthCalculator class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
public BreakoutVolumeStrengthCalculator(ILogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException("logger");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factor type identifier.
|
||||
/// </summary>
|
||||
public FactorType Type
|
||||
{
|
||||
get { return FactorType.BreakoutVolumeStrength; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates breakout volume score. Expects DailyBarContext in
|
||||
/// intent.Metadata["daily_bars"] with BreakoutBarVolume and
|
||||
/// AvgIntradayBarVolume populated.
|
||||
/// </summary>
|
||||
/// <param name="intent">Current strategy intent.</param>
|
||||
/// <param name="context">Current strategy context.</param>
|
||||
/// <param name="bar">Current bar data.</param>
|
||||
/// <returns>Calculated confluence factor.</returns>
|
||||
public ConfluenceFactor Calculate(StrategyIntent intent, StrategyContext context, BarData bar)
|
||||
{
|
||||
double score = 0.4;
|
||||
string reason = "No daily bar context available";
|
||||
|
||||
if (intent != null && intent.Metadata != null && intent.Metadata.ContainsKey("daily_bars"))
|
||||
{
|
||||
DailyBarContext daily = (DailyBarContext)intent.Metadata["daily_bars"];
|
||||
double breakoutVol = daily.BreakoutBarVolume;
|
||||
double avgVol = daily.AvgIntradayBarVolume;
|
||||
|
||||
if (avgVol > 0.0)
|
||||
{
|
||||
double ratio = breakoutVol / avgVol;
|
||||
|
||||
if (ratio >= 3.0)
|
||||
{
|
||||
score = 1.0;
|
||||
reason = String.Format("Breakout volume {0:F1}x avg — exceptional surge", ratio);
|
||||
}
|
||||
else if (ratio >= 2.0)
|
||||
{
|
||||
score = 0.85;
|
||||
reason = String.Format("Breakout volume {0:F1}x avg — strong confirmation", ratio);
|
||||
}
|
||||
else if (ratio >= 1.5)
|
||||
{
|
||||
score = 0.70;
|
||||
reason = String.Format("Breakout volume {0:F1}x avg — solid confirmation", ratio);
|
||||
}
|
||||
else if (ratio >= 1.0)
|
||||
{
|
||||
score = 0.50;
|
||||
reason = String.Format("Breakout volume {0:F1}x avg — average, neutral", ratio);
|
||||
}
|
||||
else if (ratio >= 0.7)
|
||||
{
|
||||
score = 0.25;
|
||||
reason = String.Format("Breakout volume {0:F1}x avg — below avg, low conviction", ratio);
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.10;
|
||||
reason = String.Format("Breakout volume {0:F1}x avg — weak breakout, high false-break risk", ratio);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = "Avg intraday volume not available";
|
||||
}
|
||||
}
|
||||
|
||||
return new ConfluenceFactor(
|
||||
FactorType.BreakoutVolumeStrength,
|
||||
"Breakout Volume Strength",
|
||||
score,
|
||||
0.20,
|
||||
reason,
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scores where the prior day closed within its own range.
|
||||
/// A strong prior close (top 25% for longs, bottom 25% for shorts)
|
||||
/// indicates momentum continuation into today's session.
|
||||
/// </summary>
|
||||
public class PriorDayCloseStrengthCalculator : IFactorCalculator
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the PriorDayCloseStrengthCalculator class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
public PriorDayCloseStrengthCalculator(ILogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException("logger");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factor type identifier.
|
||||
/// </summary>
|
||||
public FactorType Type
|
||||
{
|
||||
get { return FactorType.PriorDayCloseStrength; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates prior close strength score. Expects DailyBarContext in
|
||||
/// intent.Metadata["daily_bars"] with at least 2 completed bars and
|
||||
/// TradeDirection populated.
|
||||
/// </summary>
|
||||
/// <param name="intent">Current strategy intent.</param>
|
||||
/// <param name="context">Current strategy context.</param>
|
||||
/// <param name="bar">Current bar data.</param>
|
||||
/// <returns>Calculated confluence factor.</returns>
|
||||
public ConfluenceFactor Calculate(StrategyIntent intent, StrategyContext context, BarData bar)
|
||||
{
|
||||
double score = 0.5;
|
||||
string reason = "No daily bar context available";
|
||||
|
||||
if (intent != null && intent.Metadata != null && intent.Metadata.ContainsKey("daily_bars"))
|
||||
{
|
||||
DailyBarContext daily = (DailyBarContext)intent.Metadata["daily_bars"];
|
||||
|
||||
if (daily.Count >= 2 && daily.Highs != null && daily.Lows != null && daily.Closes != null)
|
||||
{
|
||||
int prev = daily.Count - 2;
|
||||
double prevHigh = daily.Highs[prev];
|
||||
double prevLow = daily.Lows[prev];
|
||||
double prevClose = daily.Closes[prev];
|
||||
double prevRange = prevHigh - prevLow;
|
||||
int tradeDir = daily.TradeDirection;
|
||||
|
||||
if (prevRange > 0.0)
|
||||
{
|
||||
double closePosition = (prevClose - prevLow) / prevRange;
|
||||
|
||||
if (tradeDir == 1)
|
||||
{
|
||||
if (closePosition >= 0.75)
|
||||
{
|
||||
score = 1.0;
|
||||
reason = String.Format("Prior close in top {0:P0} — strong bullish close", 1.0 - closePosition);
|
||||
}
|
||||
else if (closePosition >= 0.50)
|
||||
{
|
||||
score = 0.70;
|
||||
reason = "Prior close in upper half — moderate bullish bias";
|
||||
}
|
||||
else if (closePosition >= 0.25)
|
||||
{
|
||||
score = 0.40;
|
||||
reason = "Prior close in lower half — weak prior close for long";
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.15;
|
||||
reason = "Prior close near low — bearish close, headwind for long";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (closePosition <= 0.25)
|
||||
{
|
||||
score = 1.0;
|
||||
reason = String.Format("Prior close in bottom {0:P0} — strong bearish close", closePosition);
|
||||
}
|
||||
else if (closePosition <= 0.50)
|
||||
{
|
||||
score = 0.70;
|
||||
reason = "Prior close in lower half — moderate bearish bias";
|
||||
}
|
||||
else if (closePosition <= 0.75)
|
||||
{
|
||||
score = 0.40;
|
||||
reason = "Prior close in upper half — weak prior close for short";
|
||||
}
|
||||
else
|
||||
{
|
||||
score = 0.15;
|
||||
reason = "Prior close near high — bullish close, headwind for short";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = "Prior day range is zero — cannot score";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ConfluenceFactor(
|
||||
FactorType.PriorDayCloseStrength,
|
||||
"Prior Day Close Strength",
|
||||
score,
|
||||
0.15,
|
||||
reason,
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,8 +109,8 @@ namespace NT8.Core.Intelligence
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
_minimumGradeByMode.Add(RiskMode.ECP, TradeGrade.B);
|
||||
_minimumGradeByMode.Add(RiskMode.PCP, TradeGrade.C);
|
||||
_minimumGradeByMode.Add(RiskMode.ECP, TradeGrade.C);
|
||||
_minimumGradeByMode.Add(RiskMode.PCP, TradeGrade.B);
|
||||
_minimumGradeByMode.Add(RiskMode.DCP, TradeGrade.A);
|
||||
_minimumGradeByMode.Add(RiskMode.HR, TradeGrade.APlus);
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace NT8.Core.Risk
|
||||
private double _dailyPnL;
|
||||
private double _maxDrawdown;
|
||||
private bool _tradingHalted;
|
||||
private double _configuredDailyLossLimit;
|
||||
private DateTime _lastUpdate = DateTime.UtcNow;
|
||||
private readonly Dictionary<string, double> _symbolExposure = new Dictionary<string, double>();
|
||||
|
||||
@@ -27,6 +28,15 @@ namespace NT8.Core.Risk
|
||||
{
|
||||
if (logger == null) throw new ArgumentNullException("logger");
|
||||
_logger = logger;
|
||||
_configuredDailyLossLimit = 1000.0;
|
||||
}
|
||||
|
||||
public BasicRiskManager(ILogger logger, double dailyLossLimit)
|
||||
{
|
||||
if (logger == null) throw new ArgumentNullException("logger");
|
||||
if (dailyLossLimit <= 0.0) throw new ArgumentException("dailyLossLimit must be positive", "dailyLossLimit");
|
||||
_logger = logger;
|
||||
_configuredDailyLossLimit = dailyLossLimit;
|
||||
}
|
||||
|
||||
public RiskDecision ValidateOrder(StrategyIntent intent, StrategyContext context, RiskConfig config)
|
||||
@@ -216,10 +226,8 @@ namespace NT8.Core.Risk
|
||||
|
||||
private void CheckEmergencyConditions(double dayPnL)
|
||||
{
|
||||
// Emergency halt if daily loss exceeds 90% of limit
|
||||
// Using a default limit of 1000 as this method doesn't have access to config
|
||||
// In Phase 1, this should be improved to use the actual config value
|
||||
if (dayPnL <= -(1000 * 0.9) && !_tradingHalted)
|
||||
// Emergency halt if daily loss exceeds 90% of configured limit
|
||||
if (dayPnL <= -(_configuredDailyLossLimit * 0.9) && !_tradingHalted)
|
||||
{
|
||||
_tradingHalted = true;
|
||||
_logger.LogCritical("Emergency halt triggered at 90% of daily loss limit: {0:C}", dayPnL);
|
||||
|
||||
276
src/NT8.Core/Risk/PortfolioRiskManager.cs
Normal file
276
src/NT8.Core/Risk/PortfolioRiskManager.cs
Normal file
@@ -0,0 +1,276 @@
|
||||
// File: PortfolioRiskManager.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NT8.Core.Common.Models;
|
||||
using NT8.Core.Logging;
|
||||
|
||||
namespace NT8.Core.Risk
|
||||
{
|
||||
/// <summary>
|
||||
/// Portfolio-level risk coordinator. Singleton. Enforces cross-strategy
|
||||
/// daily loss limits, maximum open contract caps, and a portfolio kill switch.
|
||||
/// Must be registered by each strategy on init and unregistered on terminate.
|
||||
/// Thread-safe via a single lock object.
|
||||
/// </summary>
|
||||
public class PortfolioRiskManager
|
||||
{
|
||||
private static readonly object _instanceLock = new object();
|
||||
private static PortfolioRiskManager _instance;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the singleton instance of PortfolioRiskManager.
|
||||
/// </summary>
|
||||
public static PortfolioRiskManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
lock (_instanceLock)
|
||||
{
|
||||
if (_instance == null)
|
||||
_instance = new PortfolioRiskManager();
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly object _lock = new object();
|
||||
private readonly Dictionary<string, RiskConfig> _registeredStrategies;
|
||||
private readonly Dictionary<string, double> _strategyPnL;
|
||||
private readonly Dictionary<string, int> _strategyOpenContracts;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum combined daily loss across all registered strategies before all trading halts.
|
||||
/// Default: 2000.0
|
||||
/// </summary>
|
||||
public double PortfolioDailyLossLimit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum total open contracts across all registered strategies simultaneously.
|
||||
/// Default: 6
|
||||
/// </summary>
|
||||
public int MaxTotalOpenContracts { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// When true, all new orders across all strategies are blocked immediately.
|
||||
/// Set to true to perform an emergency halt of the entire portfolio.
|
||||
/// </summary>
|
||||
public bool PortfolioKillSwitch { get; set; }
|
||||
|
||||
private PortfolioRiskManager()
|
||||
{
|
||||
_registeredStrategies = new Dictionary<string, RiskConfig>();
|
||||
_strategyPnL = new Dictionary<string, double>();
|
||||
_strategyOpenContracts = new Dictionary<string, int>();
|
||||
PortfolioDailyLossLimit = 2000.0;
|
||||
MaxTotalOpenContracts = 6;
|
||||
PortfolioKillSwitch = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a strategy with the portfolio manager. Called from
|
||||
/// NT8StrategyBase.InitializeSdkComponents() during State.DataLoaded.
|
||||
/// </summary>
|
||||
/// <param name="strategyId">Unique strategy identifier (use Name from NT8StrategyBase).</param>
|
||||
/// <param name="config">The strategy's risk configuration.</param>
|
||||
/// <exception cref="ArgumentNullException">strategyId or config is null.</exception>
|
||||
public void RegisterStrategy(string strategyId, RiskConfig config)
|
||||
{
|
||||
if (string.IsNullOrEmpty(strategyId)) throw new ArgumentNullException("strategyId");
|
||||
if (config == null) throw new ArgumentNullException("config");
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_registeredStrategies[strategyId] = config;
|
||||
if (!_strategyPnL.ContainsKey(strategyId))
|
||||
_strategyPnL[strategyId] = 0.0;
|
||||
if (!_strategyOpenContracts.ContainsKey(strategyId))
|
||||
_strategyOpenContracts[strategyId] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a strategy. Called from NT8StrategyBase during State.Terminated.
|
||||
/// </summary>
|
||||
/// <param name="strategyId">Strategy identifier to unregister.</param>
|
||||
public void UnregisterStrategy(string strategyId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(strategyId)) return;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_registeredStrategies.Remove(strategyId);
|
||||
_strategyPnL.Remove(strategyId);
|
||||
_strategyOpenContracts.Remove(strategyId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates a new order intent against portfolio-level risk limits.
|
||||
/// Called before per-strategy risk validation in ProcessStrategyIntent().
|
||||
/// </summary>
|
||||
/// <param name="strategyId">The strategy requesting the order.</param>
|
||||
/// <param name="intent">The trade intent to validate.</param>
|
||||
/// <returns>RiskDecision indicating whether the order is allowed.</returns>
|
||||
public RiskDecision ValidatePortfolioRisk(string strategyId, StrategyIntent intent)
|
||||
{
|
||||
if (string.IsNullOrEmpty(strategyId)) throw new ArgumentNullException("strategyId");
|
||||
if (intent == null) throw new ArgumentNullException("intent");
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
// Kill switch — blocks everything immediately
|
||||
if (PortfolioKillSwitch)
|
||||
{
|
||||
var ksMetrics = new Dictionary<string, object>();
|
||||
ksMetrics.Add("kill_switch", true);
|
||||
return new RiskDecision(
|
||||
allow: false,
|
||||
rejectReason: "Portfolio kill switch is active — all trading halted",
|
||||
modifiedIntent: null,
|
||||
riskLevel: RiskLevel.Critical,
|
||||
riskMetrics: ksMetrics);
|
||||
}
|
||||
|
||||
// Portfolio daily loss limit
|
||||
double totalPnL = 0.0;
|
||||
foreach (var kvp in _strategyPnL)
|
||||
totalPnL += kvp.Value;
|
||||
|
||||
if (totalPnL <= -PortfolioDailyLossLimit)
|
||||
{
|
||||
var pnlMetrics = new Dictionary<string, object>();
|
||||
pnlMetrics.Add("portfolio_pnl", totalPnL);
|
||||
pnlMetrics.Add("limit", PortfolioDailyLossLimit);
|
||||
return new RiskDecision(
|
||||
allow: false,
|
||||
rejectReason: String.Format(
|
||||
"Portfolio daily loss limit breached: {0:C} <= -{1:C}",
|
||||
totalPnL, PortfolioDailyLossLimit),
|
||||
modifiedIntent: null,
|
||||
riskLevel: RiskLevel.Critical,
|
||||
riskMetrics: pnlMetrics);
|
||||
}
|
||||
|
||||
// Total open contract cap
|
||||
int totalContracts = 0;
|
||||
foreach (var kvp in _strategyOpenContracts)
|
||||
totalContracts += kvp.Value;
|
||||
|
||||
if (totalContracts >= MaxTotalOpenContracts)
|
||||
{
|
||||
var contractMetrics = new Dictionary<string, object>();
|
||||
contractMetrics.Add("total_contracts", totalContracts);
|
||||
contractMetrics.Add("limit", MaxTotalOpenContracts);
|
||||
return new RiskDecision(
|
||||
allow: false,
|
||||
rejectReason: String.Format(
|
||||
"Portfolio contract cap reached: {0} >= {1}",
|
||||
totalContracts, MaxTotalOpenContracts),
|
||||
modifiedIntent: null,
|
||||
riskLevel: RiskLevel.High,
|
||||
riskMetrics: contractMetrics);
|
||||
}
|
||||
|
||||
// All portfolio checks passed
|
||||
var okMetrics = new Dictionary<string, object>();
|
||||
okMetrics.Add("portfolio_pnl", totalPnL);
|
||||
okMetrics.Add("total_contracts", totalContracts);
|
||||
return new RiskDecision(
|
||||
allow: true,
|
||||
rejectReason: null,
|
||||
modifiedIntent: null,
|
||||
riskLevel: RiskLevel.Low,
|
||||
riskMetrics: okMetrics);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports a fill to the portfolio manager.
|
||||
/// Contract tracking is handled by UpdateOpenContracts().
|
||||
/// </summary>
|
||||
/// <param name="strategyId">Strategy that received the fill.</param>
|
||||
/// <param name="fill">Fill details.</param>
|
||||
public void ReportFill(string strategyId, OrderFill fill)
|
||||
{
|
||||
// Contract tracking is now handled by UpdateOpenContracts() called
|
||||
// from OnBarUpdate with the actual position size.
|
||||
// This method is retained for API compatibility and future P&L attribution use.
|
||||
if (string.IsNullOrEmpty(strategyId) || fill == null) return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the open contract count for a strategy to the actual current
|
||||
/// position size. Called from NT8StrategyBase on each bar close.
|
||||
/// This replaces fill-based inference with authoritative position data.
|
||||
/// </summary>
|
||||
/// <param name="strategyId">Strategy identifier.</param>
|
||||
/// <param name="openContracts">Actual number of open contracts (0 when flat).</param>
|
||||
public void UpdateOpenContracts(string strategyId, int openContracts)
|
||||
{
|
||||
if (string.IsNullOrEmpty(strategyId)) return;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (openContracts < 0) openContracts = 0;
|
||||
_strategyOpenContracts[strategyId] = openContracts;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports a P&L update for a strategy. Called from NT8StrategyBase
|
||||
/// whenever the strategy's realized P&L changes (typically on position close).
|
||||
/// </summary>
|
||||
/// <param name="strategyId">Strategy reporting P&L.</param>
|
||||
/// <param name="pnl">Current cumulative day P&L for this strategy.</param>
|
||||
public void ReportPnL(string strategyId, double pnl)
|
||||
{
|
||||
if (string.IsNullOrEmpty(strategyId)) return;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_strategyPnL[strategyId] = pnl;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets daily P&L accumulators for all strategies. Does not clear registrations
|
||||
/// or open contract counts. Typically called at the start of a new trading day.
|
||||
/// </summary>
|
||||
public void ResetDaily()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var keys = new List<string>(_strategyPnL.Keys);
|
||||
foreach (var key in keys)
|
||||
_strategyPnL[key] = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a snapshot of current portfolio state for diagnostics.
|
||||
/// </summary>
|
||||
public string GetStatusSnapshot()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
double totalPnL = 0.0;
|
||||
foreach (var kvp in _strategyPnL)
|
||||
totalPnL += kvp.Value;
|
||||
|
||||
int totalContracts = 0;
|
||||
foreach (var kvp in _strategyOpenContracts)
|
||||
totalContracts += kvp.Value;
|
||||
|
||||
return String.Format(
|
||||
"Portfolio: strategies={0} totalPnL={1:C} totalContracts={2} killSwitch={3}",
|
||||
_registeredStrategies.Count,
|
||||
totalPnL,
|
||||
totalContracts,
|
||||
PortfolioKillSwitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
// Removed - replaced with PlaceholderStrategy.cs
|
||||
@@ -112,6 +112,11 @@ namespace NT8.Strategies.Examples
|
||||
_factorCalculators.Add(new VolatilityRegimeFactorCalculator());
|
||||
_factorCalculators.Add(new TimeInSessionFactorCalculator());
|
||||
_factorCalculators.Add(new ExecutionQualityFactorCalculator());
|
||||
_factorCalculators.Add(new NarrowRangeFactorCalculator(_logger));
|
||||
_factorCalculators.Add(new OrbRangeVsAtrFactorCalculator(_logger));
|
||||
_factorCalculators.Add(new GapDirectionAlignmentCalculator(_logger));
|
||||
_factorCalculators.Add(new BreakoutVolumeStrengthCalculator(_logger));
|
||||
_factorCalculators.Add(new PriorDayCloseStrengthCalculator(_logger));
|
||||
|
||||
_logger.LogInformation(
|
||||
"SimpleORBStrategy initialized with OR period {0} minutes and multiplier {1:F2}",
|
||||
@@ -148,9 +153,25 @@ namespace NT8.Strategies.Examples
|
||||
UpdateRiskMode(context);
|
||||
UpdateConfluenceInputs(bar, context);
|
||||
|
||||
if (_currentSessionDate != context.CurrentTime.Date)
|
||||
DateTime thisSessionStart = context.Session != null
|
||||
? context.Session.SessionStart
|
||||
: context.CurrentTime.Date.AddHours(9.5);
|
||||
|
||||
// Guard: only reset when the TRADING DATE has changed, not just the session
|
||||
// start timestamp. Using trading date prevents mid-session resets caused by
|
||||
// _openingRangeStart holding a stale value from a previous day.
|
||||
DateTime thisTradingDate = thisSessionStart.Date;
|
||||
|
||||
TimeSpan sessionStartTime = thisSessionStart.TimeOfDay;
|
||||
bool isValidRthSessionStart = sessionStartTime >= new TimeSpan(8, 0, 0)
|
||||
&& sessionStartTime <= new TimeSpan(10, 30, 0);
|
||||
|
||||
if (isValidRthSessionStart && thisTradingDate != _currentSessionDate)
|
||||
{
|
||||
ResetSession(context.Session != null ? context.Session.SessionStart : context.CurrentTime.Date);
|
||||
ResetSession(thisSessionStart);
|
||||
|
||||
if (context.CustomData != null && context.CustomData.ContainsKey("session_open_price"))
|
||||
context.CustomData.Remove("session_open_price");
|
||||
}
|
||||
|
||||
// Only trade during RTH
|
||||
@@ -171,8 +192,26 @@ namespace NT8.Strategies.Examples
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_logger != null && _openingRangeReady)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"ORB ready: High={0:F2} Low={1:F2} Range={2:F2} TradeTaken={3}",
|
||||
_openingRangeHigh,
|
||||
_openingRangeLow,
|
||||
_openingRangeHigh - _openingRangeLow,
|
||||
_tradeTaken);
|
||||
}
|
||||
|
||||
if (_tradeTaken)
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogDebug(
|
||||
"SimpleORBStrategy skip: trade already taken for session {0:yyyy-MM-dd}; bar={1:yyyy-MM-dd HH:mm}; symbol={2}",
|
||||
_currentSessionDate,
|
||||
bar.Time,
|
||||
context.Symbol);
|
||||
return null;
|
||||
}
|
||||
|
||||
var openingRange = _openingRangeHigh - _openingRangeLow;
|
||||
var volatilityBuffer = openingRange * (_stdDevMultiplier - 1.0);
|
||||
@@ -191,18 +230,51 @@ namespace NT8.Strategies.Examples
|
||||
if (candidate == null)
|
||||
return null;
|
||||
|
||||
AttachDailyBarContext(candidate, bar, context);
|
||||
|
||||
// Hard veto: Trend against trade direction is a disqualifying condition.
|
||||
// AVWAP trend alignment is checked before confluence scoring to prevent
|
||||
// high scores from other factors masking a directionally broken setup.
|
||||
if (_config != null && _config.Parameters != null)
|
||||
{
|
||||
if (context.CustomData != null && context.CustomData.ContainsKey("avwap_slope"))
|
||||
{
|
||||
var slope = context.CustomData["avwap_slope"];
|
||||
if (slope is double)
|
||||
{
|
||||
double slopeVal = (double)slope;
|
||||
bool longTrade = candidate.Side == OrderSide.Buy;
|
||||
bool trendAgainst = (longTrade && slopeVal < 0) || (!longTrade && slopeVal > 0);
|
||||
if (trendAgainst)
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogInformation("Trade vetoed: AVWAP slope {0:F4} against {1} direction", slopeVal, candidate.Side);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var score = _scorer.CalculateScore(candidate, context, bar, _factorCalculators);
|
||||
var mode = _riskModeManager.GetCurrentMode();
|
||||
|
||||
if (!_gradeFilter.ShouldAcceptTrade(score.Grade, mode))
|
||||
int minGradeValue = 5;
|
||||
if (_config != null && _config.Parameters != null && _config.Parameters.ContainsKey("MinTradeGrade"))
|
||||
{
|
||||
var reason = _gradeFilter.GetRejectionReason(score.Grade, mode);
|
||||
var mgv = _config.Parameters["MinTradeGrade"];
|
||||
if (mgv is int)
|
||||
minGradeValue = (int)mgv;
|
||||
}
|
||||
|
||||
TradeGrade minGrade = (TradeGrade)minGradeValue;
|
||||
if ((int)score.Grade < (int)minGrade)
|
||||
{
|
||||
if (_logger != null)
|
||||
_logger.LogInformation(
|
||||
"SimpleORBStrategy rejected intent for {0}: Grade={1}, Mode={2}, Reason={3}",
|
||||
candidate.Symbol,
|
||||
"SimpleORBStrategy filtered by grade: Score={0:F3} Grade={1} MinGrade={2}",
|
||||
score.WeightedScore,
|
||||
score.Grade,
|
||||
mode,
|
||||
reason);
|
||||
minGrade);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -212,7 +284,8 @@ namespace NT8.Strategies.Examples
|
||||
|
||||
candidate.Confidence = score.WeightedScore;
|
||||
candidate.Reason = string.Format("{0}; grade={1}; mode={2}", candidate.Reason, score.Grade, mode);
|
||||
candidate.Metadata["confluence_score"] = score.WeightedScore;
|
||||
candidate.Metadata["confluence_score"] = score;
|
||||
candidate.Metadata["confluence_weighted_score"] = score.WeightedScore;
|
||||
candidate.Metadata["trade_grade"] = score.Grade.ToString();
|
||||
candidate.Metadata["risk_mode"] = mode.ToString();
|
||||
candidate.Metadata["grade_multiplier"] = gradeMultiplier;
|
||||
@@ -221,6 +294,24 @@ namespace NT8.Strategies.Examples
|
||||
|
||||
_tradeTaken = true;
|
||||
|
||||
if (_logger != null)
|
||||
_logger.LogDebug(
|
||||
"SimpleORBStrategy flag set: tradeTaken={0} session={1:yyyy-MM-dd}; bar={2:yyyy-MM-dd HH:mm}; side={3}; symbol={4}",
|
||||
_tradeTaken,
|
||||
_currentSessionDate,
|
||||
bar.Time,
|
||||
candidate.Side,
|
||||
candidate.Symbol);
|
||||
|
||||
if (_logger != null && score.Factors != null)
|
||||
{
|
||||
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
||||
sb.Append("Factors: ");
|
||||
foreach (ConfluenceFactor f in score.Factors)
|
||||
sb.Append(string.Format("{0}={1:F2}({2}) ", f.Type, f.Score, f.Weight.ToString("F2")));
|
||||
_logger.LogInformation("Confluence detail: {0}", sb.ToString().TrimEnd());
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"SimpleORBStrategy accepted intent for {0}: Side={1}, Grade={2}, Mode={3}, Score={4:F3}, Mult={5:F2}",
|
||||
candidate.Symbol,
|
||||
@@ -273,7 +364,27 @@ namespace NT8.Strategies.Examples
|
||||
/// <param name="parameters">Parameter map.</param>
|
||||
public void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
// Constructor-bound parameters intentionally remain immutable for deterministic behavior.
|
||||
if (parameters == null)
|
||||
return;
|
||||
|
||||
// force_session_reset: clear _tradeTaken and ORB state so a fresh live session
|
||||
// can trade even if historical replay set _tradeTaken before going realtime.
|
||||
if (parameters.ContainsKey("force_session_reset"))
|
||||
{
|
||||
var val = parameters["force_session_reset"];
|
||||
if (val is bool && (bool)val)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_tradeTaken = false;
|
||||
_openingRangeReady = false;
|
||||
_openingRangeHigh = Double.MinValue;
|
||||
_openingRangeLow = Double.MaxValue;
|
||||
if (_logger != null)
|
||||
_logger.LogInformation("ForceSessionReset: _tradeTaken cleared, ORB state reset for live session");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureInitialized()
|
||||
@@ -299,21 +410,55 @@ namespace NT8.Strategies.Examples
|
||||
var avwap = _avwapCalculator.GetCurrentValue();
|
||||
var avwapSlope = _avwapCalculator.GetSlope(10);
|
||||
|
||||
var bars = new List<BarData>();
|
||||
bars.Add(bar);
|
||||
var valueArea = _volumeProfileAnalyzer.CalculateValueArea(bars);
|
||||
|
||||
if (context.CustomData == null)
|
||||
context.CustomData = new Dictionary<string, object>();
|
||||
|
||||
// Use pre-calculated intraday average from daily bar context when available.
|
||||
// Fall back to current bar volume only if daily context is not yet populated.
|
||||
double avgVol = (double)bar.Volume;
|
||||
double normalAtr = bar.High - bar.Low;
|
||||
|
||||
if (_config != null && _config.Parameters != null && _config.Parameters.ContainsKey("daily_bars"))
|
||||
{
|
||||
var src = _config.Parameters["daily_bars"];
|
||||
if (src is DailyBarContext)
|
||||
{
|
||||
DailyBarContext dc = (DailyBarContext)src;
|
||||
|
||||
if (dc.AvgIntradayBarVolume > 0.0)
|
||||
avgVol = dc.AvgIntradayBarVolume;
|
||||
|
||||
// Use 10-day average daily range as the volatility baseline.
|
||||
if (dc.Count >= 5 && dc.Highs != null && dc.Lows != null)
|
||||
{
|
||||
double sumRanges = 0.0;
|
||||
int lookback = Math.Min(10, dc.Count - 1);
|
||||
int start = dc.Count - 1 - lookback;
|
||||
int end = dc.Count - 2;
|
||||
for (int i = start; i <= end; i++)
|
||||
sumRanges += dc.Highs[i] - dc.Lows[i];
|
||||
|
||||
if (lookback > 0)
|
||||
normalAtr = sumRanges / lookback;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.CustomData["current_bar"] = bar;
|
||||
context.CustomData["avwap"] = avwap;
|
||||
context.CustomData["avwap_slope"] = avwapSlope;
|
||||
context.CustomData["trend_confirm"] = avwapSlope > 0.0 ? 1.0 : 0.0;
|
||||
context.CustomData["current_atr"] = Math.Max(0.01, bar.High - bar.Low);
|
||||
context.CustomData["normal_atr"] = Math.Max(0.01, valueArea.ValueAreaHigh - valueArea.ValueAreaLow);
|
||||
context.CustomData["normal_atr"] = Math.Max(0.01, normalAtr);
|
||||
context.CustomData["recent_execution_quality"] = 0.8;
|
||||
context.CustomData["avg_volume"] = (double)bar.Volume;
|
||||
context.CustomData["avg_volume"] = avgVol;
|
||||
|
||||
// Track the first bar open of the RTH session as the session open price.
|
||||
// Only set once per session (when session_open_price is not yet in custom data).
|
||||
if (!context.CustomData.ContainsKey("session_open_price"))
|
||||
{
|
||||
context.CustomData["session_open_price"] = bar.Open;
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetSession(DateTime sessionStart)
|
||||
@@ -325,6 +470,13 @@ namespace NT8.Strategies.Examples
|
||||
_openingRangeLow = Double.MaxValue;
|
||||
_openingRangeReady = false;
|
||||
_tradeTaken = false;
|
||||
|
||||
if (_logger != null)
|
||||
_logger.LogInformation(
|
||||
"Session reset: Date={0:yyyy-MM-dd} ORB window={1:HH:mm}-{2:HH:mm}",
|
||||
_currentSessionDate,
|
||||
_openingRangeStart,
|
||||
_openingRangeEnd);
|
||||
}
|
||||
|
||||
private void UpdateOpeningRange(BarData bar)
|
||||
@@ -341,7 +493,7 @@ namespace NT8.Strategies.Examples
|
||||
var stopTicks = _config != null && _config.Parameters.ContainsKey("StopTicks")
|
||||
? (int)_config.Parameters["StopTicks"]
|
||||
: 8;
|
||||
var targetTicks = _config != null && _config.Parameters.ContainsKey("TargetTicks")
|
||||
int baseTargetTicks = _config != null && _config.Parameters.ContainsKey("TargetTicks")
|
||||
? (int)_config.Parameters["TargetTicks"]
|
||||
: 16;
|
||||
|
||||
@@ -349,10 +501,87 @@ namespace NT8.Strategies.Examples
|
||||
metadata.Add("orb_high", _openingRangeHigh);
|
||||
metadata.Add("orb_low", _openingRangeLow);
|
||||
metadata.Add("orb_range", openingRange);
|
||||
|
||||
double tickSize = 0.25;
|
||||
if (_config != null && _config.Parameters != null && _config.Parameters.ContainsKey("TickSize"))
|
||||
{
|
||||
var tickValue = _config.Parameters["TickSize"];
|
||||
if (tickValue is double)
|
||||
tickSize = (double)tickValue;
|
||||
else if (tickValue is decimal)
|
||||
tickSize = (double)(decimal)tickValue;
|
||||
else if (tickValue is float)
|
||||
tickSize = (double)(float)tickValue;
|
||||
}
|
||||
|
||||
if (tickSize <= 0.0)
|
||||
tickSize = 0.25;
|
||||
|
||||
// Scale target dynamically based on ORB range vs average daily range.
|
||||
// Tight ORBs (< 20% of daily ATR) leave the most room — extend target.
|
||||
// Wide ORBs (> 50% of daily ATR) have consumed range — keep target tight.
|
||||
// Requires daily bar context with at least 5 bars; falls back to base target otherwise.
|
||||
int targetTicks = baseTargetTicks;
|
||||
if (_config != null && _config.Parameters != null && _config.Parameters.ContainsKey("daily_bars"))
|
||||
{
|
||||
var dailySrc = _config.Parameters["daily_bars"];
|
||||
if (dailySrc is DailyBarContext)
|
||||
{
|
||||
DailyBarContext dc = (DailyBarContext)dailySrc;
|
||||
if (dc.Count >= 5 && dc.Highs != null && dc.Lows != null && tickSize > 0.0)
|
||||
{
|
||||
double sumAtr = 0.0;
|
||||
int lookback = Math.Min(10, dc.Count - 1);
|
||||
int start = dc.Count - 1 - lookback;
|
||||
int end = dc.Count - 2;
|
||||
for (int i = start; i <= end; i++)
|
||||
sumAtr += dc.Highs[i] - dc.Lows[i];
|
||||
|
||||
double avgDailyRange = lookback > 0 ? sumAtr / lookback : 0.0;
|
||||
double orbRangePoints = openingRange;
|
||||
double ratio = avgDailyRange > 0.0 ? orbRangePoints / avgDailyRange : 0.5;
|
||||
|
||||
// Ratio tiers map to target multipliers:
|
||||
// <= 0.20 (tight ORB) → 1.75x (28 ticks on 16-tick base)
|
||||
// <= 0.30 → 1.50x (24 ticks)
|
||||
// <= 0.45 → 1.25x (20 ticks)
|
||||
// <= 0.60 → 1.00x (16 ticks — base, no change)
|
||||
// > 0.60 (wide ORB) → 0.75x (12 ticks — tighten)
|
||||
double multiplier;
|
||||
if (ratio <= 0.20)
|
||||
multiplier = 1.75;
|
||||
else if (ratio <= 0.30)
|
||||
multiplier = 1.50;
|
||||
else if (ratio <= 0.45)
|
||||
multiplier = 1.25;
|
||||
else if (ratio <= 0.60)
|
||||
multiplier = 1.00;
|
||||
else
|
||||
multiplier = 0.75;
|
||||
|
||||
targetTicks = (int)Math.Round(baseTargetTicks * multiplier);
|
||||
|
||||
// Enforce hard floor of stopTicks + 4 (minimum 1:1 R plus 4 ticks)
|
||||
int minTarget = stopTicks + 4;
|
||||
if (targetTicks < minTarget)
|
||||
targetTicks = minTarget;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_logger != null && targetTicks != baseTargetTicks)
|
||||
_logger.LogInformation(
|
||||
"Dynamic target: base={0} adjusted={1} (ORB/ATR scaling)",
|
||||
baseTargetTicks, targetTicks);
|
||||
|
||||
var orbRangeTicks = openingRange / tickSize;
|
||||
metadata.Add("orb_range_ticks", orbRangeTicks);
|
||||
metadata.Add("trigger_price", lastPrice);
|
||||
metadata.Add("multiplier", _stdDevMultiplier);
|
||||
metadata.Add("opening_range_start", _openingRangeStart);
|
||||
metadata.Add("opening_range_end", _openingRangeEnd);
|
||||
metadata.Add("base_target_ticks", baseTargetTicks);
|
||||
metadata.Add("dynamic_target_ticks", targetTicks);
|
||||
|
||||
return new StrategyIntent(
|
||||
symbol,
|
||||
@@ -365,5 +594,49 @@ namespace NT8.Strategies.Examples
|
||||
"ORB breakout signal",
|
||||
metadata);
|
||||
}
|
||||
|
||||
private void AttachDailyBarContext(StrategyIntent intent, BarData bar, StrategyContext context)
|
||||
{
|
||||
if (intent == null || intent.Metadata == null)
|
||||
return;
|
||||
|
||||
if (_config == null || _config.Parameters == null || !_config.Parameters.ContainsKey("daily_bars"))
|
||||
return;
|
||||
|
||||
var source = _config.Parameters["daily_bars"];
|
||||
if (!(source is DailyBarContext))
|
||||
return;
|
||||
|
||||
DailyBarContext baseContext = (DailyBarContext)source;
|
||||
DailyBarContext daily = baseContext;
|
||||
|
||||
daily.TradeDirection = intent.Side == OrderSide.Buy ? 1 : -1;
|
||||
daily.BreakoutBarVolume = (double)bar.Volume;
|
||||
|
||||
double todayOpen = bar.Open;
|
||||
if (context != null && context.CustomData != null && context.CustomData.ContainsKey("session_open_price"))
|
||||
{
|
||||
object sop = context.CustomData["session_open_price"];
|
||||
if (sop is double)
|
||||
todayOpen = (double)sop;
|
||||
}
|
||||
daily.TodayOpen = todayOpen;
|
||||
|
||||
if (context != null && context.CustomData != null && context.CustomData.ContainsKey("avg_volume"))
|
||||
{
|
||||
var avg = context.CustomData["avg_volume"];
|
||||
if (avg is double)
|
||||
daily.AvgIntradayBarVolume = (double)avg;
|
||||
else if (avg is float)
|
||||
daily.AvgIntradayBarVolume = (double)(float)avg;
|
||||
else if (avg is int)
|
||||
daily.AvgIntradayBarVolume = (double)(int)avg;
|
||||
else if (avg is long)
|
||||
daily.AvgIntradayBarVolume = (double)(long)avg;
|
||||
}
|
||||
|
||||
// orb_range_ticks is set in CreateIntent() and preserved here.
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
0
src/_archive/legacy-orders/.gitkeep
Normal file
0
src/_archive/legacy-orders/.gitkeep
Normal file
@@ -4,6 +4,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// ARCHIVED: This namespace (NT8.Core.Orders) is superseded by NT8.Core.OMS.
|
||||
// NT8.Core.OMS is the canonical order management implementation used by NT8StrategyBase.
|
||||
// These files are retained for reference only and are not referenced by any active code.
|
||||
// Do not add new code here. Do not remove these files until a full audit confirms zero references.
|
||||
|
||||
namespace NT8.Core.Orders
|
||||
{
|
||||
/// <summary>
|
||||
@@ -6,6 +6,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// ARCHIVED: This namespace (NT8.Core.Orders) is superseded by NT8.Core.OMS.
|
||||
// NT8.Core.OMS is the canonical order management implementation used by NT8StrategyBase.
|
||||
// These files are retained for reference only and are not referenced by any active code.
|
||||
// Do not add new code here. Do not remove these files until a full audit confirms zero references.
|
||||
|
||||
namespace NT8.Core.Orders
|
||||
{
|
||||
/// <summary>
|
||||
@@ -2,6 +2,11 @@ using NT8.Core.Common.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
// ARCHIVED: This namespace (NT8.Core.Orders) is superseded by NT8.Core.OMS.
|
||||
// NT8.Core.OMS is the canonical order management implementation used by NT8StrategyBase.
|
||||
// These files are retained for reference only and are not referenced by any active code.
|
||||
// Do not add new code here. Do not remove these files until a full audit confirms zero references.
|
||||
|
||||
namespace NT8.Core.Orders
|
||||
{
|
||||
#region Core Order Models
|
||||
0
src/_archive/placeholders/.gitkeep
Normal file
0
src/_archive/placeholders/.gitkeep
Normal file
299
tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs
Normal file
299
tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs
Normal file
@@ -0,0 +1,299 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using NT8.Core.Common.Models;
|
||||
using NT8.Core.Intelligence;
|
||||
using NT8.Core.Logging;
|
||||
|
||||
namespace NT8.Core.Tests.Intelligence
|
||||
{
|
||||
[TestClass]
|
||||
public class OrbConfluenceFactorTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void NarrowRange_NR7_ScoresOne()
|
||||
{
|
||||
var calc = new NarrowRangeFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
intent.Metadata["daily_bars"] = CreateDailyContext(new double[] { 10, 10, 10, 10, 10, 10, 5 });
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(1.0, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NarrowRange_NR4_Scores075()
|
||||
{
|
||||
var calc = new NarrowRangeFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
intent.Metadata["daily_bars"] = CreateDailyContext(new double[] { 5, 5, 5, 10, 9, 8, 7 });
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(0.75, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NarrowRange_WideRange_ScoresLow()
|
||||
{
|
||||
var calc = new NarrowRangeFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
intent.Metadata["daily_bars"] = CreateDailyContext(new double[] { 5, 5, 5, 5, 5, 5, 12 });
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.IsTrue(result.Score <= 0.3);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NarrowRange_MissingContext_DefaultsTo03()
|
||||
{
|
||||
var calc = new NarrowRangeFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(0.3, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NarrowRange_InsufficientBars_DefaultsTo03()
|
||||
{
|
||||
var calc = new NarrowRangeFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
intent.Metadata["daily_bars"] = CreateDailyContext(new double[] { 8, 7, 6, 5 });
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(0.3, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrbRangeVsAtr_SmallRange_ScoresOne()
|
||||
{
|
||||
var calc = new OrbRangeVsAtrFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 10, 10, 10, 10, 10, 10, 10 });
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
intent.Metadata["orb_range_ticks"] = 8.0;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(1.0, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrbRangeVsAtr_LargeRange_ScoresVeryLow()
|
||||
{
|
||||
var calc = new OrbRangeVsAtrFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 10, 10, 10, 10, 10, 10, 10 });
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
intent.Metadata["orb_range_ticks"] = 40.0;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.IsTrue(result.Score <= 0.15);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrbRangeVsAtr_MissingContext_DefaultsTo05()
|
||||
{
|
||||
var calc = new OrbRangeVsAtrFactorCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(0.5, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GapDirection_LargeAlignedGap_ScoresOne()
|
||||
{
|
||||
var calc = new GapDirectionAlignmentCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
daily.Closes[daily.Count - 2] = 100.0;
|
||||
daily.TodayOpen = 106.0;
|
||||
daily.TradeDirection = 1;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(1.0, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GapDirection_LargeOpposingGap_ScoresVeryLow()
|
||||
{
|
||||
var calc = new GapDirectionAlignmentCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
daily.Closes[daily.Count - 2] = 100.0;
|
||||
daily.TodayOpen = 106.0;
|
||||
daily.TradeDirection = -1;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.IsTrue(result.Score <= 0.15);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GapDirection_FlatOpen_ScoresNeutral()
|
||||
{
|
||||
var calc = new GapDirectionAlignmentCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
daily.Closes[daily.Count - 2] = 100.0;
|
||||
daily.TodayOpen = 100.1;
|
||||
daily.TradeDirection = 1;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(0.55, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void BreakoutVolume_ThreeX_ScoresOne()
|
||||
{
|
||||
var calc = new BreakoutVolumeStrengthCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
daily.BreakoutBarVolume = 3000.0;
|
||||
daily.AvgIntradayBarVolume = 1000.0;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(1.0, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void BreakoutVolume_BelowAverage_ScoresLow()
|
||||
{
|
||||
var calc = new BreakoutVolumeStrengthCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
daily.BreakoutBarVolume = 800.0;
|
||||
daily.AvgIntradayBarVolume = 1200.0;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.IsTrue(result.Score <= 0.25);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PriorCloseStrength_LongTopQuartile_ScoresOne()
|
||||
{
|
||||
var calc = new PriorDayCloseStrengthCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
int prev = daily.Count - 2;
|
||||
daily.Lows[prev] = 100.0;
|
||||
daily.Highs[prev] = 120.0;
|
||||
daily.Closes[prev] = 118.0;
|
||||
daily.TradeDirection = 1;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(1.0, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PriorCloseStrength_LongBottomQuartile_ScoresLow()
|
||||
{
|
||||
var calc = new PriorDayCloseStrengthCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
int prev = daily.Count - 2;
|
||||
daily.Lows[prev] = 100.0;
|
||||
daily.Highs[prev] = 120.0;
|
||||
daily.Closes[prev] = 101.0;
|
||||
daily.TradeDirection = 1;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.IsTrue(result.Score <= 0.20);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PriorCloseStrength_ShortBottomQuartile_ScoresOne()
|
||||
{
|
||||
var calc = new PriorDayCloseStrengthCalculator(new BasicLogger("test"));
|
||||
var intent = CreateIntent();
|
||||
var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 });
|
||||
int prev = daily.Count - 2;
|
||||
daily.Lows[prev] = 100.0;
|
||||
daily.Highs[prev] = 120.0;
|
||||
daily.Closes[prev] = 101.0;
|
||||
daily.TradeDirection = -1;
|
||||
intent.Metadata["daily_bars"] = daily;
|
||||
|
||||
var result = calc.Calculate(intent, CreateContext(), CreateBar());
|
||||
|
||||
Assert.AreEqual(1.0, result.Score, 0.000001);
|
||||
}
|
||||
|
||||
private static StrategyIntent CreateIntent()
|
||||
{
|
||||
return new StrategyIntent(
|
||||
"ES",
|
||||
OrderSide.Buy,
|
||||
OrderType.Market,
|
||||
null,
|
||||
8,
|
||||
16,
|
||||
0.8,
|
||||
"test",
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
|
||||
private static StrategyContext CreateContext()
|
||||
{
|
||||
return new StrategyContext(
|
||||
"ES",
|
||||
DateTime.UtcNow,
|
||||
new Position("ES", 0, 0, 0, 0, DateTime.UtcNow),
|
||||
new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow),
|
||||
new MarketSession(DateTime.Today.AddHours(9.5), DateTime.Today.AddHours(16), true, "RTH"),
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
|
||||
private static BarData CreateBar()
|
||||
{
|
||||
return new BarData("ES", DateTime.UtcNow, 5000, 5005, 4998, 5002, 1000, TimeSpan.FromMinutes(5));
|
||||
}
|
||||
|
||||
private static DailyBarContext CreateDailyContext(double[] ranges)
|
||||
{
|
||||
DailyBarContext context = new DailyBarContext();
|
||||
context.Count = ranges.Length;
|
||||
context.Highs = new double[ranges.Length];
|
||||
context.Lows = new double[ranges.Length];
|
||||
context.Closes = new double[ranges.Length];
|
||||
context.Opens = new double[ranges.Length];
|
||||
context.Volumes = new long[ranges.Length];
|
||||
|
||||
for (int i = 0; i < ranges.Length; i++)
|
||||
{
|
||||
context.Lows[i] = 100.0;
|
||||
context.Highs[i] = 100.0 + ranges[i];
|
||||
context.Opens[i] = 100.0 + (ranges[i] * 0.25);
|
||||
context.Closes[i] = 100.0 + (ranges[i] * 0.75);
|
||||
context.Volumes[i] = 100000;
|
||||
}
|
||||
|
||||
context.TodayOpen = context.Closes[Math.Max(0, context.Count - 2)] + 1.0;
|
||||
context.BreakoutBarVolume = 1000.0;
|
||||
context.AvgIntradayBarVolume = 1000.0;
|
||||
context.TradeDirection = 1;
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
134
tests/NT8.Core.Tests/Risk/PortfolioRiskManagerTests.cs
Normal file
134
tests/NT8.Core.Tests/Risk/PortfolioRiskManagerTests.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using NT8.Core.Common.Models;
|
||||
using NT8.Core.Risk;
|
||||
|
||||
namespace NT8.Core.Tests.Risk
|
||||
{
|
||||
[TestClass]
|
||||
public class PortfolioRiskManagerTests
|
||||
{
|
||||
private PortfolioRiskManager _manager;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
_manager = PortfolioRiskManager.Instance;
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void TestCleanup()
|
||||
{
|
||||
_manager.UnregisterStrategy("strat1");
|
||||
_manager.UnregisterStrategy("strat2");
|
||||
_manager.UnregisterStrategy("strat3");
|
||||
_manager.UnregisterStrategy("strat4");
|
||||
_manager.UnregisterStrategy("strat5");
|
||||
_manager.PortfolioKillSwitch = false;
|
||||
_manager.PortfolioDailyLossLimit = 2000.0;
|
||||
_manager.MaxTotalOpenContracts = 6;
|
||||
_manager.ResetDaily();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PortfolioDailyLossLimit_WhenBreached_BlocksNewOrder()
|
||||
{
|
||||
// Arrange
|
||||
_manager.RegisterStrategy("strat1", TestDataBuilder.CreateTestRiskConfig());
|
||||
_manager.PortfolioDailyLossLimit = 500;
|
||||
_manager.ReportPnL("strat1", -501);
|
||||
var intent = TestDataBuilder.CreateValidIntent();
|
||||
|
||||
// Act
|
||||
var decision = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(decision.Allow);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MaxTotalOpenContracts_WhenAtCap_BlocksNewOrder()
|
||||
{
|
||||
// Arrange
|
||||
_manager.RegisterStrategy("strat1", TestDataBuilder.CreateTestRiskConfig());
|
||||
_manager.MaxTotalOpenContracts = 2;
|
||||
|
||||
_manager.UpdateOpenContracts("strat1", 2);
|
||||
var intent = TestDataBuilder.CreateValidIntent();
|
||||
|
||||
// Act
|
||||
var decision = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(decision.Allow);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UpdateOpenContracts_WhenPositionCloses_UnblocksTrading()
|
||||
{
|
||||
// Arrange
|
||||
_manager.RegisterStrategy("strat1", TestDataBuilder.CreateTestRiskConfig());
|
||||
_manager.MaxTotalOpenContracts = 6;
|
||||
var intent = TestDataBuilder.CreateValidIntent();
|
||||
|
||||
// Act
|
||||
_manager.UpdateOpenContracts("strat1", 6);
|
||||
var blocked = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
_manager.UpdateOpenContracts("strat1", 0);
|
||||
var unblocked = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(blocked.Allow);
|
||||
Assert.IsTrue(unblocked.Allow);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PortfolioKillSwitch_WhenTrue_BlocksAllOrders()
|
||||
{
|
||||
// Arrange
|
||||
_manager.RegisterStrategy("strat1", TestDataBuilder.CreateTestRiskConfig());
|
||||
_manager.PortfolioKillSwitch = true;
|
||||
var intent = TestDataBuilder.CreateValidIntent();
|
||||
|
||||
// Act
|
||||
var decision = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(decision.Allow);
|
||||
Assert.IsTrue(decision.RejectReason.ToLowerInvariant().Contains("kill switch"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidatePortfolioRisk_WhenWithinLimits_Passes()
|
||||
{
|
||||
// Arrange
|
||||
_manager.RegisterStrategy("strat1", TestDataBuilder.CreateTestRiskConfig());
|
||||
var intent = TestDataBuilder.CreateValidIntent();
|
||||
|
||||
// Act
|
||||
var decision = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(decision.Allow);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResetDaily_ClearsPnL_UnblocksTrading()
|
||||
{
|
||||
// Arrange
|
||||
_manager.RegisterStrategy("strat1", TestDataBuilder.CreateTestRiskConfig());
|
||||
_manager.PortfolioDailyLossLimit = 500;
|
||||
_manager.ReportPnL("strat1", -600);
|
||||
var intent = TestDataBuilder.CreateValidIntent();
|
||||
|
||||
// Act
|
||||
var blocked = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
_manager.ResetDaily();
|
||||
var unblocked = _manager.ValidatePortfolioRisk("strat1", intent);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(blocked.Allow);
|
||||
Assert.IsTrue(unblocked.Allow);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,9 +149,9 @@ namespace NT8.Core.Tests.Sizing
|
||||
|
||||
var intent = CreateIntent();
|
||||
var context = CreateContext();
|
||||
var confluence = CreateScore(TradeGrade.C, 0.61);
|
||||
var confluence = CreateScore(TradeGrade.B, 0.71);
|
||||
var config = new SizingConfig(SizingMethod.FixedDollarRisk, 2, 10, 500.0, new Dictionary<string, object>());
|
||||
var modeConfig = CreateModeConfig(RiskMode.PCP, 1.0, TradeGrade.C);
|
||||
var modeConfig = CreateModeConfig(RiskMode.PCP, 1.0, TradeGrade.B);
|
||||
|
||||
var result = sizer.CalculateGradeBasedSize(
|
||||
intent,
|
||||
|
||||
@@ -15,11 +15,20 @@ namespace NT8.Integration.Tests
|
||||
[TestClass]
|
||||
public class NT8OrderAdapterIntegrationTests
|
||||
{
|
||||
private class FakeBridge : INT8ExecutionBridge
|
||||
{
|
||||
public void EnterLongManaged(int q, string n, int s, int t, double ts) { }
|
||||
public void EnterShortManaged(int q, string n, int s, int t, double ts) { }
|
||||
public void ExitLongManaged(string n) { }
|
||||
public void ExitShortManaged(string n) { }
|
||||
public void FlattenAll() { }
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Initialize_NullRiskManager_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
var sizer = new TestPositionSizer(1);
|
||||
|
||||
// Act / Assert
|
||||
@@ -31,7 +40,7 @@ namespace NT8.Integration.Tests
|
||||
public void Initialize_NullPositionSizer_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
var risk = new TestRiskManager(true);
|
||||
|
||||
// Act / Assert
|
||||
@@ -43,7 +52,7 @@ namespace NT8.Integration.Tests
|
||||
public void ExecuteIntent_NotInitialized_ThrowsInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
|
||||
// Act / Assert
|
||||
Assert.ThrowsException<InvalidOperationException>(
|
||||
@@ -54,7 +63,7 @@ namespace NT8.Integration.Tests
|
||||
public void ExecuteIntent_RiskRejected_DoesNotRecordExecution()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
var risk = new TestRiskManager(false);
|
||||
var sizer = new TestPositionSizer(3);
|
||||
adapter.Initialize(risk, sizer);
|
||||
@@ -71,7 +80,7 @@ namespace NT8.Integration.Tests
|
||||
public void ExecuteIntent_AllowedAndSized_RecordsExecution()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
var risk = new TestRiskManager(true);
|
||||
var sizer = new TestPositionSizer(4);
|
||||
adapter.Initialize(risk, sizer);
|
||||
@@ -94,7 +103,7 @@ namespace NT8.Integration.Tests
|
||||
public void GetExecutionHistory_ReturnsCopy_NotMutableInternalReference()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
var risk = new TestRiskManager(true);
|
||||
var sizer = new TestPositionSizer(2);
|
||||
adapter.Initialize(risk, sizer);
|
||||
@@ -113,7 +122,7 @@ namespace NT8.Integration.Tests
|
||||
public void OnOrderUpdate_EmptyOrderId_ThrowsArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
|
||||
// Act / Assert
|
||||
Assert.ThrowsException<ArgumentException>(
|
||||
@@ -124,7 +133,7 @@ namespace NT8.Integration.Tests
|
||||
public void OnExecutionUpdate_EmptyExecutionId_ThrowsArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
|
||||
// Act / Assert
|
||||
Assert.ThrowsException<ArgumentException>(
|
||||
@@ -135,7 +144,7 @@ namespace NT8.Integration.Tests
|
||||
public void OnExecutionUpdate_EmptyOrderId_ThrowsArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter();
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
|
||||
// Act / Assert
|
||||
Assert.ThrowsException<ArgumentException>(
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
// Removed - placeholder for integration tests
|
||||
@@ -1 +0,0 @@
|
||||
// Removed - placeholder for performance tests
|
||||
0
tests/_archive/legacy-orders/.gitkeep
Normal file
0
tests/_archive/legacy-orders/.gitkeep
Normal file
0
tests/_archive/placeholders/.gitkeep
Normal file
0
tests/_archive/placeholders/.gitkeep
Normal file
Reference in New Issue
Block a user