PureBytes Links
Trading Reference Links
|
> is there anyway some ressources you can point to me please
> im searching a way to activate in real time trading a system
> only when the equity is above (for example ) his moving average
> x period and let him run whith no real trade in the opposite
It's extremely messy to do in TS, because you have to simulate system
execution (so you can determine what the equity WOULD have been if
you took every trade) in addition to your actual system code.
Also, in my opinion, it doesn't work. Here's something I posted to
another list several years ago.
I'll post the ELA separately so this message doesn't get too large.
Gary
From: "Gary Fritz" <fritz@xxxxxxxx>
To: Code List <code-list@xxxxxxxxxxxxx>
Date sent: Fri, 10 Dec 1999 16:29:26 -0700
Subject: Re: CL_Money management & system trading
Bob4367123@xxxxxxx wrote:
> It seems to me, in my limited experience backtesting systems, that
> all systems work, some of the time. So the question is, when do
> you trade the system?
Only when it's going to make money. :-)
> why can't you trade the system only when the equity curve is going
> up, and not trade when the system is in drawdown? My limited
> research into this looks promising.
This strategy gets recommended in a number of places. I recently saw
it in Mark Jurik's book and wished I could try it out in TS. But it
would be really nasty to do this, since you'd have to simulate all
the system trades to track the "phantom" equity, and use that PE and
the MA of the PE to determine when your system could really trade.
Then it occurred to me that it would be *easy* to write an indicator
to do this. It doesn't trade the system for you, but it gives you an
idea for how well the strategy works. I'd been meaning to post it,
so this is a good excuse. :-)
I took my "Equity Curve" indicator and modified it to only "count" a
trade if the system equity was above the equity MA. The green bars &
white line show the unmodified equity as reported by the system, the
red line is the XMA of the equity, and the yellow line is the
modified equity. See the attached GIF for an example.
The indicator takes several arguments: the same "period" input that
the regular Equity Curve indicator takes, the length of the XMA, and
two inputs to tell what to do when the equity is above & below the
MA. With the defaults (0.0 and 1.0) it has the effect of trading
only when the equity is above the MA. If you set BelowMA to 0.5 it's
as though you cut your position size in half when you're below the
MA. If BelowMA is -1 it calculates a bad approximation of what would
happen if you took the *opposite* of your system's position when the
equity is below the MA. (It just negates the result of the trade.
Obviously you would get different results if you went long instead of
short, etc.)
Note: the indicator only takes the MA of the equity at the end of
each Period you specify. If you want it to compute the MA on a trade-
by-trade basis, specify "T" as your Period.
Results: I found that it did almost nothing to really good systems
(systems that have a steadily increasing equity), as you would
expect. But few systems have a steadily-increasing equity,
especially on a trade-by-trade basis, and most systems I looked at
got HURT much more than helped by this strategy.
In order for the equity to go below the MA, you need a series of
losses. To dig out from the low-equity state, you need some wins.
But many of the digging-out wins are discarded, since the equity is
still below the MA. So you don't get all those wins. If your system
often alternates between wins and losses, you get murdered -- the
system ignores the wins that get the equity above the MA, and takes
the losses that drive it below the MA again.
The only time I saw a real benefit from the strategy was in systems
that had horrific extended drawdowns. And even then it was often
only a short-term help, since once the system finally turned around
and started winning again, the basic strategy got ahead of the
modified one.
I suspect that trading with a strategy like this would result in a
higher Sharpe ratio. It might even increase the win% -- my indicator
doesn't compute that. But it knocks hell out of your profits, and it
only seems to help really bad systems. I wouldn't want to trade any
system that benefits from this.
In fact it works so badly that I don't understand why so many books
recommend it. Does anyone see a flaw in my code?
Gary
{
Equity Curve MA MM
Version 1.01 9/6/2000
This indicator plots bars of closed equity on a periodic basis
on the last 200 bars of a chart. If your system test contains more
than 200 trades or periods, it only plots the last 200.
The indicator then computes an Exponential Moving Average of the system
equity, and determines the resulting equity if you change your
position size when the system equity falls below the MA. When the
system equity is above the MA, trade AboveMA * your normal position,
and BelowMA * your normal position when the MA is below the equity.
Unmodified system equity plots with green bars. Equity MA plots
as a red line, and modified system equity (taking the MA into
account) plots as a yellow line.
The number of periods plotted is determined by the variable "Size,"
which may be changed. If you increase it, set the size of the array
"EQ" and the "MaxBarsBack" value larger than or equal to the value
of "Size".
Inputs:
Period: Sampling period
("T"=per trade, "Y"=yearly, "Q"=quarterly, "M"=monthly,
"W"=weekly, "D"=daily)
MAlen: Length of simple moving average
BelowMA: Position size when equity is below the MA
AboveMA: Position size when equity is above the MA
(1.0 = normal position size, 0.0 = no position)
The green histogram with the white line on top is the equity of
the original system. The red line is the EMA of the equity curve.
The yellow line is the hypothetical equity of the modified system.
Gary Fritz
}
Inputs: Period("W"), MAlen(10), BelowMA(0.0), AboveMA(1.0);
Vars: Equity(0), EndEq(0), LastEq(0);
Vars: Mo(0), Countr(0), J(0), K(0), CE(0), Size(200), Last(0);
Vars: Per(0), P1(0), P2(0), P3(0), P4(0);
Vars: Factor(0), MA(0), LastModEq(0), LastMA(0), TakeNextTrade(True);
Array: EQ[200](0), EQMA[200](0), ModifiedEQ[200](0);
{ Determine the desired period, print the proper header, and
set Per to the appropriate value. Why use Per? Because if I
compare Period to "T", "Y", etc on every bar, the indicator runs
TWICE as slowly! EL string operators are sloooowwww... }
if (BarNumber = 1) then begin
if Period = "T" then begin print("Per-trade profit:"); Per = 0; end;
if Period = "Y" then begin print("Yearly profit:"); Per = 1; end;
if Period = "Q" then begin print("Quarterly profit:"); Per = 2; end;
if Period = "M" then begin print("Monthly profit:"); Per = 3; end;
if Period = "W" then begin print("Weekly profit:"); Per = 4; end;
if Period = "D" then begin print("Daily profit:"); Per = 5; end;
Factor = 2/(MAlen+1);
end;
Mo = Month(Date);
Equity = I_ClosedEquity;
{ If our chosen period has expired, record the end-of-period equity }
if ((Per = 0) and (Equity <> Equity[1]))
or ((Per = 1) and (Year(Date) <> Year(Date[1])))
or ((Per = 2) and (Mo <> Mo[1]) and (Mo=1 or Mo=4 or Mo=7 or Mo=10))
or ((Per = 3) and (Mo <> Mo[1]))
or ((Per = 4) and (DayOfWeek(Date) < DayOfWeek(Date[1])))
or ((Per = 5) and (Date <> Date[1]))
then begin
if Per = 0 then EndEq = Equity
else EndEq = Equity[1];
if (LastBarOnChart = False) then
print(Date[1]:6:0,",",EndEq:7:2,",",EndEq-LastEq:7:2);
{ Compute XMA of recent equity values }
if (MA = 0) then MA = EndEq
else MA = Factor*EndEq + (1-Factor)*MA;
EQ[Countr] = EndEq;
EQMA[Countr] = MA;
if TakeNextTrade
then ModifiedEQ[Countr] = LastModEq + AboveMA*(EndEq - LastEq)
else ModifiedEQ[Countr] = LastModEq + BelowMA*(EndEq - LastEq);
TakeNextTrade = (EndEq >= MA);
LastEq = EndEq;
LastModEq = ModifiedEQ[Countr];
Countr = Mod(Countr + 1, Size); { Move pointer in buffer }
end;
{ On last bar, plot the last Size periods of equity }
if LastBarOnChart then
for J = 0 to Size - 1 begin { Loop to plot bars }
K = Mod(Countr + J, Size); { Calc pointer into buffer }
Plot1[Size - 1 - J](EQ[K],"ClosedEq");
Plot2[Size - 1 - J](EQMA[K],"EqMA");
Plot3[Size - 1 - J](ModifiedEQ[K],"ModEQ");
Plot4[Size - 1 - J](EQ[K], ""); { Plot white line at top of histogram }
end;
|