Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

PL over specified time

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

    PL over specified time

    I am attempting to write a custom function to get a PL over last x hours in a strategy.
    The code is here but it does not return any value.
    Can you point out the mistake and a better code?

    Code:
    [FONT=Microsoft Sans Serif][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]private[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] profitlossLastnHours( [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] nHrs)
    {
    nHrs= [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif];
    
    [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif](Performance.RealtimeTrades.Count > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif])
    {
    Trade tradesAgo = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]];
    [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (DateTime.Compare(tradesAgo.EntryExecution.Time.AddHours(nHrs), DateTime.Now) >= [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]) 
    {
    pl= Instrument.MasterInstrument.Round2TickSize(tradesAgo.ProfitCurrency); 
    }
    }
    [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]return[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif](pl);
    }
    [/FONT]

    #2
    woodies,

    You dont have a default return case here, but two nested if statements. It may be that one or the other if statement isn't being satisfied so you aren't getting the pl variable set.

    Where are you calling this function at?
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hello,
      Thanks for the help.
      I have declared default value of pl in gobal variables at the top as private double pl=0;
      If I use it as a local variable, I get the messgae that pl does not exist when I use it under OnBarUpdate method.
      I just want to display the PL over last x hours on the panel and then use it as a money management tool in the strategy.
      I will check it again and let you know.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      115 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      163 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      83 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      127 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      87 views
      0 likes
      Last Post PaulMohn  
      Working...
      X