Welcome to the Traders Laboratory Forums.
Coding Forum Collaborate, receive help, or discuss coding related issues.

Reply
Old 10-31-2009, 03:37 PM   #1

Tams's Avatar

Join Date: Sep 2008
Location: Geelong
Posts: 3,779
Ignore this user

Thanks: 2,084
Thanked 1,474 Times in 912 Posts

Loops (EasyLanguage)

This thread is about the For keyword in EasyLanguage.



For

Used in combination with To or DownTo to form a loop statement
that will execute a set of instructions repeatedly until the loop count reaches the specified final value.

The loop statement specifies a numerical variable that holds the loop count,
as well as initial and final counter values.

To specifies that the value of the counter variable is to be increased by one on the completion of each loop,
while DownTo specifies that the value of the counter variable is to be decreased by one on the completion of each loop.

The use of Begin and End statements is required to group the instructions for execution within the loop;
a Begin must always be followed by an End.

Begin should not be followed by a semicolon (;),
code lines within an instruction group should end with a semicolon (;),
and End should be followed by a semicolon (;).


Usage

For Counter = IValue To FValue
Begin
Instruction1;
Instruction2;
End;


or:

For Counter = IValue DownTo FValue
Begin
Instruction1;
Instruction2;
End;

Where:
Counter - a numerical variable used store the loop count
IValue - a numerical expression specifying the initial counter value
FValue - a numerical expression specifying the final counter value



Example

Add the high prices of the last 10 bars to the HighPriceSum variable:

Code:
For BarBackNo = 0 To 9 
Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
End;

Add the high prices of the last 10 bars to the HighPriceSum variable:

Code:
For BarBackNo = 9 DownTo 0 
Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
End;


source: EasyLanguage manual
__________________



Only an idiot would reply to a stupid post
Tams is offline  
Reply With Quote
The Following 3 Users Say Thank You to Tams For This Useful Post:
aaa (11-07-2009), Blu-Ray (11-03-2009), TIKITRADER (11-01-2009)
Old 10-31-2009, 04:30 PM   #2

Join Date: Jan 2008
Location: San Francisco
Posts: 394
Ignore this user

Thanks: 17
Thanked 338 Times in 156 Posts

re: Loops (EasyLanguage)

here was very basic tutorial I found on Loops & Arrays... I didn't learn anything great but it may help someone starting out:

https://www.tradestation.com/support...ops_and_Arrays
Frank is offline  
Reply With Quote
The Following 2 Users Say Thank You to Frank For This Useful Post:
Tams (10-31-2009), TIKITRADER (11-01-2009)
Old 10-31-2009, 06:20 PM   #3

Join Date: May 2008
Location: amsterdam
Posts: 114
Ignore this user

Thanks: 25
Thanked 39 Times in 30 Posts

re: Loops (EasyLanguage)

if i am certain my condition will be true at a given time i do sometimes prefere while loops,
this loop will keep running untill the condition is valid, make sure that will happen otherwhise we are in a infinite loop


Code:
index = 0;

While
ArrayToSearch[index] < Highest_A( ArrayToSearch, Array_Size ){condition}
Begin
index = index + 1;{instruction to repeat untill condition is true}
End;

IndexOfHighestArray = index;
flyingdutchmen is offline  
Reply With Quote
The Following User Says Thank You to flyingdutchmen For This Useful Post:
Tams (10-31-2009)
Old 10-31-2009, 07:02 PM   #4

Join Date: May 2008
Location: amsterdam
Posts: 114
Ignore this user

Thanks: 25
Thanked 39 Times in 30 Posts

re: Loops (EasyLanguage)

a litle note wenn using calculations inside loops,
if one is trying to aply value(s) to a certain variable make
sure to reset the variable to "0" or whatever initial value you would
like to use before the beginning of the loop.
if you do not do this it will take the old value of the former candle
calculated from last loop and it will use that value to apply next value's
resulting in wrong calculations

it does not matter if you use a "for", "while" or "repeat / untill"-loop





