PureBytes Links
Trading Reference Links
|
After two frustrating days of coding one variation after another, I would
like to ask for help in coding this seemingly simple system/indicator. I
will include my "best" (but doesn't work) code attempt and at the end of the
message will share some TradeStation code which works (as I've been told).
The length of this request will deter a lot of people from helping but I
hope some might find this indicator "interesting" or potentially "useful"
and thus would spend some time to help me.
Here is a word description:
Buy (or place an arrow) if the C today is 2% above the latest "swing low".
This becomes a "Sell-and-Reverse" level.
Maintain this constant level "pseudo-Sell-and-Reverse (pSAR low)" if the
price decreases but does not fall below the "pSAR low". (Note: this not a
traditional SAR indicator which gets closer to the price as time continues)
Raise the "pSAR low" or reversal level if the close continues to increase.
Reverse (sell or short and establish an arrow) if the close drops below the
"pSAR low", and establish a new "pSAR high" which is 2% above the close on
the reversal bar.
Maintain this constant level "pSAR high" if the price increases but does not
go above the "pSAR high".
Drop the "pSAR high" or reversal level if the close continues to decrease.
Reverse (buy or long and establish an arrow) if the close crosses above the
"pSAR high" and reestablish the "pSAR low" as 2% below the close.
Here below is the AFL code I tried which "seemed" logical but did not work
(I tried variations on this approach also with no success). Following that
is the Tradestation code (which I tried to interprete). I also separately
tried unsuccessfully to adapt the loop-based traditional SAR indicator which
Thomasz put in the library.
I know this is long but perhaps someone can quickly spot the error in my
approach or can quickly translate the TS code. In any case, I will
appreciate any help I receive.
Thanks,
Ken
Note: disclaimer: I have never used TradeStation code and so my
interpretation of its logic may not follow what the TS code is saying---the
TS code works in TS and I would like to duplicate its action in AB.
----------------------------------------------
Code attempt:
// SWING_PSAR KSC 05/28/2006
psar[0] = Close[0];
Long[1] = 1;
PercentU = 1.02;
PercentD = 0.98;
reverse[0] = 0;
for( i = 1; i < BarCount-1; i++ )
{
// Start Long
// Calculate upper or lower band
if ( Long[i] == 1 )
{
psar[i] = PercentD * Close[i];
}
else
{
psar[i] = PercentU * Close[i];
}
// Set Reversal flag to zero, condition must be exceeded to change
flag
reverse[i] = 0;
// check for reversal
if (Long[i] == 1)
{
if ( Close[i] < psar[i-1])
{
Long[i] = 0;
Reverse[i] = 1;
// change psar value for reversal
from long to short
psar[i] = PercentU * Close[i];
}
}
else
// Not a long condition so check if Close triggers
reversal
if (Close[i] > psar[i-1])
{
Long[i] = 1;
Reverse[i] = 1;
// change psar value for reversal
from short to long
psar[i] = PercentD * Close[i];
}
if ( Reverse[i] == 0 )
{ // comes here if no reversal
but to maintain psar
if ( Long[i] == 1 )
{
if ( Close[i] < Close[i-1] ) // do
not want to lower psar(i)
{
psar[i] = psar[i-1];
}
else
{
if ( Close[i] > Close[i-1] ) // do
not want raise psar[i]
{
psar[i] = psar[i-1];
}
}
}
}
}//block1
Uband = PercentU * C;
LBand = PercentD * C;
Plot(C,"RUT",6,1);
Plot(psar,"PSAR",4,1);
GraphXSpace=20;
Buy=0;
Filter =1;
AddColumn(C,"Close",1.2);
AddColumn(Uband,"Uband",1.2);
AddColumn(Lband,"LBand",1.2);
AddColumn(psar,"psar",1.2);
AddColumn(Long,"Long",1.0);
AddColumn(Reverse,"Reverse",1.0);
---------------------------------------------------------
TradeStation code:
Input:Length(1),BuyPer(.01),SellPer(.01),BuyPrice(close),SelPrice(close);
Vars:MP(0),MAVal(0),_4Per(0),HighAvg(0),LowAvg(0);
if barnumber = 1 then
begin
if close > Average(close,Length)
then begin
buy at close;
MP = 1;
end
else begin
sell at close;
MP = -1;
end;
if MP = 1 then _4Per = (1-SellPer) * Average(SelPrice,Length);
if MP = -1 then _4Per = (1+BuyPer) * Average(BuyPrice,Length);
end
ELSE
begin
If MP = 1 then
begin
_4Per = (1-SellPer) * Average(SelPrice,Length);
if close < _4Per[1] then
begin {**Reverse direction***}
sell at close;
MP = -1;
_4Per = (1+BuyPer) *
Average(BuyPrice,Length);
end;
end
ELSE
begin
_4Per = (1+BuyPer) * Average(BuyPrice,Length);
if close > _4Per[1] then
begin {**Reverse direction***}
buy at close;
MP = 1;
_4Per = (1-SellPer) *
Average(SelPrice,Length);
end;
end;
{***If we are using just high and low prices (Moving Average Length = 1)
then we do not always want to keep moving the SAR. For example, if we are
in a down trend but the market moves up a little, we do not want to have a
higher indicator. It should stay flat if this happens***}
if MP = MP[1] and Length = 1 then
begin
if MP = -1 then _4Per = MinList(_4Per,_4Per[1]);
if MP = 1 then _4Per = MaxList(_4Per,_4Per[1]);
end;
end;
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.8.1/354 - Release Date: 6/1/2006
|