uniVocity / univocity-trader

open-source trading framework for java, supports backtesting and live trading with exchanges
577 stars 140 forks source link

Loading currently open positions and overriding method #30

Closed kausality closed 4 years ago

kausality commented 4 years ago

Is there a way to load currently open positions preferabbly as a separate object?

The method which loads the positions can be made overridable so if we have multiple strategies running on one account, and say 2 strategies have positions on asset X with quantity 10 and 20 each then we can determine what is the position for the current strategy in asset X by subtracting the total open position on the asset X in our account by a value we stored locally.

The value could have been saved in the last run of our current strategy when it received fill status.

So this way we know that even though the account has 30 shares of X, we have to only see it as 10(30 - 20) if we are inside the second strategy.

jbax commented 4 years ago

I'm not 100% clear on what you need. Could you provide some sample code illustrating what you'd like to do?

I believe what you need can be extracted from each individual Trade object inside the Trader. They store the strategy and the orders.

On Su19 Jan. 2020, 9:05 pm Kaustubh Pratap Chand, notifications@github.com wrote:

Is there a way to load currently open positions preferabbly as a separate object?

The method which loads the positions can be made overridable so if we have multiple strategies running on one account, and say 2 strategies have positions on asset X with quantity 10 and 20 each then we can determine what is the position for the current strategy in asset X by subtracting the total open position on the asset X in our account by a value we stored locally.

The value could have been saved in the last run of our current strategy when it received fill status.

So this way we know that even though the account has 30 shares of X, we have to only see it as 10(30 - 20) if we are inside the second strategy.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/uniVocity/univocity-trader/issues/30?email_source=notifications&email_token=ABWFQPWFLG35OMEB42A5J63Q6RM2NA5CNFSM4KIZNXL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IHFXS7Q, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWFQPX2SCZ4ZHAB3POCB2LQ6RM2NANCNFSM4KIZNXLQ .

kausality commented 4 years ago

You may be right. I was thinking something along the lines of: https://www.backtrader.com/docu/position/

Is there a method to check the current position size in asset on the exchange? This will be useful in case whenever we restart the strategy due to computer restarting or program crashing. Before emitting a BUY signal, we can use this method to check if we already have position in asset X on the exchange and if yes, then don't emit signal.

I tried to find something like it in the source files but couldn't.

jbax commented 4 years ago

When live trading, ClientAccount.updateBalances() should return a Map<String, Balance> with all open positions, including amounts locked (i.e. in some order). Even if you open a trade manually on the exchange directly, updateBalances will pick up the changes in your account balance.

This is done atuomatically and you can simply use Balance balance = trader.balance("BTC") from within the OrderManager that is called before (and after) placing an order.

Let me know if this helps

kausality commented 4 years ago

Looks like it is what I was looking for. Thanks.