NexusFi: Find Your Edge


Home Menu

 





C# question -- public static methods


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one vantojo with 3 posts (0 thanks)
    2. looks_two rleplae with 3 posts (0 thanks)
    3. looks_3 SpeculatorSeth with 2 posts (0 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 1,497 views
    2. thumb_up 0 thanks given
    3. group 3 followers
    1. forum 8 posts
    2. attach_file 0 attachments




 
Search this Thread

C# question -- public static methods

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

Hello,

for NT7.

I have shared routines between different indicators, even strategies.

I've created a new .cs file in the Indicator folder with it's own "namespace AAGlobal" and "public class Methods". Then inside this are the individual methods. The Ninja editor opens this "Indicator" and it compiles fine.

The methods in this class I have defined as "public static", and these can be referenced successfully (i.e. compiled) in other Indicators and Strategies when referenced with using AAGlobal and then qualifying the method in the calling statement.
"
My question is....what happens if multiple indicators and strategies execute this code at "the same time"?

Would it be better to not have the methods declared "static", and instantiate the method in the calling program?

Thanks

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
Trade idea based off three indicators.
Traders Hideout
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Diary of a simple price action trader
26 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
Tao te Trade: way of the WLD
22 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #3 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863



vantojo View Post
Hello,

for NT7.

I have shared routines between different indicators, even strategies.

I've created a new .cs file in the Indicator folder with it's own "namespace AAGlobal" and "public class Methods". Then inside this are the individual methods. The Ninja editor opens this "Indicator" and it compiles fine.

The methods in this class I have defined as "public static", and these can be referenced successfully (i.e. compiled) in other Indicators and Strategies when referenced with using AAGlobal and then qualifying the method in the calling statement.
"
My question is....what happens if multiple indicators and strategies execute this code at "the same time"?

Would it be better to not have the methods declared "static", and instantiate the method in the calling program?

Thanks

Theorethically you would run into a multi-threading issue, but i thin NT7 is not that much of a multi-threading beast
you would then have to put locks around critical sections

What was the reason to make those things 'static' in the first place ?

Another pattern is to use the singleton, if you really need multiple threads to share the same thing...

Depends what nut you want to crack

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012


rleplae View Post
Theorethically you would run into a multi-threading issue, but i thin NT7 is not that much of a multi-threading beast
you would then have to put locks around critical sections

What was the reason to make those things 'static' in the first place ?

Another pattern is to use the singleton, if you really need multiple threads to share the same thing...

Depends what nut you want to crack

Ok, that is what I wanted to know...the potential for collision....I will remove the "static" and instantiate each use....multiple indicators or strategies may want to execute some of these common routines "at the same time", more or less.

Other methods are preferred to be static because they manage global (public) static variables and will only be called by one instance of one class when the workspaces first load.

Started this thread Reply With Quote
  #5 (permalink)
 SpeculatorSeth   is a Vendor
 
Posts: 780 since Apr 2016
Thanks Given: 22
Thanks Received: 1,018


vantojo View Post
Hello,

for NT7.

I have shared routines between different indicators, even strategies.

I've created a new .cs file in the Indicator folder with it's own "namespace AAGlobal" and "public class Methods". Then inside this are the individual methods. The Ninja editor opens this "Indicator" and it compiles fine.

The methods in this class I have defined as "public static", and these can be referenced successfully (i.e. compiled) in other Indicators and Strategies when referenced with using AAGlobal and then qualifying the method in the calling statement.
"
My question is....what happens if multiple indicators and strategies execute this code at "the same time"?

Would it be better to not have the methods declared "static", and instantiate the method in the calling program?

Thanks

There is nothing wrong with this. Both processes can access the same piece of code at the same time. The execution code is basically just a location in memory, and both call stacks point to the same location.

It's data where you can run into concurrency issues. For instance, if you are using the singleton pattern you can have different pieces of code modifying the same variables.

Reply With Quote
  #6 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863


TWDsje View Post
There is nothing wrong with this. Both processes can access the same piece of code at the same time. The execution code is basically just a location in memory, and both call stacks point to the same location.

It's data where you can run into concurrency issues. For instance, if you are using the singleton pattern you can have different pieces of code modifying the same variables.

If two threads access the same data (in true multi-threading), then you DO have an issue
but i think NT 7/8 does NT execute in real multi-threading

In my code outside NT i need to keep an eye on that, or you get an exception...

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

I have a custom architecture that when NT boots and the primary workspace opens, one process (running from Market Analyzer) reads a SQL database and loads public static arrays and variables with control/configuration/news data.

Then, later multiple other processes when they are started, read this global data (arrays/variables) and load selected information they need for processing into local process variables.

Sometimes these secondary processes (indicators, strategies), write back status information into the global static arrays/variables to be shared with any process that needs it.

Having the main boot process in the Market Analyzer (against one instrument) was the only way I could figure out in NT to have a boot process load system wide, shared data. It works, but it was not really what I wanted to do.

Started this thread Reply With Quote
  #8 (permalink)
 SpeculatorSeth   is a Vendor
 
Posts: 780 since Apr 2016
Thanks Given: 22
Thanks Received: 1,018


rleplae View Post
If two threads access the same data (in true multi-threading), then you DO have an issue
but i think NT 7/8 does NT execute in real multi-threading

In my code outside NT i need to keep an eye on that, or you get an exception...

Yes as I said if two threads are accessing the same variables then you could have problems. This is why singletons become relevant at this point. There are many suggestions about how to write singletons to avoid these kinds of issues. Just search for C# Singletons.

Reply With Quote
  #9 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863


TWDsje View Post
Yes as I said if two threads are accessing the same variables then you could have problems. This is why singletons become relevant at this point. There are many suggestions about how to write singletons to avoid these kinds of issues. Just search for C# Singletons.

My understanding of a singleton is to have a class that instantiates to the same instance
so that two processes calling the class, are using the same data.

I used it in a trading risk module that is implemented as a DLL and shared by multiple threads

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote




Last Updated on September 13, 2017


© 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