PureBytes Links
Trading Reference Links
|
Hi Dave –
Have you tried the Parabolic trailing
stop? Tomasz posted the afl code for it a while back. I have
modified it so that I can set the initial level at a point of my own choosing,
rather than always at the price at which the reversal took place. If
desired, it would be easy to add code to track the number of bars since entry
and the profit to date, and exit trades based on that information.
A simplified version which handles only
long trades is copied here.
Howard
============================================================
<font size=1 color=green
face="Courier New">/////////////////////////////////
<font size=1 color=green
face="Courier New">// HB Parabolic Stop for Long positions
<font size=1 color=green
face="Courier New">//
<font size=1 color=green
face="Courier New">// Written by: <font size=1 color=green
face="Courier New">Tomasz Janeczko
<font size=1 color=green
face="Courier New">// Modified by: Howard Bandy to
allow setting of initial level
<font size=1 color=green
face="Courier New">// at
the start of each new long position.
<font size=1 color=green
face="Courier New">//
<font size=1 color=green
face="Courier New">// Parabolic SAR re-implemented in
<font size=1 color=green
face="Courier New">// native AFL.
<font size=1 color=green
face="Courier New">//
<font size=1 color=green
face="Courier New">// Example of for/if/else control statements
<font size=1 color=green
face="Courier New">//
<font size=1 color=green
face="Courier New">// Requires:
<font size=1 color=green
face="Courier New">// AmiBroker 4.31.1
<font size=1 color=green
face="Courier New">//
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// Set trade delays to trade the day the
buy and sell signals are received.
<font size=1 color=black
face="Courier New">
<font size=1 color=blue
face="Courier New">SetTradeDelays<font size=1 color=black
face="Courier New">(<span
>0<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">0<span
>,<font
size=1 color=fuchsia face="Courier New">0<font size=1
color=black face="Courier New">,<span
>0<font
size=1 color=black face="Courier New">);
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// Trading system entry logic goes here.
<font size=1 color=green
face="Courier New">// Exit will be made by the price dropping
through the
<font size=1 color=green
face="Courier New">// parabolic level.
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// For example, use moving average
crossover entry.
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">MALen = <span
>Param<font
size=1 color=black face="Courier New">(<font size=1 color=fuchsia
face="Courier New">"MALen"<font size=1 color=black
face="Courier New">,<span
>30<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">1<span
>,<font
size=1 color=fuchsia face="Courier New">100<font size=1
color=black face="Courier New">,<span
>1<font
size=1 color=black face="Courier New">);
<font size=1 color=black
face="Courier New">MAvg = <span
>MA<font
size=1 color=black face="Courier New">(C,MALen);
<font size=1 color=black
face="Courier New">Buy<font size=1 color=black
face="Courier New"> = <span
>Cross<font
size=1 color=black face="Courier New">(C,MAvg);
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// Code below assumes that entry will be
made at the close of the day the
<font size=1 color=green
face="Courier New">// buy signal is generated.
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// Setting TradeAtStop to 1 assumes that
there is a stop in place
<font size=1 color=green
face="Courier New">// and the trade exits intraday at the
stop price.
<font size=1 color=green
face="Courier New">// Setting TradeAtStop to 0 assumes that
intraday exit cannot take place
<font size=1 color=green
face="Courier New">// (as in mutual fund end-of-day trading)
and the trade takes place
<font size=1 color=green
face="Courier New">// at the close of the signal day.
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">TradeAtStop = <font size=1 color=blue
face="Courier New">Param<span
>(<font
size=1 color=fuchsia face="Courier New">"TradeAtStop"<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">1<span
>,<font
size=1 color=fuchsia face="Courier New">0<font size=1
color=black face="Courier New">,<span
>1<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">1<span
>);
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// Set the initial stop level at the lowest
low for some number of days.
<font size=1 color=green
face="Courier New">// Other ways to set the initial level
include ATR, Standard Deviation, etc.
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">LBDays = <span
>Param<font
size=1 color=black face="Courier New">(<font size=1 color=fuchsia
face="Courier New">"LBDays"<font size=1 color=black
face="Courier New">,<span
>3<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">0<span
>,<font
size=1 color=fuchsia face="Courier New">10<font size=1
color=black face="Courier New">,<span
>1<font
size=1 color=black face="Courier New">);
<font size=1 color=black
face="Courier New">
<font size=1 color=green
face="Courier New">// Set the Acceleration factor and Maximum
Acceleration.
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">IAF = <span
>Param<font
size=1 color=black face="Courier New">(<font size=1 color=fuchsia
face="Courier New">"IAF"<font size=1 color=black
face="Courier New">,<span
>0.02<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">0.001<span
>,<font
size=1 color=fuchsia face="Courier New">0.1<font size=1
color=black face="Courier New">,<span
>0.001<font
size=1 color=black face="Courier New">); <font
size=1 color=green face="Courier New">// acceleration factor
<font size=1 color=black
face="Courier New">MaxAF = <span
>Param<font
size=1 color=black face="Courier New">(<font size=1 color=fuchsia
face="Courier New">"MaxAF"<font size=1 color=black
face="Courier New">,<span
>0.2<font
size=1 color=black face="Courier New">,<font size=1 color=fuchsia
face="Courier New">0.001<span
>,<font
size=1 color=fuchsia face="Courier New">1.0<font size=1
color=black face="Courier New">,<span
>0.001<font
size=1 color=black face="Courier New">); <font
size=1 color=green face="Courier New">// max acceleration
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New">Psar = Close; <font
size=1 color=green face="Courier New">// initialize
<font size=1 color=black
face="Courier New">mp = <span
>0<font
size=1 color=black face="Courier New">; <font
size=1 color=green face="Courier New">// flat initial Market Position
<font size=1 color=black
face="Courier New">Sell<font size=1 color=black
face="Courier New"> = <span
>0<font
size=1 color=black face="Courier New">; <font
size=1 color=green face="Courier New">// clear sell signals array
<font size=1 color=black
face="Courier New">af = IAF; <font
size=1 color=green face="Courier New">// initial acceleration factor
<font size=1 color=black
face="Courier New">hp = High [ <font
size=1 color=fuchsia face="Courier New">0<font size=1
color=black face="Courier New"> ];
<font size=1 color=black
face="Courier New">lp = Low [ <font
size=1 color=fuchsia face="Courier New">0<font size=1
color=black face="Courier New"> ];
<font size=1 color=black
face="Courier New">
<font size=1 color=maroon
face="Courier New">for<font size=1 color=black
face="Courier New">( i = <span
>2<font
size=1 color=black face="Courier New">; i < BarCount;
i++ )
<font size=1 color=black
face="Courier New">{
<font size=1 color=black
face="Courier New"> <font size=1
color=green face="Courier New">// Check for exit from long position
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">if<font size=1
color=black face="Courier New"> ( (mp == <font size=1 color=fuchsia
face="Courier New">1<span
>) <span
>AND (Low[i]
< Psar[i-<span
>1<font
size=1 color=black face="Courier New">]) )
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> <span
>Sell[i] = <font size=1
color=fuchsia face="Courier New">1<span
>;
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">if<font size=1
color=black face="Courier New"> (TradeAtStop)
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> <span
>SellPrice[i] = Psar[i-<font
size=1 color=fuchsia face="Courier New">1<font size=1
color=black face="Courier New">];
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">else
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> <span
>SellPrice[i] = Close[i];
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New"> mp
= <span
>0<font
size=1 color=black face="Courier New">;
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New"> <font size=1
color=green face="Courier New">// Continuation of long position -- adjust
stop
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">if<font size=1
color=black face="Courier New"> ( mp == <font size=1 color=fuchsia
face="Courier New">1<span
> )
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">if<font size=1
color=black face="Courier New"> (High[i] > Hp)
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> Hp
= High[i];
<font size=1 color=black
face="Courier New"> af
= af + IAF;
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">if<font size=1
color=black face="Courier New"> (af > MaxAF) af = MaxAF;
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New"> psar
[ i ] = psar [ i-<span
>1<font
size=1 color=black face="Courier New"> ] + af * ( hp - psar [ i-<font
size=1 color=fuchsia face="Courier New">1<font size=1
color=black face="Courier New"> ] );
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">else
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> <font
size=1 color=green face="Courier New">// not in a long position.
<font size=1 color=black
face="Courier New"> <font
size=1 color=green face="Courier New">// value of psar is not
important.
<font size=1 color=black
face="Courier New"> <font
size=1 color=green face="Courier New">// set the psar level so it
will plot on the price graph.
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New"> psar[i]
= Close[BarCount-<font size=1 color=fuchsia
face="Courier New">1<span
>];
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New"> <font size=1
color=green face="Courier New">// Check for new long position
<font size=1 color=black
face="Courier New">
<font size=1 color=black
face="Courier New"> <font
size=1 color=maroon face="Courier New">if<font size=1
color=black face="Courier New"> ( (mp == <font size=1 color=fuchsia
face="Courier New">0<span
>) <span
>AND (Buy[i])
)
<font size=1 color=black
face="Courier New"> {
<font size=1 color=black
face="Courier New"> <span
>BuyPrice[i] = Close[i];
<font size=1 color=black
face="Courier New"> Lp
= <span
>LLV<font
size=1 color=black face="Courier New">(Low,LBDays);
<font size=1 color=black
face="Courier New"> Psar[i]
= Lp[i];
<font size=1 color=black
face="Courier New"> Hp
= High[i];
<font size=1 color=black
face="Courier New"> af
= IAF;
<font size=1 color=black
face="Courier New"> mp
= <span
>1<font
size=1 color=black face="Courier New">;
<font size=1 color=black
face="Courier New"> }
<font size=1 color=black
face="Courier New">}
<font size=1 color=black
face="Courier New">
<font size=1 color=blue
face="Courier New">Plot<span
>( <span
>Close, <font size=1
color=fuchsia face="Courier New">"Price"<font size=1 color=black
face="Courier New">, colorBlack, <span
>styleCandle );
<font size=1 color=blue
face="Courier New">Plot<span
>( MAvg, <font
size=1 color=fuchsia face="Courier New">"MAvg"<font
size=1 color=black face="Courier New">, colorBlue,<span
>styleLine);
<font size=1 color=blue
face="Courier New">Plot<span
>( psar, <font
size=1 color=fuchsia face="Courier New">"SAR"<font
size=1 color=black face="Courier New">, colorRed,
styleDots | <span
>styleNoLine | styleThick );
============================================================
-----Original Message-----
From: Dave Merrill
[mailto:dmerrill@xxxxxxx]
Sent: Friday, December 12, 2003
2:45 PM
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] exiting flat
positions
<font size=2
face="Courier New">Obviously, losses are a
problem. But so are positions that hang in there<font
size=2 face="Courier New">
forever taking up available cash but going
nowhere, without hitting profit
targets or stops.
How would you code that, assuming you're dealing
with a system that tries to
dump losers but let winners run as long as they're
advancing.
I tried starting from a modest stoploss, with the
stop percentage advancing
every day until it becomes negative, enforcing the
requirement to make a
profit or get off the bus. I'm not certain, but I
think it's working, just
not very profitable in the context I tried it:.
Here's the code:
----------------
bars_since_buy = NZ(BarsSince(buy), BarCount);
bars_since_short = NZ(BarsSince(short), BarCount);
bars_since_entry = IIf(bars_since_buy < bars_since_short,
bars_since_buy,
bars_since_short);
stoploss_rise = Optimize("stoploss
rise", .5, .1, 1, .1);
stoploss = 13 - (stoploss_rise * bars_since_entry);
ApplyStop(stopTypeLoss, stopModePercent, stoploss,
false, true, 0);
----------------
Anyone see any problems with the implementation?
Any other ideas for
avoiding sitting in stagnant positions?
Dave
<font size=2
color="#003399" face="Times New Roman">Send
BUG REPORTS to <span
>bugs@xxxxxxxxxxxxx<font size=2
face="Courier New">
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: <font
size=2 face="Courier New">amiquote@xxxxxxxxxxxxxxx<font
size=2 face="Courier New"> <font
size=2 face="Courier New">
(Web page: <a
href="">http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: <a
href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Your use of
Yahoo! Groups is subject to the Yahoo!
Terms of Service.
Yahoo! Groups Sponsor
ADVERTISEMENT
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
|