Jump to content

Welcome to the new Traders Laboratory! Please bear with us as we finish the migration over the next few days. If you find any issues, want to leave feedback, get in touch with us, or offer suggestions please post to the Support forum here.

  • Welcome Guests

    Welcome. You are currently viewing the forum as a guest which does not give you access to all the great features at Traders Laboratory such as interacting with members, access to all forums, downloading attachments, and eligibility to win free giveaways. Registration is fast, simple and absolutely free. Create a FREE Traders Laboratory account here.

Tams

Loops (EasyLanguage)

Recommended Posts

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:

 

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

 

 

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

 

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

 

 

 

source: EasyLanguage manual

Share this post


Link to post
Share on other sites

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

 

 

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;

Share this post


Link to post
Share on other sites

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.

 

 

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

 

correct:

 

[b]HighPriceSum = 0;[/b]

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

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

Switch/Case

 

 

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

 

Syntax

 

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:

 

Case 1, 2, 3, 4, 5, 6, 20: Value1 = Lowest(Close,3);

 

Ranges like this are also valid:

 

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 =.

 

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:

 

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

 

 

Example

 

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

Edited by Tams

Share this post


Link to post
Share on other sites

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

 

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:

 

BarBackNo = 0;

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

 

 

 

Source: EasyLanguage manual

Share this post


Link to post
Share on other sites

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"

 

HighPriceSum = 0;
BarBackNo = -1;

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

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

note this maxbarsback is valid for each and every script that uses an fix amount

of bars before the first calcuation made is able to give correct value's; this does not only

apply for loops.

 

i allways try to write my script in a way it does never have the "need" for gathering

historical data aka "looking back". all data stored in arrays and variables are ohlc values of current bar without using any displacements ( [1] ). i never have the need of using displacements, then the skript will not start before all arrays are filled to the max index

 

edit: this ofcourse is not of value to the one's simply wanting to calculate a standard indikator like an rsi,macd,ma etc. i do not use any of them

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

INSANITY! This loop runs multiple times giving me faulty data, what is going on!

 

		
inputs: 	myTotalFloat( 0 );

variables:	intVolumeCounter( 0 ),
x( 0 );

If myTotalFloat > 0 Then Begin
//	While intVolumeCounter < myTotalFloat Begin
intVolumeCounter = intVolumeCounter + Volume[x];
x = x + 1;
//	End;
Print(GetSymbolName, " had ", x, " bars counted");
End;

 

SO ... if I leave the above, as is, I get ONE print in the Print Log for EACH symbol in a radar screen window, just ONCE.

 

If I take the comment lines OUT on the WHILE loop in the above, and let it run, I get two or three PRINT statements for each symbol. WTF!!

 

Here is the output with the LOOP commented OUT

LVLT had 1.00 bars counted

FCEL had 1.00 bars counted

JCG had 1.00 bars counted

BDCO had 1.00 bars counted

 

Here is the output with the LOOP NOT commented OUT

LVLT had 159.00 bars counted

LVLT had 118.00 bars counted

FCEL had 263.00 bars counted

JCG had 8.00 bars counted

JCG had 14.00 bars counted

JCG had 18.00 bars counted

JCG had 21.00 bars counted

JCG had 24.00 bars counted

BDCO had 1533.00 bars counted

 

 

Here's what I want, if it helps (skip section if not important)

I want to INPUT the float for a stock. Then, I want to start at the current bar, and compare the DAILY volume with the float. If total volume is less than the float, go back one day and add that volume, test again, and so on. At some point, we go back far enough to see if the total float has been traded. I want to identify that bar, as in 5 days back or 300 days back, etc. I have found that when the total float is turned over, the price activity is different depending on how long it took to turn over and the prices during the turn over. So the length of time it takes to trade the entire float is important to me.

 

There is some secret thing in EasyLanguage I do not know...

I have programmed for 20 years and NEVER have I seen a loop repeat unless it is told or looped within another loop. There is NO REASON a loop, once it's done, should run again as the assembly code for this creates a break when the loop condition is fulfilled.

 

I want a SINGLE check back on each stock, then move on to the next stock. TradeStation EasyLanguage seems to have some secret, hidden thing somewhere that makes loops run a few times that I do not know about.

 

What I have tried...

I am using the indicator on DAILY chart or RadarScreen with nothing fancy at all. I have stripped it down to its most simple aspects and still, mulitiple runs with multiple outputs for multiple stocks ... crazy!

 

I tried a FOR loop, a WHILE loop and even did a few IF/THEN statements and I can not get it to just check back on a single stock, ONE time, and move on to the next symbol!

 

I tried initializing variables, I tried putting all the loop values into INPUT values, it did not matter. As soon as the LOOP aspect was introduced to the indicator, it starts spitting out multiple runs.

 

Help is appreciated beyond imagination! Thanks.

Share this post


Link to post
Share on other sites

