Parametre.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from Model.Donnee import *
  2. from Model.profil import profile
  3. from Config.paths import SysExploite
  4. import datetime as dt
  5. import json
  6. import hashlib
  7. import os
  8. import uuid
  9. import random
  10. ProbabiliteMutationNombre = 0.5
  11. ProbabiliteMutationReste = 0.5
  12. class CL_Param:
  13. def __init__(self):
  14. self.PortefeuilleDemarage = 50000.0
  15. self.NBAction = NombreActionFichier()
  16. """
  17. L'horizon depent directement du nombre de jour de recul la regle epirique est : lookback ≈ 3× à 10× horizon
  18. | Fast | a testé | Optimal | Robuste |
  19. NBjourRecul | 5 | 32 jours | 64 | 100 |
  20. horizon | 1 | 5 jours | 10 | 15 |
  21. """
  22. self.NbJourRecul = 32
  23. self.Horizon = 5
  24. self.Analysis = ["Close","others_dr","trend_sma_fast","trend_sma_slow","momentum_rsi","volatility_atr","volatility_bbw","volume_obv"]
  25. self.NbAnalysis = len(self.Analysis)
  26. self.poidsAchat = [0.25, 0.20, 0.15, 0.10, 0.05]
  27. self.NBachat = len(self.poidsAchat)
  28. self.ValVente = 0
  29. if SysExploite == "windows":
  30. self.epochs = 2
  31. else :
  32. self.epochs = 20000
  33. self.batch_size = 128
  34. self.LearningRate = 1e-3
  35. # Groupes pour éviter les features corrélés
  36. self.groups = {
  37. # ---- MACD ----
  38. "macd": {
  39. "trend_macd": 5,
  40. "trend_macd_signal": 5,
  41. "trend_macd_diff": 5
  42. },
  43. # ---- PPO ----
  44. "ppo": {
  45. "momentum_ppo": 5,
  46. "momentum_ppo_signal": 5,
  47. "momentum_ppo_hist": 5
  48. },
  49. # ---- Stoch RSI ----
  50. "stoch_rsi": {
  51. "momentum_stoch_rsi": 5,
  52. "momentum_stoch_rsi_k": 5,
  53. "momentum_stoch_rsi_d": 5
  54. },
  55. # ---- Stochastic ----
  56. "stoch": {
  57. "momentum_stoch": 3,
  58. "momentum_stoch_signal": 3
  59. },
  60. # ---- ATR / Volatility ----
  61. "atr": {
  62. "volatility_atr": 5,
  63. "volatility_ui": 1
  64. },
  65. # ---- Bollinger ----
  66. "bollinger": {
  67. "volatility_bbm": 5,
  68. "volatility_bbh": 5,
  69. "volatility_bbl": 5,
  70. "volatility_bbw": 5,
  71. "volatility_bbp": 3,
  72. "volatility_bbhi": 3,
  73. "volatility_bbli": 3
  74. },
  75. # ---- Keltner ----
  76. "keltner": {
  77. "volatility_kcc": 5,
  78. "volatility_kch": 5,
  79. "volatility_kcl": 5,
  80. "volatility_kcw": 3,
  81. "volatility_kcp": 3,
  82. "volatility_kchi": 3,
  83. "volatility_kcli": 3
  84. },
  85. # ---- Donchian ----
  86. "donchian": {
  87. "volatility_dcl": 3,
  88. "volatility_dch": 5,
  89. "volatility_dcm": 3,
  90. "volatility_dcw": 3,
  91. "volatility_dcp": 3
  92. },
  93. # ---- SMA / EMA ----
  94. "moving_avg": {
  95. "trend_sma_fast": 3,
  96. "trend_sma_slow": 3,
  97. "trend_ema_fast": 3,
  98. "trend_ema_slow": 3
  99. },
  100. # ---- ADX ----
  101. "adx": {
  102. "trend_adx": 5,
  103. "trend_adx_pos": 3,
  104. "trend_adx_neg": 3
  105. },
  106. # ---- Vortex ----
  107. "vortex": {
  108. "trend_vortex_ind_pos": 3,
  109. "trend_vortex_ind_neg": 3,
  110. "trend_vortex_ind_diff": 5
  111. },
  112. # ---- Aroon ----
  113. "aroon": {
  114. "trend_aroon_up": 3,
  115. "trend_aroon_down": 3,
  116. "trend_aroon_ind": 5
  117. },
  118. # ---- Ichimoku ----
  119. "ichimoku": {
  120. "trend_ichimoku_conv": 3,
  121. "trend_ichimoku_base": 3,
  122. "trend_ichimoku_a": 3,
  123. "trend_ichimoku_b": 3,
  124. "trend_stc": 3
  125. },
  126. # ---- Volume indicators ----
  127. "volume": {
  128. "volume_obv": 5,
  129. "volume_mfi": 5,
  130. "volume_cmf": 5,
  131. "volume_vpt": 3,
  132. "volume_vwap": 3,
  133. "volume_adi": 3,
  134. "volume_fi": 1,
  135. "volume_em": 1,
  136. "volume_nvi": 1
  137. },
  138. # ---- Momentum divers ----
  139. "momentum_other": {
  140. "momentum_rsi": 5,
  141. "momentum_wr": 3,
  142. "momentum_uo": 3,
  143. "momentum_ao": 3,
  144. "momentum_kama": 3,
  145. "momentum_tsi": 3
  146. },
  147. # ---- Trend divers ----
  148. "trend_other": {
  149. "trend_trix": 3,
  150. "trend_mass_index": 3,
  151. "trend_kst": 3,
  152. "trend_kst_sig": 3,
  153. "trend_kst_diff": 3,
  154. "trend_dpo": 3,
  155. "trend_psar_up_indicator": 5,
  156. "trend_psar_down_indicator": 5
  157. },
  158. # ---- Faible pertinence ----
  159. "low_value": {
  160. "others_dr": 1,
  161. "others_dlr": 1
  162. }
  163. }
  164. @profile
  165. def StockParam(self, NomDuModel):
  166. #Sauvegarde les paramètres dans un fichier .param
  167. data = {
  168. "NbJourRecul": self.NbJourRecul,
  169. "Horizon":self.Horizon,
  170. "NBAction": self.NBAction,
  171. "Analysis": self.Analysis,
  172. "NbParam": self.NbAnalysis,
  173. "PortefeuilleDemarage": self.PortefeuilleDemarage,
  174. "poidsAchat":self.poidsAchat,
  175. "ValVente": self.ValVente,
  176. "epochs":self.epochs,
  177. "batch_size":self.batch_size,
  178. "LearningRate":self.LearningRate,
  179. }
  180. with open(f"{NomDuModel}.param", "w") as f:
  181. json.dump(data, f, indent=4)
  182. @profile
  183. def RestaureParam(self, NomDuModel):
  184. #Restaure les paramètres depuis un fichier .param
  185. NomDuModel = NomDuModel + ".param"
  186. if not os.path.exists(NomDuModel):
  187. raise FileNotFoundError(f"Le fichier {NomDuModel}.param est introuvable.")
  188. with open(NomDuModel, "r") as f:
  189. data = json.load(f)
  190. self.NbJourRecul = data.get("NbJourRecul", self.NbJourRecul)
  191. self.Horizon = data.get("Horizon", self.Horizon)
  192. self.NBAction = data.get("NBAction", self.NBAction)
  193. self.Analysis = data.get("Analysis", self.Analysis)
  194. self.NbAnalysis = data.get("NbParam", len(self.Analysis))
  195. self.PortefeuilleDemarage = data.get("PortefeuilleDemarage", self.PortefeuilleDemarage)
  196. self.poidsAchat = data.get("poidsAchat", self.poidsAchat)
  197. self.ValVente = data.get("ValVente", self.ValVente)
  198. self.epochs = data.get("",self.epochs)
  199. self.batch_size =data.get("",self.batch_size)
  200. self.LearningRate =data.get("",self.LearningRate)
  201. def UUIDData(self, Demmarage, NBNJour):
  202. data = {
  203. "Demmarage": Demmarage,
  204. "NBJour": NBNJour,
  205. "NbJourRecul": self.NbJourRecul,
  206. "Horizon": self.Horizon,
  207. "NBAction": self.NBAction,
  208. "Analysis": self.Analysis
  209. }
  210. # sérialisation stable (ordre garanti)
  211. payload = json.dumps(data, sort_keys=True).encode("utf-8")
  212. # hash
  213. h = hashlib.blake2s(payload, digest_size=8).hexdigest() # 16 chars
  214. return h
  215. def UUIDRAM(self, date_debut, nb_jours):
  216. data = {
  217. "Demmarage": date_debut,
  218. "NBJour": nb_jours,
  219. "NbJourRecul": self.NbJourRecul
  220. }
  221. # sérialisation stable (ordre garanti)
  222. payload = json.dumps(data, sort_keys=True).encode("utf-8")
  223. # hash
  224. h = hashlib.blake2s(payload, digest_size=8).hexdigest() # 16 chars
  225. return h