QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.76k stars 3.26k forks source link

Strange behaviour in bitfinex brokerage (or AlphaFramework) #2739

Closed ksanderer closed 5 years ago

ksanderer commented 5 years ago

Expected Behavior

I'm trading AlphaFramework ma cross strategy (Bitfinex). Portfolio construction may return signals -1/1 BTCUSD position. I'm expecting that BTCUSD position can't be bigger that 100% margin wallet USD balance.

Actual Behavior

Somehow x2 exposure was reached.

screen shot 2018-12-06 at 20 01 25

Below is a snippet how portfolio construction target was logged.

# It doesn't really matters how signal was calculated
# I just want to show that Portfolio construction target getting exactly the same values as algorithm.Log string
# and if we see logs `BTCUSD -> 100%` that means that there was 
# PortfolioTarget.Percent(algorithm, "BTCUSD",  1)

target_pct = get_target_pct() 
targets.append(PortfolioTarget.Percent(algorithm, symbol, target_pct))
algorithm.Log("> target: %s ->  %.2f %%" % (symbol, 100 * target_pct))

Logs:

Lean - lean.log Bitfinex Ledgers - https://gist.github.com/ksanderer/a3f9b5f3988691325c9cd35ea736177e


My research on Lean logs and Bitfinex Ledgers

2018-11-30

2018-11-30T00:00:05.1105540Z TRACE:: Log: ==================================================
2018-11-30T00:00:05.1105840Z TRACE:: Log: =============PortfolioConstructionModel===========
2018-11-30T00:00:05.1106140Z TRACE:: Log: ==================================================
2018-11-30T00:00:05.1106440Z TRACE:: Log: > algorithm.UtcTime:
2018-11-30T00:00:05.1106720Z TRACE:: Log: > 2018-11-30 00:00:00+00:00
2018-11-30T00:00:05.1107010Z TRACE:: Log: > ---------------------
2018-11-30T00:00:05.1107300Z TRACE:: Log: > target: BTCUSD ->  0.00 %
2018-11-30T00:00:05.1107590Z TRACE:: Log: ==================================================
2018-11-30T00:00:05.1107880Z TRACE:: Log: ==========/end PortfolioConstructionModel=========
2018-11-30T00:00:05.1108180Z TRACE:: Log: ==================================================

2018-11-30 is the latest log point i'm understanding.
Portfolio construction returns target: BTCUSD -> 0.00 % and position was liquidated.

2018-12-01

2018-12-01T00:00:03.3308740Z TRACE:: Log: ==================================================
2018-12-01T00:00:03.3309030Z TRACE:: Log: =============PortfolioConstructionModel===========
2018-12-01T00:00:03.3309330Z TRACE:: Log: ==================================================
2018-12-01T00:00:03.3309630Z TRACE:: Log: > algorithm.UtcTime:
2018-12-01T00:00:03.3309930Z TRACE:: Log: > 2018-12-01 00:00:00+00:00
2018-12-01T00:00:03.3310430Z TRACE:: Log: > ---------------------
2018-12-01T00:00:03.3310740Z TRACE:: Log: > target: BTCUSD ->  -100.00 %
2018-12-01T00:00:03.3311050Z TRACE:: Log: ==================================================
2018-12-01T00:00:03.3311340Z TRACE:: Log: ==========/end PortfolioConstructionModel=========
2018-12-01T00:00:03.3311640Z TRACE:: Log: ==================================================

Here strange stuff is started. We can see 100% BTCUSD short portfolio target.
BUT!

Trading fees for 0.376 BTC (BTCUSD) @ 4038.5 on BFX (0.2%) on wallet margin USD -3.036952   1296.68226365   18-12-01 00:00:03   margin
Settlement @ 4336.6 on wallet margin    USD -0.18832414 1299.71921565   18-11-30 00:01:27   margin

There was 1299.71 USD on margin wallet and BTC price was at 4038.5 level:

1299.71 / 4038.5 = 0.321829887 (This is approximate calculation without fees and bitfinex minimal trading step)

Somehow there was trade for 0.376 BTC instead.

