Routing a series of #'s through a condition...

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 639 Views - Last Post: 22 July 2013 - 08:07 PM Rate Topic: -----

#1 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Routing a series of #'s through a condition...

Posted 11 July 2013 - 06:19 PM

I am fairly new to programming as will likely be obvious..

For this code..
protected override void OnBarUpdate()
{
if (BarsInProgress == index)
{
if (SMA(BarsArray[INDEX],Fast)[0] > SMA(BarsArray[INDEX],Slow)[0] && Closes[index][0] > Highs[INDEX][1])
}
{
Do something
}
}


If I have "INDEX" is 0-10 and "index" is 11-20. And I want to first route through 0 for INDEX AND 11 for index through the code above, and then 1 for INDEX and 12 for index and so on, how would I go about making those route through? Or if you could potentially guide me to some relevant material that I can study?

Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: Routing a series of #'s through a condition...

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

Re: Routing a series of #'s through a condition...

Posted 11 July 2013 - 06:33 PM

Seems like a good use of for loops!
Was This Post Helpful? 0
  • +
  • -

#3 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 11 July 2013 - 06:47 PM

View Postmodi123_1, on 11 July 2013 - 06:33 PM, said:

Seems like a good use of for loops!


Would the below route it through like I had previously mentioned?...
protected override void OnBarUpdate()
{
for (int INDEX = 0; INDEX < 11; INDEX++)
for (int index = 11; index < 21; index++) 
if (BarsInProgress == index)
{
if (SMA(BarsArray[INDEX],Fast)[0] > SMA(BarsArray[INDEX],Slow)[0] && Closes[index][0] > Highs[INDEX][1])
}
{
Do something
}
}


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

Re: Routing a series of #'s through a condition...

Posted 11 July 2013 - 06:58 PM

What happens when you try it?
Was This Post Helpful? 0
  • +
  • -

#5 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 11 July 2013 - 07:16 PM

View Postmodi123_1, on 11 July 2013 - 06:58 PM, said:

What happens when you try it?


It compiles but it doesn't route the INDEXES or indexes through. It looks like it just takes INDEX 0 through and that's it.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,168
  • Joined: 05-May 12

Re: Routing a series of #'s through a condition...

Posted 11 July 2013 - 09:20 PM

Let me ask a silly question: What is the significance of BarsInProgress? Why must it be equal to index?

As an aside, don't you already have another topic about this same issue open?
Was This Post Helpful? 0
  • +
  • -

#7 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 12 July 2013 - 06:43 AM

View PostSkydiver, on 11 July 2013 - 09:20 PM, said:

Let me ask a silly question: What is the significance of BarsInProgress? Why must it be equal to index?

As an aside, don't you already have another topic about this same issue open?


Yes I have that other topic but I thought it was too confusing so I opened this one. I suppose the other can be deleted. The BarsInProgress is the time frame on a stock trading chart. INDEX is the primary time bars or 5min bars, and index is 1min bars. Then I have multiple stock instruments each have an INDEX(5min) and index(1min). I want to run a large amount of instruments through the code posted above. So if I'm running the first instrument MSFT(microsoft) it will have INDEX 0 and index 11. The next stock will be INDEX 1 and index 12 and so on.

But I don't think the for loop above is plugging these #'s into the if statement at all. I think it's just looping through 0-10 in the for loop and skipping right to { do something }. How would I get 0-10 and 11-20 to plug into INDEX & index in the if statement before { do something }?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

Re: Routing a series of #'s through a condition...

Posted 12 July 2013 - 08:41 AM

Let's just look at the structure you are trying to pull off.. There are problems a plenty.
        //-- your code is a hot mess.  Notice how it doesn't format right?  there is a lack of cohesive structure or logical steps.
        protected override void OnBarUpdate() //-- what are you overriding?  Are you sure you need it?
        {//--start of the method.
        for (int INDEX = 0; INDEX < 11; INDEX++) //-- for's are best when they have a brackets around their body.  Sure you can not use them if you just want to execute the next line, but for learning always use a brack.

            for (int index = 11; index < 21; index++) //-- see above's comment on for-loop
                if (BarsInProgress == index)// starting an if statemetn
                {//-- bracket opens up to start the body of the 'true' statement
                    
                    if (SMA(BarsArray[INDEX],Fast)[0] > SMA(BarsArray[INDEX],Slow)[0] && Closes[index][0] > Highs[INDEX][1])// okay another if statemetn
                        //--... with no body.  So that if does nothing.
                }//-- closes the body of the 'true' statement. 
                { //-- random opening of a new chunk of scope.. if you thought this was the 'else' of an if-than-else then you need to use the word 'ELSE'!
                    Do something
                }//-- closed scop
       }//-- end function.
        /*
         * In the end you have a method that contains: 
         * two for loops, 
         * one if statement that contains 
         * another if statement.. (which has no conditional code in the body) 
         * and a random new piece of scope that has "do something" in it.  
         * 
         * */

