# Margin-db Pull Request Review
## Calculations
### Binance interest formula
I (interest) = P (borrowed money) * R (daily_interest/24) * ceiling(T) (in hours)
[source](https://www.binance.com/en/support/faq/360030157812)
### Kraken interest formula
Opening fee = P (borrowed money) * R (quat_hourly_interest)
Rollover fee = P (borrowed money) * R (quat_hourly_interest) * ceiling(T/4) (in hours)
I (interest) = Opening fee + Rollover fee
[source](https://support.kraken.com/hc/en-us/articles/206161568-What-are-the-fees-for-margin-trading-)
### Profit ratio for short trades
`profit_ratio = (1 - (close_trade_value/self.open_trade_value))`
### Profit ratio for leveraged trades
`leveraged_profit_ratio = profit_ratio * leverage`
## Tests
To add shorting and leverage functionality to freqtrade, the majority of changes involved editing existing methods within `freqtrade/persistence/models.py`. `freqtrade/persistence/models` was already fully tested, so to test the new functionality, new tests were created inside `tests/persistence/test_persistence_short.py` and `tests/persistence/test_persistence_leverage.py`, which mirrored the the tests inside `tests/persistence/test_persistence.py`, but accounted for additional factors created by _leverage_ and _short trading_
### Factors added to freqtrade/persistence
#### Short trading
- Exit trade amounts are slightly higher than their enter amounts, due to the extra amount purchased for paying back interest owed for the amount of currency borrowed
- Profit calculation were calculated inversely compared to longs
- Stoplosses moved in the reverse direction
- The entering/starting trade was a "sell" and the exiting/closing trade was a "buy"
#### Leveraged long trading
- Leveraged long trades had the amount of interest owed subtracted from the value of their exiting trade
test_persistence |
test_persistence_short |
test_persistence_leverage |
test_update_with_binance |
test_update_with_binance_short |
test_update_with_binance_lev |
test_update_market_order |
test_update_market_order_short |
test_update_market_order_lev |
test_update_open_order |
test_update_open_order_short |
test_update_open_order_lev |
test_calc_open_trade_value |
test_calc_open_trade_value_short |
test_calc_open_trade_value_lev |
test_calc_open_close_trade_price |
test_calc_open_close_trade_price_short |
test_calc_open_close_trade_price_lev |
test_trade_close |
test_trade_close_short |
test_trade_close_lev |
test_calc_close_trade_price_exception |
test_calc_close_trade_price_exception_short |
test_calc_close_trade_price_exception_lev |
test_calc_close_trade_price |
test_calc_close_trade_price_short |
test_calc_close_trade_price_lev |
test_calc_close_trade_price_exception |
test_calc_close_trade_price_exception_short |
test_calc_close_trade_price_exception_lev |
test_calc_profit & test_calc_profit_ratio |
test_calc_profit_short |
test_calc_profit_lev |
Tests with no equivelent in test_persistence_lev
|
test_adjust_stop_loss |
test_adjust_stop_loss_short |
test_get_open |
test_get_open_short |
test_total_open_trades_stakes |
test_total_open_trades_stakes_short |
test_stoploss_reinitialization |
test_stoploss_reinitialization_short |
test_get_best_pair |
test_get_best_pair_short |
### Tests not repeated
These tests did not have an equivelent version created inside `test_persistence_short.py` or `test_persistence_lev.py` because no new situations arise in the methods they test when adding leverage or short trading to `freqtrade/persistence`
-