NexusFi: Find Your Edge


Home Menu

 





foreach loop problem


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one gomad with 6 posts (0 thanks)
    2. looks_two Fat Tails with 5 posts (6 thanks)
    3. looks_3 gulabv with 2 posts (1 thanks)
    4. looks_4 Big Mike with 1 posts (0 thanks)
    1. trending_up 6,378 views
    2. thumb_up 9 thanks given
    3. group 3 followers
    1. forum 14 posts
    2. attach_file 4 attachments




 
Search this Thread

foreach loop problem

  #1 (permalink)
 gomad 
Tirgu Mures, Romania
 
Experience: Intermediate
Platform: Ninja
Broker: ZenFire
Trading: CL, GC
Posts: 36 since Sep 2010
Thanks Given: 42
Thanks Received: 7

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.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Exit Strategy
NinjaTrader
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Are there any eval firms that allow you to sink to your …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
The Program
18 thanks
Battlestations: Show us your trading desks!
18 thanks
  #2 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102

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
Thanked by:
  #3 (permalink)
 
gulabv's Avatar
 gulabv 
Dallas, TX
 
Experience: Beginner
Platform: Ninjatrader
Broker: Zen-Fire
Trading: ZN, 6E
Posts: 286 since May 2010
Thanks Given: 161
Thanks Received: 169


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
Thanked by:
  #4 (permalink)
 gomad 
Tirgu Mures, Romania
 
Experience: Intermediate
Platform: Ninja
Broker: ZenFire
Trading: CL, GC
Posts: 36 since Sep 2010
Thanks Given: 42
Thanks Received: 7


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.

Started this thread Reply With Quote
  #5 (permalink)
 
gulabv's Avatar
 gulabv 
Dallas, TX
 
Experience: Beginner
Platform: Ninjatrader
Broker: Zen-Fire
Trading: ZN, 6E
Posts: 286 since May 2010
Thanks Given: 161
Thanks Received: 169

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

Reply With Quote
  #6 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


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)
 gomad 
Tirgu Mures, Romania
 
Experience: Intermediate
Platform: Ninja
Broker: ZenFire
Trading: CL, GC
Posts: 36 since Sep 2010
Thanks Given: 42
Thanks Received: 7


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.

Started this thread Reply With Quote
  #8 (permalink)
 gomad 
Tirgu Mures, Romania
 
Experience: Intermediate
Platform: Ninja
Broker: ZenFire
Trading: CL, GC
Posts: 36 since Sep 2010
Thanks Given: 42
Thanks Received: 7


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.

Started this thread Reply With Quote
  #9 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


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)
 gomad 
Tirgu Mures, Romania
 
Experience: Intermediate
Platform: Ninja
Broker: ZenFire
Trading: CL, GC
Posts: 36 since Sep 2010
Thanks Given: 42
Thanks Received: 7



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
Elite Membership required to download: StatsIndicatorFor.zip
Elite Membership required to download: StatsIndicatorForeach.zip
Started this thread Reply With Quote




Last Updated on May 17, 2011


© 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