PureBytes Links
Trading Reference Links
|
Hi Ton What you are doing here is referencing a variable that was already defined in a previous line
longPos =
1000;
LongPos = Ref(LongPos,-1);
What you cannot do is reference the variable in the same line that is first calculating it, try this alone LongPos = Ref(LongPos,-1);
AB steps through the AFL one line at a time and fills in the full array for that line. It does not step through the entire code bar by bar, you need to write specific loops for that.
-- Cheers Graham Kav
AFL Writing Service http://www.aflwriting.com
On 30/03/2008, Ton Sieverding <ton.sieverding@xxxxxxxxxx> wrote:
Graham thanks. An interesting remark : "You
cannot define the value of an array by referencing its own precious
values". That's hard to accept, as I am doing this for many years in many
environments. Any way, so the following should
not work :
longPos =
1000;
LongPos = Ref(LongPos,-1);
But it does and without any problem or error
message. I am getting an array LongPos filled with 1000's. Where the second
line did reference to its own precious values ...
Question is of course if the initialization in
the first line did not do the job already ? By simply removing the second line I
should get the answer on that question. And the answer is : Yes it did. So
the above example tells me nothing ... Therefore let's create an array with three elements and
initialize the first three elements and see if I still get an array filled
with 1000's as above :
LongPos[0] = 1000;
LongPos[1] = 0; LongPos[2] = 0;
LongPos = Ref(LongPos,-1);
Answer is No. Only the first element is 1000 and
the next two remain 0. So you remark is correct. I cannot reference an array to
it's own previous values. At least not in AFL. And this of course also
gives me an answer to my initial question why my first statement does not work
properly where a 'For Loop' does give me the correct answer ...
BTW my question was purely theoretical. As you can
see in an answer to Mike/Bill I am using the Flip() function to get the answer
to the question if I am Long, Short or Flat. But l still have a hard
time working with arrays in AFL ... Even the basic
principles remain confusing ...
Many thanks again ....
and regards, Ton.
----- Original Message -----
Sent: Saturday, March 29, 2008 4:24
AM
Subject: Re: [amibroker] Re: Something is
different. But what ?
You cannot define the value of an array by referencing its own previous
values, ie this formula is wrong InLongPos =
IIf(Buy==1,1000,IIf(Sell==1,0,Ref(InLongPos,-1))); You
need to do this within a loop or by totally different method
here is
loop for it:
InLongPos[0] = 0; for ( i=1; i<BarCount; i++
) { if (Buy[i]) InLongPos[i] = 1000; else if (Sell[i])
InLongPos[i] = 0; else InLongPos[i] =
InLongPos[i-1]; }
OR an alternative loop if you do not like
using tons of else statements and xxx[i]=xxx[i-1]
InLongPos =
inLong = 0; for ( i=1; i<BarCount; i++ ) { if (Buy[i])
InLong = 1000; if (Sell[i]) InLong = 0; InLongPos[i] =
InLong; }
Alternatively for possibly the simplest
solution:
InLongPos = flip(buy,sell) * 1000;
There are of course
many other ways tog et same result, these above are just a
couple
-- Cheers Graham Kav AFL Writing Service http://www.aflwriting.com
On 29/03/2008, wavemechanic <timesarrow@xxxxxxxxxxxxx>
wrote:
Mike:
I don't think I'm off the mark but perhaps you did
not follow what I said. Of course Buy (and Sell) is an array (who said
it wasn't) and it is generated by something such as a function that returns
an array or by specifying conditions (e.g., stateA and
stateB). In the absence of the code that generated Buy
(and Sell) I assumed that it was generated by a function that returned an
array.
Ton stated "I want to get 1000 in inLongPos
after Buy and before Sell". In other words he wants the inLongPos
array to be filled with 1000 between the Buy bar and the bar before the Sell
bar and the other bars are 0. IIF() correctly puts 1000 in the
inLongPos at the Buy bar and 0 at the Sell bar. However, Ref()
does not fill inLongPos with 1000 before the Sell. The inLongPos
array cannot be changed but a new array can be created (which I think
is what you are saying with your Temp array). Hence, as I said
before to understand what is going on in answer to Ton's "Why not"
it is necessary to read up on arrays and to do what Ton wants with IIF()
additional code is needed to generate an array that is 1000 between Buy
and the bar before Sell and 0 elsewhere.
I think we are on the same wavelength but would
agree that things can get muddled in these types of messages where stuff is
quickly knocked out (at least in my case). I suppose all messages in
this type of forum should have the standard congressional caveat about
reserving the right to revise and extend remarks - including this
message. ;-)
Bill
----- Original Message -----
Sent: Friday, March 28, 2008 5:16
PM
Subject: [amibroker] Re: Something is different.
But what ?
> Bill, > > You're comments appear to be off the
mark. You already know that: > > 1. Buy is a special array
declared by AmiBroker. So it is always an > array. > 2. Tomasz
advises "If you're having trouble coding AFL I > suggest you generate
the arrays in the example in Excel for > yourself."
http://www.amibroker.com/guide/h_understandafl.html
> > Following Ton's thread, I believe that he is saying
that given: > > Buy = 0,1,0,0,... > Sell=
0,0,0,1,... > > In accordance with the User's Guide, he expects
his IIF statement to > perform the following logic for his InLongPos
array: > > InLongPos[0] is null due to Ref(InLongPos, -1) being
undefined. > InLongPos[1] is 1000 due to Buy[1] being 1. >
InLongPos[2] is 1000 due to Ref(InLongPos, -1) being 1000 as just >
calculated above. > InLongPos[3] is 0 due to Sell being 1. >
... > > Ton, is this what you are trying to express? >
> Perhaps the reason you are not seeing the expected results is
because > the new values for InLongPos are being calculated in a
temporary > array, then reassigned to the old InLongPos variable as
follows (this > is just speculation, I haven't tried it): >
> InLongPos = 0,0,0,0 // After your first initialization to
zero. > > Temp[0] is null due to Ref(InLongPos, -1) being
undefined. > Temp[1] is 1000 due to Buy[1] being 1. > Temp[2] is
0 due to Ref(InLongPos, -1) being 0. > Temp[3] is 0 due to Sell being
1. > > InLongPos = Temp; > > Would that explain
what you are seeing? > > Mike > > --- In
amibroker@xxxxxxxxxxxxxxx, "wavemechanic" <timesarrow@xxx>
>
wrote: >> >> So what if that's what you did with
Excel. I don't know how > Excel "thinks" but it makes no
difference because all that you have > to worry about is AFL. I
assume that Buy (and consequently > inLongPos) is an array that was
generated by a function that returns > an array (e.g., Cross(), MA(),
etc.) in which case no matter what you > do you will have to deal
with that fact. If it is not an array why > did you use Ref()
which operates on arrays? Forget about what you > did in Excel
and ask yourself if you did or did not generate Buy with > a function
that returns an array? If you conclude that Buy is an > array
then deal with it as such because nothing else will work, >
including >> >> Bill
>> ----- Original Message -----
>> From: Ton Sieverding >> To:
amibroker@xxxxxxxxxxxxxxx
>> Sent: Friday, March 28, 2008 11:45
AM >> Subject: Re: [amibroker] Re: Something is
different. But what ? >> >> >> Bill
that's what I have done in Excel. And Ref(-1) show me the > previous
cell. So if the >> previous cell shows 1.000 then the
actual cell should also be > 1.000 if there is no Sell. Please look
what the statement says : >> >> 1. If Buy let
the actual cell be 1.000 >> 2. If Sell let the actual
cell be 0. >> 3. Otherwise Cell(-1) = Cell(0)
... >> >> Regards, Ton. >>
>> ----- Original Message -----
>> From: wavemechanic
>> To: amibroker@xxxxxxxxxxxxxxx
>> Sent: Friday, March 28, 2008
2:57 PM >> Subject: Re: [amibroker] Re:
Something is different. But what ? >> >> >>
>> To understand "why not" take a look at
the array discussion in > the Users Guide and think about what the
inlongpos array looks like > at each bar and what ref(..., -1) is
looking at. >> >>
Bill >> ----- Original Message
----- >> From: Ton Sieverding
>> To: amibroker@xxxxxxxxxxxxxxx
>> Sent: Friday, March
28, 2008 5:52 AM >> Subject:
Re: [amibroker] Re: Something is different. But > what ? >>
>> >> Mike/Bill
thanks for the answers. Mike I am already using > Flip for LongPos
and ShortPos. This works fine for me ( LongPos = Flip > (Buy,Sell) and
ShortPos=Flip(Short,Cover) ). So that's not my > problem. My
problem is that I still do not see the difference between > the two
statements I have given and would like to know why there is a >
difference. Bill in the first statement your should get same result >
as with the For Loop. So LongPos will become '1000' as soon as we >
have a Buy and will switch to '0' again with a 'Sell'. Assume T-1 had
> a Buy then LongPos(T-1) = 1000. Therefore LongPos(T) will be set
> to '1000' also. But you're right. This is not what is happening.
And > I still do not understand why not ... >>
>> Regards, Ton. >>
>> ----- Original
Message ----- >>
From: Mike >> To:
amibroker@xxxxxxxxxxxxxxx
>> Sent:
Friday, March 28, 2008 12:33
AM >> Subject:
[amibroker] Re: Something is different. But what ? >> >>
>>
Sorry, >>
>> That should
probably read Flip(ExRem(Buy, Sell), Sell) * >
1000; >> And maybe
not much easier after all ;) >>
>>
Mike >>
>> --- In
amibroker@xxxxxxxxxxxxxxx, "Mike" <sfclimbers@>
>
wrote: >>
> >> > You
could probably leverage the Flip function to make > this easier
>> on
>> >
yourself. >> >
>> >
e.g. >> >
>> > Buy =
... >> > Sell =
... >> >
InLongPos = Flip(ExRem(Buy, Sell)) *
1000; >> >
>> >
Mike >> >
>> > --- In
amibroker@xxxxxxxxxxxxxxx, "wavemechanic"
> <timesarrow@>
>> >
wrote: >> >
> >> > >
The iif() does not give the same result because ref > (inlongpos,
- >> 1)
>> > == 0
except when the previous bar is a buy. You can see > exactly
>> what
>> > is
happening graphically
with >> > >
>> > > buy =
>> > > sell
= >> > >
inlongpos =
iif(... >> >
> plot(c, "", iif(buy, colorred, iif(sell, coloryellow,
>> >
colorpalegreen)),
stylebar); >> >
> title = "inlongpos = " + inlongpos + " ref > (inlongpos..."
+ ref >> >
(inlongpos...) + " buy = " + buy + " sell =" +
sell >> > >
>> > > If
you want the iif() approach to hold either a buy or > sell value
>> > for each
bar additional code is needed to create this >
condition. >> >
> >> > >
Bill >> > >
>> > >
>> > > -----
Original Message -----
>> > > From:
Ton Sieverding >>
> > To: amibroker@xxxxxxxxxxxxxxx
>> >
> Sent: Thursday, March 27, 2008 8:10
AM >> > >
Subject: Re: [amibroker] Something is different. But > what
? >> > >
>> > >
>> > > Sure.
This of course if part of an AFL with Buy and > Sell
>> defined.
>> > Also an
init for
InLongPos >> >
> being set to zero as a starter. Again the ForLoop works > fine.
I >> > checked
that with following statement :
AddColumn >> >
(InLongPos,"Long",1);
>> > > My
problem is that I do not understand why the first > statement
>> > does not
give me the correct answer where the second > does
... >> > >
>> > >
Regards, Ton. >>
> > >> >
> ----- Original Message -----
>> > > From:
wavemechanic >>
> > To: amibroker@xxxxxxxxxxxxxxx
>> >
> Sent: Thursday, March 27, 2008 12:55
PM >> > >
Subject: Re: [amibroker] Something is different. But > what
? >> > >
>> > >
>> > >
>> > > Is
there more to the code? Are you getting a
>> >
syntax/initialization error? How are you handling the > case
when i >> ==
>> >
1? >> > >
>> > >
Bill >> > >
>> > > -----
Original Message -----
>> > > From:
"amsiev"
<ton.sieverding@> >>
> > To: <amibroker@xxxxxxxxxxxxxxx>
>> >
> Sent: Thursday, March 27, 2008 7:01
AM >> > >
Subject: [amibroker] Something is different. But what
? >> > >
>> > >
>> > > >
Why is following AFL statement
: >> > > >
>> > > >
InLongPos = IIf(Buy==1,1000,IIf(Sell==1,0,Ref >
(InLongPos,- >>
1))); >> > >
> >> > >
> giving me a different result as following ForLoop
: >> > > >
>> > > >
for ( i=1; i<BarCount; i++
) >> > > >
{ >> > > >
if (Buy[i]==1) >>
> > > InLongPos[i] =
1000; >> > >
> else >> >
> > { >> >
> > if
(Sell[i]==1) >>
> > > InLongPos[i] =
0; >> > >
> else >> >
> > InLongPos[i] =
InLongPos[i-1]; >>
> > > } >>
> > > } >>
> > > >>
> > > The result I am getting from the ForLoop is correct. >
The >>
first >> > >
> statement gives me a wrong answer. I want to get 1000 > in
>> >
InLongPos >> >
> > after Buy and before Sell ... When testing the > statement
in >> >
Excel >> > >
> it works fine with : =IF(A6=1;1000;IF(B6=1;0;C5))
... >> > >
> >> > >
> What's wrong
? >> > > >
__._,_.___
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
__,_._,___
|