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 algospoke, Today, 06:40 PM
      0 responses
      2 views
      0 likes
      Last Post algospoke  
      Started by maybeimnotrader, Today, 05:46 PM
      0 responses
      6 views
      0 likes
      Last Post maybeimnotrader  
      Started by quantismo, Today, 05:13 PM
      0 responses
      6 views
      0 likes
      Last Post quantismo  
      Started by AttiM, 02-14-2024, 05:20 PM
      8 responses
      168 views
      0 likes
      Last Post jeronymite  
      Started by cre8able, Today, 04:22 PM
      0 responses
      8 views
      0 likes
      Last Post cre8able  
      Working...
      X