NexusFi: Find Your Edge


Home Menu

 





Architecture / Public Array / Instantiate


Discussion in NinjaTrader

Updated
    1. trending_up 1,862 views
    2. thumb_up 7 thanks given
    3. group 2 followers
    1. forum 8 posts
    2. attach_file 3 attachments




 
Search this Thread

Architecture / Public Array / Instantiate

  #1 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

Hello All,

(addressed to the more advanced coders...)

I have an architecture that uses Public variables to communicate between decoupled indicators and strategies, or between two indicators. This allows me more flexibility and modularity than binding say, an indicator to a strategy.

This works fine as long as long as I only want to communicate between two or more objects (indicators/strategies) that are working on the exact same Instrument.

However, I need to take it to the next step...and have this work for multiple instruments.

My goal is to create a dynamic real time pool of in-memory public data for each instrument I monitor that can be accessed by any executing object.

I want this data to be available simply through public variables, and not say, an external source, like an in-memory SQL datastore. (I don't want to issue an I-O just to get the variables, on each tick the pool needs to be immediately available.)

I have this implemented (for a single instrument) by way of a non executing indicator with a Public variables. I call it Common.

It would be so simple to put in a public array or list, with each entry in the list being a structure for a specific instrument.

However, unlike the public variables I use now, and are available to all executing objects without the Common Indicator even being executed, lists and arrays are not instantiated until run time.

This indicator is a global store, and not tied to any instrument, strategy, or indicator. It is not executed.

I don't know how to get around this issue in what I know about Ninja....any ideas?

Thank you.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
NexusFi Journal Challenge - May 2024
Feedback and Announcements
MC PL editor upgrade
MultiCharts
REcommedations for programming help
Sierra Chart
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
48 thanks
Just another trading journal: PA, Wyckoff & Trends
35 thanks
Tao te Trade: way of the WLD
26 thanks
Bigger Wins or Fewer Losses?
24 thanks
GFIs1 1 DAX trade per day journal
20 thanks
  #3 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425



vantojo View Post
I don't know how to get around this issue in what I know about Ninja....any ideas?

I use a public SortedDictionary for exactly this purpose, properties are basically ascii strings with int values (could be doubles instead, I will probably switch this at some stage). Properties can be added from anywhere and referenced from anywhere across charts or workspaces. I also have a network distributed system on top of it, so I know the mechanism is reliable. I am contemplating extending it to use DataSeries in the Properties but have not done so yet as I am currently more interested in discretionary than automated trading.

As I was a massive fan of Blake's 7 it is obviously called Orac ...

Basic data constructs:
 
Code
public class OracItem
{
    public int value;
    public int port;
}
	
 
public static class Orac
{
    public static Dictionary<string, OracItem> Properties = new Dictionary<string, OracItem>();
    public static int localPort = 0;		// set to publishPort by Clients stays at zero for Servers unless inbound data
    public static string  Lock = "LOCK";
}

Will tidy/improve a few things in time, e.g. Lock as object, but it's fine. I'm simple.
The two main interface indicators are simple :

To set a value:
 
Code
    public class OracSet : Indicator
    {
        #region Variables
        // Wizard generated variables
            private string property = ""; // Default setting for Property
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Overlay = true;
            CalculateOnBarClose = false;
			
            if (property != "")
            {
                lock (Orac.Lock)
                {
                    OracItem  oi;
					
                    if (!Orac.Properties.ContainsKey (property))
                    {
                        Orac.Properties.Add (property, new OracItem());
                    }
					
                    oi = Orac.Properties[property];
					
                    oi.port  = Orac.localPort;
                    oi.value = 0;
                }
            }
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (property != "")
            {
                 lock (Orac.Lock)
                {
                    Orac.Properties[property].value = (int) Close[0];
                }
            }
        }
To Plot a value:
 
Code
    public class OracPlot : Indicator
    {
        #region Variables
        // Wizard generated variables
            private string property = ""; // Default setting for Property
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Add(new Plot(Color.FromKnownColor(KnownColor.SeaGreen), PlotStyle.Line, "Plot0"));

            Overlay = false;
            CalculateOnBarClose = false;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (property != "")
            {
                int  value = 0;
				
                lock (Orac.Lock)
                {
                    if (Orac.Properties.ContainsKey (property))
                    {
                        value = Orac.Properties[property].value;
                    }
                }
				
                Plot0.Set(value);
            }
        }


    }
