Share Data between two Charts - futures io
futures io futures trading



Share Data between two Charts


Discussion in NinjaTrader

Updated
    1. trending_up 1,822 views
    2. thumb_up 14 thanks given
    3. group 4 followers
    1. forum 20 posts
    2. attach_file 4 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Share Data between two Charts

(login for full post details)
  #1 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Hello Community,

I want to exchange data between two charts.
With Streamreader I write data from Chart Write to an SSD and read the data in Chart Read with streamwriter.
Because the process is quite slow, I want to speed up the process.

For this I use a static variable, which real-time transmits the data correctly.
double readtestvalue = Write.writetestvalue ==> works

My problem is the historical representation of the data
The assignment ReadArray [0] = Write.WriteArray [0];
is accepted by the compiler but leads to a runtime error (missing object instance)

Write [] readArray = new Write [];
Does not solve the problem ....

How would you solve the problem?

1. Is it correct to transfer the data with an array?
2. My WriteArray is public static ==> As far as I know, using static in Read does not require a constructor
Does the runtime error result for other reasons?
3. I tried to transfer the data via a property in Write.
The public property in Write is not visible in Read ==> how do I access from Chart1 a property in Chart2?
Is the property in Ninjatrader 8 read-only?
4. Are there examples in the community for my concern?

Many thanks for the support!

Reply With Quote

Can you help answer these questions
from other members on futures io?
COTbase for quant trading?
Elite Algorithmic NinjaTrader Trading
Automatic pattern research with NT
Elite Algorithmic NinjaTrader Trading
Which kind of Blockvolume is visible? - order processing
Emini and Emicro Index
MACD line not plotting after change of plot order
NinjaTrader
Cross border fund flows / fx correlations
Treasury Notes and Bonds
 
 
(login for full post details)
  #3 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received



user10 View Post
Write [] readArray = new Write [];
Does not solve the problem ....

You still need to allocate actual space for the array, e.g.

 
Code
public static double[] values = new double[8];

Remember that you are responsible for any synchronisation (unlike the native NT DataSeries), and also that any recompile needs a reload of all charts that are using global data, otherwise the connections are lost.

Cheers

Travel Well
Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to ratfink for this post:
 
(login for full post details)
  #4 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
The public property in Write is not visible in Read ==> how do I access from Chart1 a property in Chart2?
Is the property in Ninjatrader 8 read-only?

You need to use the class name as part of the reference, e.g.

 
Code
public static class myData
{
    public static int fred;
}

...

myData.fred = 3;

The values are not read-only, but connections do get lost when compiling, all charts need to be F5'd or re-opened. Global data does work fine under NT and there are plenty of code examples on the forum, just use the search facility, it does however need care, as do most good things.

Cheers

Travel Well
Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to ratfink for this post:
 
(login for full post details)
  #5 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Thanks for the hint.
I can now access the Write.WriteArray.

However, the data should be presented historically.
This works neither with (State = = State.Historical) nor with OnBarUpdate.

for (int i = 0; i <ReadArray.Length; i ++)
ReadArray [0] = Write.WriteArray [0];
or
ReadArray [i] = Write.WriteArray [i];
only shows the current data.

How can I present the historical data?
Many Thanks!

Reply With Quote
The following user says Thank You to user10 for this post:
 
(login for full post details)
  #6 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
Thanks for the hint.
I can now access the Write.WriteArray.

However, the data should be presented historically.
This works neither with (State = = State.Historical) nor with OnBarUpdate.

for (int i = 0; i <ReadArray.Length; i ++)
ReadArray [0] = Write.WriteArray [0];
or
ReadArray [i] = Write.WriteArray [i];
only shows the current data.

How can I present the historical data?
Many Thanks!

Really depends on what you are trying to do, basically you will have to make use of bar indexing in the indicator OnBarUpdate code using the 'CurrentBar' or BarsSinceSession variables (NT7, I can't be sure about NT8 equivalents as I haven't written code for that for a year or so).

Note that NT uses inverse bar indexing so Close[0] is the current bar, Close[2] is 2 bars earlier.

e.g. in OnBarUpdate

 
Code
Write.WriteArray[CurrentBar] = Close[0]; // assumes your arrays are big enough, else you need wrap around code
Data will only get written to your array on each OnBarUpdate call when the writer chart is first loaded or F5'd (for Historical) and will then be updated on each OnBarUpdate call when live. So your reader chart will only see valid data if the writer chart was loaded first, and again it will only see anything at all if they are both refreshed in that order after a recompile. Once stable and both are running it will update fine but you have no guarantee on individual tick update order between the charts, that's not usually a big deal though. Note that NT8 charts may be running on different threads, not the case in NT7.

