Hi,
Here is a copy of a message from Howard Bandy of Quantitative Trading
Systems in which he provides the SAR (long only) . It will follow the Buy
signal of your indicator and generate the parabolic SAR.
Grover
Howard's message begins here.
Greetings all --
Here is an AFL code that will implement the parabolic trailing stop for a
long position. Set the Buy independently. Set the initial level of the
trailing stop as you wish. The code takes care of raising the stop level as
the price rises.
////////////////////////////////////////////
// ParabolicStop.afl
//
// This implementation is for a long position.
//
// The parabolic trailing stop is set below the
// entry point on the first day of long position,
// and rises according to a formula as the price rises.
// Unlike the traditional trailing stop, the
// parabolic stop continues to rise even as the
// price holds steady or drops. Eventually, the
// price and the parabolic stop meet, which triggers
// an exit.
//
SetTradeDelays(0,0,0,0);
// Trading system entry logic goes here.
// Exit will be made by the parabolic stop.
// For example, use moving average crossover entry.
MALen1 = Optimize("MALen1",30,1,31,1);
MAvg = AMA(C,2/(MALen1+1));
MALen2 = Optimize("MALen2",15,1,31,2);
Pass = C>=MA(C,MALen2);
Buy = pass AND Cross(C,MAvg);
// The code for the Parabolic Trailing Stop begins here.
//
// Assume that entry will be made at the close of the day the
// buy signal is generated.
//
// Setting TradeAtStop to 1 assumes that there is a stop
// in place and the trade exits intraday at the stop price.
// Setting TradeAtStop to 0 assumes that intraday exit
// cannot take place (as in mutual fund end-of-day
// trading) and the trade takes place at the close
// of the signal day.
TradeAtStop = Param("TradeAtStop",0,0,1,1);
// Set the initial stop level.
// For this example, it is set at the Lowest Low
// for some number of days.
LBDays = Optimize("LBDays",1,0,10,1);
// Set the Acceleration factor and Maximum Acceleration.
IAF = Param("IAF",0.02,0.001,0.1,0.001); // acceleration factor
MaxAF = Param("MaxAF",0.2,0.001,1.0,0.001); // max acceleration
Psar = Close; // initialize
mp = 0; // flat initial conditions
Sell = 0; // clear sell signals
af = IAF; // initial acceleration factor
hp = High [ 0 ];
lp = Low [ 0 ];
Lp = LLV(Low,LBDays);
// Loop through all the bars.
for( i = 2; i < BarCount; i++ )
{
// Check for exit from long position
if ( (mp == 1) AND (Low[i] < Psar[i-1]) )
{
Sell[i] = 1;
if (TradeAtStop)
{
SellPrice[i] = Psar[i-1];
}
else
{
SellPrice[i] = Close[i];
}
mp = 0;
}
// Continuation of long position -- adjust stop
if ( mp == 1 )
{
if (High[i] > Hp)
{
Hp = High[i];
af = af + IAF;
if (af > MaxAF) af = MaxAF;
}
psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
}
else
{
// not in a long position.
// value of psar is not important.
// set the psar level so it will plot
// on the price graph.
psar[i] = Close[BarCount-1];
}
// Check for new long position
if ( (mp == 0) AND (Buy[i]) )
{
BuyPrice[i] = Close[i];
Psar[i] = Lp[i];
Hp = High[i];
af = IAF;
mp = 1;
}
}
// The code for the Parabolic Trailing Stop ends here.
Plot( Close, "Price", colorBlack, styleCandle );
Plot( MAvg, "MAvg", colorBlue,styleLine);
Plot( psar, "SAR", colorRed,
styleDots | styleNoLine | styleThick );
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Figure 7.11 Parabolic Stop - Looping Code
////////////////////////////////////////////
Thanks,
Howard
www.quantitativetradingsystems.com
From: [email protected] [mailto:[email protected]] On Behalf
Of gmorlosky
Sent: Saturday, April 25, 2009 4:27 AM
To: [email protected]
Subject: [amibroker] Need SAR in raw code form ?
Does anyone have the SAR formula in it's raw Amibroker code. I want to use
my indicator in place of the high and low value.
Thanks