Developing, Debugging and Testing your first strategy (upd)

Moderator: admin

Re: Developing, Debugging and Testing your first strategy (upd)

Postby davetherave110179 » Thu Mar 10, 2011 9:55 am

Hi, I have used this (and many others) as a template for creating my first stratagy based upon the HPFO indicator. My programming exprience is somewhat limited. ALL my attempts have failed and I don't have any more hair to pull out!

The wonderful 'Sunshine' has said they will post a 'corrected' version 'a bit later', so I am left scratcing my bald head until then.

I was wondering if anyone could tell me where I went wrong with the coding as I am eager to learn LUA programming.

You can download and view my first attempt here:

http://www.fxcodebase.com/code/viewtopic.php?f=27&t=3597&p=8694&hilit=HPFO#p8635

I look forward to any advice anyone can give.
Please post any advice under the above link, as I am checking it daily.

davetherave110179
davetherave110179
 
Posts: 50
Joined: Wed Feb 09, 2011 6:09 am

Re: Developing, Debugging and Testing your first strategy (upd)

Postby sunshine » Fri Mar 11, 2011 4:24 am

Hi,
I've posted my answer here:
viewtopic.php?f=27&t=3597&p=8739#p8739
sunshine
 

Re: Developing, Debugging and Testing your first strategy (upd)

Postby appress » Thu Apr 14, 2011 1:28 pm

Hi Nikolay, this is a really useful starting point for me, much appreciated in its simplicity. I want to say that after trying MT4 and MT5 I find the simplicity of Lua more than makes up for the smaller code base. Who said big is best anyways, look at Switzerland!

So I have a question about your example and it's the handling of the array

not in any function
line 11 local EMAs = {}; -- an array of outputs

inside function CreateEMA
line 18 EMAs[index] = instance:addStream(label, core.Line, name .. label, label,
color, source:first() + N - 1);
inside function CalcEMA
line 54 EMAs[index][period] = core.avg(source, range);


on line 11 the declaration is not in any function, so I assume it's globally available. So why do you declare it as local?
then on line 18 the array EMAs[index] is filled by the function instance:addStream - this is a one dimensional array
on line 54 why is EMAs then referenced in a 2 dimensional way? I can only assume it's a result of the addStream function.

Your comments appreciated!
appress
 
Posts: 13
Joined: Fri Mar 18, 2011 2:47 pm

Re: Developing, Debugging and Testing your first strategy (upd)

Postby Apprentice » Fri Apr 15, 2011 3:07 am

I hope I can answer your question.

Local variable name
This is common Lau syntax.
To define whether a variable is local or global, its position is important.

A variable defined within a function.
Its scope is the functions, it is a local variable for functions within which is defined.

If you define it outside the function, then you are talking about global variables.

This definition
Array = {}
Defines one dimensional array.

Calling each member of Array.
Array [Index]

Using instance: addInternalStream you define new stream for the indicator internal purposes, it is very similar to the one dimension of Array.
Stream = instance: addInternalStream (first, 0);

Calling Stream Elements.
Stream [Period]

Joining Array and Stream, you get a kind of two-dimensional array.
Access to an individual member of such Array
Array [Index] [period]
Index Defines which stream you use to access.
Period defines which element of the stream, you are to access.
Array
[Stream 1] Period 1, Period2, ...., Period n
[Stream2] Period 1, Period2, ...., Period n
.....
[Stream n] Period 1, Period2, ...., Period n
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36480
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Developing, Debugging and Testing your first strategy (upd)

Postby Timon55 » Fri Apr 15, 2011 3:58 am

Hi appress!

Question:
on line 11 the declaration is not in any function, so I assume it's globally available. So why do you declare it as local?

Answer:
EMAs variable declared as local for current module(lua file). So if you will add an addiotional module(lua file) via require() function this variable will be not visible in that module. We recommend declare all variables as local to avoid any conflicts between variables in different modules.

Question:
then on line 18 the array EMAs[index] is filled by the function instance:addStream - this is a one dimensional array
on line 54 why is EMAs then referenced in a 2 dimensional way? I can only assume it's a result of the addStream function.

Answer:
You are right. This is a result of the addStream function. Technically the addStream function returns an array(or 'table' in Lua terms), so you get an array of output streams which are also arrays.
Timon55
FXCodeBase: Confirmed User
 
Posts: 43
Joined: Thu Jul 15, 2010 5:35 am
Location: Omsk

Re: Developing, Debugging and Testing your first strategy (upd)

Postby appress » Sun Apr 17, 2011 10:49 am

Thanks! still working through code, but questions are answered. :mrgreen:
appress
 
Posts: 13
Joined: Fri Mar 18, 2011 2:47 pm

Re: Developing, Debugging and Testing your first strategy (upd)

Postby Timon55 » Mon Apr 18, 2011 12:25 am

appress wrote:Thanks! still working through code, but questions are answered. :mrgreen:


Please feel free to ask any questions about indicators\strategies development!
Timon55
FXCodeBase: Confirmed User
 
Posts: 43
Joined: Thu Jul 15, 2010 5:35 am
Location: Omsk

Re: Developing, Debugging and Testing your first strategy (upd)

Postby sunshine » Thu Dec 22, 2011 12:42 pm

Update Dec, 23.
The top 4 posts were updated.
1) Added ELS stop/limit for risk management on NFA-regulated accounts.
2) Updated instructions for the new version of Indicore (2.1) and Marketscope (November 2011).
sunshine
 

Previous

Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 51 guests