Add to Position
Author: maninjapan
Creation Date: 4/16/2010 9:35 AM
profile picture

maninjapan

#1
I'm trying to create a very simple system that adds a position at a second level (both long and short side) and uses the same exit. I know I need to use ActivePositions.Count somehow, but not sure exactly how. Ive had a look around for some other similiar threads but the ones I found kinda went over my head. Can anyone point me in the direction of a simple example of how this should work? Below is what I ahve at the moment.
CODE:
Please log in to see this code.
profile picture

Cone

#2
If you want conditional logic for a specific number of Positions, use if (ActivePositions.Count == n), where n is 0, 1, or 2 in your code above. Also, checking the Position.Count after a trading signal is a "no no" (peeking). Use if.. else between, for example, where you're checking less than 1 or 2 ActivePositions.

Aside:
The .Value method (as in ATR.Value) is an expensive operation because it recomputes the "Value" each time the statement is hit. There are relatively few reasons to use the .Value method, and your script isn't one of them. Instead, create the series (outside the loop), and just use the DataSeries variable within your loop.

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

maninjapan

#3
Thanks for the tip on ATR, will fix that up.

As for the second part, still not getting it.. (its just me, not your explanation). Ar ethere any hard examples of this.. ( itred adding else in between)

QUOTE:

for(int bar = GetTradingLoopStartBar(150); bar < Bars.Count; bar++)
{
if ( ActivePositions.Count > 0 )
{
CoverAtStop(bar + 1, Position.AllPositions, Close[bar] + ATR.Value(bar, Bars, 100) * 30.0, "Group4");
SellAtStop(bar + 1,Position.AllPositions, Close[bar] - ATR.Value(bar, Bars, 100) * 30.0, "Group2");
SellAtLimit(bar + 1,Position.AllPositions, ma[bar], "Group1");
CoverAtLimit(bar + 1,Position.AllPositions, ma[bar] , "Group3");
}
else
{
if ( ActivePositions.Count < 1 )
{
ShortAtLimit(bar + 1, ma[bar] + ATR.Value(bar, Bars,100) *2.0, "Short Entry 1");
BuyAtLimit(bar + 1, ma[bar] - ATR.Value(bar, Bars, 100) * 2.0, "Long Entry 1");
}

else
{
if( ActivePositions.Count < 2 )
{
ShortAtLimit(bar + 1, ma[bar] + ATR.Value(bar, Bars, 100) *4.0, "Short Entry 2");
BuyAtLimit(bar + 1, ma[bar] - ATR.Value(bar, Bars, 100) * 4.0, "Long Entry 2");
}
}


}

profile picture

maninjapan

#4
Just tried to fix up the ATR section and add some parameters to optimize, but says I cant use int with a Series. Whats the correct method to reference these parameters?

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

dan_rozenberg

#5
I think "ATR" is a reserved variable name (henve, ATR.Series, Atr.Value). You are trying to cast it as an int and then assigning a value to it...

CODE:
Please log in to see this code.


Try changing the name of that variable.
profile picture

maninjapan

#6
Yes, its official, Im THAT stupid....

Thanks dan, fixed it right up.
profile picture

Eugene

#7
Dan is right. "ATR" is not a reserved word in C# though - it's the name of the already existing ATR data series, that's why you can't use the name of a class as a variable name.
profile picture

maninjapan

#8
Thanks, learned something today!! now how bout this pesky multiple positions? any examples for me to follow off?
profile picture

Eugene

#9
Look up the many examples in the WealthScript Guide, QuickRef, downloadable Strategies and in the Wiki.
profile picture

maninjapan

#10
Thanks for that extremely informative tip their Eugene. Unfortunately my search of the wiki only turned up one example which I didnt quite get. I used the search function for the following terms.

pyramiding
Multiple Entry
ActivePositions


http://www2.wealth-lab.com/WL5Wiki/(X(1)S(sg20a5v1ysux2lyblzadjdid))/kbPyramid.ashx

If anyone is able to point me in the direction of some other specific examples it would be much appreciated.

Cone Said I needed to use an if.... else statement to seperate the counting of positions. I attempted the following, but obviously still incorrect. Any examples of wehre this works correctly?

CODE:
Please log in to see this code.


Thank you

Note: I also referred to the Quickref and I can see how it works there, just not sure of where the if else statement fits in.
profile picture

Eugene

#11
How about Programming Trading Strategies > Multi-Position Strategies in the WealthScript Guide?
profile picture

maninjapan

#12
Thanks for that one too Eugene, looked at it but the example template only shows code for the exit, not the multiple entries..... Looking through all the strategies now though for examples.
profile picture

maninjapan

#13
sorry guys, Ive been through the strategies, the programming guide, quick ref and wiki but cant figure out why this second position wont enter......
profile picture

Eugene

#14
It does enter.
CODE:
Please log in to see this code.

Note that you should not start the trading loop on bar 100 due to the use of an unstable indicator (WealthScript Guide for more, there's a chapter.)
profile picture

maninjapan

#15
Thanks Euegene, works fine now. Looks like I had the else part setup incorrectly. Im trying to add some stop losses but the programming guide warns against using Lastposition with multiple positions. How can I refer tot he average entry price? Is it possible to add the following even if Im using multiple positions?
CODE:
Please log in to see this code.
profile picture

Eugene

#16
QUOTE:
How can I refer tot he average entry price?

See my reply dated 2/2/2009 3:06 PM in this thread for a working average entry price code:

Stop for RSI Agita
profile picture

maninjapan

#17
Thanks Eugene, Ive added it below the entries here. It compiles ok, but when I run it I recieve a runtime error index out of bounds,... needs to be smaller than the collection size?? (translated)
the script runs as expected without this stoploss added.

CODE:
Please log in to see this code.



profile picture

Eugene

#18
QUOTE:
I recieve a runtime error index out of bounds,... needs to be smaller than the collection size?? (translated)

Then your strategy has to be debugged. Hope this helps:

Errors | Strategy > Index was out of range
profile picture

maninjapan

#19
yep, found that, thanks. couldnt find any reference to the 'needs to be smaller than the collection size' part of the error though.....

Definately due to my complete lack of programming knowledge here, but the code runs until the first coveratstop order using the average price. It executes 3 trades fine until then......
will look at debugging.....
profile picture

Eugene

#20
btw, try moving the exits out of the ActivePositions loop - you're exiting all open positions at once anyway.
profile picture

maninjapan

#21
just a quick question. How come when I do a search for debug in the wiki quick search I get no results? surely theres articles or info in wiki on debugging right? (If theres an article or some info on how to best use wiki, that would be much appreciated)
profile picture

maninjapan

#22
Actually Eugene, that seems to have worked.

Thanks!!
profile picture

maninjapan

#23
There seems to be something wrong with the way its calculating the average positions.
It prints the correct price for the first position, but on adding a 2nd position the average price is out. (possibly due to an error on my part).
First entry @ 234.875
Second Entry @ 234.96875
Average Price @ 354.9609375

The second number is the Stop loss Price (this is a long position)

QUOTE:

236.34375 235.428020690872
236.34375 235.432490483963
236.34375 235.432228079123
354.9609375 354.038218298332
354.9609375 354.035726740349
354.9609375 354.035603847945
354.9609375 354.029232184466
354.9609375 354.033861737621
354.9609375 354.034538745245
354.9609375 354.035208982793
354.9609375 354.035872517965
354.9609375 354.032623167785
0 0
0 0


Below is the code as I have it.

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

Eugene

#24
Most likely, it's because you are not resetting the AvgEntryPrice after exiting a position. Refer to the AvgEntryPrice example for a how-to.
profile picture

maninjapan

#25
Eugene, I dont believe that is the case. When one positions enters and exits it works fine, the problem happens when the second position enters. As you can see from the following print, price goes back to zero (and I crosschecked the initial entry price, and this matches)

QUOTE:

0 0
0 0
0 0
0 0
0 0
0 0
0 0
234.875 233.858000129721
234.875 233.859576378424
234.875 233.86504311464
234.875 233.868892683494
234.875 233.873485006659
234.875 233.877250156592
234.875 233.882540155026
234.875 233.891683503476
234.875 233.897610418441
234.875 233.902696814257
234.875 233.912419846114
234.875 233.914233147653
234.875 233.913684566176
234.875 233.918610220515
352.3828125 351.42114286831
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
234.53125 233.580241044799
234.53125 233.584282384351
234.53125 233.587502060507
234.53125 233.585220789902
234.53125 233.576712332003
234.53125 233.577663958683
234.53125 233.577824819096
234.53125 233.574859070905
234.53125 233.576610480196
234.53125 233.576781875394
234.53125 233.567576556641
234.53125 233.571744541074
234.53125 233.569620845663
234.53125 233.565955887207
0 0
0 0
0 0
0 0
profile picture

maninjapan

#26
well, Im still not having any luck with this average price. It resets on exits fine, just seems to be an issue when the second position is added? Is is the way Ive added the code? It was pretty much a copy and paste from the example provided so I think it should be right....
profile picture

maninjapan

#27
anyone got any ideas on this average price code?
profile picture

maninjapan

#28
Ok, HAve tried adding the reset for AvgEntryPrice, but not having any luck. Are there any glaring errors in my code here?
I have referred to the example as you suggested multiple times...

CODE:
Please log in to see this code.

profile picture

Eugene

#29
I think this should work:
CODE:
Please log in to see this code.

Also don't forget to set the correct exit order. See the WealthScript Programming Guide, Programming Trading Strategies > Peeking > Order of Trading Signals > Example.
profile picture

Eugene

#30
On an unrelated note. This will not work as intended:
CODE:
Please log in to see this code.

Remove "!=null": you're not checking whether a Position has been created or not (i.e. null or not null), you're evaluating a boolean condition here.
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).