PureBytes Links
Trading Reference Links
|
Hi,
this is an idea, don´t know if it works...
BuyProfit=IIf(Sell,(SellPrice-ValueWhen(Buy,BuyPrice)),0);
ShortProfit=IIf(Cover,(ValueWhen(Short,ShortPrice)-CoverPrice),0);
Profit=Cum(BuyProfit+ShortProfit);
Plot( Profit, "Profit", colorBlack, 65537);
Regards
T.O.
--- In amibroker@xxxxxxxxxxxxxxx, Thomas Ludwig <Thomas.Ludwig@xxx>
wrote:
>
> After Herman's remark some time ago that it's much faster to do a
visual
> optimization by running the system as an indicator and plotting the
equity
> curve, I've searched for a convenient way to achieve this.
>
> I'm doing it right now by using Daniel LaLiberte's
TradeVisualizer.afl which
> he published in this list (Dan was so kind to send me his newest
version -
> thanks again!) - see the code at the end of this posting. I saved
it in my
> Include folder and add it with #include <TradeVisualizer.afl> to
the
> appropriate system. I added some code to Dan's formula from
Amibroker's
> Portfolio.afl which I modified a bit in order to plot the Equity
curve, Cash
> and Drawdown.
>
> So far it works well - but not always! There is a phenomenon that
really
> puzzles me: Yesterday the formula worked well. Today I started
Amibroker and
> doubleclicked a formula (with #include <TradeVisualizer.afl>). The
Equity
> curve was plotted - but only as a horizontal straight line
equivalent to the
> initial equity amount. Changing the variables via the Param slider
didn't
> have any effect. The same was true for other formulas I tried. Then
I
> performed a backtest of a formula - now all of a sudden the equity
curve as
> an indicator plotted as expected! In order to evaluate this I
restarted
> Amibroker but this time everything worked as it should - funny. Do
you have
> any idea what's going on here? If that's called a consistent
behavior of
> Amibroker then I simply don't understand it.
>
> A second problem is that the equity curve plotted by
TradeVisualizer is *very*
> similar to the equity curve plotted by the Backtester but not
completely
> identical. The end values are identical, but in some situations the
values
> for equity and drawdown differ a bit from the ones of the
Backtester, in
> other situations they are identical. To give you an impression of
the
> magnitude: If I select 50000 as initial equity, the differences in
the equity
> and drawdown figures are not higher than a few hundred bucks. No
big deal,
> but nevertheless I would like to understand why.
>
> Any help would be highly appreciated.
>
> Regards, Thomas
>
> Here's Dan' code which I modified as mentined above:
>
> // TradeVisualizer.afl -- Indicate trades and change in equity.
> // Daniel LaLiberte liberte@xxx
>
> /*
> Features:
> Show realized and unrealized net profit
> Show trade signals.
> Show trade arrows.
>
> Bugs, assumptions, limitations, and side effects:
> Assumes GraphZOrder == 0 which is the default.
> Assume only one open position.
> Assumes Short and Cover are defined - at least do this: Short
= Cover = false
> Assumes order delay is 1. (It should never be 0.)
> Calls Equity(0, 0), which should not have side effects.
> This visualizer should not affect your actual signals.
> The unrealized net profit is moved back one bar, which is
easier to
> understand.
>
> Instructions:
> Save TradeVisualizer.afl in your Include folder.
> Add the following at the end of your scripts, after your
final Buy, Sell,
> Short, Cover assignments:
> // #include <TradeVisualizer.afl>
> (It does not work to add this as an Overlay)
> Remove your ExRem() calls to see regions of raw Buy, Sell,
Short, Cover
> signals
>
> More ideas:
> Show stops/limits.
> Deal with order delays - it assumes 1 bar delay.
> Show pyramid trades, number of shares per trade.
> Display text of profit/loss.
> Optionally compute equity across stocks.
> Parameterize all this.
>
> Please send improvements or suggestions.
> */
>
> _SECTION_BEGIN("TradeVisualizer");
>
> //========================================
>
> backgroundColor = colorWhite;
>
> // Colors of signal bars
> buyColor = colorPaleGreen; // ColorRGB(230, 255, 230); // very pale
> sellColor = colorRose;
> shortColor = colorLavender;
> coverColor = colorSkyblue;
>
> // Colors of arrows
> buyArrowColor = colorLime;
> sellArrowColor = colorRed;
> shortArrowColor = colorViolet;
> coverArrowColor = colorBlue;
>
> // Colors of net profit
> gainColor = colorLime; // unrealized gain relative to last trade
> lossColor = colorOrange; // unrealized loss relative to last trade
> netPositiveColor = colorGreen; // overall realized net profit is
positive
> netNegativeColor = colorRed; // overall realized net profit is
negative
>
>
> //========================================
> // Only show stuff if this is running as an indicator
> GraphXSpace = 5; // adds 5% extra space above AND below.
>
> isIndicator = Status("action") == actionIndicator;
> if (isIndicator)
> {
>
> rawBuy = Buy;
> rawSell = Sell;
> rawShort = Short;
> rawCover = Cover;
>
> // Assume only one open position.
> buyExRem = ExRem(Buy, Sell);
> sellExRem = ExRem(Sell, Buy);
> shortExRem = ExRem(Short, Cover);
> coverExRem = ExRem(Cover, Short);
>
> sinceBuy = BarsSince (buyExRem);
> sincesell = BarsSince(Sellexrem);
> sinceshort = BarsSince(ShortExRem);
> sincecover = BarsSince(CoverExRem);
> incash=sincesell<sincebuy OR sincecover<sinceshort;
>
>
> eq=Equity();
> dr = eq - Highest(eq);
> bslh = HighestBars(eq);
> Cash=ValueWhen( incash, eq);
> GraphZOrder=1;
> Plot(eq, "Portfolio Equity", IIf(incash,colorGreen,colorLightBlue),
> styleArea );
> //if( ParamToggle("Show Cash", "No|Yes", 1 ) ) Plot(cash, "Cash",
colorGreen,
> styleArea );
> if( ParamToggle("Show Drawdown", "No|Yes", 1 ) ) Plot
(dr, "Drawdown",
> colorDarkRed, styleArea );
> if( ParamToggle("Show #bars since last high", "No|Yes", 0 ) )
> Plot(bslh, "#bars since last high", colorDarkYellow, styleLine |
> styleOwnScale, 0, 10 * LastValue( Highest( bslh ) ) );
> islastbar = Status("lastbarintest");
> isfirstbar = Status("firstbarintest");
> bar = BarIndex();
> firstbar = LastValue( ValueWhen( isfirstbar, bar ) );
> lastbar = LastValue( ValueWhen( islastbar, bar ) );
> al = LastValue( ValueWhen( islastbar, LinRegSlope( eq, Lastbar -
firstbar +
> 1 ) ) );
> bl = LastValue( ValueWhen( islastbar, LinRegIntercept( eq, Lastbar -
firstbar
> + 1 ) ) );
> Lr = al * ( BarIndex() - firstbar ) + bl;
> Lr = IIf( bar >= firstbar AND bar <= lastbar , Lr, Null );
> if( ParamToggle("Show lin. reg.", "No|Yes", 0 ) )Plot( Lr , "Linear
Reg",
> colorRed, styleThick );
> RelPerf=eq/C;
> if(ParamToggle("Show Rel.Perf Equity", "No|Yes",0)) Plot
(RelPerf, "Rel.Perf.",
> colorBlack,styleThick|styleOwnScale);
>
> if( ParamToggle("Show Buy-and-Hold?", "No|Yes", 1 ) )
> {
> /* now buy and hold simulation */
> Short=Cover=0;
> Buy=Status("firstbarintest");
> Sell=Status("lastbarintest");
> SetTradeDelays(0,0,0,0); PositionSize = -100;
> ApplyStop(0,0,0,0);
> ApplyStop(1,0,0,0);
> ApplyStop(2,0,0,0);
> Plot( Equity( 0, -2 ), "Buy&Hold", -9 );
> }
>
>
>
> // Show realized and unrealized gain or loss relative to last buy
or short
> trade.
>
>
> if (ParamToggle("Show net profit", "no|yes", 0))
> {
>
> // Last trade event: buy, sell, short or cover.
> //lastTrade = buyExRem + shortExRem + sellExRem + coverExRem;
> closeTrade = sellExRem + coverExRem;
>
>
> // Calculate equity change since initial equity, i.e. the
unrealized net
> profit.
> // Equity is what we might have at the beginning of each bar,
if we were to
> sell all assets.
> // Avoid calling Equity(1) which has side effects on trades.
> netProfit = Nz( Equity(0, 0) - GetOption("InitialEquity"));
>
> // Calculate realized net profit at beginning of last close
trade event.
> // Since trade happens at the beginning of the bar after the
signal (i.e. a
> delay of 1),
> // then we check whether there was a close trade in the
previous bar.
> realP = Nz( ValueWhen( Ref(closeTrade, -1), netProfit) );
>
> //realP = Ref(realizedNetProfit, 0);
>
>
> // Move the unrealized profit back to when it was generated
by looking
> forward 1 bar.
> unrealP = Ref(netProfit, 1); // try 0 to see if you like it
better.
>
> PlotOHLC( realP , realP , unrealP , unrealP , "net profit",
> IIf (unrealP > realP , gainColor,
> IIf (unrealP < realP , lossColor,
> // no change, so height is zero. Use realized net
profit color instead.
> IIf (realP > 0, netPositiveColor,
netNegativeColor))),
> styleCandle | styleOwnScale ); // Use
styleLeftAxisScale if also showing
> realP above.
>
>
> }
>
> //========================================
> // Show buy, sell, short, or cover signals
>
> if (ParamToggle("Show trade signals", "no|yes", 0))
> {
>
>
> Plot(IIf(buyExRem OR sellExRem, 95, 3), "",
> IIf(rawBuy, buyColor, IIf(rawSell, sellColor,
backgroundColor)),
> styleHistogram | styleThick | styleOwnScale |
styleNoLabel, 0, 100);
> Plot(IIf(shortExRem OR coverExRem, -95, -3), "",
> IIf(rawShort, shortColor, IIf(rawCover, coverColor,
backgroundColor)),
> styleHistogram | styleThick | styleOwnScale |
styleNoLabel, -100, 0);
>
> }
>
> //========================================
> // Add up and down arrows. The vertical position will be your first
plot, if
> any.
> // Too noisy with many trades.
>
> if (ParamToggle("Show trade arrows", "no|yes", 0))
> {
> PlotShapes(shapeUpArrow * buyExRem, buyArrowColor);
> PlotShapes(shapeDownArrow * sellExRem, sellArrowColor);
> // Bump the short and cover arrows out one if there is a sell
or buy arrow
> PlotShapes(shapeHollowDownArrow * shortExRem,
shortArrowColor, 0,
> IIf(sellExRem, -26, -15));
> PlotShapes(shapeHollowUpArrow * coverExRem, coverArrowColor,
0,
> IIf(buyExRem, -26, -15));
> }
>
>
>
> /*
> sinceBuy = BarsSince (buyExRem);
> sinceSell = BarsSince (sellExRem);
> sinceShort = BarsSince (shortExRem);
> sinceCover = BarsSince (coverExRem);
>
> inLong = ref(sinceSell, -1) > sinceBuy;
> inShort = ref(sinceCover, -1) > sinceShort;
>
> lastBuyPrice = ref ( BuyPrice, - sinceBuy);
> lastShortPrice = ref ( ShortPrice, - sinceShort);
> percentBuy = inLong * 100 * (SellPrice - lastBuyPrice) /
lastBuyPrice;
> percentShort = inShort * 100 * (CoverPrice - lastShortPrice) /
lastShortPrice;
> */
>
> } // end if indicator
>
> _SECTION_END();
>
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/amibroker/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:amibroker-digest@xxxxxxxxxxxxxxx
mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|