PureBytes Links
Trading Reference Links
|
Hello,
One (unrelated) warning:
Anthony reported to me that MP variable used in the formula
below may conflict with
Stephane's Candlestick DLL (from 3rd party area) because he
uses MP for "median price" function.
You may need to rename the variable or remove the
plugin.
Best regards,Tomasz Janeczkoamibroker.com
<BLOCKQUOTE
>
----- Original Message -----
<DIV
>From:
Howard
Bandy
To: <A title=amibroker@xxxxxxxxxxxxxxx
href="">amibroker@xxxxxxxxxxxxxxx
Sent: Wednesday, December 17, 2003 2:13
PM
Subject: RE: [amibroker] Modified SAR
code: Was: exiting flat positions
<SPAN
>Hi Ken
–
<SPAN
>
<SPAN
>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?
<SPAN
>
<SPAN
>Thanks,
<SPAN
>Howard
<SPAN
>
<DIV
>
<SPAN
>-----Original
Message-----From: Ken Close
[mailto:closeks@xxxxxxxx] <SPAN
>Sent: <FONT face=Tahoma
size=2>Wednesday, December
17, 2003<SPAN
> <FONT face=Tahoma
size=2>4:01
AM<SPAN
><SPAN
>To: amibroker@xxxxxxxxxxxxxxx<SPAN
>Subject: [amibroker] Modified SAR code:
Was: exiting flat positions
<SPAN
>
<SPAN
>Howard—I think I
responded thanking you for this code, it looks like it has some good potential
for a number of uses.
<SPAN
>
<SPAN
>I have one large
question that I hope you – or anyone else – can respond to.
<SPAN
>
<SPAN
>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.
<SPAN
>
<SPAN
>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.
<SPAN
>
<SPAN
>Any
comments?
<SPAN
>
<SPAN
>Ken
<SPAN
>
<SPAN
>-----Original
Message-----From: Howard
Bandy [mailto:howardbandy@xxxxxxxxx] <SPAN
>Sent: <FONT face=Tahoma
size=2>Sunday, December 14,
2003<SPAN
> <FONT face=Tahoma
size=2>8:59
AM<SPAN
><SPAN
>To: amibroker@xxxxxxxxxxxxxxx<SPAN
>Subject: RE: [amibroker] exiting flat
positions
<SPAN
>
<SPAN
>Hi Dave
–
<SPAN
>
<SPAN
>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.
<SPAN
>
<SPAN
>A simplified version
which handles only long trades is copied here.
<SPAN
>
<SPAN
>Howard
<SPAN
>
<SPAN
>============================================================
<SPAN
>
<SPAN
>/////////////////////////////////
<SPAN
>// HB
Parabolic Stop for Long positions
<SPAN
>//
<SPAN
>// Written
by: <SPAN
>Tomasz
Janeczko
<SPAN
>//
Modified by: Howard Bandy to allow setting of initial
level
<SPAN
>//
at the start of each new long position.
<SPAN
>//
<SPAN
>// Parabolic
SAR re-implemented in
<SPAN
>// native
AFL.
<SPAN
>//
<SPAN
>// Example of
for/if/else control statements
<SPAN
>//
<SPAN
>//
Requires:
<SPAN
>// AmiBroker
4.31.1
<SPAN
>//
<SPAN
>
<SPAN
>
<SPAN
>
<SPAN
>//
Set trade delays to trade the day the buy and sell signals are
received.
<SPAN
>
<SPAN
>SetTradeDelays<FONT
face="Courier New" color=black size=1><SPAN
>(<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>);
<SPAN
>
<SPAN
>//
Trading system entry logic goes here.
<SPAN
>//
Exit will be made by the price dropping through the
<SPAN
>//
parabolic level.
<SPAN
>
<SPAN
>//
For example, use moving average crossover entry.
<SPAN
>
<SPAN
>MALen =
<SPAN
>Param<FONT
face="Courier New" color=black size=1><SPAN
>(<FONT
face="Courier New" color=fuchsia size=1><SPAN
>"MALen"<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>30<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>100<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>);
<SPAN
>MAvg =
<SPAN
>MA<FONT
face="Courier New" color=black size=1><SPAN
>(<SPAN
>C,MALen);
<SPAN
>Buy<FONT
face="Courier New" color=black size=1><SPAN
> =
<SPAN
>Cross<FONT
face="Courier New" color=black size=1><SPAN
>(<SPAN
>C,MAvg);
<SPAN
>
<SPAN
>//
Code below assumes that entry will be made at the close of the day the
<SPAN
>//
buy signal is generated.
<SPAN
>
<SPAN
>//
Setting TradeAtStop to 1 assumes that there is a stop in
place
<SPAN
>//
and the trade exits intraday at the stop price.
<SPAN
>//
Setting TradeAtStop to 0 assumes that intraday exit cannot take
place
<SPAN
>//
(as in mutual fund end-of-day trading) and the trade takes
place
<SPAN
>//
at the close of the signal day.
<SPAN
>
<SPAN
>TradeAtStop =
<SPAN
>Param<FONT
face="Courier New" color=black size=1><SPAN
>(<FONT
face="Courier New" color=fuchsia size=1><SPAN
>"TradeAtStop"<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>);
<SPAN
>
<SPAN
>//
Set the initial stop level at the lowest low for some number of
days.
<SPAN
>//
Other ways to set the initial level include ATR, Standard Deviation,
etc.
<SPAN
>
<SPAN
>LBDays =
<SPAN
>Param<FONT
face="Courier New" color=black size=1><SPAN
>(<FONT
face="Courier New" color=fuchsia size=1><SPAN
>"LBDays"<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>3<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>10<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>);
<SPAN
>
<SPAN
>//
Set the Acceleration factor and Maximum Acceleration.
<SPAN
>
<SPAN
>IAF =
<SPAN
>Param<FONT
face="Courier New" color=black size=1><SPAN
>(<FONT
face="Courier New" color=fuchsia size=1><SPAN
>"IAF"<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.02<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.001<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.1<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.001<FONT
face="Courier New" color=black size=1><SPAN
>);
<SPAN
>//
acceleration factor
<SPAN
>MaxAF =
<SPAN
>Param<FONT
face="Courier New" color=black size=1><SPAN
>(<FONT
face="Courier New" color=fuchsia size=1><SPAN
>"MaxAF"<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.2<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.001<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1.0<FONT
face="Courier New" color=black size=1><SPAN
>,<FONT
face="Courier New" color=fuchsia size=1><SPAN
>0.001<FONT
face="Courier New" color=black size=1><SPAN
>);
<SPAN
>// max
acceleration
<SPAN
>
<SPAN
>Psar =
<SPAN
>Close;
<SPAN
>//
initialize
<SPAN
>mp =
<SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>;
<SPAN
>// flat
initial Market Position
<SPAN
>Sell<FONT
face="Courier New" color=black size=1><SPAN
> =
<SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>;
<SPAN
>// clear sell
signals array
<SPAN
>af =
IAF; <FONT
face="Courier New" color=green size=1><SPAN
>// initial
acceleration factor
<SPAN
>hp = <SPAN
>High [ <FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>
];
<SPAN
>lp = <SPAN
>Low [ <FONT
face="Courier New" color=fuchsia size=1><SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>
];
<SPAN
>
<SPAN
>for<FONT
face="Courier New" color=black size=1><SPAN
>( i =
<SPAN
>2<FONT
face="Courier New" color=black size=1><SPAN
>; i <
BarCount; i++
)
<SPAN
>{
<SPAN
>
<SPAN
>//
Check for exit from long position
<SPAN
>
<SPAN
>
<SPAN
>if<FONT
face="Courier New" color=black size=1><SPAN
> ( (mp
== <SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>) <SPAN
>AND (<SPAN
>Low[i] < Psar[i-<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>])
)
<SPAN
>
{
<SPAN
>
Sell[i] = <FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>;
<SPAN
>
<SPAN
>if<FONT
face="Courier New" color=black size=1><SPAN
>
(TradeAtStop)
<SPAN
>
{
<SPAN
>
SellPrice[i] =
Psar[i-<SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>];
<SPAN
>
}
<SPAN
>
<SPAN
>else
<SPAN
>
{
<SPAN
>
SellPrice[i] = <SPAN
>Close[i];
<SPAN
>
}
<SPAN
>
mp = <SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>;
<SPAN
>
}
<SPAN
>
<SPAN
>
<SPAN
>//
Continuation of long position -- adjust stop
<SPAN
>
<SPAN
>
<SPAN
>if<FONT
face="Courier New" color=black size=1><SPAN
> ( mp ==
<SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>
)
<SPAN
>
{
<SPAN
>
<SPAN
>if<FONT
face="Courier New" color=black size=1><SPAN
> (<SPAN
>High[i] > Hp)
<SPAN
>
{
<SPAN
>
Hp = High[i];
<SPAN
>
af = af + IAF;
<SPAN
>
<SPAN
>if<FONT
face="Courier New" color=black size=1><SPAN
> (af >
MaxAF) af = MaxAF;
<SPAN
>
}
<SPAN
>
psar [ i ] = psar [ i-<FONT face="Courier New" color=fuchsia
size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
> ] + af * (
hp - psar [ i-<FONT face="Courier New" color=fuchsia
size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
> ]
);
<SPAN
>
}
<SPAN
>
<SPAN
>else
<SPAN
>
{
<SPAN
>
<SPAN
>//
not in a long position.
<SPAN
>
<SPAN
>//
value of psar is not important.
<SPAN
>
<SPAN
>//
set the psar level so it will plot on the price graph.
<SPAN
>
<SPAN
>
psar[i] = Close[<SPAN
>BarCount-<FONT
face="Courier New" color=fuchsia size=1><SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>];
<SPAN
>
}
<SPAN
>
<SPAN
>
<SPAN
>//
Check for new long position
<SPAN
>
<SPAN
>
<SPAN
>if<FONT
face="Courier New" color=black size=1><SPAN
> ( (mp
== <SPAN
>0<FONT
face="Courier New" color=black size=1><SPAN
>) <SPAN
>AND (<SPAN
>Buy[i]) )
<SPAN
>
{
<SPAN
>
BuyPrice[i] = <SPAN
>Close[i];
<SPAN
>
Lp = <SPAN
>LLV<FONT
face="Courier New" color=black size=1><SPAN
>(<SPAN
>Low,LBDays);
<SPAN
>
Psar[i] = Lp[i];
<SPAN
>
Hp = High[i];
<SPAN
>
af = IAF;
<SPAN
>
mp = <SPAN
>1<FONT
face="Courier New" color=black size=1><SPAN
>;
<SPAN
>
}
<SPAN
>}
<SPAN
>
<SPAN
>Plot<FONT
face="Courier New" color=black size=1><SPAN
>( <SPAN
>Close, <FONT
face="Courier New" color=fuchsia size=1><SPAN
>"Price"<FONT
face="Courier New" color=black size=1><SPAN
>, <SPAN
>colorBlack, <SPAN
>styleCandle );
<SPAN
>Plot<FONT
face="Courier New" color=black size=1><SPAN
>( MAvg,
<SPAN
>"MAvg"<FONT
face="Courier New" color=black size=1><SPAN
>, <SPAN
>colorBlue,<SPAN
>styleLine);
<SPAN
>Plot<FONT
face="Courier New" color=black size=1><SPAN
>( psar,
<SPAN
>"SAR"<FONT
face="Courier New" color=black size=1><SPAN
>, <SPAN
>colorRed, <SPAN
>styleDots | <SPAN
>styleNoLine | <SPAN
>styleThick );
<SPAN
>
<SPAN
>
<SPAN
>
<SPAN
>============================================================
<SPAN
>
<SPAN
>-----Original
Message-----From: Dave
Merrill [mailto:dmerrill@xxxxxxx] <SPAN
>Sent: Friday, December 12, 2003 2:45
PMTo:
amibroker@xxxxxxxxxxxxxxx<SPAN
>Subject: [amibroker] exiting flat
positions
<SPAN
>
<FONT face="Courier New"
size=2>Obviously, losses are a problem. But so
are positions that hang in there<FONT face="Courier New"
size=2><FONT
face="Courier New">forever taking up available cash but going nowhere, without
hitting profittargets or
stops.How would you code
that, assuming you're dealing with a system that tries
todump losers but let winners run
as long as they're advancing.<FONT
face="Courier New">I tried starting from a modest stoploss, with the stop
percentage advancingevery day
until it becomes negative, enforcing the requirement to make
aprofit or get off the bus. I'm
not certain, but I think it's working, just<FONT
face="Courier New">not very profitable in the context I tried
it:.Here's the
code:<FONT
face="Courier New">----------------<FONT
face="Courier New">bars_since_buy = NZ(BarsSince(buy),
BarCount);bars_since_short =
NZ(BarsSince(short), BarCount);<FONT
face="Courier New">bars_since_entry = IIf(bars_since_buy <
bars_since_short, bars_since_buy,<FONT
face="Courier New">bars_since_short);<FONT
face="Courier New">stoploss_rise = Optimize("stoploss rise", .5, .1, 1,
.1);stoploss = 13 -
(stoploss_rise * bars_since_entry);<FONT
face="Courier New">ApplyStop(stopTypeLoss, stopModePercent, stoploss, false,
true, 0);<FONT
face="Courier New">----------------<FONT
face="Courier New">Anyone see any problems with the implementation? Any other
ideas foravoiding sitting in
stagnant positions?<FONT
face="Courier New">Dave
<TD
bgColor=#ffffcc>
<FONT
face="Times New Roman" color=#003399 size=2><SPAN
>Send BUG
REPORTS to <FONT face="Courier New"
size=2><SPAN
>bugs@xxxxxxxxxxxxx<FONT
face="Courier New" size=2><SPAN
><FONT
face="Courier New">Send SUGGESTIONS to
suggest@xxxxxxxxxxxxx<FONT
face="Courier New">-----------------------------------------<FONT
face="Courier New">Post AmiQuote-related messages ONLY to:
<FONT face="Courier New"
size=2><SPAN
>amiquote@xxxxxxxxxxxxxxx<FONT
face="Courier New" size=2><SPAN
>
<SPAN
><FONT
face="Courier New">(Web page: <A
href="">http://groups.yahoo.com/group/amiquote/messages/)<FONT
face="Courier New">--------------------------------------------<FONT
face="Courier New">Check group FAQ at: <A
href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html
<SPAN
>Your use of Yahoo! Groups is
subject to the Yahoo! Terms
of Service.<SPAN
>
<FONT
face="Times New Roman" size=3><SPAN
>
<SPAN
><FONT
face="Courier New" size=2><SPAN
>Send BUG REPORTS to
<SPAN
>bugs@xxxxxxxxxxxxx<FONT
face="Courier New" size=2><SPAN
><FONT
face="Courier New">Send SUGGESTIONS to
suggest@xxxxxxxxxxxxx<FONT
face="Courier New">-----------------------------------------<FONT
face="Courier New">Post AmiQuote-related messages ONLY to:
<FONT face="Courier New"
size=2><SPAN
>amiquote@xxxxxxxxxxxxxxx<FONT
face="Courier New" size=2><SPAN
>
<SPAN
><FONT
face="Courier New">(Web page: <A
href="">http://groups.yahoo.com/group/amiquote/messages/)<FONT
face="Courier New">--------------------------------------------<FONT
face="Courier New">Check group FAQ at: <A
href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html
<SPAN
>Your use of Yahoo! Groups is
subject to the Yahoo! Terms
of Service.<SPAN
>
<SPAN
>
<FONT face="Times New Roman"
size=3><FONT
face="Courier New" size=2>Send BUG REPORTS to
<SPAN
>bugs@xxxxxxxxxxxxx<FONT
face="Courier New" size=2><SPAN
><FONT
face="Courier New">Send SUGGESTIONS to
suggest@xxxxxxxxxxxxx<FONT
face="Courier New">-----------------------------------------<FONT
face="Courier New">Post AmiQuote-related messages ONLY to:
<SPAN
>amiquote@xxxxxxxxxxxxxxx<FONT
face="Courier New" size=2>
<SPAN
><FONT
face="Courier New">(Web page: <A
href="">http://groups.yahoo.com/group/amiquote/messages/)<FONT
face="Courier New">--------------------------------------------<FONT
face="Courier New">Check group FAQ at: <A
href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html
<FONT
face="Courier New" size=2>
<SPAN
>Yahoo! Groups
Links
<SPAN
>To visit your group on
the web, go to:<A
href="">http://groups.yahoo.com/group/amibroker/
<SPAN
>To unsubscribe from this
group, send an email to:<A
href="">amibroker-unsubscribe@xxxxxxxxxxxxxxx
<SPAN
>Your use of Yahoo!
Groups is subject to the Yahoo!
Terms of Service. Send
BUG REPORTS to bugs@xxxxxxxxxxxxxSend SUGGESTIONS to
suggest@xxxxxxxxxxxxx-----------------------------------------Post
AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx (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
Yahoo! Groups Links
To visit your group on the web, go to:<A
href="">http://groups.yahoo.com/group/amibroker/
To unsubscribe from this group, send an email to:<A
href="">amibroker-unsubscribe@xxxxxxxxxxxxxxx
Your use of Yahoo! Groups is subject to the <A
href="">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.
|