foreach loop problem - NinjaTrader Programming | futures io social day trading
futures io futures trading


foreach loop problem
Updated: Views / Replies:3,200 / 14
Created: by gomad Attachments:4

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors – all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you don’t need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

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

Reply
 4  
 
Thread Tools Search this Thread
 

foreach loop problem

  #1 (permalink)
Elite Member
Tirgu Mures, Romania
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: ZenFire
Favorite Futures: CL, GC
 
Posts: 36 since Sep 2010
Thanks: 42 given, 6 received

foreach loop problem

I have a problem with "foreach loop" in NT. My code is as follows:

 
Code
protected override void OnBarUpdate()
        {
			int[] values = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,};
			int Up = 0;
			int Down = 0;

			foreach(int i in values)
			{
				if (Close[i] > Open[i])
				
  			  Up = Up + 1;
			}
			
			Plot0.Set(Up);
			
			
			foreach(int i in values)
			{
				if (Close[i] < Open[i])
				
  			  Down = Down + 1;
			}
			
			Plot1.Set(Down);
						
        }
The indicator works fine if the collection doesn't contain number 13 (or higher), but with number 13 it doesn't return any number on my plots.

Are there some limitations in NT soft? Or is there something that I'm doing wrong?


And another question about collections. Is there a method to use some interval (something like.. from 1 to 100) instead enumerating each element of collection?

My collection is nt[] values = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,};. But if I want my collection to contain 100 elements, writing these numbers manually is not a very good solution.

Thank you.

Reply With Quote
 
  #2 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,653 since Mar 2010
Thanks: 4,226 given, 25,601 received
Forum Reputation: Legendary

If you want to use more than 13 numbers, you could define an ArrayList object and introduce a period. For 100 elements, it would look like this:

 
Code
#region Variables
private int period = 100;
private ArrayList myArray = new ArrayList();

protected override void OnStartUp()
{
    for (int i = 0; i < period; i++)
            myArray.Add( (int) i);  // better start with zero to include the current bar
}
Your array, which is variable-sized should now contain the numbers from 0 to 99.
 
Code
private override void OnBarUpdate():
{
        int up = 0;
        int down = 0;
        for (int i = 0; i < period; i++)
        {
            if (Close[i] > Open[i])
                  up = up +1;
            else if (Close[i] < Open[i])
                  down = down +1;
        }
        Plot0.Set(up);
        Plot1.Set(down);
}
Now as you note, the array has not been used so far. You can now store other values in the array and access them accordingly. But I do not understand, what you need it for.

Reply With Quote
The following 2 users say Thank You to Fat Tails for this post:
 
  #3 (permalink)
Elite Member
Dallas, TX
 
Futures Experience: Beginner
Platform: Ninjatrader
Broker/Data: Zen-Fire
Favorite Futures: ZN, 6E
 
gulabv's Avatar
 
Posts: 284 since May 2010
Thanks: 159 given, 155 received

Use for loop instead


Not sure what you are trying to achieve but you could try using a for loop

for (int i=1; i < 101; i++)
{
// do something
}

You can replace the number 101 with a constant variable to make it easier for you to change the number of times to loop through

Reply With Quote
The following user says Thank You to gulabv for this post:
 
  #4 (permalink)
Elite Member
Tirgu Mures, Romania
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: ZenFire
Favorite Futures: CL, GC
 
Posts: 36 since Sep 2010
Thanks: 42 given, 6 received

Problem persists


Fat Tails View Post
If you want to use more than 13 numbers, you could define an ArrayList object and introduce a period. For 100 elements, it would look like this:

 
Code
#region Variables
private int period = 100;
private ArrayList myArray = new ArrayList();

protected override void OnStartUp()
{
    for (int i = 0; i < period; i++)
            myArray.Add( (int) i);  // better start with zero to include the current bar
}
Your array, which is variable-sized should now contain the numbers from 0 to 99.
 
Code
private override void OnBarUpdate():
{
        int up = 0;
        int down = 0;
        for (int i = 0; i < period; i++)
        {
            if (Close[i] > Open[i])
                  up = up +1;
            else if (Close[i] < Open[i])
                  down = down +1;
        }
        Plot0.Set(up);
        Plot1.Set(down);
}
Now as you note, the array has not been used so far. You can now store other values in the array and access them accordingly. But I do not understand, what you need it for.

I want to create an indicator which returns different statistical data within a predefined number of bars or events in the past. And I want to use this data in some statistical tests (lile Student Test, Fisher, etc.). You can call it some sort of „scientific trading” if you want. One day I will start a thread about this, but I need some results before.

Now, regarding the code you provided above. Here is still the same problem. If the period is higher than 13, the „for loop” doesn't return any value. If it is 13 or lower, the indicator works fine.

I supose that the ArrayList idea is working just for "foreach" loop. I will test it now.

Thank you.

Reply With Quote
 
  #5 (permalink)
Elite Member
Dallas, TX
 
