Merge pull request #4543 from brookmiles/fix-math-custom-stoploss-docs
correct math used in examples and clarify some terminology regarding …
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from math import isclose
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from freqtrade.strategy import merge_informative_pair, timeframe_to_minutes
|
||||
from freqtrade.strategy import merge_informative_pair, stoploss_from_open, timeframe_to_minutes
|
||||
|
||||
|
||||
def generate_test_data(timeframe: str, size: int):
|
||||
@@ -95,3 +97,38 @@ def test_merge_informative_pair_lower():
|
||||
|
||||
with pytest.raises(ValueError, match=r"Tried to merge a faster timeframe .*"):
|
||||
merge_informative_pair(data, informative, '1h', '15m', ffill=True)
|
||||
|
||||
|
||||
def test_stoploss_from_open():
|
||||
open_price_ranges = [
|
||||
[0.01, 1.00, 30],
|
||||
[1, 100, 30],
|
||||
[100, 10000, 30],
|
||||
]
|
||||
current_profit_range = [-0.99, 2, 30]
|
||||
desired_stop_range = [-0.50, 0.50, 30]
|
||||
|
||||
for open_range in open_price_ranges:
|
||||
for open_price in np.linspace(*open_range):
|
||||
for desired_stop in np.linspace(*desired_stop_range):
|
||||
|
||||
# -1 is not a valid current_profit, should return 1
|
||||
assert stoploss_from_open(desired_stop, -1) == 1
|
||||
|
||||
for current_profit in np.linspace(*current_profit_range):
|
||||
current_price = open_price * (1 + current_profit)
|
||||
expected_stop_price = open_price * (1 + desired_stop)
|
||||
|
||||
stoploss = stoploss_from_open(desired_stop, current_profit)
|
||||
|
||||
assert stoploss >= 0
|
||||
assert stoploss <= 1
|
||||
|
||||
stop_price = current_price * (1 - stoploss)
|
||||
|
||||
# there is no correct answer if the expected stop price is above
|
||||
# the current price
|
||||
if expected_stop_price > current_price:
|
||||
assert stoploss == 0
|
||||
else:
|
||||
assert isclose(stop_price, expected_stop_price, rel_tol=0.00001)
|
||||
|
Reference in New Issue
Block a user