Was This Post Helpful? 0
  • +
  • -

#9 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 12 July 2013 - 11:37 AM

View Postmodi123_1, on 12 July 2013 - 08:41 AM, said:

Let's just look at the structure you are trying to pull off.. There are problems a plenty.
        //-- your code is a hot mess.  Notice how it doesn't format right?  there is a lack of cohesive structure or logical steps.
        protected override void OnBarUpdate() //-- what are you overriding?  Are you sure you need it?
        {//--start of the method.
        for (int INDEX = 0; INDEX < 11; INDEX++) //-- for's are best when they have a brackets around their body.  Sure you can not use them if you just want to execute the next line, but for learning always use a brack.

            for (int index = 11; index < 21; index++) //-- see above's comment on for-loop
                if (BarsInProgress == index)// starting an if statemetn
                {//-- bracket opens up to start the body of the 'true' statement
                    
                    if (SMA(BarsArray[INDEX],Fast)[0] > SMA(BarsArray[INDEX],Slow)[0] && Closes[index][0] > Highs[INDEX][1])// okay another if statemetn
                        //--... with no body.  So that if does nothing.
                }//-- closes the body of the 'true' statement. 
                { //-- random opening of a new chunk of scope.. if you thought this was the 'else' of an if-than-else then you need to use the word 'ELSE'!
                    Do something
                }//-- closed scop
       }//-- end function.
        /*
         * In the end you have a method that contains: 
         * two for loops, 
         * one if statement that contains 
         * another if statement.. (which has no conditional code in the body) 
         * and a random new piece of scope that has "do something" in it.  
         * 
         * */



This is just a small piece of the entire code. The CalculateOnBarClose property of indicators embedded within a strategy are overriden by the CalculateOnBarClose property of the strategy. I'm not concerned about the cute curly brackets at this point, I realize that they are supposed to be in there, but in reality that are not changing the net profit of the strategy if they are in there or not as I have checked this before. I was just trying to provide a cut down version of the code to keep it simple. Back to the original question does anyone know of a way to route the indexes through the if statement please or can direct me to documentation??

thank you
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

Re: Routing a series of #'s through a condition...

Posted 12 July 2013 - 11:53 AM

Quote

I realize that they are supposed to be in there, but in reality that are not changing the net profit of the strategy if they are in there or not as I have checked this before.



I would have to disagree with that. If line 13-15 (of the commented code in teh post above) is supposed to be an 'else' then not having 'else' really janks up the flow, right? The if statement ends at line 12!

Also, if line 10 was supposed to be conditional for something then yeah.. it is important to at least _SHOW_ there was a body there with the curly braces.. else you just have this 'if' statment doing jack and showing jack.

As it is your for loops should be "route the indexes through" (what ever that means) your if statement. Cut down the for loops to just their bare bones:

            for (int INDEX = 0; INDEX < 11; INDEX++)
            {
                for (int index = 11; index < 21; index++) //-- see above's comment on for-loop
                {
                    Console.WriteLine(String.Format("INDEX: {0}      index: {1}", INDEX, index));
                }
            }




output:
Spoiler


See the output? That's what you are asking for, right? Well then if not there's an issue with what ever is going on in that if statement (and the lack of a body, else statement, etc)


Side note - I really really REALLY dislike it when you have INDEX and index as two variables.. gotta change that for readability, right? ;)
Was This Post Helpful? 0
  • +
  • -

#11 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Routing a series of #'s through a condition...

Posted 12 July 2013 - 12:55 PM

I feel like there might be a lack of understanding on some of the fundamentals of the language, and programming in general here. And you're not being very clear about what exactly it is you want to do. Something like this perhaps?
void method()
{
	for (int index = 0; index < 10; index++)
	{
		if (if index == /*whatever*/ && index + 10 == /*whatever*/)
		{
			//do stuff
		}
	}
}


In any case you're doing stuff with stocks, but it looks like you're hardcoding each one. What you should do is write a stock class, with general functionality, then store those in a list or some other appropriate collection class. Then you can use a loop or LINQ to go through each one, updating them or whatever you want to do.
Was This Post Helpful? 1
  • +
  • -

#12 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 21 July 2013 - 06:52 PM

View Postmodi123_1, on 12 July 2013 - 11:53 AM, said:

Quote

