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, 03-31-2026, 09:41 PM
        1 response
        77 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        63 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        53 views
        0 likes
        Last Post CarlTrading  
        Working...
        X