what are the resolution of the symbols?

 

 

Here is the output with the LOOP commented OUT

LVLT had 1.00 bars counted

FCEL had 1.00 bars counted

JCG had 1.00 bars counted

BDCO had 1.00 bars counted

 

Here is the output with the LOOP NOT commented OUT

LVLT had 159.00 bars counted

LVLT had 118.00 bars counted

FCEL had 263.00 bars counted

JCG had 8.00 bars counted

JCG had 14.00 bars counted

JCG had 18.00 bars counted

JCG had 21.00 bars counted

JCG had 24.00 bars counted

BDCO had 1533.00 bars counted

 

 

have you done a manual calculation?

what kind of results you were expecting from the above?

Share this post


Link to post
Share on other sites

GREAT IDEA ... so yes I did manually calculate the data, for example, for FRO (a stock I trade all the time) I put 1500000 (1.5 M) in for the FLOAT value (input value above as myTotalFloat). Then, the PrintLog window shows this:

 

FRO had 3.00 bars counted

FRO had 3.00 bars counted

 

Which is CORRECT, since in three days, it will do 1.5M shares. BUT ... I don't want it running TWO times! Look at the code, there is NO REASON for this to run and print TWO outputs!!

 

When the numbers get larger, the calculations get repeated more. This is the most simple thing I can imagine, and yet, there is something in the way EasyLanguage PROCESSES loops that is unknown to me.

 

So, for the same symbol, FRO, I put in 11500000 (11.5M) this time and I get:

FRO had 23.00 bars counted

FRO had 11.00 bars counted

 

Here, however, 11 bars is the correct answer!

 

I expect a single calculation for each stock in the RadarScreen window. Not multiple random loops run with various different answers. I thought it was simple, but something is eluding me in the black box of EL.

 

IF the volume I have counted so far is less than the total float THEN

Add the volume from the day before and test again

 

OR

 

Keep adding the volume from each day backwards from today until you total a value that is equal to or greater than the float INPUT value

 

What is going on that I do not know here?!?!

 

THX so much, I am about to throw my computer into the canal!!

Share this post


Link to post
Share on other sites

OK, I modified the code as follows so anyone can test:

 

inputs: MyTotalFloat( 0 );

variables: intVolumeCounter( 0 ), x( 0 );

If myTotalFloat > 0 Then Begin
If GetSymbolName = "FRO" Then Begin
While intVolumeCounter < myTotalFloat Begin
intVolumeCounter = intVolumeCounter + Volume[x];
x = x + 1;
Print(GetSymbolName, " has volume total of ", intVolumeCounter, " at bar ", x);
End;
End;
End;

 

For the INPUT value in the RadarScreen, I have 11500000 (11.5M) for myTotalFloat.

 

AND IT STILL RUNS TWO TIMES!! Here is the PrintLog Result:

 

FRO has volume total of 519831.00 at bar 1.00

FRO has volume total of 1039662.00 at bar 2.00

FRO has volume total of 1559493.00 at bar 3.00

FRO has volume total of 2079324.00 at bar 4.00

FRO has volume total of 2599155.00 at bar 5.00

FRO has volume total of 3118986.00 at bar 6.00

FRO has volume total of 3638817.00 at bar 7.00

FRO has volume total of 4158648.00 at bar 8.00

FRO has volume total of 4678479.00 at bar 9.00

FRO has volume total of 5198310.00 at bar 10.00

FRO has volume total of 5718141.00 at bar 11.00

FRO has volume total of 6237972.00 at bar 12.00

FRO has volume total of 6757803.00 at bar 13.00

FRO has volume total of 7277634.00 at bar 14.00

FRO has volume total of 7797465.00 at bar 15.00

FRO has volume total of 8317296.00 at bar 16.00

FRO has volume total of 8837127.00 at bar 17.00

FRO has volume total of 9356958.00 at bar 18.00

FRO has volume total of 9876789.00 at bar 19.00

FRO has volume total of 10396620.00 at bar 20.00

FRO has volume total of 10916451.00 at bar 21.00

FRO has volume total of 11436282.00 at bar 22.00

FRO has volume total of 11956113.00 at bar 23.00

FRO had 23.00 bars counted

FRO has volume total of 519831.00 at bar 1.00

FRO has volume total of 1226332.00 at bar 2.00

FRO has volume total of 1979514.00 at bar 3.00

FRO has volume total of 2601584.00 at bar 4.00

FRO has volume total of 3404746.00 at bar 5.00

FRO has volume total of 5382056.00 at bar 6.00

FRO has volume total of 6225056.00 at bar 7.00

FRO has volume total of 8147119.00 at bar 8.00

FRO has volume total of 9546118.00 at bar 9.00

FRO has volume total of 11463405.00 at bar 10.00

FRO has volume total of 12276220.00 at bar 11.00

FRO had 11.00 bars counted

 

Madness I tell you!!

