- ago
In my strategy that uses external symbols (option contracts based on an underlying), I am processing the datatype Position during backtest and the datatype BrokerPosition on the last bar (for live trading). I do not see a way to type cast a BrokerPosition to a (backtest) Position.

This makes for mostly duplicative coding to process the last bar, with a "bPos" vs. a "pos". For example, I can close a position with ClosePosition for a "pos" but not a "bPos." I use separate coding using PlaceTrade.

Is there a sample coding pattern or suggestion to help reduce the amount of duplicate code for entry and exit signals in this setup?
0
89
2 Replies

Reply

Bookmark

Sort
- ago
#1
Can you clarify? So, when live trading, you want to take the broker position (BrokerPosition) and associate that with an open position (Position)? If that's the case, you don't want to cast BrokerPosition to Position because then you just have a new Position object that is not associated with the whole set of Position(s). I believe you want to figure out which open Position is associated with that BrokerPosition. Right?
0
- ago
#2
No, that's not the issue. When using external symbols, you have to do some internal management of the actual positions at the broker. You cannot enable Live Positions in Trading Preferences, for example, to do the work for you.

The issue is that the BrokerPosition type and Position type have different properties and are not interchangeable. Certain methods that work with backtesting won't work with live broker positions (broker position object), such as "ClosePosition." So, you have to code a workaround for live trading.

It can be done but requires a lot of duplication. You can't just optionally pass a Position type or a BrokerPosition type variable to a method. It's expecting one or the other and will throw an error. Overloading methods is not a solution in this case. There may be a clever way to do it, but I don't know what it is.
0

Reply

Bookmark

Sort