Coverage for django_napse/core/models/bots/implementations/dca/strategy.py: 96%

27 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1from django.db import models 

2 

3from django_napse.core.models.bots.architectures.single_pair import SinglePairArchitecture 

4from django_napse.core.models.bots.implementations.dca.config import DCABotConfig 

5from django_napse.core.models.bots.strategy import Strategy 

6from django_napse.utils.constants import SIDES 

7 

8 

9class DCAStrategy(Strategy): 

10 variable_last_buy_date = models.DateTimeField(null=True, blank=True) 

11 

12 def __str__(self) -> str: 

13 return f"DCA BOT STRATEGY: {self.pk=}" 

14 

15 def info(self, verbose=True, beacon=""): 

16 string = "" 

17 string += f"{beacon}Strategy ({self.pk=}):\n" 

18 string += f"{beacon}Args:\n" 

19 string += f"{beacon}\t{self.config=}\n" 

20 string += f"{beacon}\t{self.architecture=}\n" 

21 if verbose: # pragma: no cover 

22 print(string) 

23 return string 

24 

25 @classmethod 

26 def config_class(cls): 

27 return DCABotConfig 

28 

29 @classmethod 

30 def architecture_class(cls): 

31 return SinglePairArchitecture 

32 

33 def give_order(self, data: dict) -> list[dict]: 

34 controller = data["controllers"]["main"] 

35 if ( 

36 self.variable_last_buy_date is None 

37 or data["candles"][controller]["current"]["open_time"] - self.variable_last_buy_date >= data["config"]["timeframe"] 

38 ): 

39 return [ 

40 { 

41 "controller": controller, 

42 "ArchitectureModifications": [], 

43 "StrategyModifications": [ 

44 { 

45 "key": "last_buy_date", 

46 "value": str(data["candles"][controller]["current"]["open_time"]), 

47 "target_type": "datetime", 

48 "ignore_failed_order": False, 

49 }, 

50 ], 

51 "ConnectionModifications": [], 

52 "connection": data["connection"], 

53 "asked_for_amount": 20, 

54 "asked_for_ticker": controller.quote, 

55 "pair": controller.pair, 

56 "price": data["candles"][controller]["latest"]["close"], 

57 "side": SIDES.BUY, 

58 }, 

59 ] 

60 return [ 

61 { 

62 "controller": controller, 

63 "ArchitectureModifications": [], 

64 "StrategyModifications": [], 

65 "ConnectionModifications": [], 

66 "connection": data["connection"], 

67 "asked_for_amount": 0, 

68 "asked_for_ticker": controller.quote, 

69 "pair": controller.pair, 

70 "price": data["candles"][controller]["latest"]["close"], 

71 "side": SIDES.KEEP, 

72 }, 

73 ]