logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Drew  
#1 Posted : Tuesday, April 15, 2014 7:50:43 AM(UTC)
Drew

Rank: Newbie

Groups: Registered, Registered Users, Unverified Users
Joined: 4/15/2014(UTC)
Posts: 6

Hi guys - new here so take it easy on me.

I've been having my first shot at writing some MSFL code, but its unlike any other language I've worked with, and I'm having trouble getting my head around it.

From what I can tell every variable is really an array of values - one for each period on the chart - is that sort of right?

I know I can assign a var a single value such as...
period := 15;
but then I can plot that value as a horizontal line across the chart, so it turns out to be a quasi-array too.

Also I'm not really sure how it flows through the code during execution. When it reaches a defined variable, does it calculate variable's entire array of values before going on to the next line of the code?

Also from what I can read, we can't call other user-defined formulas passing parameter values into them, although you can suck in variable values from external formulas.

And is there any way of doing loops?

I've read the Formula Primer and the FAQ but I'd like to get my head around how it hangs together more. Any other reading people could direct me to?

Cheers all
Drew
wabbit  
#2 Posted : Tuesday, April 15, 2014 8:09:16 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Drew wrote:
From what I can tell every variable is really an array of values - one for each period on the chart - is that sort of right?

I know I can assign a var a single value such as...
period := 15;
but then I can plot that value as a horizontal line across the chart, so it turns out to be a quasi-array too.

A little later you're eventually come across another limitation, which is that variables have a "starting index" and an "end index" i.e. the bars for which the variable "exists". For example:
Code:

x:=ValueWhen(1, someEvent, someValue);

will return N/A for all of the bars before 'someEvent' became TRUE for the first time and will then return a value for the remainder of the bars of a chart.
Code:

Ref(C,+10);

will return a value on all bars of a chart EXCEPT the last 10 bars, when the function will return N/A because it cannot see into the future passed the last bar of data.

But, once "turned on" a function can only ever be "turned off" once and cannot be restarted. There is no such thing as NULL in MS (unfortunately).

Drew wrote:
Also I'm not really sure how it flows through the code during execution. When it reaches a defined variable, does it calculate variable's entire array of values before going on to the next line of the code?
Like it or not, MS "fully evaluates" every line of code, meaning every aspect of every line is fully examined and computed before moving to the next line, which is why code like:
Code:

numerator:=some number;
denominator:=some number;

if(denom=0, 0, numerator/denominator);

will still produce Divide By Zero errors, even though you might think that you have trapped this; the numerator/denominator is ALWAYS processed regardless of the remainder of the If() statement.

Drew wrote:
Also from what I can read, we can't call other user-defined formulas passing parameter values into them, although you can suck in variable values from external formulas.
You cannot pass a parameter using a Fml() or FmlVar() call; and to make matters worse, if the function being called uses Input() functions, then regardless of what the user may have set those values to on their chart, the function will return the DEFAULT value from the Input().

Drew wrote:
And is there any way of doing loops?

No.

Drew wrote:
I've read the Formula Primer and the FAQ but I'd like to get my head around how it hangs together more. Any other reading people could direct me to?

