// File: SimpleORBNT8.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.Strategies;
using NT8.Core.Common.Interfaces;
using NT8.Strategies.Examples;
using SdkSimpleORB = NT8.Strategies.Examples.SimpleORBStrategy;

namespace NinjaTrader.NinjaScript.Strategies
{
    /// <summary>
    /// Simple Opening Range Breakout strategy integrated with NT8 SDK.
    /// </summary>
    public class SimpleORBNT8 : NT8StrategyBase
    {
        [NinjaScriptProperty]
        [Display(Name = "Opening Range Minutes", GroupName = "ORB Strategy", Order = 1)]
        [Range(5, 120)]
        public int OpeningRangeMinutes { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Std Dev Multiplier", GroupName = "ORB Strategy", Order = 2)]
        [Range(0.5, 3.0)]
        public double StdDevMultiplier { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Stop Loss Ticks", GroupName = "ORB Risk", Order = 1)]
        [Range(1, 50)]
        public int StopTicks { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Profit Target Ticks", GroupName = "ORB Risk", Order = 2)]
        [Range(1, 100)]
        public int TargetTicks { get; set; }

        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "Simple ORB NT8";
                Description = "Opening Range Breakout with NT8 SDK integration";

                OpeningRangeMinutes = 30;
                StdDevMultiplier = 1.0;
                StopTicks = 8;
                TargetTicks = 16;

                DailyLossLimit = 1000.0;
                MaxTradeRisk = 200.0;
                MaxOpenPositions = 1;
                RiskPerTrade = 100.0;
                MinContracts = 1;
                MaxContracts = 3;

                Calculate = Calculate.OnBarClose;
                BarsRequiredToTrade = 50;
            }

            base.OnStateChange();
        }

        protected override IStrategy CreateSdkStrategy()
        {
            return new SdkSimpleORB(OpeningRangeMinutes, StdDevMultiplier);
        }

        protected override void ConfigureStrategyParameters()
        {
            _strategyConfig.RiskSettings.DailyLossLimit = DailyLossLimit;
            _strategyConfig.RiskSettings.MaxTradeRisk = MaxTradeRisk;
            _strategyConfig.RiskSettings.MaxOpenPositions = MaxOpenPositions;

            // Guard: Instrument may be null during strategy list loading
            if (Instrument != null && Instrument.MasterInstrument != null)
            {
                var pointValue = Instrument.MasterInstrument.PointValue;
                var tickSize = Instrument.MasterInstrument.TickSize;
                var dollarRisk = StopTicks * tickSize * pointValue;

                if (dollarRisk > _strategyConfig.RiskSettings.MaxTradeRisk)
                    _strategyConfig.RiskSettings.MaxTradeRisk = dollarRisk;
            }

            _strategyConfig.SizingSettings.RiskPerTrade = RiskPerTrade;
            _strategyConfig.SizingSettings.MinContracts = MinContracts;
            _strategyConfig.SizingSettings.MaxContracts = MaxContracts;

            _strategyConfig.Parameters["StopTicks"] = StopTicks;
            _strategyConfig.Parameters["TargetTicks"] = TargetTicks;
            _strategyConfig.Parameters["OpeningRangeMinutes"] = OpeningRangeMinutes;

            if (_logger != null)
            {
                _logger.LogInformation(
                    "Simple ORB configured: OR={0}min, Stop={1}ticks, Target={2}ticks",
                    OpeningRangeMinutes,
                    StopTicks,
                    TargetTicks);
            }
        }
    }
}

