PureBytes Links
Trading Reference Links
|
Glen,
PREV returns the last value of the statement, not the indicator result. I got
around that problem with the following code when implementing LaBeau's 25x25
System. I used the same type of indicator as you are trying to implement. I
don't have time at the moment to rewrite your code but see if the following
doesn't help.
Gary Randall -- Brunswick, Maine
"25x25 LongEntry"
{Note: 1 price point = $1000}
{Returns long trade entry price. }
{A non-zero number if in a long trade. }
{A negative value if the last day of a trade. }
{Note: Modifications to MetaStock indicators }
{were req'd to simulate TradeStation results }
{ RSI: rounded to two decimal places }
{ ATR: Wilder's smoothing removed }
{Variables to avoid duplicate function calls }
PLLV2 := Ref(LLV(L,2),-1);
PLLV25 := Ref(LLV(L,25),-1);
{ Was yesterday a setup day? }
IsSetUp :=
Cum(1) > 50 AND
Ref(PDI(14),-1) > Ref(MDI(14),-1) AND
Ref(ADX(14),-1) > 20 AND
PREC(Ref(RSI(4),-1)+.005,2) < 50;
{Determine initial entry price condition}
EntryPriceCond := Ref(C,-1) + 0.5625;
{Adjust it to enter on open if open is greater}
EntryPriceCond :=
If(O > EntryPriceCond, O, EntryPriceCond);
{Return entry price, zero if no trade. }
If(PREV <= 0,
{Not in a long trade}
If(IsSetUp AND H >= EntryPriceCond,
{Trade entered today, was it stopped?}
If(L <= PLLV25 OR
L <= EntryPriceCond - 2.5,
-EntryPriceCond, {Yes}
EntryPriceCond {No}
),
{Not in trade and not entered today}
0
),
{Have been in trade for over one day. }
{Was it stopped today? }
{Note: BarsSince() gives days in trade }
If(L <= PREV - 2.5, - PREV,
If(BarsSince(PREV=0) > 24,
{More than 24 days in trade}
If(L <= PLLV2, -PREV, PREV),
{Less than 25 days in trade}
If(L <= PLLV25, -PREV,
If(Ref(C,-1) - PREV >
5*Ref(Mov(ATR(1),45,S),-1),
If(L <= PLLV2, -PREV, PREV),
PREV
)
)
)
)
);
____________________Reply Separator____________________
Subject: Problems coding an exit
Author: metastock@xxxxxxxxxxxxx
Date: 10/12/99 12:30 PM
Could some of you code-jockeys give me a hand with a long exit rule?
The variable "EntryPrice" is assigned the closing price upon entering a
trade (at the close). If the trade is exited, EntryPrice is assigned a
negative value. If there is no position, it is assigned a zero value. I
use this to tell me when a trade has been entered, at what price, and then
to compute certain entry-price-dependent exit conditions. If EntryPrice has
been reassigned a negative value, that is the trigger to exit the position.
If EntryPrice has been reassigned a zero value, that is the signal to watch
for a new long position being entered.
My problem is that the interaction of the PREV functions seems to always
assign EntryPrice a zero value. Below is the code.
{DEFINE ENTRY PRICE, OR 0 IF NO TRADE}
{SEE REDEFINITION BELOW IF TRADE EXITED}
EntryPrice:= If(PREV <= 0,
{TRADE ENTERED TODAY?}
If(LongEntryCondition AND LongSetUp, CLOSE, 0),
PREV);
{DEFINE LONG EXIT RULES IF WE'RE IN A TRADE}
Exit1:= If(EntryPrice > 0 AND ADXValue < 20,
EntryPrice + 2.5*AvgTrueRange, 0);
Exit2:= If(EntryPrice > 0 AND ADXValue >= 20 AND ADXValue <= 30,
EntryPrice + 3.5*AvgTrueRange, 0);
Exit3:= If(EntryPrice > 0 AND ADXValue > 30,
EntryPrice + 0.5*AvgTrueRange, 0);
Exit4:= If(EntryPrice > 0 AND CLOSE > EntryPrice + 1.5*AvgTrueRange,
EntryPrice + LockedInProfit, PREV);
Exit5:= If(EntryPrice > 0 AND LOW <= EntryPrice - MaxLoss,
EntryPrice - MaxLoss, 0);
{REDEFINE ENTRY PRICE IF TRADE EXITED}
EntryPrice:=
If(Exit1>0 AND HIGH>=Exit1, -PREV,
If(Exit2>0 AND HIGH>=Exit2, -PREV,
If(Exit3>0 AND HIGH>=Exit3, -PREV,
If(Exit4>0 AND LOW<=Exit4, -PREV,
If(Exit5>0, -PREV,
PREV)))));
{IF ENTRY PRICE IS NEGATIVE, AN EXIT RULE APPLIES AND TRADE IS EXITED}
EntryPrice < 0
|