Transactions that do not map to Positions
Author: ronc
Creation Date: 7/20/2013 8:36 PM
profile picture

ronc

#1
I am coding a strategy that buys and sells amounts that are calculated on the fly and at bars that are determined on the fly. For example, add $D1 to the position on one bar, sell $D2 from the position on a later bar, and buy/add $D3 on yet a later bar. The amounts and bars are calculated by my strategy.

From the documentation, it seems that I need to use SetShareSize() and WealthScript Override (SetShareSize). However, I found this post.


That post does not use SetShareSize(). I assume this is because that post is using a constant transaction size, correct?

How do we handle selling shares that do not map directly to positions? The wealth-lab code I have written to-date all assumes that I open and close positions completely, e.g. buy N shares (open the position) and then later sell all shares (close the position). But now I want to buy N shares one day, buy M shares another day, and later sell an arbitrary number of shares. So the shares that I am selling could be part of one of the positions or could cut across multiple positions, if each purchase equates to a distinct position. How to do this in Wealth-Lab? When I go to sell N shares out of the various positions that I had previously opened for the symbol, which particular position do I sell from? Do I need to write code to calculate how much to sell from various positions or is there a way to do this directly in Wealth-Lab?

Thanks.
Ron
profile picture

Eugene

#2
Wealth-Lab is inherently position-based. It's not designed for tasks like "sell N shares from positions A and B, where N = part of A+B". You can pyramid (buy a new position), and/or make a partial exit from a position. Your understanding is correct in that to implement the logic you described, you will have to write code to calculate how much to sell from various positions. Keep in mind that SetShareSize() will not help exit from a position: it only applies to entries. For partial exits, you use SplitPosition() as exemplified in a link above.
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).