I always use simple Print calls to see the ins and outs, but beware opening Output Windows in 2 different workspaces under NT8 as this always uses to crash it (maybe fixed now I haven't tried for a long time.)

Cheers

Travel Well
Visit my futures io Trade Journal Reply With Quote
 
(login for full post details)
  #7 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Thank you for that information; unfortunately I do not get correct results.

In Chart Write, I set calculations that I want to use in Chart Read.
For the sake of simplicity we assume that in Chart Write with 1000 bars I count a counter from 1 to 1000.
In Chart Write WriteArray [0] correctly displays these numbers for each bar from 1 - 1000.

I would now like to use the result of this calculation in Chart Read (1000 bars).
I assign ReadArray = Write.WriteArray and get the value 1000.
(ReadArray [0] = Write.WriteArray [0] returns the value 0.)

The chart Read should represent all numbers from 1 - 1000 from bar 1 to bar 1000.
I tried to read the ReadArray or the Write.WriteArray with the methods
for (int i = 0; i <(..) Array.Length; i ++) {...}
foreach {...}
while {...}

But I always only get the last value 1000 in Chart Read.

If I print in the chart Read Write.WriteArray [x] at the position x (x = 100, 500, ...), I always get 0;
Is it possible that the data will be lost? But the last value 1000 is present!

The problem should be in the historical assignment.
My WriteArray is one-dimensional and contains no further indexing.
Because the number of bars in both charts is identical, I tried to synchronize the arrays with a counter in the chart read.
Unfortunately, without success: Chart Read tries to load the data and gets no result ....
I am thankful for every hint!

Reply With Quote
 
(login for full post details)
  #8 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
I am thankful for every hint!

You will need to attach a zip or post or PM the .cs for me to see much further in I think.

Travel Well
Visit my futures io Trade Journal Reply With Quote
 
(login for full post details)
  #9 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Thank you for your support.
Enclosed a zip file with the example code ..

Attached Files
Register to download File Type: zip WriteRead.zip (4.4 KB, 5 views)
Reply With Quote
The following user says Thank You to user10 for this post:
 
(login for full post details)
  #10 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received



user10 View Post
Thank you for your support.
Enclosed a zip file with the example code ..

[deleted here where I wrote about using State.Configure rather than State.DataLoaded but shouldn't matter and I think is a red-herring in any case]

Any global sharing I have done has used static classes outside of the Ninja namespaces, as I'm not sure whether it honours public static declarations inside its own non-static indicator/straegy instances as you are trying to do.

They can still be in the same .cs file, but before the Ninja strategy/indicator, e.g.

public static class myData
{
public static int[] array = new int[1000];
}

The shared area is pre-allocated at compile time so you can delete the OnStateChange allocation code and it is referenced in the same way i.e. test = myData.array[0];

I think this is the better way, let me know.

Cheers

Travel Well
Visit my futures io Trade Journal Reply With Quote
 
(login for full post details)
  #11 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received

And always, always, always remember that global data connections are lost every time you recompile anything, all charts need to be reloaded. Pain in the neck but compared to development times when I was a boy... still luxury.

Travel Well
Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to ratfink for this post:
 
(login for full post details)
  #12 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

I tried to implement your message; Unfortunately, only the current data is transmitted.

Ninjatrader Support does not support static methods. However, I have received the following notice from the forum:

"The strategies you have provided do not reference each other in any way and do not share data in any way.
*The arrays created are within the scope of the instance of the class and can only be accessible in that class."

The suggested method is the use of a shared method. I also tried to implement these.
Unfortunately without success. I only ever get the realtime data ....

Enclosed the current zip file.

Is there really only the solution via streamwriter / streamreader?

Many thanks for the support!

Attached Files
Register to download File Type: zip WriteRead1.zip (4.5 KB, 5 views)
Reply With Quote
The following user says Thank You to user10 for this post:
 
(login for full post details)
  #13 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received

Hi @user10

It's not so hard! NinjaTrader don't support it because it needs care if you are in a development/compile cycle where it is easy to forget to refresh the sharers, but when trading this shouldn't be the case anyway. I have many NT7 projects that reliably use this mechanism in far more complex ways.

I have quickly created a simple demonstration of the concept for you and attached it here, a simple writer that puts the current price into the array, indexed by CurrentBar (with the % modifier to ensure safe wrap-around).

Note where the shared array is declared in the code file.

If you put rfkWriter into a chart, then put rfkReader into another chart (obviously more useful if same time bars but not essential) then you can see the following example with YM prices displayed as an indicator in an ES chart. The value can of course be anything you want it to be.



Of course the reader chart will only see historical data if the writer was loaded first.

Cheers

Travel Well
Attached Files
Register to download File Type: zip rfkWriteRead.zip (2.6 KB, 11 views)
Visit my futures io Trade Journal Reply With Quote
The following 3 users say Thank You to ratfink for this post:
 
(login for full post details)
  #14 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

I bow to the pro!
You made my day!!!
Thank you very much

Reply With Quote
The following user says Thank You to user10 for this post:
 
(login for full post details)
  #15 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
I bow to the pro!
You made my day!!!
Thank you very much

Great!

You can improve it in many ways, e.g add rfkSDT.array[(CurrentBar + 1) % 1000)] = 0; to the writer so it forward clears a buffer slot, then the reader can check for zero (or any magic number of your choice) to see whether to update the plot or not.

There are lock issues to be considered between threads, but most of these can be bypassed if you use simple action variable + incrementing sequencer counters.

Have fun

Cheers

Travel Well
Visit my futures io Trade Journal Reply With Quote
 
(login for full post details)
  #16 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Hello ratfink,

In the meantime, I have rewritten my program and it works flawlessly during operation.
When I program, there are occasional program crashes.

When I activate the strategy, I get the error message:
"Error calling the 'OnStateChange' method: The object reference was not set to an instance of an object."
I tried to catch the error with the following code:
if (Write.WriteArray [CurrentBar]! = null) ReadArray [0] = Write.WriteArray [CurrentBar];
Unfortunately that is not successful.

NinjaTrader.Cbi.Log.Process gives me no further information ...

Do you have an idea how I can solve the problem?

Many Thanks!

Reply With Quote
 
(login for full post details)
  #17 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
Do you have an idea how I can solve the problem?

I always use simple

Print("x happened value is " + value);

type statements first off, sometimes with counters/flags to limit unnecessary output

and try/catch blocks next, often good practice to leave these in anyway, e.g.

try
{
// put my dodgy code in here
}
catch (Exception ex)
{
Print("my code failed");
Print (ex.ToString();
}

Puts output to the Ninja Output window.

Cheers

p.s. I notice you are using CurrentBar with no wrap around, you will need very big arrays if you have lots of bars of data, check the values there, and that your arrays are as you would expect them to be allocated. This sort of stuff is non-trivial to get right.

Travel Well
Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to ratfink for this post:
 
(login for full post details)
  #18 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Thank you for your support!

I found the mistake:
else if(State == State.Terminated)
{
if (Position.MarketPosition == MarketPosition.Long) {ExitLong();}
if (Position.MarketPosition == MarketPosition.Short) {ExitShort();}
}

Actually, I just wanted to clear all orders and positions when the strategy ended.

What do you mean by wrap around?

Reply With Quote
 
(login for full post details)
  #19 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
Thank you for your support!

I found the mistake:
else if(State == State.Terminated)
{
if (Position.MarketPosition == MarketPosition.Long) {ExitLong();}
if (Position.MarketPosition == MarketPosition.Short) {ExitShort();}
}

Actually, I just wanted to clear all orders and positions when the strategy ended.

What do you mean by wrap around?

Good news, and good work!

By wrap around I mean using the '%' operator to compress an incrementing counter into a fixed range to use as an indexer, e.g.

 
Code
value = array[count % 128];   // gets us a 0 - 127 subscript

Travel Well
Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to ratfink for this post:
 
(login for full post details)
  #20 (permalink)
Dresden + Germany
 
 
Posts: 21 since Feb 2018
Thanks: 11 given, 5 received

Thanks for the hint!
I save historical and real-time data.
The number of historical dates is known;
if I limit the array to the number of historical data with%, will the realtime data be transferred correctly?

Reply With Quote
The following user says Thank You to user10 for this post:
 
(login for full post details)
  #21 (permalink)
Birmingham UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
 
ratfink's Avatar
 
Posts: 3,651 since Dec 2012
Thanks: 17,422 given, 8,403 received


user10 View Post
Thanks for the hint!
I save historical and real-time data.
The number of historical dates is known;
if I limit the array to the number of historical data with%, will the realtime data be transferred correctly?

Yes, you can limit it to any smaller number so long as the overwrite is no problem, I use this often.

Travel Well
Visit my futures io Trade Journal Reply With Quote
The following user says Thank You to ratfink for this post:


futures io Trading Community Platforms and Indicators NinjaTrader > Share Data between two Charts


Last Updated on July 3, 2018


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Battlestations! Show us your trading desk - $1,500 in prizes!

March
 

Importance of Finding Your Own Way w/Adam Grimes

Elite only
 

Journal Challenge w/Jigsaw

April
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts