Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SamplePnL and Log only ONCE...

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

    SamplePnL and Log only ONCE...

    Hi Ninja Team,

    This is probably my poor coding causing a problem, but your input is welcome. I've used some of your SamplePnL code and added a line to log the result as an alert level log, and it seems to go into a loop and I get a "Mail queue overflow" event in the log (alert-level log items get emailed to me). Not pretty!

    Note this code seemed to work fine yesterday, but upon a daily reboot and reload it entered a loop that required a hard shutdown.

    Here is the code snippet;

    PHP Code:
    protected override void OnBarUpdate()
    {
     
     
    if(BarsInProgress != 0)
    return;
     
    ////Start of Strategy daily loss limit code///
    // At the start of a new session
    if (Bars.FirstBarOfSession)
    {
    // Store the strategy's prior cumulated realized profit 
    priorTradesCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
     
    }
     
    /* Prevents further trading if the current session's realized profit exceeds variable StrategyProfitTarget 
    or if realized losses exceed variable StrategyStopLoss. */
    if (Performance.AllTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit >= StrategyProfitTarget
    || Performance.AllTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit <= (StrategyStopLoss*-1))
    {
     
    // Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
     
    Log(Instrument.FullName + " Strategy halted. Session profit/loss limits exceeded. Check your open positions. ",NinjaTrader.Cbi.LogLevel.Alert); //Log the shutdown
     
    return;
    }
     
     
    ////// GO LONG GO LONG GO LONG///// 
    
    Is there a way to code it so the halt of the strategy is only logged once?

    A seperate minor issue in the same strategy is generated by this code;

    PHP Code:
    {
    EnterLong("Long 1a");
    EnterLong("Long 1b");
    Log(Instrument.FullName + " LONG signal generated with ASK at: " + GetCurrentAsk(),NinjaTrader.Cbi.LogLevel.Information); //For basic TCA 
    } 
    
    Where my intention is to log the ASK price when my strategy gets a LONG signal so that after hours I can do a basic slippage calculation. When I enable the strategy I get signals logged from the past. Is there a simple way around that?

    Any input or advice is welcome. Thanks.

    #2
    On the 1st issue, add a local to the strategy bool variable such as:

    private bool isHalted = false;

    then where you log the halt of the strategy, add code in the if statement

    if (!isHalted .....)
    {
    // Log here
    isHalted = true;

    }

    On your second issue, do something like:

    if (!Historical) Log(....);
    RayNinjaTrader Customer Service

    Comment


      #3
      Thanks Ray,

      On the second one... I've tried this;

      PHP Code:
      EnterLong("Long 1a");
      EnterLong("Long 1b");
      if(!Historical)
      {
      Log(Instrument.FullName + " LONG signal generated with ASK at: " + GetCurrentAsk(),NinjaTrader.Cbi.LogLevel.Information); //For basic TCA
      } 
      
      But "historical" signals are still logged in the sense that the timestamp of the log entry is the time I activate the strategy, but there is roughly ten or more log events at different prices. I'm guessing it is based on the bars loaded. Is there an alternative suggestion?

      On the first one I need to think about what you wrote a bit. This is a VMWare weekend and not a C# weekend for me. My head hurts .

      Comment


        #4
        Not sure I follow what you are saying.

        Based on the code below:

        if(!Historical)
        {
        Log(Instrument.FullName + " LONG signal generated with ASK at: " + GetCurrentAsk(),NinjaTrader.Cbi.LogLevel.Informati on); //For basic TCA
        }

        This will NOT send output to the log on historical bars.
        RayNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        628 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        359 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        562 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X