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 kaywai, 09-01-2023, 08:44 PM
      5 responses
      601 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      22 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      16 views
      0 likes
      Last Post Pattontje  
      Started by flybuzz, 04-21-2024, 04:07 PM
      17 responses
      230 views
      0 likes
      Last Post TradingLoss  
      Started by agclub, 04-21-2024, 08:57 PM
      3 responses
      17 views
      0 likes
      Last Post TradingLoss  
      Working...
      X