Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

I cant get the daily PNL into my Code - Frustrating

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

    I cant get the daily PNL into my Code - Frustrating

    This is my current Strategy

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    
        public class HeikenAshiRealTime : Strategy
        {
            private LinReg LinReg1;
            private VWMA VWMA1;
            private int stopLossTicks;
            private int takeProfitTicks;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Run in Real time - Heiken Ashi 1-Min - 13/20/10/-40/20/60 settings";
                    Name                                        = "HeikenAshiRealTime";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.High;
                    OrderFillResolutionType                        = BarsPeriodType.Minute;
                    OrderFillResolutionValue                    = 1;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    LinRegres                                    = 13;
                    VMARes                                        = 20;
                    StopLossTicks                                 = 20;
                    TakeProfitTicks                             = 50;
    
                }
                else if (State == State.Configure)
                {
    
                    stopLossTicks = StopLossTicks;
                    takeProfitTicks = TakeProfitTicks;
                }
                else if (State == State.DataLoaded)
                {                
                    LinReg1                = LinReg(Close, Convert.ToInt32(LinRegres));
                    VWMA1                = VWMA(Close, Convert.ToInt32(VMARes));
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                if(State == State.Historical)
                    return;
    
                if (Position.MarketPosition == MarketPosition.Flat)
                    {
                 // Set 1
    
                if (CrossAbove(LinReg1, VWMA1, 1))
    
                {
    
                    SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                    SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                    EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                    //SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                    //SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                    //EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                }
    
                 // Set 2
                if (CrossBelow(LinReg1, VWMA1, 1))
    
                {
    
                    SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                    SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                    EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                    //SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                    //SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                    //EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                }
    
            }
            }
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="LinRegres", Order=1, GroupName="Parameters")]
            public int LinRegres
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="VMARes", Order=2, GroupName="Parameters")]
            public int VMARes
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Stop Loss Ticks", Order=3, GroupName="Parameters")]
            public int StopLossTicks
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Take Profit Ticks", Order=4, GroupName="Parameters")]
            public int TakeProfitTicks
            { get; set; }
    
            #endregion​
    I'm trying to incorporate the DailyLossLimitExample but it's shooting all kinds of errors

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class DailyLossLimitExample : Strategy
        {
            private double currentPnL;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Prevents new entries after PnL is less than the LossLimit";
                    Name                                        = "DailyProfitLossExampleTSmod";
                    Calculate                                    = Calculate.OnBarClose;
                    BarsRequiredToTrade                            = 1;
    
                    LossLimit                                    = 500;
                    ProfitLimit                                    = 1000; ///Define your Profit target
                }
                else if (State == State.DataLoaded)
                {
                    ClearOutputWindow();
                    SetStopLoss("long1", CalculationMode.Ticks, 50, false);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (State != State.Realtime) //Only trades realtime. Ignores historical trades.
                {
                    return;
                }
    
                // at the start of a new session, reset the currentPnL for a new day of trading
                if (Bars.IsFirstBarOfSession)
                    currentPnL = 0;
    
                // if flat and below the loss limit of the day enter long
                if (
                    (Position.MarketPosition == MarketPosition.Flat)
                        && (currentPnL > -LossLimit) ///Loss remains 'above' limit
                            && (currentPnL < ProfitLimit) ///Profit remains 'below' limit
                    )
                {
                    EnterLong(DefaultQuantity, "long1");
                }
    
                // if in a position and the realized day's PnL plus the position PnL is greater than the loss limit then exit the order
                if (
                    (Position.MarketPosition == MarketPosition.Long)
                        &&  (  ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
                            || ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) >= ProfitLimit)  ) ///If unrealized goes under LossLimit 'OR' Above ProfitLimit
    
                    )    
    
                {
                    //Print((currentPnL+Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)) + " - " + -LossLimit);
                    // print to the output window if the daily limit is hit in the middle of a trade
                    Print("daily limit hit, exiting order " + Time[0].ToString());
                    ExitLong("Daily Limit Exit", "long1");
                }
            }
    
            protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
            {
                if (Position.MarketPosition == MarketPosition.Flat && SystemPerformance.AllTrades.Count > 0)
                {
                    // when a position is closed, add the last trade's Profit to the currentPnL
                    currentPnL += SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
    
                    // print to output window if the daily limit is hit
                    if (currentPnL <= -LossLimit)
                    {
                        Print("daily limit hit, no new orders" + Time[0].ToString());
                    }
    
                    if (currentPnL >= ProfitLimit)
                    {
                        Print("daily Profit limit hit, no new orders" + Time[0].ToString()); ///Prints message to output
                    }
                }
            }
    
            #region Properties
            ///Needed to customize our Profit
            [NinjaScriptProperty]
            [Range(0, double.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name="ProfitLimit", Description="Amount of dollars of acceptable Win", Order=1, GroupName="NinjaScriptStrategyParameters")]
            public double ProfitLimit
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(0, double.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name="LossLimit", Description="Amount of dollars of acceptable loss", Order=2, GroupName="NinjaScriptStrategyParameters")]
            public double LossLimit
            { get; set; }
    
            #endregion​
    I don't know what else to do.

    #2
    Hello zayone,

    What are the full error messages you would assistance with?

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3


      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
      
          public class HeikenAshiRealTime : Strategy
          {
              private LinReg LinReg1;
              private VWMA VWMA1;
              private int stopLossTicks;
              private int takeProfitTicks;
              private double currentPnL;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Run in Real time - Heiken Ashi 1-Min - 13/20/10/-40/20/60 settings";
                      Name                                        = "HeikenAshiRealTime";
                      Calculate                                    = Calculate.OnBarClose;
                      EntriesPerDirection                            = 1;
                      EntryHandling                                = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy                = true;
                      ExitOnSessionCloseSeconds                    = 30;
                      IsFillLimitOnTouch                            = false;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution                            = OrderFillResolution.High;
                      OrderFillResolutionType                        = BarsPeriodType.Minute;
                      OrderFillResolutionValue                    = 1;
                      Slippage                                    = 0;
                      StartBehavior                                = StartBehavior.WaitUntilFlat;
                      TimeInForce                                    = TimeInForce.Gtc;
                      TraceOrders                                    = false;
                      RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade                            = 20;
                      IsInstantiatedOnEachOptimizationIteration    = true;
                      LinRegres                                    = 13;
                      VMARes                                        = 20;
                      StopLossTicks                                 = 20;
                      TakeProfitTicks                             = 50;
                      LossLimit                                     = 2000;
                      ProfitLimit                                 = 1000;
                  }
                  else if (State == State.Configure)
                  {
      
                      stopLossTicks = StopLossTicks;
                      takeProfitTicks = TakeProfitTicks;
                  }
                  else if (State == State.DataLoaded)
                  {                
                      LinReg1                = LinReg(Close, Convert.ToInt32(LinRegres));
                      VWMA1                = VWMA(Close, Convert.ToInt32(VMARes));
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (BarsInProgress != 0)
                      return;
      
                  if (CurrentBars[0] < 1)
                      return;
      
                  //if(State == State.Historical)
                      //return;
      
                  if (Position.MarketPosition == MarketPosition.Flat)
                      {
                   // Set 1
      
                  if (CrossAbove(LinReg1, VWMA1, 1) && currentPnL > -LossLimit)
      
                  {
      
                      SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                      SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                      EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                      //SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                      //SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                      //EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                  }
      
                   // Set 2
                  if (CrossBelow(LinReg1, VWMA1, 1) && currentPnL > -LossLimit)
      
                  {
      
                      SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                      SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                      EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                      //SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                      //SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                      //EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                  }
                   if ((currentPnL <= -LossLimit || currentPnL >= ProfitLimit)
      
                        &&  (  ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
                              || ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) >= ProfitLimit)  
      
                  protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
              {
                  if (Position.MarketPosition == MarketPosition.Flat && SystemPerformance.AllTrades.Count > 0)
                  {
                      currentPnL += SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
      
                      if (currentPnL <= -LossLimit)
                      {
                          Print("daily loss limit hit, no new orders" + Time[0].ToString());
                      }
      
                      if (currentPnL >= ProfitLimit)
                      {
                          Print("daily profit limit hit, no new orders" + Time[0].ToString());
                      }
                  }
              }
              #region Properties
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="LinRegres", Order=1, GroupName="Parameters")]
              public int LinRegres
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="VMARes", Order=2, GroupName="Parameters")]
              public int VMARes
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Stop Loss Ticks", Order=3, GroupName="Parameters")]
              public int StopLossTicks
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Take Profit Ticks", Order=4, GroupName="Parameters")]
              public int TakeProfitTicks
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(0, double.MaxValue)]
              [Display(ResourceType = typeof(Custom.Resource), Name="ProfitLimit", Description="Amount of dollars of acceptable Win", Order=5, GroupName="NinjaScriptStrategyParameters")]
              public double ProfitLimit
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(0, double.MaxValue)]
              [Display(ResourceType = typeof(Custom.Resource), Name="LossLimit", Description="Amount of dollars of acceptable loss", Order=6, GroupName="NinjaScriptStrategyParameters")]
              public double LossLimit
              { get; set; }
      
              #endregion
      
          }​
      Attached Files

      Comment


        #4
        Hello zayone,

        There is no closing curly brace for OnBarUpdate().

        In C# every opening curly brace must have a matching closing curly brace. For every opening bracket there must be a matching closing bracket. For every opening parenthesis there must be a matching closing parenthesis.

        Below I am providing a link to a forum post with helpful resources on getting started with C# and NinjaScript.



        Further, You need put some kind of action for the if statement.

        if ((currentPnL <= -LossLimit || currentPnL >= ProfitLimit)
        && ( ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) <= -LossLimit)
        || ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) >= ProfitLimit)​

        If condition is true, what do you want to do? You need an action. You need to assign a value or call a method or something.

        In the original example, ExitLong() is called in the action block of the condition.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I think it's working now but when the daily loss hits, it stops the strategy but when it hits the daily profit it does not stop and continues.

          Code:
          public class HeikenAshiRealTime : Strategy
              {
                  private LinReg LinReg1;
                  private VWMA VWMA1;
                  private int stopLossTicks;
                  private int takeProfitTicks;
                  private double currentPnL;
          
                  protected override void OnStateChange()
                  {
                      if (State == State.SetDefaults)
                      {
                          Description                                    = @"Run in Real time - Heiken Ashi 1-Min - 13/20/10/-40/20/60 settings";
                          Name                                        = "HeikenAshiRealTime";
                          Calculate                                    = Calculate.OnBarClose;
                          EntriesPerDirection                            = 1;
                          EntryHandling                                = EntryHandling.AllEntries;
                          IsExitOnSessionCloseStrategy                = true;
                          ExitOnSessionCloseSeconds                    = 30;
                          IsFillLimitOnTouch                            = false;
                          MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                          OrderFillResolution                            = OrderFillResolution.High;
                          OrderFillResolutionType                        = BarsPeriodType.Minute;
                          OrderFillResolutionValue                    = 1;
                          Slippage                                    = 0;
                          StartBehavior                                = StartBehavior.WaitUntilFlat;
                          TimeInForce                                    = TimeInForce.Gtc;
                          TraceOrders                                    = false;
                          RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                          StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                          BarsRequiredToTrade                            = 20;
                          IsInstantiatedOnEachOptimizationIteration    = true;
                          LinRegres                                    = 13;
                          VMARes                                        = 20;
                          StopLossTicks                                 = 20;
                          TakeProfitTicks                             = 50;
                          LossLimit                                     = 2000;
                          ProfitLimit                                 = 1000;
                      }
                      else if (State == State.Configure)
                      {
          
                          stopLossTicks = StopLossTicks;
                          takeProfitTicks = TakeProfitTicks;
                      }
                      else if (State == State.DataLoaded)
                      {                
                          LinReg1                = LinReg(Close, Convert.ToInt32(LinRegres));
                          VWMA1                = VWMA(Close, Convert.ToInt32(VMARes));
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (BarsInProgress != 0)
                          return;
          
                      if (CurrentBars[0] < 1)
                          return;
          
                      if(State == State.Historical)
                          return;
          
                      if (Position.MarketPosition == MarketPosition.Flat)
                          {
                       // Set 1
          
                      if (CrossAbove(LinReg1, VWMA1, 1) && currentPnL > -LossLimit)
          
                      {
          
                          //SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                          //SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                          //EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                          SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                          SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                          EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                      }
          
                       // Set 2
                      if (CrossBelow(LinReg1, VWMA1, 1) && currentPnL > -LossLimit)
          
                      {
          
                          //SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                          //SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                          //EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                          SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                          SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                          EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                      }
                       if ((currentPnL <= -LossLimit || currentPnL >= ProfitLimit)
                          && ( ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
                          || ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) >= ProfitLimit)​))
          
          
          
                          //Print((currentPnL+Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)) + " - " + -LossLimit);
                          // print to the output window if the daily limit is hit in the middle of a trade
                          Print("daily limit hit, exiting order " + Time[0].ToString());
                          ExitLong("Daily Limit Exit", "long1");
          
                          }
                  }
          
                      protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
                  {
                      if (Position.MarketPosition == MarketPosition.Flat && SystemPerformance.AllTrades.Count > 0)
                      {
                          currentPnL += SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
          
                          if (currentPnL <= -LossLimit)
                          {
                              Print("daily loss limit hit, no new orders" + Time[0].ToString());
                          }
          
                          if (currentPnL >= ProfitLimit)
                          {
                              Print("daily profit limit hit, no new orders" + Time[0].ToString());
                          }
                      }
                  }​

          Comment


            #6
            Hello zayone,

            To understand why a condition has evaluated as false (or true) print the time of the bar and all values used in the condition with labels for each value and comparison operator.

            Below is a link to a forum post on using prints to diagnose behavior.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I got the code to work but It just wont close on 1000+ profit.

              Code:
                  else if (State == State.Configure)
                          {
              
                              stopLossTicks = StopLossTicks;
                              takeProfitTicks = TakeProfitTicks;
                          }
                          else if (State == State.DataLoaded)
                          {                
                              LinReg1                = LinReg(Close, Convert.ToInt32(LinRegres));
                              VWMA1                = VWMA(Close, Convert.ToInt32(VMARes));
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
                          if (BarsInProgress != 0)
                              return;
              
                          if (CurrentBars[0] < 1)
                              return;
              
                          if(State == State.Historical)
                              return;
              
                          if (Position.MarketPosition == MarketPosition.Flat)
                              {
                           // Set 1
              
                          if (CrossAbove(LinReg1, VWMA1, 1) && currentPnL > -LossLimit)
              
                          {
              
                              //SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                              //SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                              //EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                              SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                              SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                              EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                          }
              
                           // Set 2
                          if (CrossBelow(LinReg1, VWMA1, 1) && currentPnL > -LossLimit || currentPnL >= ProfitLimit)
              
                          {
              
                              //SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                              //SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);​
                              //EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                              SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                              SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);​
                              EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                          }
              
                          if ((currentPnL <= -LossLimit || currentPnL >= ProfitLimit)
                              && ( ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
                              || ((currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) >= ProfitLimit)))
              
              
                          // Exit Long if in Long position
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                              ExitLong("Daily Limit Exit", "long1");
                          }
                          // Exit Short if in Short position
              
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                              ExitShort("Daily Limit Exit", "short1");
                          }
              
                              }
                      }
                          protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
                      {
                          if (Position.MarketPosition == MarketPosition.Flat && SystemPerformance.AllTrades.Count > 0)
                          {
                              currentPnL += SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
              
                              if (currentPnL <= -LossLimit)
                              {
                                  Print("daily loss limit hit, no new orders" + Time[0].ToString());
                              }
              
                              if (currentPnL >= ProfitLimit)
                              {
                                  Print("daily profit limit hit, no new orders" + Time[0].ToString());
                              }
                          }
                      }​

              Comment


                #8
                Hello zayone,

                It will be necessary to find out why the condition is not evaluating as true and not submitting the expected order.

                Print the time of the bar, currentPnl, -LossLimit, ProfitLimit, and currentPnL + unrealized. Include labels for each value and comparison operator.
                By viewing the output we can find out which condition in the set caused the condition to evaluate as false.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                50 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                126 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                69 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X