Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge Positioning #1229

Merged
merged 187 commits into from Nov 13, 2018
Merged

Edge Positioning #1229

merged 187 commits into from Nov 13, 2018

Conversation

mishaker
Copy link
Member

@mishaker mishaker commented Sep 21, 2018

Summary

Currently FT analyzes pairs in whitelist randomly (or by order in config.json) and takes a position as soon as it finds a buy signal with a fixed stake_amount. a better approach would be:

  • Calculating the chance of winning for each pair and sorting whitelist accordingly.
  • Adjusting the position size according to risk/reward.

This PR addresses the above issues by adding a package called "Edge". Edge will run a process every X minutes (default 30 min) using the mainloop to go through all pairs in order to:

  • Calculate WinRate and Expected Risk Reward ratio for a range of stoplosses.
  • Finding the best WinRate/Expected Risk Reward ratio for a pair. finding the best stoploss value.
  • Forcing the stoploss value (calculated above) into the strategy.

The process uses the backtesting download script to fetch historical data. and cache a matrix of [pair, stoploss, win_rate, expected_risk_reward, required_risk_reward]. an example of the cached matrix:

pair stoploss winrate risk_reward_ratio required_risk_reward
NEO/BTC -0.04 0.666667 3.071181 0.500000
STEEM/BTC -0.02 0.500000 2.399500 1.000000
DASH/BTC -0.01 0.625000 1.181620 0.600000
BNB/BTC -0.08 0.625000 1.128478 0.600000
CMT/BTC -0.05 0.583333 0.952720 0.714286
XEM/BTC -0.03 0.500000 1.194416 1.000000

WinRate is the number of winning trades divided by number of trades (e.x. if 4 trades out of 10 are profitable then the winrate is 40%).

Risk Reward Ratio is the ratio between your loss and your take profit. as an example lets say you take a position with an entry price of 100$. you put your stoploss at 90$ (i.e. 10%) and your take profit (sell point) is 120$. your risk reward would be 2 (your profit would be two times your loss).

While going through historical data we are only interested in finding two and only two things:

  • Amount/Number of losses if the stoploss hit.
  • Amount/Number of profits if sell signal is triggered.

TODO list before merging the PR:

  • Fixing .travis script which breaks tests currently.
  • Fixing already broken tests.
  • Finding an alternative for py_find_1st, otherwise try to make its installation clean.
  • Finding a proper way to sort the matrix according to WinRate and RRR.
  • Make backslap function more readable
  • Writing tests
  • Writing documentation

Will replace #1147 and #1114

creslinux and others added 30 commits July 14, 2018 22:54
science project replacement for freqtrade backtest analysis

- appprox 300-500x quicker to execute

- fixes stop on close take close price bug in FT
Bslap is configurable but by default stops are triggerd on
low and pay stop price

Not implimented dynamic stops or roi
Fixed Index Alignment that was off moving from scratch to FT
Fixed Stoploss,
  its a negative in FT, had been using positve stop -1 in scratch
Bugs
Calculating % prof ok, but abs wrong

BAT/BTC DF is very broken all OHLC are the same - but exposes a
buy after stop on last row "oddness" to be investigated / handled
updated logic
added vector fill for abs/profit/duration in single hit on results.
If self.use_backslap = True   Backslap executes
If self.use_backslap = False  Original Backtest Code executes
Switched Stops to trigger on Low
Switched Stops to pay stop-rate not close.
Switched Stops to trigger on Low
Switched Stops to pay stop-rate not close.
If true, prints trades ordered by date after summary.
Useful for spotting trends.
Seperated save trades and print trades options.
We will never see this, as buy is on close which is the end of backtest
e.g there is no next candle OPEN to buy at, or on
when buy opening on Index 1
stop_stops is an int value
when number of stops in a pair reached the int the pair is stopped
trading.

This allows backtest to align with my pre_trade_mgt that does the same
in dry and live operations
vector calcs redone.
vector calcs redone.
@creslinux
Copy link
Contributor

creslinux commented Nov 10, 2018 via email

@doftorul
Copy link

Guys of course you have to check your wallet to see if you have free balance and scale into it for new positions. That does not change total capital for the calcs to get your ratios. How ever capital is spread over X assets it's still total capital.

On Sat, Nov 10, 2018, 12:52 PM Misagh @.*** wrote: I am saying your total capital at risk is your stake_amount multiplied by max_open_trades. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1229 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AhCkwxBM38Qd8RwOo0-O6xZaNiccaPuBks5utq_agaJpZM4W0fHj .

@creslinux a different approach where we're applying Edge and its calculations to a portion of the total wallet capital would work as well.
Even taking into account a split of 3 trades, it optimizes the risk management of the portfolio if we consider the total capital/3 allowed parallel trades and go with the R/R calculation on each of the trades.
Then, at a later stage when the wallet amount can be fetched from the exchange one should be able to decide if it wants the capital spread among 4 trades or to increase the stake available for a trade by dividing the new total wallet value fetched from the exchange to the same 3 max trades in parallel.