Using this route it's easy to set/plot indicators across charts/workspace from any standard Ninja Data series or Indy series using the standard user interaction. But as yet I don't support historical data, that's the biggest current limitation, happy to think about the future if there is more general interest in this area or maybe that is enough idea to help?

Cheers.

p.s. seem to remember there was something else about global variables on futures.io (formerly BMT) too but that might be TradeStation

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

Thanks RF....here is what I'm thinking..something like this (attachment)...but I need to access globally, in all objects, the array that was instantiated in this indicator, in the Initialize.

I would love to not have to have code to init the array...but it appears this is the only way it can be done in C#



AATest.cs

Started this thread Reply With Quote
  #5 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

not sure how to insert code...this is the attachment
==================================================================

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion

public class Global
{
public struct Instrument_Parameters
{
public int ix;
public string Instrument;
public bool Enable_Voice;

public string AccountName;
public string Action;
public bool ExitPosition;
public double Price;
public string ATM_Name;

};
}

namespace NinjaTrader.Indicator
{
public class AATest : Indicator
{
#region Variables

public static Instrument_Parameters[] G;

#endregion


#region Initialize
protected override void Initialize()
{
Print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>> BEGIN COMMON INITIALIZE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");


Overlay = false;

G = new Instrument_Parameters[12];

for (int i = 0; i<=11; i++)
{
G[i].ix = i;
G[i].Instrument = "";

switch (i)
{
case 0:
G[i].Instrument = "CL";
break;
case 1:
G[i].Instrument = "GC";
break;
case 2:
G[i].Instrument = "NG";
break;
case 3:
G[i].Instrument = "TF";
break;
case 4:
G[i].Instrument = "6S";
break;
case 5:
G[i].Instrument = "6A";
break;
case 6:
G[i].Instrument = "6E";
break;
case 7:
G[i].Instrument = "NQ";
break;
}

G[i].Enable_Voice = true;

G[i].AccountName = "";
G[i].Action = "";
G[i].ExitPosition = false;
G[i].Price = 0;
G[i].ATM_Name = "";
}

Print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>> END COMMON INITIALIZE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");


}
#endregion


#region OnBarUpdate
protected override void OnBarUpdate()
{
}
#endregion


#region Properties
#endregion
}
}

Started this thread Reply With Quote
  #6 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


vantojo View Post
Thanks RF....here is what I'm thinking..something like this (attachment)...but I need to access globally, in all objects, the array that was instantiated in this indicator, in the Initialize.

I would love to not have to have code to init the array...but it appears this is the only way it can be done in C#

You should be able to move 'G' into the Global class itself, it can then be referenced as Global.G[i].xxxxx, etc everywhere.

I guess you could also have the init code simplified into a seperate routine that just takes a string as parameter for the instrument name, as the rest of the init values appear to be common. I don't see a big deal to worry about having a dynamic rather than static init really, unless you need lots of instruments. I'm not a comprehensive C# expert so I'm sure there probably are many other ways as well that I just don't use.

For my own stuff I use a dynamic file reader/loader to read and Add instrument lists into multi-instrument indicators/strategies but unless I misunderstood I don't think that is what you are asking.

Cheers

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


vantojo View Post
not sure how to insert code...this is the attachment

For code inserts you just use the word CODE and then /CODE in square boxes as you would for QUOTE - just try making a Quote Reply of my post and look inside to see for a test.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
  #8 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

Yes, that is right....I don't want to do any I-O or function calls to access the data, just simply a variable reference.

this is because there will be multiple objects accessing this data, and on each tic of data...so it needs to be as simple, fast, and direct as possible.....

I think I tried having G in global...will try again...but I believe it would not compile...

C# is not my first language...

should it be something other than Static?

Started this thread Reply With Quote
  #9 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

to verify:

see attachments

load AATest into a Market Analyzer
and AATest2 onto a chart

when the Market Analyzer initializes AATest will instantiate the global array

every time there is a new bar AATest2 will access the global array created in AATest Initialize and print (all of) one of the array elements. Will test more later to make sure AATest2 can write into the global array.

If this works it is going to allow me to do what I need to do....have a memory based array of data available across all unbound indicators and strategies, and a way for them to share state data.

Then no need to bind objects, or for I-O (even to memory based stores) OR functions or methods to get/store data from/into the array.

Thank you Ratfink for the comraderie!!! It helped....

Attached Files
Elite Membership required to download: AATest.cs
Elite Membership required to download: AATest2.cs
Started this thread Reply With Quote




Last Updated on November 3, 2013


© 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