Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b5d2a47bc | ||
|
|
ae3a8f797e | ||
|
|
686a6fa0ba | ||
|
|
9fe8877533 |
@@ -260,6 +260,29 @@ def get_strategy_class(strategy_name):
|
||||
return locate('strategies.{}.{}'.format(strategy_name, strategy_name))
|
||||
|
||||
|
||||
def hp_rules_valid(hp, rules):
|
||||
check = np.full((len(rules)), False, dtype=bool)
|
||||
|
||||
for i, rule in enumerate(rules):
|
||||
if rule['operator'] not in ["<", ">", "<=", ">="]:
|
||||
raise ValueError("{} is not a supported operator. Choose from < > <= >=".format(rule['operator']))
|
||||
if rule['hp_name1'] not in hp:
|
||||
raise ValueError("The hp name {} doesn't exist.".format(rule['hp_name1']))
|
||||
if rule['hp_name2'] not in hp:
|
||||
raise ValueError("The hp name {} doesn't exist.".format(rule['hp_name2']))
|
||||
|
||||
if rule['operator'] == ">":
|
||||
check[i] = hp[rule['hp_name1']] > hp[rule['hp_name2']]
|
||||
elif rule['operator'] == "<":
|
||||
check[i] = hp[rule['hp_name1']] < hp[rule['hp_name2']]
|
||||
elif rule['operator'] == ">=":
|
||||
check[i] = hp[rule['hp_name1']] >= hp[rule['hp_name2']]
|
||||
elif rule['operator'] == "<=":
|
||||
check[i] = hp[rule['hp_name1']] <= hp[rule['hp_name2']]
|
||||
|
||||
return np.all(check == True)
|
||||
|
||||
|
||||
def insecure_hash(msg: str) -> str:
|
||||
return hashlib.md5(msg.encode()).hexdigest()
|
||||
|
||||
|
||||
@@ -92,7 +92,14 @@ class Genetics(ABC):
|
||||
|
||||
try:
|
||||
for _ in range(self.cpu_cores):
|
||||
dna = ''.join(choices(self.charset, k=self.solution_len))
|
||||
|
||||
while True:
|
||||
dna = ''.join(choices(self.charset, k=self.solution_len))
|
||||
hp = jh.dna_to_hp(self.options['strategy_hp'], dna)
|
||||
if jh.hp_rules_valid(hp, self.options['hyperparameters_rules']) or len(
|
||||
self.options['hyperparameters_rules']) == 0:
|
||||
break
|
||||
|
||||
w = Process(target=get_fitness, args=(dna, dna_bucket))
|
||||
w.start()
|
||||
workers.append(w)
|
||||
@@ -165,9 +172,16 @@ class Genetics(ABC):
|
||||
self.population = list(sorted(self.population, key=lambda x: x['fitness'], reverse=True))
|
||||
|
||||
def mutate(self, baby):
|
||||
replace_at = randint(0, self.solution_len - 1)
|
||||
replace_with = choice(self.charset)
|
||||
dna = '{}{}{}'.format(baby['dna'][:replace_at], replace_with, baby['dna'][replace_at + 1:])
|
||||
|
||||
while True:
|
||||
replace_at = randint(0, self.solution_len - 1)
|
||||
replace_with = choice(self.charset)
|
||||
dna = '{}{}{}'.format(baby['dna'][:replace_at], replace_with, baby['dna'][replace_at + 1:])
|
||||
hp = jh.dna_to_hp(self.options['strategy_hp'], dna)
|
||||
if jh.hp_rules_valid(hp, self.options['hyperparameters_rules']) or len(
|
||||
self.options['hyperparameters_rules']) == 0:
|
||||
break
|
||||
|
||||
fitness_score, fitness_log = self.fitness(dna)
|
||||
|
||||
return {
|
||||
@@ -182,11 +196,16 @@ class Genetics(ABC):
|
||||
|
||||
dna = ''
|
||||
|
||||
for i in range(self.solution_len):
|
||||
if i % 2 == 0:
|
||||
dna += daddy['dna'][i]
|
||||
else:
|
||||
dna += mommy['dna'][i]
|
||||
while True:
|
||||
for i in range(self.solution_len):
|
||||
if i % 2 == 0:
|
||||
dna += daddy['dna'][i]
|
||||
else:
|
||||
dna += mommy['dna'][i]
|
||||
hp = jh.dna_to_hp(self.options['strategy_hp'], dna)
|
||||
if jh.hp_rules_valid(hp, self.options['hyperparameters_rules']) or len(
|
||||
self.options['hyperparameters_rules']) == 0:
|
||||
break
|
||||
|
||||
fitness_score, fitness_log = self.fitness(dna)
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ class Optimizer(Genetics):
|
||||
self.timeframe = router.routes[0].timeframe
|
||||
StrategyClass = jh.get_strategy_class(self.strategy_name)
|
||||
self.strategy_hp = StrategyClass.hyperparameters(None)
|
||||
self.hyperparameters_rules = StrategyClass.hyperparameters_rules(None)
|
||||
solution_len = len(self.strategy_hp)
|
||||
|
||||
if solution_len == 0:
|
||||
@@ -43,7 +44,9 @@ class Optimizer(Genetics):
|
||||
'strategy_name': self.strategy_name,
|
||||
'exchange': self.exchange,
|
||||
'symbol': self.symbol,
|
||||
'timeframe': self.timeframe
|
||||
'timeframe': self.timeframe,
|
||||
'strategy_hp': self.strategy_hp,
|
||||
'hyperparameters_rules': self.hyperparameters_rules
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -138,6 +138,9 @@ class Strategy(ABC):
|
||||
def hyperparameters(self):
|
||||
return []
|
||||
|
||||
def hyperparameters_rules(self):
|
||||
return []
|
||||
|
||||
def _execute_long(self):
|
||||
self.go_long()
|
||||
|
||||
|
||||
@@ -216,6 +216,17 @@ def test_get_strategy_class():
|
||||
assert issubclass(jh.get_strategy_class("Test01"), Strategy)
|
||||
|
||||
|
||||
def test_hp_rules_valid():
|
||||
hp = {'hp1': 0.08518987341772151, 'hp2': 3, 'hp3': 16, 'hp4': 10}
|
||||
|
||||
rules = [
|
||||
{'hp_name1': 'hp1', 'operator': "<", 'hp_name2': 'hp2'},
|
||||
{'hp_name1': 'hp3', 'operator': ">=", 'hp_name2': 'hp4'},
|
||||
]
|
||||
|
||||
assert jh.hp_rules_valid(hp, rules) == True
|
||||
|
||||
|
||||
def test_insecure_hash():
|
||||
assert jh.insecure_hash("test") == "098f6bcd4621d373cade4e832627b4f6"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user