Does that makes sense ?

Instead of using 1% of your total wallet and having to only be able to support a single trade, applying the risk calculations and hence the max amount per trade with a ceiling at total_wallet/number_of_max_trades

@mishaker
Copy link
Member Author

I am sorry I meant your total capital.

@creslinux
Copy link
Contributor

creslinux commented Nov 10, 2018 via email

@mishaker
Copy link
Member Author

total capital = 1500
max_trade = 3
allowed risk = 1%
stoploss = 2%
trade 1: 250 => stop at 245
trade 2: 250 => stop at 245
trade 3: 250 => stop at 245
that means total capital at risk is 15.

@mishaker
Copy link
Member Author

you are talking about scaling down after position sizing. I am saying you can scale down from the beginning. it comes to the exact same result mathematically

@creslinux
Copy link
Contributor

creslinux commented Nov 10, 2018 via email

@doftorul
Copy link

Consider the below scenario:

ID Pair Since Profit


2 ZIL/BTC 9 h 0.08%
3 ZRX/BTC 6 h -0.06%
4 XMR/BTC 3 h -0.26%

The trades are some 4 hours apart from each other, the amounts are 0.07493757, 0.04987999, 0.02501785 from a total of 0.15 BTC made available to Edge.

Once the first trade happened, the amount it took limited the other potential trades, i missed on 4 different trades between the first trade and the 2nd one due to Edge setting a higher stake amount for that trade. Between trade 2 and 3 i missed 5 different trades due to the same larger amounts set by the Edge for the stake of a trade.

Now, its only logical to consider that the trades Edge enters into after the first one are those where you risk less and less capital and hence tend to have a lower performance.

@mishaker
Copy link
Member Author

yep that's a big problem. if stoploss is 10% then you invest only 50 of 500 then the remaining is dead money ...
I can ditch them yep. but then I have to calculate amount of already invested in open trades then remove it from the total capital ?

@creslinux
Copy link
Contributor

creslinux commented Nov 10, 2018 via email

@mishaker
Copy link
Member Author

But when would you stop opening a trade ? simply when you don't have enough remaining free capital ?

@creslinux
Copy link
Contributor

creslinux commented Nov 10, 2018 via email

@doftorul
Copy link

Went and supplemented the total wallet amount without changing in config the total_capital_in_stake_currency amount.
Edge went and opened other trades that exceed the total stake set in config.json.

@mishaker
Copy link
Member Author

After discussion with @xmatthias we decided to do the following:

  • I am going to limit this PR to stake_amount and max_trades_open. so total capital is considered the stake amount on each trade.
  • Followup for remaining tasks here in this issue Followup PR to edge #1319.
  • Once this PR is merged I am going to adjust the position sizing as described in Followup PR to edge #1319.
    The reason is that this PR is becoming too large. so if there is no objection from your end we will do as described above.

The advantage is that it won't break the native functioning in FT and it allows to have more eyes on different parts of Edge. It then will be relatively easy to refactor for real position sizing.

@doftorul Thanks a lot for testing and finding the problem ! I will probably ask you again to test if you can once position sizing will be repaired.

@creslinux thanks a lot for challenging me. I will consider total capital and fetch balance on exchange for the next version and max_open_trades will be probably ignored (unless we find a compatibility somehow)

docs/edge.md Outdated

#### allowed_risk
Percentage of allowed risk per trade<br/>
(default to 1%)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you should say 0.01 here - this is confusing if i need to set 1 (%) - or 0.01 ...

docs/index.md Outdated
@@ -25,6 +25,7 @@ Pull-request. Do not hesitate to reach us on
- [Change your strategy](https://github.com/freqtrade/freqtrade/blob/develop/docs/bot-optimization.md#change-your-strategy)
- [Add more Indicator](https://github.com/freqtrade/freqtrade/blob/develop/docs/bot-optimization.md#add-more-indicator)
- [Test your strategy with Backtesting](https://github.com/freqtrade/freqtrade/blob/develop/docs/backtesting.md)
- [Edge positionning](https://github.com/mishaker/freqtrade/blob/money_mgt/docs/edge.md)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

positioning

Copy link
Member

@xmatthias xmatthias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

will leave this around til tomorrow evening so @creslinux and @doftorul can comment should they not agree to take the "left open" points (#1319 ) in seperate followup-PR's

@xmatthias xmatthias merged commit 5c8544a into freqtrade:develop Nov 13, 2018
@mishaker mishaker mentioned this pull request Nov 28, 2018
4 tasks
@mishaker mishaker deleted the money_mgt branch December 1, 2018 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Enhancements to the bot. Get lower priority than bugs by default.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants