How to "split" long and short positions into individual systems
Author: Lunev
Creation Date: 6/2/2012 4:45 AM
profile picture

Lunev

#1
In simple code below based on price channel the shorts will sometimes intersect with the longs. How to split longs and shorts properly so that they offset but not override each other?


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

Eugene

#2
Let's start by saying that the logic of entering long and short positions in this single-position script (derived from "Channel Breakout VT") is mutually exclusive. In other words, short positions can not intersect with long positions here.

So, are you trying to add a logic for splitting positions and need an example? Should the system remain a single-position, or you'd like to take both (how many positions can be held simultaneously)?
profile picture

Lunev

#3
From this code we see that when a long position is opened we're waiting for it to be closed at 50-period low while getting a signal to open a new short at 20-period low. At this signal our short will inevitably intersect with our long. So I want it to offset part of the long,i.e. sell some lots if short size is lesser than long size or close long positions fully and add some shorts calculated as difference short size minus long size. I don't need a multi-position strategy which opens multiple positions in one direction. The strategy needed is long single-position + short single-position one where opposite positions effectively interact in positions sizes as written above.
profile picture

Cone

#4
QUOTE:
we're waiting for it to be closed at 50-period low while getting a signal to open a new short at 20-period low
Not correct. Like Eugene said, the entry and exit logic are mutually exclusive, i.e., it's either doing the entry OR the exit on a bar-by-bar basis.

To say it another way, when holding a long position, the code will ONLY look to exit that Position. Likewise for a short Position.

The block format of your paste is a bit mangled, so maybe this cleaned-up version will make it more clear to you:

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

Lunev

#5
Sorry for mangled paste. I've modified the code to illustrate what i'm trying to explain. Long and short sizes are different and logic is not mutually exclusive now. In this case opposite positions will overlap and how to split them to be treated separately.


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

Eugene

#6
This is incorrect, because you're taking two positions yet managing the exits as if it was just one. Please take a look at the WealthScript Programming Guide, Programming Trading Strategies > Multi-Position Strategies. "MP Strategy Template" in particular.
profile picture

Lunev

#7
I've slightly modified original Eugene's multisystem code presented below.
Now I've got strange message "No data available" when I try to apply this code while any other code runs smoothly on my data.

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

Eugene

#8
Maxim,

How about splitting your logic into two separate Strategies and combining them in a new Combination Strategy? Perhaps this might be the easiest way for you.
profile picture

Lunev

#9
This approach won't work. I've tried Combination Strategy already. It simply divides starting equity among several strategies and allow them to be traded each on its equity share but can't effectively refer to overall summed equity value for any strategy in combination.
profile picture

Eugene

#10
Starting the system too early is the culprit. It requires at least 50 bars of seed data:
CODE:
Please log in to see this code.

Or change "50" to "obj.GetTradingLoopStartBar(50)" (QuickRef the method name for more).
profile picture

Lunev

#11
Thanks, it worked fine. But now when I try to assign position size with Setsharesize I get error CS0103 "doesn't exist in current context". Every other parameter and function added give that error. Is obj. prefix needed before names or this code simply doesn't work this way?

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

Eugene

#12
This is not a regular script. Have you had a chance to notice the "WealthScript obj" passed by reference as parameter? For this very special kind of script to work, you should preface inserted WealthScript methods accordingly. Compare existing *AtStop method calls and you'll quickly find out what SetShareSize(1) is missing here:
CODE:
Please log in to see this code.
profile picture

Lunev

#13
with this obj.Setsharesize(1); we've got other error CS0117 "is not defined by Wealtlab.Wealthscript".
profile picture

Eugene

#14
Right. What on Earth is Setsharesize? ;)

Two things to take a careful look:

1. any C# 101 book, very first pages where case sensitivity is discussed
2. QuickRef for SetShareSize
profile picture

Lunev

#15
Understood Thank you.
Now this method DataSeries H50 = obj.Highest.Series(High,50); is not accepted by this code with CS0116 error
profile picture

Eugene

#16
Because Highest is not a member of WealthScript. Remember, "obj" means "an instance of WealthScript passed by reference" here - whereas Highest is a DataSeries and a member of WealthLab.Indicators. No need to modify these lines.
profile picture

Lunev

#17
Now how to properly pass declared Dataseries into Wealthscript obj? This "if (BBvol[bar] > BBhigh[bar]) Marker= true;" gives errors. Should we call created Series public?


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

Eugene

#18
As to passing anything into "WealthScript obj", you can't, and it doesn't make sense. Pass whatever you need along with "WealthScript obj". Easy, right?

At the risk of repeating, it doesn't appear to me that you should be passing the DataSeries at all. Just keep them in the System1/System2 classes. Why the need??


P.S. As I already suggested above, it will really be beneficial if you take some time to read any entry-level C# book. We're not falsely assuming that everyone's a programmer, but since you are trying to modify this solution, you should be aware of how objects are passed and shared. Really, this is C# 101. So, let me stress my advice: visit either one of these free resources and/or books and study the basics:

FAQ > How do I start with C# ?

I suggest keeping a C# book open while you're playing with code, and look for answers while you browse. Hardly any other self-study technique can be more effective than the combination of the motivating necessity and practical application of a concept.

P.P.S. Please wrap your code in CODE tags before posting. tia.
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).