in this example you will recieve a correct value only on the first bar as the
variable does not reset before the calculation.on the next bar it will take that old
value for its new calculation.

the reason you will recieve a correct value on the first bar/loop is simply because
the variable has been declared as "0" which gives it an initial first bar value of "0"
( Variables: HighPriceSum(0); )
untill this value will be changed, this is emediatly after the first calculation.


Code:
For BarBackNo = 0 To 9 
Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
End;
correct:

Code:
HighPriceSum = 0;

For BarBackNo = 0 To 9 
Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
End;

Last edited by flyingdutchmen; 10-31-2009 at 07:11 PM.
flyingdutchmen is offline  
Reply With Quote
The Following 3 Users Say Thank You to flyingdutchmen For This Useful Post:
Tams (10-31-2009), TIKITRADER (11-04-2009), YandexRu (Today)
Old 11-03-2009, 03:28 PM   #5

Tams's Avatar

Join Date: Sep 2008
Location: Geelong
Posts: 3,779
Ignore this user

Thanks: 2,084
Thanked 1,474 Times in 912 Posts

Re: Loops (EasyLanguage)

Switch/Case


Reserved word used to transfer control to an associated Case or Default statement.

Syntax

Code:
Switch ( expression )
Begin
	Case case-expression:  statements;

	Default: statements;
End;
Control passes to the statements whose case-expression matches the value of the switch ( expression ).
The switch statement can include any number of case instances,
but no two case constants within the same switch statement can have the same constant value.

Note Once the statements associated with a matching case are evaluated,
control passes to the end of the switch statement.
This is an implied break and is different than a similar structure
found in some other languages that require an explicit break.


Remarks
A single case statement can carry multiple values, as the following example shows:

Quote:
Case 1, 2, 3, 4, 5, 6, 20: Value1 = Lowest(Close,3);
Ranges like this are also valid:

Quote:
Case 1 to 6, 20: Value2 = Highest(High,5);
In both of the above examples,
if case-expression equals any number between 1 and 6 or equal to 20,
a function is called and assigned to a value.

In addition,
logical operators may be used with case statements including: >, <, >=, <=, <> and =.

Quote:
Case > Average( Close, 50 ): Value1 = Close ;
The "is" keyword is an EasyLanguage skip keyword and can be use for better clarity as in the following:

Quote:
Case is < Average( High, 75 ): Value1 = High ;

Example

Code:
Switch(Value1) 
Begin

    Case 1 to 5:

        Value2 = Value2 + 1;

    Case 10, 20, 30:

        Value3 = Highest(High,10);

    Case is > 40:

        Value3 = Value3 + 1;

    Default:
    Value5 = Value5 + 1;
End;

The above switch statement Increments Value2 when the switch expression (Value1) is a value from 1 to 5;
assigns Value3 to the highest high of 10 bars ago when the switch expression is either 10, 20 or 30;
increments Value4 for all switch expression values above 40;
and increments Value5 for any other values of the switch expression.



source: EasyLanguage manual
__________________



Only an idiot would reply to a stupid post

Last edited by Tams; 11-03-2009 at 03:35 PM.
Tams is offline  
Reply With Quote
The Following 4 Users Say Thank You to Tams For This Useful Post:
Blu-Ray (11-03-2009), flyingdutchmen (11-03-2009), Frank (11-03-2009), TIKITRADER (11-04-2009)
Old 11-04-2009, 02:19 PM   #6

Tams's Avatar

Join Date: Sep 2008
Location: Geelong
Posts: 3,779
Ignore this user

Thanks: 2,084
Thanked 1,474 Times in 912 Posts

Re: Loops (EasyLanguage)

While

Used in combination with Begin and End to form a conditional loop statement
that will execute a set of instructions repeatedly as long as a logical expression is true.
If the logical expression is not true, the instructions will not be executed.

Begin and End statements are used to group instructions for conditional execution;
a Begin must always be followed by an End.