Share this post


Link to post
Share on other sites

OK, I added another print statement to test output, looks like loops need to be PRIMED or something in TradeStation?

 

I changed the float INPUT to 5.5M so the output here would not be so lengthy, showing it all just for completeness.

 

As you can see, the loop does NOT allow the pointer to increment on the first loop, so it reads the same volume (519831.00) for each bar on the FIRST loop.

 

Then when it gets going, it reads the proper values. SO ... how do you PRIME a LOOP in TS so it will work properlty?!

 

That is the BLACK BOX stuff I do not expect as they are not adherent to standards of probramming!

 

Here's the output on this one:

-- Found FRO, entering loop --

FRO at bar 0.00 volume was 519831.00

FRO at bar 0.00 total so far 519831.00

FRO at bar 1.00 volume was 519831.00

FRO at bar 1.00 total so far 1039662.00

FRO at bar 2.00 volume was 519831.00

FRO at bar 2.00 total so far 1559493.00

FRO at bar 3.00 volume was 519831.00

FRO at bar 3.00 total so far 2079324.00

FRO at bar 4.00 volume was 519831.00

FRO at bar 4.00 total so far 2599155.00

FRO at bar 5.00 volume was 519831.00

FRO at bar 5.00 total so far 3118986.00

FRO at bar 6.00 volume was 519831.00

FRO at bar 6.00 total so far 3638817.00

FRO at bar 7.00 volume was 519831.00

FRO at bar 7.00 total so far 4158648.00

FRO at bar 8.00 volume was 519831.00

FRO at bar 8.00 total so far 4678479.00

FRO at bar 9.00 volume was 519831.00

FRO at bar 9.00 total so far 5198310.00

FRO at bar 10.00 volume was 519831.00

FRO at bar 10.00 total so far 5718141.00

FRO done processing and counted 11.00 bars

-- Found FRO, entering loop --

FRO at bar 0.00 volume was 519831.00

FRO at bar 0.00 total so far 519831.00

FRO at bar 1.00 volume was 706501.00

FRO at bar 1.00 total so far 1226332.00

FRO at bar 2.00 volume was 753182.00

FRO at bar 2.00 total so far 1979514.00

FRO at bar 3.00 volume was 622070.00

FRO at bar 3.00 total so far 2601584.00

FRO at bar 4.00 volume was 803162.00

FRO at bar 4.00 total so far 3404746.00

FRO at bar 5.00 volume was 1977310.00

FRO at bar 5.00 total so far 5382056.00

FRO at bar 6.00 volume was 843000.00

FRO at bar 6.00 total so far 6225056.00

FRO done processing and counted 7.00 bars

Share this post


Link to post
Share on other sites

I want to be able to scan, so switching charts would be time consuming. Want to be able to track a few hundred stocks with this. Chart is great for individual lookups, but when scanning, it's tough :-)

 

What I want to see is that the closing price, as the float has turned over, is increasing and that the weighted average price based on volume was at least 10% below the latest close. So, ultimately, if the stock has had a lower price over the history of the float turning over, than it does now, AND, it starts moving up on volume, then it is reasonable to assume that the holders of the stock in this current "batch" of holders (that batch being this floats holders of the stock) are into a profit and should hold until a certain return level above the average volume/price level during the float turn over period.

 

When the float turns over, and say, it turned after being at $20 for 80 of the last 90 bars of the current float turnover, and in the last 10 bars, it moved up $1, it is reasonable to assume that supply might dry up as those holding are in at an average price of $20 and will look for at least a 10-20% return before selling. Assumptive of course, but the logic to start testing all of this can't even BEGIN because the DAMN LOOP WILL NOT WORK!!!

 

:-)

Edited by mrtraderguy

Share this post


Link to post
Share on other sites

I tried your code in MultiCharts,

I am getting weird results as well.

 

I remember reading about similar problems before,

I have to look up the answer/solution.

Share this post


Link to post
Share on other sites

I have been looking and can't find anything; this is REALLY pissin me off!

 

If you have anywhere to look, let me know, it's makin me crazy, otherwise, I'm going to have to do it in excel and just transfer all the numbers every day and recalculate ... and I pay alot of money for that stupid tradestation crap too!

Share this post


Link to post
Share on other sites

on the maxbarsback,

 

turn off the Auto-Detect and use User Specified period.

 

It solved one of my code misbehaviors.

 

I haven't tested your code yet.

Share this post


Link to post
Share on other sites
interesting posts, Tams, have you solved the issue now?

 

 

I think it has to do with the Auto-Detect on MaxBarsBack.

I will look into it further over the weekend.

Share this post


Link to post
Share on other sites
on the maxbarsback,

 

turn off the Auto-Detect and use User Specified period.

 

It solved one of my code misbehaviors.

 

I haven't tested your code yet.

 

Tams, do you program for clients in TS?

 

Thx

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.