NexusFi: Find Your Edge


Home Menu

 





NT8 Ninjascript. Code to obtain the time and date of an event and display it on chart


Discussion in NinjaTrader

Updated
    1. trending_up 2,674 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

NT8 Ninjascript. Code to obtain the time and date of an event and display it on chart

  #1 (permalink)
futurenow
Earth planet
 
Posts: 53 since Feb 2017
Thanks Given: 42
Thanks Received: 13

Hello

Having as base the NinjaTrader 8 open source indicator Position Display Indicator:
https://ninjatraderecosystem.com/user-app-share-download/position-display-indicator/


I'm making it some customizations to calculate the lower Unrealized Account Balance value that if reached then it means the trading strategy you are using has failed. Lower Unrealized Account Balance value = lower Net Liquidation value.

Now what I need is that if this described event happens, then that the indicator put a mark of this event:
- Displaying in red color the date and exact time (hh:mm:ss) it has happened.
- If possible and for better visual illustration, in addition to the previous, that the indicator also draws a vertical time line in red color showing the moment/bar the event took place.

I need a guide about how to add these 2 features to the indicator.

I need this works either in Live, in Demo, in Simulated or in Playback (Market Replay), so for Live, Demo and Simulated accounts the date and time I need to be displayed is the real-time calendar date and time, as the normal, but about the Playback the thing is different, because 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 23, 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 something similar like:

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


Note: I have not programming backgraound but 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

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
What broker to use for trading palladium futures
Commodities
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Quant vue
Trading Reviews and Vendors
 
  #2 (permalink)
futurenow
Earth planet
 
Posts: 53 since Feb 2017
Thanks Given: 42
Thanks Received: 13

The indicator I mention "Position Display Indicator" draws a textbox in any chart you want showing the information pre-defined, and for this you just need to import and load the indicator as any other indicator. I need to display the failed event notification in this textbox.

to display information any extra iformation I've been using the defeult way the indicator use, for example
 
Code
DrawTextLayout(RenderTarget, ChartPanel, ChartPanel.X + HorizontalOffset, ChartPanel.Y + VerticalOffset, P&Lstring, Font, P&L < 0 ? negativeBrush : positiveBrush);
The situation I'm asking for is not mainly about how to draw the time/date, this will help but is secundary, becuase the main I need to know is how I get the time-and-date when the event happened, to then take that information and display it in screen (a chart).

For example, let's suppose a starting account balance = $1,000.00, and you have the rule that if the account balance < or = $500, either with the account unrealized balance or with the account realized balance, 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:


 
Code
...
private double NetLiquidation;
private double FailureLevel = 500;
private DateTime time&date_FailureLevel;  // As NinjaTrader specifies a time/date needs to be setted as 'DateTime'

...

if (NetLiquidation <= FailureLevel)
{
    // Pseudocode:
    time&date_FailureLevel = code_to_get_time_of_the_event;
    // If met the condition, then display the message: "The strategy failed to meet the minimum necessary requirements at   HH:MM:SS a/pm, on Mmm DD, YYYY"
}

else
{
    // If not met the condition, then continue doing everything as normal
}

...

string time&date_FailureLevelstring = time&date_FailureLevel.ToString("N2");

...
DrawTextLayout(RenderTarget, ChartPanel, ChartPanel.X + HorizontalOffset, ChartPanel.Y + VerticalOffset, time&date_FailureLevel, Font, time&date_FailureLevel < 0 ? negativeBrush : positiveBrush);


Finally, as far as I know the Playback runs the Market Replay at 1 second update that for my case is ok because I can obtain the time of the event with 1 second of maximum inaccuracy time distortion, either the Market Replay speed be at 1x or at 100x or at Max speed.

Reply With Quote
  #3 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047



futurenow View Post

I need this works either in Live, in Demo, in Simulated or in Playback (Market Replay), so for Live, Demo and Simulated accounts the date and time I need to be displayed is the real-time calendar date and time, as the normal, but about the Playback the thing is different, because 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.

So I may have a general idea of what you're trying to do here...

But as you want to capture TIME... there are issues with storing and capturing these values.

So... NT does not actually have an object that can be easily referenced that permantly stores the time of LIVE trades taken in real or SIM accounts(as odd as this is). So while you'll always have access to balances, you won't have access to times/dates. Actually, when you go across a session, or when you restart your PC, you will lose access to times of trades taken in live or SIM.

Now... if you run a strategy for example, and are placing trades... you can always STORE those times... but those will only be available for the life cycle of the script, so unless you write that info to a text file, it will be destroyed on a restart of the indicator.

So to recap....

For LIVE/SIM trades taken by a strategy, your access to events are limited. You would have to store those events(date/time), and you would have access to it for the life-cycle of the indicator.

If you want to store that time of strategy taking the trades, and NOT the PC time, that is standard, so no worries there. I.e., when a balance dips below your desire value, you would simply flag that event, and store that date with "Time[0]", and you would have the time that event occurred.

I actually built and indicator for someone else in THIS thread, where he wanted to have his TIME in trade, where I found out about all these pitfalls of attempting to capture times of trades placed.

The posts about that indie are interspersed throughout the thread, but you can start reading from that link, and maybe you can get a sense of what I'm trying to explain here.

This probably isn't the most thorough answer, but maybe it will get the discussion started.

Reply With Quote
Thanked by:
  #4 (permalink)
futurenow
Earth planet
 
Posts: 53 since Feb 2017
Thanks Given: 42
Thanks Received: 13

Thank you very much for response, it really helps


forrestang View Post
If you want to store that time of strategy taking the trades, and NOT the PC time, that is standard, so no worries there. I.e., when a balance dips below your desire value, you would simply flag that event, and store that date with "Time[0]", and you would have the time that event occurred.

I'm new in the world to develop an indicator for NT8 and I have not programming background apart from some basics. With that said, I have some days making tests and finally could get the time of an 'if' conditional event with 'Times[1][0]' to then print the alert message of the condition with 'Draw.TextFixed()', so yes, finally I'm able to see a time and date printed for first time.

Once the condition is met the indicator prints the time of the event, but there is a little problem, that is, it keeps "updating" or replacing that time every time the condition is met again and again, so you could see on the chart something like 15:10:21, then 15:10:24, then 15:10:26, then 15:10:30,... and so on. And following this example the only time I need to see is "15:10:21", the others are not important for the feature I need, only the initial one.

So in fact, with this problem the indicator is still useless because the idea is if I'm in Live or making a Market Replay that if I'm not in the front the PC every single second, then if I'm out for some minutes and the condition was met during that time, then that when I return I be able to quickly see the time of the event, and as it actually is it will be impossible because as you can see is like if the indicator keeps updating the time every time it see the condition is met once again, so I haven't found the way to the indicator only show the "first" initial date when the event happened.

As a reference, I've tried putting the condition into different part of the code, into 'OnBarUpdate()', into 'OnAccountItemUpdate()', into 'OnRender()' and some others places trying to avoid this kind of loop the 'if' portion of code is getting, but I've been unable to fix the problem, to only get the first exact time of the event.

Could you please help me to solve this kind of unwanted auto-updating problem.


Quoting 
I actually built and indicator for someone else in THIS thread, where he wanted to have his TIME in trade, where I found out about all these pitfalls of attempting to capture times of trades placed.

Thank you for provide the link as reference.

Reply With Quote




Last Updated on February 27, 2021


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts