NexusFi: Find Your Edge


Home Menu

 





Need help with C# list in Strategy


Discussion in NinjaTrader

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




 
Search this Thread

Need help with C# list in Strategy

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

Hello, C# gurus...

Attached is a strategy that shows a conundrum (for me).

I have a List of Struct....

If the elements of the List Struct are Private I cannot access them inside the strategy..."inaccessible due to its protection level" compiler error. Not sure why.

When the code is as is, it will compile.

BUT when I load multiple instruments in Strategy Analyzer with this strategy, the list acts like it is global...i.e. public.

If you run this strategy against multiple instruments at the same time in Strategy Analyzer, for every 10 bar it will print (for each instance) the List.

As you would see, the list for any instrument shows the data loaded by the last strategy to execute. In other words, it is acting like a public list...

Probably the answer is to have the Struct elements Private...but then it won't compile...

It is the same if I change the Struct to a Class.

There must be some simple nuance I'm missing...can anyone tell me how to fix this?

Many thanks!

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

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Deepmoney LLM
Elite Quantitative GenAI/LLM
Build trailing stop for micro index(s)
Psychology and Money Management
Exit Strategy
NinjaTrader
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
 
  #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


Your problem is due to the fact you create it as a static, static means it has no instantiation
and it's shared across multiple, making different strategies overwrite the same object.


Try this approach :
 
Code
List<tester> testerList = new List<tester>();	
		
public class tester
		{
			public string		instrumentName {get;set;}
			public int			counter {get;set;}
		};
When adding to the list you create an object

 
Code
	private void DoFirstTime()
		{
			firstTime = false;
			
			testerList.Clear();
			
			tester tl = new test();
			
			for (int i = 0; i < 10; i++)
			{
				tl.instrumentName = Instrument.FullName;
				tl.counter 		  = i;
				
				testerList.Add(tl);
			}							
			
			PrintList();
		}

Follow me on Twitter 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

I put this inside the loop

tester tl = new test();

works good...!!

though....

what is the difference between

List<tester> testerList = new List<tester>();

and

private static List<tester> testerList = new List<tester>();

in this case?

Started this thread Reply With Quote
  #5 (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
I put this inside the loop

tester tl = new test();

works good...!!

though....

what is the difference between

List<tester> testerList = new List<tester>();

and

private static List<tester> testerList = new List<tester>();

in this case?

If you create the list as a static, you are referring to the same memory in all
indicators/strategies that call it.. (google c# static for class, members .. it will
be explained in more detail

Without the static, the new creates a new object, that is different for every
invokation

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

what is confusing to me is....

there is one strategy code base

but when multiple instance of the strategy are running...that is one each on multiple Instruments...

then I was expecting each instance of the strategy to be isolated from the other, completely

but this does not appear to be the case

private static List<tester> testerList = new List<tester>();

the "private static", I was expecting to only have the scope of the single instance running, not all instances of the code base

but, no?

Started this thread Reply With Quote
  #7 (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
what is confusing to me is....

there is one strategy code base

but when multiple instance of the strategy are running...that is one each on multiple Instruments...

then I was expecting each instance of the strategy to be isolated from the other, completely

but this does not appear to be the case

private static List<tester> testerList = new List<tester>();

the "private static", I was expecting to only have the scope of the single instance running, not all instances of the code base

but, no?

Then you don't need static.
Static would be if you want a central place to hold something that you can refer to from, from different places

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

so it seems the scope of static is across all running instances of the strategy...

C# is a different model than I'm used to

still, with help of people like you in futures.io, I have been able to implement what I need

many thanks!

Started this thread Reply With Quote




Last Updated on October 27, 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