Skip to content

Commit f9f85b5

Browse files
authored
Merge pull request #2 from luisrx7/dev
add Strats and better view of strats results
2 parents b260025 + dbc8844 commit f9f85b5

File tree

6 files changed

+375
-43
lines changed

6 files changed

+375
-43
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,38 @@ To set up and use AviatorStratChecker, follow these simple steps:
4949
```
5050

5151

52-
5. **Execute the script:**
52+
5. **Execute the script to start gathering live results:**
5353
```bash
5454
python main.py
5555
```
5656

5757

58+
59+
60+
## To test Strategies
61+
62+
1. **Edit the file strats/custom_strats.py to add your strategies:**
63+
```python
64+
class ExampleStrat(Strat):
65+
66+
def on_win(self):
67+
super().on_win()
68+
69+
self.bet = self.base_bet * 1.5
70+
71+
def on_lose(self):
72+
super().on_lose()
73+
74+
self.bet = self.base_bet
75+
```
76+
77+
2. **Edit the checker.py script to test your new strat**
78+
79+
3. **Run the checker.py script to test your strat:**
80+
```bash
81+
python checker.py
82+
```
83+
5884
## Contributing
5985
We welcome contributions! If you encounter any issues or have suggestions, please open an issue or submit a pull request.
6086

checker.py

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,96 @@
1-
from strats.custom_strats import MartingaleStrat
21

2+
from strats.custom_strats import MartingaleStrat, AntiMartingaleStrat, DAlembertStrat, ParoliStrat, one_3_2_6Strat
33
from stratchecker.stratchecker import Strat_checker
44

55
import logging
66

7-
logging.basicConfig(level=logging.INFO)
7+
logging.basicConfig(format='%(message)s', level=logging.ERROR, datefmt='%m/%d/%Y %I:%M:%S')
8+
89

910
#set logger format
1011

1112

1213

14+
start_balance=25
15+
base_bet=0.1
16+
max_bet=1
17+
multiplier=2
18+
max_bets=1000
19+
runs_per_strat=5
20+
21+
22+
23+
def read_results(results_filename):
24+
#read file
25+
with open(results_filename) as f:
26+
file_content = f.readlines()
27+
28+
#remove timestamp and newline
29+
results = [float(x.split(",")[1].replace("\n","")) for x in file_content]
30+
#invert list
31+
results.reverse()
32+
33+
return results
34+
35+
36+
1337
def main():
14-
strat = MartingaleStrat("Martingale",
15-
start_balance=25,
16-
base_bet=0.1,
17-
max_bet=1,
18-
multiplier=2,
19-
max_bets=5 )
20-
print(strat.describe())
21-
22-
strat_checker = Strat_checker(strat, "results1.txt")
23-
strat_checker.read_results()
24-
strat_checker.run()
25-
print(strat.balance)
38+
slices = []
39+
strats = []
40+
41+
strats.append(MartingaleStrat("Martingale",
42+
start_balance=start_balance,
43+
base_bet=base_bet,
44+
max_bet=max_bet,
45+
multiplier=multiplier,
46+
max_bets=max_bets ))
47+
48+
49+
strats.append(AntiMartingaleStrat("AntiMartingale",
50+
start_balance=start_balance,
51+
base_bet=base_bet,
52+
max_bet=max_bet,
53+
multiplier=multiplier,
54+
max_bets=max_bets ))
55+
56+
strats.append(DAlembertStrat("DAlembert",
57+
start_balance=start_balance,
58+
base_bet=base_bet,
59+
max_bet=max_bet,
60+
multiplier=multiplier,
61+
max_bets=max_bets ))
62+
63+
strats.append(ParoliStrat("Paroli",
64+
start_balance=start_balance,
65+
base_bet=base_bet,
66+
max_bet=max_bet,
67+
multiplier=multiplier,
68+
max_bets=max_bets ))
69+
strats.append(one_3_2_6Strat("one_3_2_6",
70+
start_balance=start_balance,
71+
base_bet=base_bet,
72+
max_bet=max_bet,
73+
multiplier=multiplier,
74+
max_bets=max_bets ))
75+
76+
77+
results = read_results("results.txt")
78+
slices = Strat_checker.slice_results(count=5,max_bets=max_bets,results_length=len(results))
79+
80+
strat_runners = []
81+
for strat in strats:
82+
strat_runners.append(Strat_checker(strat, results, slices=slices))
83+
84+
85+
for strat_runner in strat_runners:
86+
strat_runner.run(count=runs_per_strat)
87+
88+
89+
90+
for strat_runner in strat_runners:
91+
strat_runner.report()
92+
93+
2694

2795

2896

stratchecker/stratchecker.py

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,106 @@
11
from strats.strats import Strat
2+
from strats.exceptions import MaxBetsReached, InsufficientFunds
23

34
import logging
45

6+
import random
7+
from rich import print
58

69
logger = logging.getLogger(__name__)
710

811

912

1013
class Strat_checker():
11-
def __init__(self, strat:Strat, results_filename:str) -> None:
14+
def __init__(self, strat:Strat, results, slices=[]) -> None:
1215
self.strat = strat
13-
self.results_filename = results_filename
14-
15-
16-
def read_results(self):
17-
#read file
18-
with open(self.results_filename) as f:
19-
file_content = f.readlines()
20-
21-
#remove timestamp and newline
22-
self.results = [float(x.split(",")[1].replace("\n","")) for x in file_content]
23-
#invert list
24-
self.results.reverse()
25-
16+
self.slices = slices
17+
self.strat_reports = []
18+
self.results = results
2619

2720

21+
2822

23+
@staticmethod
24+
def slice_results(count,max_bets,results_length):
25+
26+
'''
27+
slice results in a random place making sure that
28+
the max_length of the slice is not exceeded
29+
return the slice indexes
30+
'''
31+
slices = []
32+
for i in range(count):
33+
#get random start index
34+
start_index = random.randint(0,results_length-max_bets)
35+
36+
#get random end index
37+
end_index = start_index+max_bets
38+
39+
slices.append((start_index,end_index))
40+
41+
return slices
42+
43+
44+
45+
46+
47+
2948

30-
def run(self):
31-
self.strat.on_game_start()
49+
def report(self):
50+
print(f"\nStrategy: {self.strat.description}")
51+
for report in self.strat_reports:
52+
print(report)
3253

33-
for result in self.results:
34-
self.strat.gamble()
35-
self.strat.calculate_bet(result)
54+
55+
def get_slices(self):
56+
return self.slices
57+
58+
def set_slices(self,slices):
59+
self.slices = slices
60+
3661

62+
def run(self,count:int=1,random_slice:bool=True):
63+
'''
64+
run the strat count times
65+
if random_slice is True, slice the results in a random places
66+
count must be greater than 1 if random_slice is True
67+
'''
68+
69+
70+
71+
if random_slice is True and count == 1:
72+
logging.warning("random_slice is True and count is 1, setting random_slice to False")
73+
random_slice = False
74+
75+
if self.strat.max_bets > len(self.results):
76+
logging.warning(f"max bets {self.strat.max_bets} is greater than the number of results {len(self.results)}, setting max bets to {len(self.results)}")
77+
self.strat.max_bets = len(self.results)
78+
79+
80+
if len(self.slices) == 0:
81+
#if no slices are defined, generate count slices
82+
self.slices = self.slice_results(self.strat.max_bets,count)
83+
84+
85+
for i in range(count):
86+
87+
self.strat.reset()
88+
try:
89+
for result in self.results[self.slices[i][0]:self.slices[i][1]]:
90+
self.strat.gamble()
91+
self.strat.calculate_bet(result)
92+
93+
except MaxBetsReached or InsufficientFunds:
94+
pass
3795

96+
except Exception as e:
97+
logging.error(e)
98+
99+
finally:
100+
self.strat_reports.append(self.strat.report() + f"slice: {self.slices[i]}")
101+
102+
103+
38104

39105

40106

0 commit comments

Comments
 (0)