Begin should not be followed by a semicolon (;),
code lines within an instruction group should end with a semicolon (;),
and End should be followed by a semicolon (;).


Usage

Code:
While Condition1 = True 
Begin
    Instruction1;
    Instruction2;
    Instruction3;
End;
Where:
Condition1 - a true/false expression
InstructionX - conditional instructions



Example

Add the high prices of the last 10 bars to the HighPriceSum variable:

Code:
BarBackNo = 0;

While BarBackNo < 10 
Begin
    HighPriceSum = HighPriceSum + High[ BarBackNo ];
    BarBackNo = BarBackNo + 1;
End;


Source: EasyLanguage manual
__________________



Only an idiot would reply to a stupid post
Tams is offline  
Reply With Quote
The Following User Says Thank You to Tams For This Useful Post:
TIKITRADER (11-04-2009)
Old 11-04-2009, 02:43 PM   #7

Join Date: May 2008
Location: amsterdam
Posts: 114
Ignore this user

Thanks: 25
Thanked 39 Times in 30 Posts

Re: Loops (EasyLanguage)

i will ad one more

it will keep running like Tam's loop untill the "Until" condition is met.
This Loop is not yet supported in the old 2000i version but i believe it is supported in the
newer versions of TradeStations and most likely in MC.

it will produce the exact same outcome as the loop in the last post with the exeption
that there is no need for "Begin" or "End" regardless of how many statements one would
place between and this one is likely a bit easyer to understand for "LoopStarters"

Code:
HighPriceSum = 0;
BarBackNo = -1;

Repeat
    BarBackNo = BarBackNo + 1;
    HighPriceSum = HighPriceSum + High[ BarBackNo ];
Until BarBackNo = 9;

Last edited by flyingdutchmen; 11-04-2009 at 02:50 PM.
flyingdutchmen is offline  
Reply With Quote
The Following 2 Users Say Thank You to flyingdutchmen For This Useful Post:
Tams (11-04-2009), TIKITRADER (11-04-2009)
Old 11-09-2009, 11:24 AM   #8

BlowFish's Avatar

Join Date: Mar 2007
Location: In Da House
Posts: 3,292
Ignore this user

Thanks: 129
Thanked 1,054 Times in 702 Posts

Re: Loops (EasyLanguage)

Loops are very important and powerful constructs however one should be aware they can be quite slow as all the code in the loop is repeated for each iteration. This is particularly so if you have nested loops (loops within loops) or many iterations particularly on indicators that update every tick. Always keep the code in loops to the bare minimum. Novices often put all sorts of things that could go outside the loop on the inside Often there is a way to do things without using them at all.

e.g. using the original example.

HighPriceSum = HighPriceSum + High - High[9];

Will return the same value though it should be noted that this is 'unstable' for the first 9 bars on the chart (mind you so is the other looping code). Both tradestation and Multicharts do a pretty decent job of detecting how many bars are needed before an indicator 'works' and so there is no need to worry about this (unless you are a purist).
BlowFish is offline  
Reply With Quote
The Following User Says Thank You to BlowFish For This Useful Post:
Tams (11-09-2009)

Reply

Tags
loop

Thread Tools
Display Modes Help Others By Rating This Thread
Help Others By Rating This Thread:


Similar Threads
Thread Thread Starter Forum Replies Last Post
Buy at Open with Easylanguage cleon Coding Forum 5 03-02-2011 11:16 AM
Alert (EasyLanguage) Tams Coding Forum 0 10-27-2009 12:47 PM
Easylanguage Error #408 and #410?? isisisis Coding Forum 2 04-10-2009 06:30 PM
Looking For Coder That Knows MQL & EasyLanguage CafeTrader Coding Forum 3 03-18-2009 07:05 PM
Some EasyLanguage Help Please jjthetrader Coding Forum 8 06-02-2008 04:20 PM

All times are GMT -4. The time now is 02:10 PM.
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
CS to VB integration by DeskLancer
©2006-2011 Traders Laboratory, All Rights Reserved.