Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

limit a Strategy to that it can run only 1 time simultaneously

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    limit a Strategy to that it can run only 1 time simultaneously

    How could I limit a Strategy so that it can only be Active 1 time in an account?

    I came up with this, but it doesn't work


    private bool isStrategyActive = false;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults) {}
    else if (State == State.Configure) {}
    else if (State == State.DataLoaded) {isStrategyActive = true; }
    else if (State == State.Terminated) { isStrategyActive = false; }
    }


    protected override void OnBarUpdate()
    {​
    if (!isStrategyActive) {
    Print("La estrategia no está activa en este momento."); return; }
    }



    #2
    Hello HGTrader,

    There wouldn't be anything native to NinjaTrader to accomplish this. NinjaScript strategies do not have a native way of communicating.

    However, one idea might be to use unsupported code to have a static property that is set / read from the strategy.
    https://ninjatrader.com/support/foru...ork#post712245
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I was able to do this but it didn't work, any suggestions?


      public class MyStrategy : Strategy { // Variable estática para controlar si la estrategia está activa

      public static bool IsStrategyActive { get; set; } // Objeto para bloqueo de sincronización

      rivate static readonly object StrategyLock = new object();


      protected override void OnStateChange()
      {
      if (State == State.SetDefaults) { // Configuración por defecto }

      else if (State == State.Configure) { // Configuración adicional }

      else if (State == State.DataLoaded) { // La estrategia está activa al cargar los datos IsStrategyActive = true; }

      else if (State == State.Terminated) { // La estrategia se desactiva al terminar IsStrategyActive = false; }
      }

      protected override void OnBarUpdate() { // Bloque de sincronización para controlar la ejecución simultánea

      lock (StrategyLock)

      { if (!IsStrategyActive) { // No se ejecuta si la estrategia no está activa
      return; } // lógica de entrada y salida aquí // ... } } }

      Comment


        #4
        Hello HGTrader,

        To confirm, the static class and property are in a separate namespace as demonstrated in the example correct?
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        35 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        199 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Started by CaptainJack, 04-24-2026, 11:07 PM
        0 responses
        365 views
        0 likes
        Last Post CaptainJack  
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        283 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X