PureBytes Links
Trading Reference Links
|
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
|