Hi,
You must write custom backtester code to do what you are asking. Your
final ~~~Equity is simply an array of the bar by bar values of the
backtester's Equity property (verify that Plots overlap). As such, you
can build up an equivalent equity array as you iterate through the bars
in a custom backtest, calculate a MA using that array, and reduce your
postion size accordingly.
I have not done any extensive testing on the following code. But, I
believe that it will give you what you are asking for. The logic is
simply to refer to the equity of the previous bar compared to the MA of
equity for that same bar, and reduce position size of the current bar
when the relation is not favorable.
Double check the trades to make sure that the timing is right.
Mike
optimistic = Param("Optimistic", 20, 1, 100, 1);
pessimistic = Param("Pessimistic", 10, 1, 100, 1);
period = Param("Period", 20, 1, 50, 1);
SetTradeDelays(0, 0, 0, 0);
PositionSize = -1 * optimistic;
weekDay = DayOfWeek();
Buy = weekDay == 1;
BuyPrice = Open;
Sell = weekDay == 5;
SellPrice = Close;
SetCustomBacktestProc("");
if (Status("action") == actionPortfolio) {
bo = GetBacktesterObject();
bo.PreProcess();
eq[0] = 0;
for (bar = 0; bar < BarCount; bar++) {
reduceSize = false;
if (bar > period) {
eqMA = MA(eq, period);
if (eq[bar - 1] <= eqMA[bar - 1]) {
reduceSize = true;
}
}
if (reduceSize) {
for (sig = bo.GetFirstSignal(bar); sig; sig =
bo.getNextSignal(bar)) {
sig.PosSize = -1 * pessimistic;
}
}
bo.ProcessTradeSignals(bar);
eq[bar] = bo.Equity;
}
bo.PostProcess();
AddToComposite(eq, "~~~EQ", "X", atcFlagDefaults |
atcFlagEnableInPortfolio);
}
eq = Foreign("~~~EQ", "C");
Plot(eq, "EQ", colorGreen, styleLine);
Plot(Foreign("~~~Equity", "C"), "Equity", colorBlue, styleLine);
Plot(MA(eq, period), "MA20-EQ", colorRed, styleLine);
--- In [email protected], Wolfgang Hader <fa...@...> wrote:
>
> Hi all,
>
> how can I code some like this in Amibroker
> (I know the following code is wrong, but how can I do it correctly in
> Amiboker)?
>
> iif(equity() > ma(equity(),20), positionsize=-20, positionsize=-10);
>
> Thank you,
> BR
>