Coverage for source/agent/strategies/performance_testing_strategy_handler.py: 21%

43 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-06-06 12:00 +0000

1# agent/strategies/performance_testing_strategy_handler.py 

2 

3# global imports 

4from typing import Any 

5import random 

6import numpy as np 

7 

8# local imports 

9from source.environment import TradingEnvironment 

10from source.agent import TestingStrategyHandlerBase 

11from source.agent import PerformanceTestable 

12 

13class PerformanceTestingStrategyHandler(TestingStrategyHandlerBase): 

14 """""" 

15 

16 PLOTTING_KEY: str = 'performance_testing' 

17 

18 def evaluate(self, testable_agent: PerformanceTestable, environment: TradingEnvironment) -> \ 

19 tuple[list[str], list[dict[str, Any]]]: 

20 """""" 

21 

22 history = {} 

23 assets_values = [] 

24 reward_values = [] 

25 infos = [] 

26 iterations = [] 

27 done = False 

28 

29 state = environment.state 

30 trading_data = environment.get_trading_data() 

31 current_assets = trading_data.current_budget + trading_data.currently_invested 

32 iterations.append(current_iteration) 

33 assets_values.append(current_assets) 

34 reward_values.append(0) 

35 infos.append({}) 

36 

37 while(not done): 

38 next_action = testable_agent.perform(state) 

39 state, reward, done, info = environment.step(next_action) 

40 

41 if current_assets != info['current_budget'] + info['currently_invested'] or done: 

42 current_iteration = environment.current_iteration 

43 current_assets = info['current_budget'] + info['currently_invested'] 

44 iterations.append(current_iteration) 

45 assets_values.append(current_assets) 

46 reward_values.append(reward) 

47 infos.append(info) 

48 

49 solvency_coefficient = (assets_values[-1] - assets_values[0]) / (iterations[-1] - iterations[0]) 

50 assets_values = (np.array(assets_values) / assets_values[0]).tolist() 

51 currency_prices = environment.get_data_for_iteration(['close'], iterations[0], iterations[-1]) 

52 currency_prices = (np.array(currency_prices) / currency_prices[0]).tolist() 

53 

54 history['assets_values'] = assets_values 

55 history['reward_values'] = reward_values 

56 history['currency_prices'] = currency_prices 

57 history['infos'] = infos 

58 history['iterations'] = iterations 

59 history['solvency_coefficient'] = solvency_coefficient 

60 

61 return PerformanceTestingStrategyHandler.PLOTTING_KEY, history