Exit after n periods
Author: topcat77
Creation Date: 1/21/2009 8:14 PM
profile picture

topcat77

#1
Hello all and (since this is my first post this year) a prosperous 2009

I'm trying to install an additional exit condition - in a modified form of RSI Agita- that will exit after n periods if the Rsi-exit has not trigerred

So,to command an exit say 14 periods after the Signal Bar, I first set up the Signal Bar using:
QUOTE:
int signalBar=0


and then installed the additional exit condition . the compiler tells me " operator || cannot be applied to operands of type 'bool' and 'int'

CODE:
Please log in to see this code.



How do I do this correctly ?
Thanks




profile picture

jsmith2007z

#2
If you change the assignment statement giving the error, bar-signalBar=14, to an equality test statement of the form bar-signalBar==14, which is what you probably intended, it compiles.
profile picture

jsmith2007z

#3
By the way, I (not very experienced in C# and even less so in WL5 coding) do not understand the syntax of the following statement in the above code:
CODE:
Please log in to see this code.


Can someone explain it? The baffling parts are
?
:
0d
profile picture

Eugene

#4
jsmith2007z

This is the so called ternary operator "?": you may take a look at MSDN for details.

In short, it's an elegant C# shortcut for saying "if IsLastPositionActive is true, then lastEntryPrice = LastPosition.EntryPrice otherwise make it zero".

0d means zero where the suffix "d" asserts that an integer value should be treated as type double. Again, more details here at MSDN.
profile picture

Eugene

#5
topcat77

CODE:
Please log in to see this code.

jsmith2007z hit the nail on the head here. For the compiler, it looks as if you were doing some math in a boolean condition.

Therefore you need to enclose the snippet in braces: || (bar-signalBar=14) )
profile picture

Cone

#6
Actually it should be: bar-signalBar == 14

= is for assignment
== is for comparison

(Of course the parentheses don't hurt)
profile picture

Eugene

#7
QUOTE:
== is for comparison

Good catch, thanks. Am I getting old?
profile picture

topcat77

#8

jsmith, Eugene, Cone thank you - this was many lessons rolled into one
now I can go back into my corner and trade
profile picture

topcat77

#9

If only life were simpler:

CODE:
Please log in to see this code.


The script (see first post)is not at impressed with the new snippet :
(bar-signalBar)==14
The (additional) exit does nothing (although it compiles ok)
Moreover, if it is made the only exit condition it also does nothing; that is, all positions remain open.

To be clear I am attempting to exit each position at the 14th bar afer the Signal bar if it has not already exited through the Rsi condition. (The script allows multiple positions, if this is the complication)
profile picture

Eugene

#10
QUOTE:
(bar-signalBar)==14
The (additional) exit does nothing (although it compiles ok)

That's right. The script works exactly as programmed: signalBar is initialized with 0 and is never changed or assigned later on. To make it work, just assign the entry bar value if entry was successful.
profile picture

topcat77

#11
QUOTE:
To make it work, just assign the entry bar value if entry was successful


Eugene, please could you give me an example. Thanks
profile picture

Eugene

#12
Well, in multi-position script the single variable is less helpful. Let's instead use the Position.Tag property to hold the signal bar, then loop through only active positions and compare current bar with the one we saved at entry. Note that we could as well use the Position.EntryBar property instead of .Tag for that.

CODE:
Please log in to see this code.
profile picture

topcat77

#13
Eugene

This is brilliant, thx
. . . . there's a lot to digest in here, it will tug me
up my learning curve
profile picture

emskiphoto

#14
Hello,

Please help enable the "Exit after n periods" in the code below. Different than the example above, this strategy uses an "or" exit signal, either selling because the Sell at profit target/loss criteria was met OR after holding one of multiple positions for X bars.

The overall strategy here is to eliminate trades that hold for too many bars (and show up as winners in results because of patience holding a stock in a long uptrend, not necessarily because of ability to pick a great entry point).

CODE:
Please log in to see this code.
profile picture

Eugene

#15
Matt,

Here you go.

P.S.Note that the strategy will require more "seed data" to stabilize than before since it uses EMA so make sure you load enough bars:
CODE:
Please log in to see this code.


CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).