Hi John --
This is the code for the long portion of a parabolic trailing exit.
It is not everything you asked for, but it may help you get started.This is the code for the long portion of a parabolic trailing exit.
The code is intended to be educational. After you have studied it, modified it to suit your purposes, you will be able to make it more efficient (if you need to).
// 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
On Wed, Feb 3, 2016 at 1:03 PM, pzt27@yahoo.co.uk [amibroker] <amibroker@yahoogroups.com> wrote:
I have version 5.9 & recently downloaded an indicator called ParabXO from the library. Code check produced 14 errors but underneath the item were comments including "this is exactly what I was looking for" & "great work."
Can anyone advise whether older library contributions are incompatible with more recent editions?
Thank you,
John
__._,_.___
Posted by: Howard B <howardbandy@gmail.com>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (4) |
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
.
__,_._,___
EmoticonEmoticon