Re: Reference other array objects from within loop?
- From: D Herring <dherring@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 19 Jul 2009 00:33:45 -0400
Bata wrote:
Hello all,
I am in the process of making a program that I hope will help me
simulate trading programs in CL. Helps me to learn the language if I am
making something that I would have some actual use for. I have already
(with guidance from this newsgroup) written the code that reads the raw
data from a csv-file and puts the relevant data into an array of
objects. What I need to figure out now is how best to analyze the data.
What I need is a way to run a particular (arbitrary) piece of code over
each and every object in the array. This is the easy part since I can
just put this code within a 'loop' command (or the closest most useful
mash-up of 'do' or something). The conceptual difficulty that I am
facing right now is because of the nature of the code within the loop.
I need this code to be able to gather data from "this" object ("this"
being any particular iteration of the loop in the array) as well as (for
example) the last 15 objects in that same array that is being traversed.
For example, I would like to know how to do the following in the code
that is within the loop:
Is the current bar's high above or below the moving average (of
high's (inconsequential to the main question asked here)) of the _last_
15 bars?
I feel that I should define a macro that will evaluate the above code
(well, the code that answers the above question actually) over each and
every bar (naturally, there will be a check to make sure that I don't
get kooky results for the first 15 objects (bars) in the array). How do
I get the code within the loop to be able to call the slots in the past
15 objects of the array?
Do this (more or less) the same way you would in any other language. Change the inner function definition to take two arguments: the current value and also any shared state it may need. Or bind a dynamic variable (see defvar and special) to use as a hidden parameter.
For kicks, a circular list makes an easy ring buffer. Here's a simple helper function.
(defun make-circle (length)
(let ((list nil))
(dotimes (n length)
(push nil list))
(setf (cdr (last list)) list)
list))
Be careful with circular lists; don't pass them to LENGTH, and make sure to set *print-circle* to t.
- Daniel
.
- References:
- Prev by Date: Re: Reference other array objects from within loop?
- Next by Date: இ◥இ◤【paypal payment】(www.guoshitrade.com) wholesale nike dunk sneakers wholesale puma sneakers wholesale adidas sneakers
- Previous by thread: Re: Reference other array objects from within loop?
- Next by thread: இ◥இ◤【paypal payment】(www.guoshitrade.com) wholesale nike dunk sneakers wholesale puma sneakers wholesale adidas sneakers
- Index(es):