C# Ninjascript - NinjaTrader 8 (NT8) - Code to obtain the time and date of an event to then be displayed on chart in a textbox

Viewed 1463

Having as base the NinjaTrader 8 open source indicator Position Display Indicator which can be downloaded form the official NinjaTrader site to check the .cs archive: https://ninjatraderecosystem.com/user-app-share-download/position-display-indicator/

I'm making some customizations to this indicator, and one of the features I'm adding it is to calculate the Lower Unrealized Account Balance, and if this Lower Unrealized Account Balance value reaches a certain pre-defined Balance value, then it means the trading strategy you are using has failed. Please note that Lower Unrealized Account Balance value = Lower Net Liquidation value.

Now what I need is that if this described conditional event happens, then the indicator displays a mark of this event in its built-in textbox:

  • 1st: Displaying in red color the date and exact time (hh:mm:ss) the failure event has happened.
  • 2nd: In addition to the previous, and If possible, that the indicator also draws a vertical time line in red color showing the moment/bar the event took place, this is for better visual illustration.

I need a guide about the needed code to add these 2 features into the base indicator above.

I need this works either in Live, in Demo, in Simulated or in Playback (Market Replay). For Live, Demo and Simulated accounts the date and time displayed will be the real-time calendar date and time the event happened, as the normal, but about the Playback the thing is different, because in this special case what it is needed is the event's date and time of the Market Replay moment, i.e. not the date and time in the computer for these cases.

For example, if today Feb 25, 2021 you are running the Market Replay of Nov 30, 2020 and the failure event happens at 11:05:42 am (on Nov 30, 2020) then I need the indicator displays in its textbox something similar like:

"The strategy failed to meet the minimum necessary requirements at 11:05:42 am, on Nov 30, 2020"

Please note the main I need to know is how I get the time-and-date when an conditional event happenes, to then take that date information and display it on screen (a chart) in the way the indicator does this by default, in a predefined textbox drawn in any chart you want.

For example, let's suppose a starting account balance = $1,000.00, and you have the rule that if the Net Liquidation (current account balance) < or = $500, then it means your strategy failed to meet the minimum necessary requirements. So what I need to know is how I can get the time-and-date when the example event happens, i.e. when the account have a net liquidation < or = $500, to then draw this information in the screen, also drawing a reference vertical time line of the event.

I have a basic idea about the needed code for the example described, but seeing how C# works, I'm almost sure it won't be so simplified:

....
// simplified code portion with '...' where extra code is needed but not written to not to have a very large message because this is already in the Position Display Indicator
...
private double NetLiquidation;    // NetLiquidation is the Total Unrealized Account Balance, so the calculations and conditions need to be based in this main variable.
private double FailureLevel = 500;
private DateTime time&date_ofFailureLevel;    // I see in Ninjascripts the time/date needs to be setted as a 'DateTime' type.

...
if (NetLiquidation =< FailureLevel)
{
    // time&date_ofFailureLevel = code_to_get_time_of_the_event;
    ...
    string label = "Strategy progress status:" +"\n\n"
    ...
    // the next would be the idea to print the message "The strategy has failed to meet the minimum necessary requirements at hh:mm:mm a/pm, on Mmm DD, YYYY"
    "The strategy has failed to meet the minimum necessary requirements at"+ string time&date_ofFailureLevelstring = time&date_ofFailureLevel.ToString("N2");
    ...
    DrawTextLayout(RenderTarget, ChartPanel, ChartPanel.X + HorizontalOffset, ChartPanel.Y + VerticalOffset, time&date_ofFailureLevelstring, Font, time&date_ofFailureLevel < 0 ? negativeBrush : positiveBrush);
}

else
{
    string label = "Strategy progress status:" +"\n\n"
    ...
    // permanent print the message "The strategy is currently in positive territory, you can continue trading" //while NetLiquidation > 500.00 in this example.
}
...

Note: I'm wrinting this with as much details as I can because I have a very short programming backgraound. Some years ago I learned the basis for programming. I started with these modifications some days ago, so as you can imagine I'm in a knowledge refreshing process and I'm noticing that to do any simple action in C# needs almost the double of code than others more simplified programming languages.

Thank you

1 Answers

Old thread so the OP has probably moved on but I'll put this out there for others since it has a couple of upvotes.

Time/Date issue:

The time/ date issue in a live market vs Playback (Replay) mode can be mitigated by using Time[0] instead of DateTime.Now. This pulls the date and time from the current bar being processed in the OnBarUpdate routine(which is where your code would be). I know this from the semi painful process of retrofitting a bunch of indicators to work in playback mode.

Vertical lines:

Vertical lines can be drawn with this command VerticalLine Vl=Draw.VerticalLine(this, "Tag"+Time[0], Time[0], Brushes.Red, DashStyleHelper.Dash, 1);

Related