Tile DataSet in Volume bins
Author: dansmo
Creation Date: 6/11/2010 3:17 AM
profile picture

dansmo

#1
Hi,

I would like to create bins of average traded volume over a dataset.
I started with the following code, which should save average Value numbers for each symbol to a Dictionary.

CODE:
Please log in to see this code.


Then the values and keys in the dictionary need to be sorted and tiled in x parts, where x shoule be 3 in my case. So I get a list of symbols for high volume, mid volume and low volume.
Later, I want to size the positions accoarding to this list.

However, I seem to run into problems with datasets where the DateTimes of bar numbers is different.
Is there a chance to get help with this one?
profile picture

Eugene

#2
What kind of problems?
profile picture

dansmo

#3
The index was outside the region. It must be non negative and smaller than the list.

I guess this has to do with
CODE:
Please log in to see this code.

Because this will try to call the function with a BarNumber for Symbol 1 that is not available in symbol2.
I guess I must synchronize the dataset. so that each bar in symbol 1 is the same bar and date as in the other symbols.
profile picture

Eugene

#4
This Wiki article lists 3 typical causes of the "Index out of range" error, and your understanding is correct -- the method can be made accessing the data at an invalid bar number (Bars.Count), i.e. the one that doesn't exist for the symbol with a shorter history. Yes, you need to make sure that the symbol's data are synchronized.
profile picture

dansmo

#5
I want to sort the dictionary by values for creating lists of volume bins later on. However, I dont understand the google examples I found on sorting dictionaries. Is there a better way to do this? How can I sort the dictionary?
profile picture

Eugene

#6
Try this: How do you sort a C# dictionary by value? The first example uses .NET 2.0.
profile picture

dansmo

#7
Thank you, I think this shoudl work now:
CODE:
Please log in to see this code.
profile picture

dansmo

#8
Follow up question:
Like I said above, I´d like to size my positions accoarding to the volume bins created above,
so that the mid volume bin gets 100% size, bin1 150% and bin3 only 50%.
I wonder what would be the best method to do this?
I think about storng teh factor in a TAG and have a PosSizer that extends standard PosSizers with TAG modified position size.
Would you suggest another or more simple way?
profile picture

Eugene

#9
Sounds like your approach can work out.

As to the more simple solution, there's the Priority Adjustment PosSizer. If you don't mind the loss of the Max Risk and Fixed Dollar options (i.e. being able to size using percentages of equity), then storing the volume bins in the Priority property and defining only three groups (50, 100, 150) with the appropriate % equity should suffice for a quick & dirty workaround w/o having to code.
profile picture

dansmo

#10
Thank you for your reply Eugene.
At first I thought that this would really be a good & simple solution. However, I need to use priority in my script for determining which positions to take if there is not enough cash available for all trades. So this solution does not work for my intention.
Any other solutions come to mind?
profile picture

Eugene

#11
In this case, the Position.Tag property + PosSizer seems like the way to go.
profile picture

dansmo

#12
Okay, then I´ll do it that way.
One more question though.
I run the script on a whole dataset, but I need only create the bins once. It is important to run this procedure on the symbol that was in the dataset the whole time, so that I dont mess up synchronization.
Do you think it is the best way to have a symbol selected in the beginning and run the bin creation once on that symbol and have the strategy itself executed in another part of the script?

QUOTE:
In this case, the Position.Tag property + PosSizer seems like the way to go.

So, if I need a percent of equity position size and nothing else...I "only" need to change this PosSizer from getting info from .Tag instead of .priority?
profile picture

Eugene

#13
QUOTE:
[QUOTE]In this case, the Position.Tag property + PosSizer seems like the way to go.


So, if I need a percent of equity position size and nothing else...I "only" need to change this PosSizer from getting info from .Tag instead of .priority?[/QUOTE]
Yes, and you'd need to cast the value to double since .Tag holds objects.
CODE:
Please log in to see this code.
profile picture

dansmo

#14
How do I store a Tag for a BuyAtLimit Signal??
profile picture

Eugene

#15
For example:
CODE:
Please log in to see this code.

profile picture

dansmo

#16
Ahhh. Sounds quite logical ;-)
profile picture

Eugene

#17
Update: Fixed a typo in PosSizer code above.
profile picture

dansmo

#18
I will set a factor direclty in the Position.Tag, so that anything can be controlled directly from within the stratey script. However, size results to 0 here. Can you see any mistake?
CODE:
Please log in to see this code.
profile picture

Eugene

#19
On the surface, the logic sounds like it should work. Maybe the factor is too small to drive size to 0? Try setting a breakpoint on "return size" in the debugger and you will find out the culprit.
profile picture

dansmo

#20
hmm.
CODE:
Please log in to see this code.

This gives me an InvalidCastException.
This is the code that sets the Tags:
CODE:
Please log in to see this code.

I dont see why I cannot cast them to double!?
profile picture

Eugene

#21
Using converting instead of casting should fix it:
CODE:
Please log in to see this code.
profile picture

dansmo

#22
Do I understand correct that
CODE:
Please log in to see this code.

gives me the number of shares for the position?
I modified the Random.PosSizer demo. So selecting 5.000 per Position should not result in a size of 28669 when the stock trades at 17.5.

Edit: found the mistake. 5.000 is not equal to 5000,00 ;-).
profile picture

Eugene

#23
QUOTE:
gives me the number of shares for the position?

Exactly right.
profile picture

dansmo

#24
It seems to work now. Just one more thing I need to implement:
I´d like to limit the value of a position to 1% of the avg. turnover of the last month.
How can I get access to Volume and Close series in PosSizer?
profile picture

Eugene

#25
Each Position object in a PosSizer (Visualizer, Strategy...), including currentPos, has the Bars property. Derive the Volume and Close series from the p.Bars obect.
profile picture

dansmo

#26
Every time you answer I get angry about myself...not doing the obvious.
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).