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 NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        54 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        72 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X