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

Re: Indicator System problem



PureBytes Links

Trading Reference Links

Sean

Some more adjustments. I have moved the Ref() function from the Position variable to each of the
entry/exit variables. This can be done directly which will also delay Init by one bar, or by listing
the variable again and applying the Ref() which will not delay Init. In this case when I apply Ref()
directly to the entry/exits the timing of some trades seems to be differ from your original code,
but not others. I need to work through that issue still.

Note that Ref() is still applied to Position, but with the delay set to zero.

To get around the Equity (late display) problem I created four new variables that signal entry and
close for both long and short trades. By  substituting these for the Cross() functions and adding in
'OR Init' we now have an Equity variable that displays from the first bar that Init is valid.

One problem with my adding in new variables is that one of your original statistic variables had to
be commented out because the 20 variable limit was reached. These stat variables can be built into
an exploration if you are planning to do multiple stock testing. The other approach is to insert the
new variable code directly into the Equity variable. However this makes for quite verbose code, and
it can also mean that the character limit for the total indicator is reached prematurely.

For your interest here are working variables from a Fixed Equity indicator that I have that may help
you as move along in your indicator development. These assume a fixed position size (e.g $10,000 for
every trade) and long only. Short requires a slightly different calculation I seem to recall (I
don't trade short), and componding equity requires the use of a Prev() or other work-around.

Cutting and pasting code from this post directly into MS may throw up one or two errors, asit it did
when I copied your code, but removing spurious "new-line" characters from where the errors are
repoted will usually solve that problem.

Gain:=If(CloseLong,(PositionSize-Costs)*(ExitPrice/EntryPrice)-PositionSize-Costs,0);
Equity:=Cum(Gain)+ If(LongPosition,(PositionSize-Costs)*(ExitPrice/EntryPrice),PositionSize);

I hope this helps some.

Roy

  {ST Equity}
LongEntryPoint:=Ref(H,-1); {calc. on day of entry, limit order entry}
ShortEntryPoint:=Ref(L,-1); {calc. on day of entry, limit order entry}
LongExitPoint:=O;
ShortExitPoint:=O;
LongEntry:=Ref(C,-1)<C AND C<L+((H-L)/4) AND
  C<O AND H<Ref(H,+1);
{last part only for system test - following day entry condition}
LongExit:=Alert(LongEntry,4) AND Alert(LongEntry,3)=0;
  {auto exit 3 days later}
ShortEntry:=Ref(C,-1)>C AND C>H-((H-L)/4) AND
  C>O AND L>Ref(L,+1);
  {last part only for system test - following day entry condition}
ShortExit:=Alert(ShortEntry,4) AND Alert(ShortEntry,3)=0;
  {auto exit 3 days later}

LongEntry:=Ref(LongEntry,-1);
LongExit:=Ref(LongExit,-1);
ShortEntry:=Ref(ShortEntry,-1);
ShortExit:=Ref(ShortExit,-1);

Init:=Cum(LongEntry<>2 AND LongExit<>2 AND
  ShortEntry<>2 AND ShortExit<>2)=1;
Position:=Ref(If(BarsSince(LongEntry OR Init)>=
  BarsSince(LongExit OR Init),0,1) +
  If(BarsSince(ShortEntry OR Init)>=
  BarsSince(ShortExit OR Init),0,-1),-0);

EnterLong:=Alert(Position<=0,2) AND Position=1;
CloseLong:=Alert(Position=1,2) AND Position<>1;
EnterShort:=Alert(Position>=0,2) AND Position=-1;
CloseShort:=Alert(Position=-1,2) AND Position<>-1;

Equity:=Cum(If(CloseLong,ValueWhen(1,CloseLong OR Init, LongExitPoint)-
ValueWhen(1,EnterLong OR Init,LongEntryPoint), If(CloseShort,ValueWhen(1,EnterShort
OR Init, ShortEntryPoint) - ValueWhen(1,CloseShort OR Init ,ShortExitPoint),0)));

TotalLongWin:=Cum(If(Cross(0.5,Position),ValueWhen(1,Cross(Position,0.5),LongEntryPoint)<
ValueWhen(1,Cross(0.5,Position),LongExitPoint),0));
TotalLong:=Cum(Cross(Position,0.5));
TotalShort:=Cum(Cross(-0.5,Position));
TotalLongLoss:=TotalLong-TotalLongWin;
TotalShortWin:=Cum(If(Cross(Position,-0.5),ValueWhen(1,Cross(-0.5,Position),
ShortEntryPoint)>ValueWhen(1,Cross(Position,-0.5),ShortExitPoint),0));
{TotalShortLoss:=TotalShort-TotalShortWin;}
Equity;