Futures Experience: Beginner
Platform: Ninjatrader
Broker/Data: Zen-Fire
Favorite Futures: ZN, 6E
 
gulabv's Avatar
 
Posts: 284 since May 2010
Thanks: 159 given, 155 received

Do you have more than 13 bars on the chart with this indicator?

Reply With Quote
 
  #6 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,653 since Mar 2010
Thanks: 4,226 given, 25,601 received
Forum Reputation: Legendary


gomad View Post
I want to create an indicator which returns different statistical data within a predefined number of bars or events in the past. And I want to use this data in some statistical tests (lile Student Test, Fisher, etc.). You can call it some sort of „scientific trading” if you want. One day I will start a thread about this, but I need some results before.

Now, regarding the code you provided above. Here is still the same problem. If the period is higher than 13, the „for loop” doesn't return any value. If it is 13 or lower, the indicator works fine.

I supose that the ArrayList idea is working just for "foreach" loop. I will test it now.

Thank you.

If you have 13 values added, the first value should be 0 and the last value 12. Also you cannot access bars with a negative index.

For 13 values you would need at least 13 bars, so you should add:

 
Code
private override void OnBarUpdate()
{
    if (CurrentBar < 13)
         return;
}

Reply With Quote
 
  #7 (permalink)
Elite Member
Tirgu Mures, Romania
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: ZenFire
Favorite Futures: CL, GC
 
Posts: 36 since Sep 2010
Thanks: 42 given, 6 received


Fat Tails View Post
If you have 13 values added, the first value should be 0 and the last value 12. Also you cannot access bars with a negative index.

For 13 values you would need at least 13 bars, so you should add:

 
Code
private override void OnBarUpdate()
{
    if (CurrentBar < 13)
         return;
}

Yes, I understand this things, but the problem is that it works with any number lower then 13, but not with higher numbers.

For example, the „foreach loop” is working fine with 12, but doesn't calculate anything with number 13. It is the same code, the only thing which differs is the number of candels to look back.

I can't understand why it is working with 12 or lower, but not with 13 or higher.

The same problem with the "for loop". It is working now with number 13 or lower, but not with 14 or higher.

Are there some "build in" limitations in NT?

I want to explain now what I want to obtain.

I want my indicator to return the number of events in some predefined back period. In example above I have a simple condition Close[i] > Open[i] and viceversa. The code you wrote above (for loop) and my code (foreach loop) are working well for any value lower than 14 (for loop) and for any value lower than 13 (foreach loop).

The indicator compiles no matter if I use a higher number than 14. But if I use this values, the indicator - when loaded on chart - returns no value. If I use a lower number, the indicator returns the correct values on plots.

And this is strange, because I left the code intact and the only thing which is changed is the number of bars.

I hope that this time I was clearer.

Reply With Quote
 
  #8 (permalink)
Elite Member
Tirgu Mures, Romania
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: ZenFire
Favorite Futures: CL, GC
 
Posts: 36 since Sep 2010
Thanks: 42 given, 6 received


gulabv View Post
Do you have more than 13 bars on the chart with this indicator?

When loaded on chart, I have more than 13 bars on chart. But the indicator returns no value on plots.

Reply With Quote
 
  #9 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,653 since Mar 2010
Thanks: 4,226 given, 25,601 received
Forum Reputation: Legendary


gomad View Post
When loaded on chart, I have more than 13 bars on chart. But the indicator returns no value on plots.

Why don't you post your indicator here? That would make it easier for us to find out why it does not run.

Reply With Quote
 
  #10 (permalink)
Elite Member
Tirgu Mures, Romania
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: ZenFire
Favorite Futures: CL, GC
 
Posts: 36 since Sep 2010
Thanks: 42 given, 6 received

Upload



Fat Tails View Post
Why don't you post your indicator here? That would make it easier for us to find out why it does not run.

Here are both indicators. One is using „for loop”, the another is using „foreach loop”.

One mention: select „New Panel” when you load the indicator on chart.

Thanks fo help.

Attached Files
Register to download File Type: zip StatsIndicatorFor.zip (1.8 KB, 15 views)
Register to download File Type: zip StatsIndicatorForeach.zip (2.1 KB, 17 views)
Reply With Quote

Reply



futures io > > > > > foreach loop problem

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
ZigZagUTC problem lookOutBelow NinjaTrader 7 February 20th, 2013 02:27 PM
OODA Loop and Trading...... tradersam Psychology and Money Management 1 May 18th, 2011 03:36 PM
Translate For Loop in Easy Language to NT C# Crassius NinjaTrader Programming 6 February 23rd, 2011 08:34 AM
could be a problem websouth Off-Topic 11 February 1st, 2010 02:04 PM
Problem with Thanks Given; Thanks Received lolu Feedback and Announcements 2 December 14th, 2009 08:30 AM


All times are GMT -4. The time now is 10:18 AM.

Copyright © 2017 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
Page generated 2017-12-15 in 0.13 seconds with 20 queries on phoenix via your IP 54.221.76.68