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 andrewtrades, Today, 04:57 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      6 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      7 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X