PureBytes Links
Trading Reference Links
|
Hi Ken –
I don’t quite understand. I
don’t think you are saying that changing the Color of the psar variable
makes a difference? If you have changed the code and something weird is happening,
could you post the new version of the code so we can take a look at it?
Thanks,
Howard
-----Original Message-----
From: Ken Close
[mailto:closeks@xxxxxxxx]
Sent: Wednesday,
December 17, 2003<span
> <font
size=2 face=Tahoma>4:01 AM<font
size=2 face=Tahoma>
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] Modified SAR
code: Was: exiting flat positions
Howard—I think I responded thanking
you for this code, it looks like it has some good potential for a number of
uses.
I have one large question that I hope you
– or anyone else – can respond to.
In your code, to make it a “Long
Only” Parabolic Trailing Stop, you plot the psar variable (the SAR
itself) as equal to the last day’s Close price when the system is in the
“reverse” position. I modified the code only to plot this
part of the indicator in my background color so I do not see it on the screen
during the period the system is not long.
I have tried to analyze this but cannot
– it seems that having a psar value during out period does not produce
the correct values when trying to use this stop within an actual
backtest. Again, I so far have not been able to diagnose my hypothesis,
but am still trying. It is almost like one needs to fill the psar
array with NULLS during the period the indicator is out (not long) RATHER THAN
the last value of the Close array. Why? Again I am not sure.
Any comments?
Ken
-----Original Message-----
From: Howard Bandy
[mailto:howardbandy@xxxxxxxxx]
Sent: Sunday,
December 14, 2003<span
> <font
size=2 face=Tahoma>8:59 AM<font
size=2 face=Tahoma>
To: amibroker@xxxxxxxxxxxxxxx
Subject: RE: [amibroker] exiting
flat positions
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;
<span
>// initialize
<font size=1 color=black
face="Courier New">mp = <span
>0<font
size=1 color=black face="Courier New">;
<span
>// 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">;
<span
>// 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">
Sell[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">
<span
>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">
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">
<span
>else
<font size=1 color=black
face="Courier New">
{
<font size=1 color=black
face="Courier New">
SellPrice[i] = <span
>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">
<span
>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">
<span
>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-<font size=1 color=fuchsia
face="Courier New">1<span
> ] + af * ( hp -
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"> <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
>//
not in a long position.
<font size=1 color=black
face="Courier New">
<span
>//
value of psar is not important.
<font size=1 color=black
face="Courier New">
<span
>//
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[<span
>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">
BuyPrice[i] = <span
>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 <font
size=2 face="Courier New">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 <a
href="">Yahoo! Terms of Service.<span
>
<font size=3
face="Times New Roman">
<span
>
<span
>Send BUG REPORTS to <font
size=2 face="Courier New">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 <a
href="">Yahoo! Terms of Service.<span
>
<font size=3
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
<font size=2
face="Courier New">
<span
>Yahoo! Groups Links
To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
To unsubscribe from this group, send an
email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
Your use of Yahoo! Groups is subject to
the Yahoo! Terms of Service.
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
Yahoo! Groups Links
To visit your group on the web, go to:http://groups.yahoo.com/group/amibroker/
To unsubscribe from this group, send an email to:amibroker-unsubscribe@xxxxxxxxxxxxxxx
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
|