I realize that they are supposed to be in there, but in reality that are not changing the net profit of the strategy if they are in there or not as I have checked this before.



I would have to disagree with that. If line 13-15 (of the commented code in teh post above) is supposed to be an 'else' then not having 'else' really janks up the flow, right? The if statement ends at line 12!

Also, if line 10 was supposed to be conditional for something then yeah.. it is important to at least _SHOW_ there was a body there with the curly braces.. else you just have this 'if' statment doing jack and showing jack.

As it is your for loops should be "route the indexes through" (what ever that means) your if statement. Cut down the for loops to just their bare bones:

            for (int INDEX = 0; INDEX < 11; INDEX++)
            {
                for (int index = 11; index < 21; index++) //-- see above's comment on for-loop
                {
                    Console.WriteLine(String.Format("INDEX: {0}      index: {1}", INDEX, index));
                }
            }




output:
Spoiler


See the output? That's what you are asking for, right? Well then if not there's an issue with what ever is going on in that if statement (and the lack of a body, else statement, etc)


Side note - I really really REALLY dislike it when you have INDEX and index as two variables.. gotta change that for readability, right? ;)/>


Sorry for delay here, have been trying to study some basics, just finished the Bob Tabor video series. I agree with your variable naming, I will change it to A & B or something simple. Anyhow I'm looking at your output and I had just noticed the same thing when running a debug and using break points and pinning the variables to watch there sequence as the code executes. So pulling a small sample of your output above, instead of it being..
INDEX: 0 index: 11
INDEX: 0 index: 12
INDEX: 0 index: 13

What I am needing is...
INDEX: 0 index: 11
INDEX: 1 index: 12
INDEX: 2 index: 13

Is there a way to do this? Basically make INDEX & index switch simultaneously? Your probably going to laugh but I tried something like this below and it's not compiling...

for (int INDEX = 0; INDEX < 11; INDEX++) && for (int index = 11; index < 21; index++)

or also was thinking maybe this...

for (int INDEX = 0 && int index = 11; INDEX < 11 && index < 21; INDEX++ && index++)

Thanks
Was This Post Helpful? 0
  • +
  • -

#13 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 21 July 2013 - 07:24 PM

Or I'm not sure if i could do something like this below using just the one variable INDEX. Where I would use "INDEX +10" in place of "index". As index was always 10 above INDEX.

protected override void OnBarUpdate()

   {
    for (int INDEX = 0; INDEX < 10; INDEX++)
      {
       if ((BarsInProgress == INDEX + 10) &&
          (SMA(BarsArray[INDEX],Fast)[0] > SMA(BarsArray[INDEX],Slow)[0] && Closes[INDEX + 10][0] > Highs[INDEX][1]))
         {
         Do something
         }
      }
   }

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

Re: Routing a series of #'s through a condition...

Posted 22 July 2013 - 07:07 PM

It looks like you are incrementing a second variable while running a loop, right? Well, then, do it.
            int lCounter = 11;

            for (int i = 0; i < 11; i++)
            {
                Console.WriteLine(String.Format("INDEX: {0}      index: {1}", INDEX, index));
                lCounter += 1;
            }



INDEX: 0      index: 11
INDEX: 1      index: 12
INDEX: 2      index: 13
INDEX: 3      index: 14
INDEX: 4      index: 15
INDEX: 5      index: 16
INDEX: 6      index: 17
INDEX: 7      index: 18
INDEX: 8      index: 19
INDEX: 9      index: 20
INDEX: 10      index: 21

Was This Post Helpful? 0
  • +
  • -

#15 zachj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 13

Re: Routing a series of #'s through a condition...

Posted 22 July 2013 - 07:46 PM

I should probably explain, all 0-10 is, is a 5min price bar of a stock. So 0 is a place holder for the first stock in a portfolio, 1 would be the second stock etc. Then for 11-20, 11 is a 1min price bar of the first stock, then 12 is a 1min bar of the second stock and so on. So the two different variables are just 5min bar and 1min bar for each stock.

In the initialize section of the script I have... Add("MSFT", PeriodType.Minute, 5) for instance which would be index 1. I have done this for each stock.

So how would I apply your lCounter to the below code? You can see in some parts of the code I need it to increment index or 0-9 and other parts I would need it to increment index + 10 which would be 10-20.

protected override void OnBarUpdate()
{
for (int index = 0; index < 10; index++)
  {if (BarsInProgress == index + 10)
   &
   if (SMA(BarsArray[index],Fast)[0] > SMA(BarsArray[index],Slow)[0] & Closes[index + 10][0] > Highs[index][1])
      {
      Enter a Long Position in the Stock
      }
   }
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2