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

Re: Coding MaxLoss and ProfitTarget Stops



PureBytes Links

Trading Reference Links

Herman

I seem to have deleted most of our earlier discussion on this topic but I don't think I posted the
code in which I eliminated the PREV functions. I know there is some stuff in this that will not
interest you. However you may want to take a look at the EntryAmt variable. It looks cumbersome, and
it is, but what makes it look messy is the nested ValueWhen and BarsSince functions. What amazes me
is that the code actually works.

Note that this code is intended for use with fixed equity but will work with compounding equity as
long as commissions are % based. Including compounding equity in the stop calculation using (when
using a fixed commission and without reverting to PREV) is a much more difficult task.

Roy

> Has anybody succeeded handcoding the Maximum Loss and Profit Target Stops
> such that System Tester Stop results are identical to using the custom
> coded stops?

  {Multiple Stop Test}
  {** Set options and defaults as required **}
EntryCost:=Input("Entry cost $",0,100,25);
ExitCost:= Input(" Exit cost $",0,100,25);
Profit:=Input("Profit goal %",1,50,15)/100;
Loss:=Input("  Stop loss %"  ,1,50,10)/100;
Equity:=Input("Trade equity $",500,100000,10000);
EntryPrice:=Input("Entry price option, 1=O 2=C 3=Stop",1,3,3);
ExitOpt:=3; {Exit option,  1=O 2=C 3=Stop}

n:=Fml("Your Long Entry"); {binary for O or C, stop value for entry stop}
x:=Fml("Your Long Exit"); {binary for O or C, stop value for exit stop}
i:=Cum(n<>-1 AND x<>-1)=1; {initialisation}
EntryPrice:=If(EntryPrice=1,O,If(EntryPrice=2,C,n));
ExitPrice:=If(ExitOpt=1,O,If(ExitOpt=2,C,H)); {needs refinement for stop exit}
pFactor:=((Equity*(1+Profit)+ExitCost)/ (Equity-EntryCost)); {profit factor}
lFactor:=((Equity*(1-Loss)+ExitCost)/ (Equity-EntryCost)); {loss factor}

EntryAmt:=If(BarsSince(i OR n>0)>=BarsSince(i OR x>0 OR
ExitPrice>pFactor*(ValueWhen(1,i OR (n>0 AND Alert(If(BarsSince(i
OR n>0)>=BarsSince(i OR x>0),0,1)=0,2)), EntryPrice)) OR If(ExitOpt<3,
ExitPrice,L)<=lFactor*(ValueWhen(1,i OR (n>0 AND Alert(If(BarsSince(i
OR n>0)>=BarsSince(i OR x>0),0,1)=0,2)), EntryPrice))),0,
ValueWhen(1,i OR (n>0 AND Alert(If(BarsSince(i OR n>0)>=
BarsSince(i OR x>0),0,1)=0,2)),EntryPrice));
  {EntryAmt is simply a trade flag that stores the entry price either until a
  normal exit is triggered or or the profit stop or stop loss prices are hit}

y:=If(EntryAmt=0 AND Alert(EntryAmt>0,2) AND ExitPrice>=
ValueWhen(1,i OR EntryAmt>0,EntryAmt)*pFactor,If(ExitOpt=1,O,
If(ExitOpt=2,C,ValueWhen(1,i OR EntryAmt>0,EntryAmt)*pFactor)),0);
  {Entry amount multiplied by profit factor}

z:=If(EntryAmt=0 AND Alert(EntryAmt>0,2) AND If(ExitOpt<3,
ExitPrice,L)<ValueWhen(1,i OR EntryAmt>0,EntryAmt)*lFactor,
If(ExitOpt=1,O,If(ExitOpt=2,C,ValueWhen(1,i OR EntryAmt>0,
EntryAmt)*lFactor)),0);
  {Entry amount multiplied by loss factor}

pTarget:=If(Y=0,0,If(Y>0.1 AND Y<=0.5,
If(Int(Y*200)=Y*200,Y, Int((Y)*200)/200),
If(Y<=0.1,If(Int(Y*1000)=Y*1000,Y,
Int((Y)*1000)/1000),If(Int(Y*100)=Y*100,Y, Int((Y)*100)/100))));
lTarget:=If(Z=0,0,If(Z>0.1 AND Z<=0.5,
If(Int(Z*200)=Z*200,Z, Int((Z)*200)/200),
If(Z<=0.1,If(Int(Z*1000)=Z*1000,Z,
Int((Z)*1000)/1000),If(Int(Z*100)=Z*100,Z, Int((Z)*100)/100))));
  {pTarget and lTarget adjusts the stop values down to next ASX tradable value}

EntryAmt; {lTarget; pTarget}