0.376 - 0.3218 = 0.0542 (quite close to yesterdays closing trade at 0.058 BTC

2018-12-02

2018-12-02T00:00:05.1055470Z TRACE:: Log: ==================================================
2018-12-02T00:00:05.1055990Z TRACE:: Log: =============PortfolioConstructionModel===========
2018-12-02T00:00:05.1056510Z TRACE:: Log: ==================================================
2018-12-02T00:00:05.1057080Z TRACE:: Log: > algorithm.UtcTime:
2018-12-02T00:00:05.1057620Z TRACE:: Log: > 2018-12-02 00:00:00+00:00
2018-12-02T00:00:05.1065310Z TRACE:: Log: > ---------------------
2018-12-02T00:00:05.1065920Z TRACE:: Log: > target: BTCUSD ->  0.00 %
2018-12-02T00:00:05.1066480Z TRACE:: Log: ==================================================
2018-12-02T00:00:05.1067040Z TRACE:: Log: ==========/end PortfolioConstructionModel=========
2018-12-02T00:00:05.1067570Z TRACE:: Log: ==================================================

Closing trade. But instead of liquidating -0.376 BTC short position only 0.318 BTC was bought.

0.318 - 0.376 = -0.058

Same here we can see -0.058 BTC diff with exchange and lean BTC position.

2018-12-03

2018-12-03T00:00:05.1253700Z TRACE:: Log: ==================================================
2018-12-03T00:00:05.1253990Z TRACE:: Log: =============PortfolioConstructionModel===========
2018-12-03T00:00:05.1254290Z TRACE:: Log: ==================================================
2018-12-03T00:00:05.1254910Z TRACE:: Log: > algorithm.UtcTime:
2018-12-03T00:00:05.1255230Z TRACE:: Log: > 2018-12-03 00:00:00+00:00
2018-12-03T00:00:05.1255520Z TRACE:: Log: > ---------------------
2018-12-03T00:00:05.1255830Z TRACE:: Log: > target: BTCUSD ->  0.00 %
2018-12-03T00:00:05.1256130Z TRACE:: Log: ==================================================
2018-12-03T00:00:05.1256420Z TRACE:: Log: ==========/end PortfolioConstructionModel=========
2018-12-03T00:00:05.1256720Z TRACE:: Log: ==================================================

Here is the most strange thing is happened. Instead of going long for 0.058 BTC to finally liquidate BTC position there was short trade -0.318 BTC.

Have no idea how this may happen, may be there was some connections errors?

2018-12-04

2018-12-04T00:00:01.3142630Z TRACE:: Log: ==================================================
2018-12-04T00:00:01.3143000Z TRACE:: Log: =============PortfolioConstructionModel===========
2018-12-04T00:00:01.3143320Z TRACE:: Log: ==================================================
2018-12-04T00:00:01.3143630Z TRACE:: Log: > algorithm.UtcTime:
2018-12-04T00:00:01.3143940Z TRACE:: Log: > 2018-12-04 00:00:00+00:00
2018-12-04T00:00:01.3144240Z TRACE:: Log: > ---------------------
2018-12-04T00:00:01.3144550Z TRACE:: Log: > target: BTCUSD ->  -100.00 %
2018-12-04T00:00:01.3144850Z TRACE:: Log: ==================================================
2018-12-04T00:00:01.3145330Z TRACE:: Log: ==========/end PortfolioConstructionModel=========
2018-12-04T00:00:01.3145650Z TRACE:: Log: ==================================================

Finally when short signal arrived another -0.328 BTC was sold.

-0.37600000 (1 nov) + 0.318 (2 nov) - 0.318 (3 nov) - 0.328 (4 nov) = -0.704

At time of -0.704 BTC short position there was 1288.12450416 USD on margin wallet (BTC price 3903.0 USD)

1288.12450416 / 3903 = 0.330034462

There shouldn't be positions neither short or long bigger than 0.33 BTC isn't it?

Checklist

jaredbroad commented 5 years ago

The percent helper doesn't force a specific total sum of positions. That is the primary job of the portfolio model -- to take limited resources and allocate it to maximize profit.

Please post the portfolio model to the lean page on QuantConnect.com. We moved discussion there to concentrate efforts

On Fri, Dec 7, 2018, 1:45 AM Alex T <notifications@github.com wrote:

Expected Behavior

I'm trading AlphaFramework ma cross strategy (Bitfinex). Portfolio construction may return signals -1/1 BTCUSD position. I'm expecting that BTCUSD position can't be bigger that 100% margin wallet USD balance. Actual Behavior

Somehow x2 exposure was reached.

[image: screen shot 2018-12-06 at 20 01 25] https://user-images.githubusercontent.com/1606635/49601084-e9a2bd00-f995-11e8-819a-447d63af0e2a.png

Below is a snippet how portfolio construction target was logged.

portfolio construction target and logging string (log file is attached below)

target_pct = insight_result['result'] / insight_result['count'] targets.append(PortfolioTarget.Percent(algorithm, symbol, target_pct)) algorithm.Log("> target: %s -> %.2f %%" % (symbol, 100 * target_pct))

Logs:

Lean - lean.log https://github.com/QuantConnect/Lean/files/2654046/clean_log.log Bitfinex Ledgers - https://gist.github.com/ksanderer/a3f9b5f3988691325c9cd35ea736177e

My research on Lean logs and Bitfinex Ledgers 2018-11-30

2018-11-30T00:00:05.1105540Z TRACE:: Log: ================================================== 2018-11-30T00:00:05.1105840Z TRACE:: Log: =============PortfolioConstructionModel=========== 2018-11-30T00:00:05.1106140Z TRACE:: Log: ================================================== 2018-11-30T00:00:05.1106440Z TRACE:: Log: > algorithm.UtcTime: 2018-11-30T00:00:05.1106720Z TRACE:: Log: > 2018-11-30 00:00:00+00:00 2018-11-30T00:00:05.1107010Z TRACE:: Log: > --------------------- 2018-11-30T00:00:05.1107300Z TRACE:: Log: > target: BTCUSD -> 0.00 % 2018-11-30T00:00:05.1107590Z TRACE:: Log: ================================================== 2018-11-30T00:00:05.1107880Z TRACE:: Log: ==========/end PortfolioConstructionModel========= 2018-11-30T00:00:05.1108180Z TRACE:: Log: ==================================================

2018-11-30 is the latest log point i'm understanding. Portfolio construction returns target: BTCUSD -> 0.00 % and position was liquidated. 2018-12-01

2018-12-01T00:00:03.3308740Z TRACE:: Log: ================================================== 2018-12-01T00:00:03.3309030Z TRACE:: Log: =============PortfolioConstructionModel=========== 2018-12-01T00:00:03.3309330Z TRACE:: Log: ================================================== 2018-12-01T00:00:03.3309630Z TRACE:: Log: > algorithm.UtcTime: 2018-12-01T00:00:03.3309930Z TRACE:: Log: > 2018-12-01 00:00:00+00:00 2018-12-01T00:00:03.3310430Z TRACE:: Log: > --------------------- 2018-12-01T00:00:03.3310740Z TRACE:: Log: > target: BTCUSD -> -100.00 % 2018-12-01T00:00:03.3311050Z TRACE:: Log: ================================================== 2018-12-01T00:00:03.3311340Z TRACE:: Log: ==========/end PortfolioConstructionModel========= 2018-12-01T00:00:03.3311640Z TRACE:: Log: ==================================================

Here strange stuff is started. We can see 100% BTCUSD short portfolio target. BUT!

Trading fees for 0.376 BTC (BTCUSD) @ 4038.5 on BFX (0.2%) on wallet margin USD -3.036952 1296.68226365 18-12-01 00:00:03 margin Settlement @ 4336.6 on wallet margin USD -0.18832414 1299.71921565 18-11-30 00:01:27 margin

There was 1299.71 USD on margin wallet and BTC price was at 4038.5 level:

1299.71 / 4038.5 = 0.321829887 (This is approximate calculation without fees and bitfinex minimal trading step)

Somehow there was trade for 0.376 BTC instead.

0.376 - 0.3218 = 0.0542 (quite close to yesterdays closing trade at 0.058 BTC 2018-12-02

2018-12-02T00:00:05.1055470Z TRACE:: Log: ================================================== 2018-12-02T00:00:05.1055990Z TRACE:: Log: =============PortfolioConstructionModel=========== 2018-12-02T00:00:05.1056510Z TRACE:: Log: ================================================== 2018-12-02T00:00:05.1057080Z TRACE:: Log: > algorithm.UtcTime: 2018-12-02T00:00:05.1057620Z TRACE:: Log: > 2018-12-02 00:00:00+00:00 2018-12-02T00:00:05.1065310Z TRACE:: Log: > --------------------- 2018-12-02T00:00:05.1065920Z TRACE:: Log: > target: BTCUSD -> 0.00 % 2018-12-02T00:00:05.1066480Z TRACE:: Log: ================================================== 2018-12-02T00:00:05.1067040Z TRACE:: Log: ==========/end PortfolioConstructionModel========= 2018-12-02T00:00:05.1067570Z TRACE:: Log: ==================================================

Closing trade. But instead of liquidating -0.376 BTC short position only 0.318 BTC was bought.

0.318 - 0.376 = -0.058

Same here we can see -0.058 BTC diff with exchange and lean BTC position. 2018-12-03

2018-12-03T00:00:05.1253700Z TRACE:: Log: ================================================== 2018-12-03T00:00:05.1253990Z TRACE:: Log: =============PortfolioConstructionModel=========== 2018-12-03T00:00:05.1254290Z TRACE:: Log: ================================================== 2018-12-03T00:00:05.1254910Z TRACE:: Log: > algorithm.UtcTime: 2018-12-03T00:00:05.1255230Z TRACE:: Log: > 2018-12-03 00:00:00+00:00 2018-12-03T00:00:05.1255520Z TRACE:: Log: > --------------------- 2018-12-03T00:00:05.1255830Z TRACE:: Log: > target: BTCUSD -> 0.00 % 2018-12-03T00:00:05.1256130Z TRACE:: Log: ================================================== 2018-12-03T00:00:05.1256420Z TRACE:: Log: ==========/end PortfolioConstructionModel========= 2018-12-03T00:00:05.1256720Z TRACE:: Log: ==================================================

Here is the most strange thing is happened. Instead of going long for 0.058 BTC to finally liquidate BTC position there was short trade -0.318 BTC.

Have no idea how this may happen, may be there was some connections errors? 2018-12-04

2018-12-04T00:00:01.3142630Z TRACE:: Log: ================================================== 2018-12-04T00:00:01.3143000Z TRACE:: Log: =============PortfolioConstructionModel=========== 2018-12-04T00:00:01.3143320Z TRACE:: Log: ================================================== 2018-12-04T00:00:01.3143630Z TRACE:: Log: > algorithm.UtcTime: 2018-12-04T00:00:01.3143940Z TRACE:: Log: > 2018-12-04 00:00:00+00:00 2018-12-04T00:00:01.3144240Z TRACE:: Log: > --------------------- 2018-12-04T00:00:01.3144550Z TRACE:: Log: > target: BTCUSD -> -100.00 % 2018-12-04T00:00:01.3144850Z TRACE:: Log: ================================================== 2018-12-04T00:00:01.3145330Z TRACE:: Log: ==========/end PortfolioConstructionModel========= 2018-12-04T00:00:01.3145650Z TRACE:: Log: ==================================================

Finally when short signal arrived another -0.328 BTC was sold.

-0.37600000 (1 nov) + 0.318 (2 nov) - 0.318 (3 nov) - 0.328 (4 nov) = -0.704

At time of -0.704 BTC short position there was 1288.12450416 USD on margin wallet (BTC price 3903.0 USD)

1288.12450416 / 3903 = 0.330034462

There shouldn't be positions neither short or long bigger than 0.33 BTC isn't it? Checklist

  • I have completely filled out this template

  • This build was running from the master brunch 2018-11-07

  • I have confirmed that this is not a duplicate issue by searching issues https://github.com/QuantConnect/Lean/issues

  • Have no idea how to reproduce this step, but I have all logs and dumps from that strategy instance.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/QuantConnect/Lean/issues/2739, or mute the thread https://github.com/notifications/unsubscribe-auth/ACI6mU3xPXnQ6NypqV_UVZrOfFNWdQCoks5u2jjFgaJpZM4ZIGqK .

markudevelop commented 5 years ago

@ksanderer sorry for off topic but how do you trade bitfinex i thought only gdax is supported?

jaredbroad commented 5 years ago

@voidale - https://github.com/QuantConnect/Lean/tree/master/Brokerages/Bitfinex

ksanderer commented 5 years ago

@jaredbroad, here is strategy on QC https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_fcf8bb151e50fdcb07fdd46e9eeea996.html

take a look at: equal_insight_weighting_portfolio_construction.py#69

Each MA cross alpha signal can vote for 1/len(alpha_list) of available capital.

As I get the idea of PortfolioCinstrustion, it will maintain target percent of the required asset, am I right?

Is there something more I can do?