Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Daily Loss Limit Not working

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

    Daily Loss Limit Not working

    Hello,
    I am trying to test the: https://ninjatrader.com/support/foru...limit-examples
    Daily Pnl. I am testing this under Sim101 account. I am getting all return value as 0. Here is my code:

    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 = "DailyLossLimitExample";
    Calculate = Calculate.OnBarClose;
    BarsRequiredToTrade = 1;
    
    LossLimit = 500;
    }
    else if (State == State.DataLoaded)
    {
    ClearOutputWindow();
    }
    }
    
    protected override void OnBarUpdate()
    {
    // 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)
    {
    Print("daily limit hit" + Time[0].ToString());
    Print("currentPnl" + currentPnL);
    Print("Profit per month of all trades is: " + SystemPerformance.AllTrades.TradesPerformance.Curr ency.ProfitPerMonth);
    }
    
    if (Position.MarketPosition == MarketPosition.Flat && currentPnL < -LossLimit)
    {
    Print("daily limit NOT hit " + Time[0].ToString());
    }
    
    
    }
    
    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());
    }
    }
    }
    
    #region Properties
    [NinjaScriptProperty]
    [Range(0, double.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="LossLimit", Description="Amount of dollars of acceptable loss", Order=1, GroupName="NinjaScriptStrategyParameters")]
    public double LossLimit
    { get; set; }
    #endregion
    
    }
    }
    Here is my output:
    Code:
    daily limit hit1/13/2021 12:30:00 PM
    currentPnl0
    Profit per month of all trades is: 0
    Disabling NinjaScript strategy 'DailyLossLimitExample/219006979'
    I am uploaded my actual trade performance screenshot as well.
    Please advice the soultion.
    Thanks

    #2
    Hello szaman05,

    Thanks for your post and welcome to the Ninjatrader forums!

    It looks like you have modified the example (which you are welcome to do but we recommend always working with a copy of the original). Where is the entry condition?

    Please note that the strategy will only report on trades that the strategy itself performed.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,
      Thanks for your reply. My main problem is any SystemPerformance.AllTrades.TradesPerformance value is coming back zero.
      I am testing the strategy to get the different performance / PnL values from system performance to limit daily lose.
      How can I access my daily lose from a strategy?
      Please also let me know why am I getting monthly profit lose as zero on following code?

      Code:
      Print("Profit per month of all trades is: " + SystemPerformance.AllTrades.TradesPerformance.Curr ency.ProfitPerMonth)

      Comment


        #4
        Hello szaman05,

        Thanks for your reply.

        " My main problem is any SystemPerformance.AllTrades.TradesPerformance value is coming back zero." - Does your strategy place any trades? Based on the code you posted in the first post there are no entry orders being placed.

        For a daily PNL, of your strategy trades, you would need to save the accumulated PNL at the beginning of each session into a double type variable. The Daily PNl would then be the accumulatedPNL - the saved PNL.

        Are you looking for your Trades performance for manual trades?





        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Dear Paul,
          I am trying to implement a logic to stop trade if daily lose of all trades including manual and all the strategy trades reach x amount. So yes I need the trade performance on daily basis.
          Thanks

          Comment


            #6
            Hello szaman05,

            Thanks for your reply.

            The system performance values would relate to the strategy alone. If you want to know the complete picture of all trades made regardless of the source of the trade, you would need to work with the account class.

            Please see the help guide here: https://ninjatrader.com/support/help...ount_class.htm There is an example at the bottom that may be helpful.

            At the start of the day, you would want to save your cash value into a local variable and then as the account is updated, check that the current cash value minus the saved cash value relative to your daily loss limit.


            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by yertle, Yesterday, 08:38 AM
            7 responses
            28 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by bmartz, 03-12-2024, 06:12 AM
            2 responses
            21 views
            0 likes
            Last Post bmartz
            by bmartz
             
            Started by funk10101, Today, 12:02 AM
            0 responses
            4 views
            0 likes
            Last Post funk10101  
            Started by gravdigaz6, Yesterday, 11:40 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by MarianApalaghiei, Yesterday, 10:49 PM
            3 responses
            10 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X