Read all of the built-in formulas, system tests and experts (that aren't password protected)


Drew  
#3 Posted : Tuesday, April 15, 2014 8:40:14 AM(UTC)
Drew

Rank: Newbie

Groups: Registered, Registered Users, Unverified Users
Joined: 4/15/2014(UTC)
Posts: 6

Wow Wabbit - there's a wealth of information in there that would have taken me ages to digest - thank you so much for taking the time - you're a legend. I've been reading many of your posts on the forum. Thanks for all your hard work.

wabbit wrote:
Drew wrote:
From what I can tell every variable is really an array of values - one for each period on the chart - is that sort of right?

I know I can assign a var a single value such as...
period := 15;
but then I can plot that value as a horizontal line across the chart, so it turns out to be a quasi-array too.

A little later you're eventually come across another limitation, which is that variables have a "starting index" and an "end index" i.e. the bars for which the variable "exists". For example:
Code:

x:=ValueWhen(1, someEvent, someValue);

will return N/A for all of the bars before 'someEvent' became TRUE for the first time and will then return a value for the remainder of the bars of a chart.
Code:

Ref(C,+10);

will return a value on all bars of a chart EXCEPT the last 10 bars, when the function will return N/A because it cannot see into the future passed the last bar of data.
Yes I've seen this! Is there a way to test for a N/A value? Or do you just test for the presence of a value above or below or equal to 0 (zero), say?
Waddaya mean it can't see into the future? I was basing my whole trading plan on that! [;)]
Quote:

But, once "turned on" a function can only ever be "turned off" once and cannot be restarted. There is no such thing as NULL in MS (unfortunately).
Wow - can't be restarted again? So the array isn't like a sparse array where it can have a few values here and a few there and N/A in between - if it has anything at all it has just a single continuous block of values until it turns off again, and then that's it. OK thanks - I'll keep that in mind.
Quote:
Drew wrote:
Also I'm not really sure how it flows through the code during execution. When it reaches a defined variable, does it calculate variable's entire array of values before going on to the next line of the code?
Like it or not, MS "fully evaluates" every line of code, meaning every aspect of every line is fully examined and computed before moving to the next line, which is why code like:
Code:

numerator:=some number;
denominator:=some number;

if(denom=0, 0, numerator/denominator);

will still produce Divide By Zero errors, even though you might think that you have trapped this; the numerator/denominator is ALWAYS processed regardless of the remainder of the If() statement.
Wow again - thanks for the heads up
Quote:
Drew wrote:
Also from what I can read, we can't call other user-defined formulas passing parameter values into them, although you can suck in variable values from external formulas.
You cannot pass a parameter using a Fml() or FmlVar() call; and to make matters worse, if the function being called uses Input() functions, then regardless of what the user may have set those values to on their chart, the function will return the DEFAULT value from the Input().
Yes I've hit that hurdle already
Quote:
Drew wrote:
And is there any way of doing loops?

No.
So if you have a complex formula which plots an indicator on your chart, and you want to plot a dozen varieties of it with slightly different variable values each time, you have to physically copy and paste the code 12 times, change all the var values in each copied instance, and rename the final variable in each block which is plotted on the screen.
Quote:
Drew wrote:
I've read the Formula Primer and the FAQ but I'd like to get my head around how it hangs together more. Any other reading people could direct me to?

Read all of the built-in formulas, system tests and experts (that aren't password protected)
Yes - I've cherry-picked a bit of stuff out of those

Thanks again for your invaluable help mate
Cheers
Drew
wabbit  
#4 Posted : Tuesday, April 15, 2014 8:55:53 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Drew wrote:
Yes I've seen this! Is there a way to test for a N/A value? Or do you just test for the presence of a value above or below or equal to 0 (zero), say?

There are the IsDefined() and IsUndefined() functions, but they don't work like many people think they should; read about them in the Manual. Many times they are best combined with a Cum() function, e.g.
Code:

ma:=Mov(c,20,s); {will return N/A on first 19 bars}

signal:=cum(isdefined(ma))=1;

{plot - new inner window}
signal;


here are a number of libraries where programmers have "overloaded" the IsDefined() and/or IsUndefined() functions to work differently; I cannot think which ones right now though.

Drew wrote:
Waddaya mean it can't see into the future? I was basing my whole trading plan on that!

You'd be surprised how many people ACTUALLY think like that though... Shocking!

Drew wrote:
Wow - can't be restarted again? So the array isn't like a sparse array where it can have a few values here and a few there and N/A in between - if it has anything at all it has just a single continuous block of values until it turns off again, and then that's it. OK thanks - I'll keep that in mind.

Correct.

Drew wrote:
Yes I've hit that hurdle already

That was quick!

Drew wrote:
So if you have a complex formula which plots an indicator on your chart, and you want to plot a dozen varieties of it with slightly different variable values each time, you have to physically copy and paste the code 12 times, change all the var values in each copied instance, and rename the final variable in each block which is plotted on the screen.

There are ways to streamline some processes, but without seeing exactly your situation, it's hard to provide details on what might be doable.

Drew wrote:
Yes - I've cherry-picked a bit of stuff out of those

There are some good examples, and some bad examples... but they're good enough to get the ideas flowing, in most instances.
Drew  
#5 Posted : Tuesday, April 15, 2014 9:15:41 AM(UTC)
Drew

Rank: Newbie

Groups: Registered, Registered Users, Unverified Users
Joined: 4/15/2014(UTC)
Posts: 6

Thanks again Wabbit - you've really helped me out a lot with these pointers. Your time, effort and patience is beyond measure mate.

I might submit some code at some stage soon to see where I can improve

Kind Regards
Drew
Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.