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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        90 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        135 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        119 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        69 views
        0 likes
        Last Post PaulMohn  
        Working...
        X