[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Trading based on equity curve


  • To: omega-list@xxxxxxxxxx
  • Subject: Trading based on equity curve
  • From: "Gary Fritz" <fritz@xxxxxxxx>
  • Date: Wed, 28 Nov 2001 18:30:34 -0800
  • In-reply-to: <00f001c17873$a0cac520$a0ecb440@xxxxxxxxxxxxxxxxx>

PureBytes Links

Trading Reference Links

An unnamed (since I haven't taken the time to get his permission to 
post this small quote from his mail :-) correspondent asked me:

> Do you happen to have TS code that can filter a system's trades by
> referencing the equity curve (i.e. if the equity curve is below
> its own 10 period MA then don't take trades)?  I'm obviously too
> dumb too code something like this as I have tried without success. 

Don't feel bad.  It's VERY nasty.  You have to simulate trading, 
complete with simulated orders, simulated positions, etc, so you can 
compute the equity curve with all trades.  Then, using that, you 
determine whether you actually issue regular TS buy/sell orders.

That was too much work for me to bother with, just to look at the 
concept to see how it worked.  So I cheated.  :-)

I wrote an indicator that plots the system's actual (unmodified) 
equity curve, AND it plots what the equity curve WOULD have been if 
you reduced size when the eq curve fell below a moving average.

Source is appended.  I'll send the ELA in a separate message -- that 
way you have the styles/colors all set up right.

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)

   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;