Need help with C# list in Strategy - futures io
futures io futures trading



Need help with C# list in Strategy


Discussion in NinjaTrader

Updated
    1. trending_up 721 views
    2. thumb_up 3 thanks given
    3. group 4 followers
    1. forum 7 posts
    2. attach_file 1 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
 

Need help with C# list in Strategy

(login for full post details)
  #1 (permalink)
Neuquen, Argentina
 
Experience: Intermediate
Platform: Ninja
Trading: CL, TF, 6S, NG, sometimes GC
 
Posts: 200 since Jul 2012
Thanks: 25 given, 42 received

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
Register to download File Type: cs A00Test.cs (1.9 KB, 7 views)
Started this thread Reply With Quote

Journal Challenge February 2021 results (so far):
Competing for $1500 in prizes from Topstep
looks_oneSBtrader82 's Trading Journalby SBtrader82
(169 thanks from 31 posts)
looks_twoJust BEING a Trader: Letting Go!!by iqgod
(116 thanks from 33 posts)
looks_3Wisdom is Emptinessby Mtype
(68 thanks from 25 posts)
looks_4Deetee’s DAX Trading Journal (time based)by Deetee
(31 thanks from 17 posts)
looks_5Journal for peanuts1956by peanuts1956
(23 thanks from 13 posts)
 
 
(login for full post details)
  #3 (permalink)
Gits (Hooglede) Belgium
 
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
 
rleplae's Avatar
 
Posts: 2,991 since Sep 2013
Thanks: 2,437 given, 5,801 received


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 futures io Trade Journal Reply With Quote
The following user says Thank You to rleplae for this post:
 
(login for full post details)
  #4 (permalink)
Neuquen, Argentina
 
Experience: Intermediate
Platform: Ninja
Trading: CL, TF, 6S, NG, sometimes GC
 
Posts: 200 since Jul 2012
Thanks: 25 given, 42 received

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
 
(login for full post details)
  #5 (permalink)
Gits (Hooglede) Belgium
 
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
 
rleplae's Avatar
 
Posts: 2,991 since Sep 2013
Thanks: 2,437 given, 5,801 received


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 futures io Trade Journal Reply With Quote
The following user says Thank You to rleplae for this post:
 
(login for full post details)
  #6 (permalink)
Neuquen, Argentina
 
Experience: Intermediate
Platform: Ninja
Trading: CL, TF, 6S, NG, sometimes GC
 
Posts: 200 since Jul 2012
Thanks: 25 given, 42 received

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
 
(login for full post details)
  #7 (permalink)
Gits (Hooglede) Belgium
 
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
 
rleplae's Avatar
 
Posts: 2,991 since Sep 2013
Thanks: 2,437 given, 5,801 received


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 futures io Trade Journal Reply With Quote
The following user says Thank You to rleplae for this post:
 
(login for full post details)
  #8 (permalink)
Neuquen, Argentina
 
Experience: Intermediate
Platform: Ninja
Trading: CL, TF, 6S, NG, sometimes GC
 
Posts: 200 since Jul 2012
Thanks: 25 given, 42 received

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


futures io Trading Community Platforms and Indicators NinjaTrader > Need help with C# list in Strategy


Last Updated on October 27, 2017


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Journal Challenge w/$1500 prizes from Topstep!

February
 

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

March
 

Call Option Buying: The New Pain Trade? w/Carley Garner

Elite only
     



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