Trading Articles
Article Categories
Article Tools
Need help with C# list in Strategy
Updated October 27, 2017
trending_up
721 views
thumb_up
3 thanks given
group
4 followers
forum
7 posts
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)
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!
Best Threads (Most Thanked) in the last 7 days on futures io
(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
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();
}
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?
(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
Posts: 2,991 since Sep 2013
Thanks: 2,437 given,
5,801
received
vantojo
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
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?
(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
Posts: 2,991 since Sep 2013
Thanks: 2,437 given,
5,801
received
vantojo
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
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!
Last Updated on October 27, 2017
Right now
Ongoing
Right now
February
Coming soon
March
Register to Attend
Elite only