|
| 1 | +from pinescriptgen_core.strategy import Strategy |
| 2 | +from pinescriptgen_core.conditions import Condition |
| 3 | +from pinescriptgen_core.indicators import IndicatorFactory, Indicator |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class FormHandler: |
| 12 | + def __init__(self, request_form): |
| 13 | + self.request_form = request_form |
| 14 | + self.strategy = Strategy() |
| 15 | + |
| 16 | + def handle_form_input(self): |
| 17 | + for i in range(4): |
| 18 | + # Comprueba si hay valores en longEmaSma y los procesa |
| 19 | + if f'longEmaSma_{i}_indicatorA' in self.request_form: |
| 20 | + self._process_longEmaSma(i) |
| 21 | + # Comprueba si hay valores en longPrice y los procesa |
| 22 | + if "longPrice" in self.request_form: |
| 23 | + self._process_longPrice() |
| 24 | + |
| 25 | + # Comprueba si hay valores en longAdxCci y los procesa |
| 26 | + for i in range(2): # Since you have 2 sets of conditions for longAdxCci |
| 27 | + if f'longAdxCci_{i}_indicator' in self.request_form: |
| 28 | + self._process_longAdxCci(i) |
| 29 | + if 'longDmi_indicatorA' in self.request_form: |
| 30 | + self._process_longDmi() |
| 31 | + |
| 32 | + for i in range(2): |
| 33 | + if f'longRsiMacd_{i}_indicatorA' in self.request_form: |
| 34 | + self._process_longRsiMacd(i) |
| 35 | + |
| 36 | + def _process_longEmaSma(self, index): |
| 37 | + indicatorA_value = self.request_form[f'longEmaSma_{index}_indicatorA'] |
| 38 | + valueA = self.request_form[f'longEmaSma_{index}_valueA'] |
| 39 | + condition_value = self.request_form[f'longEmaSma_{index}_condition'] |
| 40 | + indicatorB_value = self.request_form[f'longEmaSma_{index}_indicatorB'] |
| 41 | + valueB = self.request_form[f'longEmaSma_{index}_valueB'] |
| 42 | + |
| 43 | + # Check if any of the values are empty; if they are, skip this condition |
| 44 | + if not (indicatorA_value and valueA and condition_value and indicatorB_value and valueB): |
| 45 | + return |
| 46 | + |
| 47 | + # Use the factory to create the indicators |
| 48 | + indicator1 = IndicatorFactory.create_indicator(indicatorA_value, valueA) |
| 49 | + indicator2 = IndicatorFactory.create_indicator(indicatorB_value, valueB) |
| 50 | + |
| 51 | + # Create the Condition object |
| 52 | + condition = Condition(condition_value, indicator1, indicator2) |
| 53 | + self.strategy.add_long_condition(condition) |
| 54 | + |
| 55 | + def _process_longPrice(self): |
| 56 | + price = self.request_form.get('longPrice', '') |
| 57 | + condition = self.request_form.get('longPrice_condition', '') |
| 58 | + indicator_value = self.request_form.get('longPrice_indicator', '') |
| 59 | + indicator_value_num = self.request_form.get('longPrice_indicatorValue', '') |
| 60 | + |
| 61 | + # Check if any of the values are empty; if they are, skip this condition |
| 62 | + if not (price and condition and indicator_value and indicator_value_num): |
| 63 | + return |
| 64 | + |
| 65 | + # If all values are non-empty, proceed with creating the Condition object |
| 66 | + if price == 'price': |
| 67 | + price = 'close' |
| 68 | + price_indicator = Indicator(price) |
| 69 | + other_indicator = Indicator(indicator_value, indicator_value_num) |
| 70 | + price_condition = Condition(price_indicator, condition, other_indicator) |
| 71 | + print(price_condition) |
| 72 | + self.strategy.add_long_condition(price_condition) |
| 73 | + |
| 74 | + def _process_longAdxCci(self, index): |
| 75 | + indicator_value = self.request_form.get(f'longAdxCci_{index}_indicator', '') |
| 76 | + condition = self.request_form.get(f'longAdxCci_{index}_condition', '') |
| 77 | + value = self.request_form.get(f'longAdxCci_{index}_value', '') |
| 78 | + |
| 79 | + # Check if any of the values are empty; if they are, skip this condition |
| 80 | + if not (indicator_value and condition and value): |
| 81 | + return |
| 82 | + |
| 83 | + # If all values are non-empty, proceed with creating the Condition object |
| 84 | + if indicator_value == 'adx': |
| 85 | + condition_indicator = Indicator(indicator_value, value) |
| 86 | + adx_cci_condition = Condition(condition_indicator, condition) |
| 87 | + else: |
| 88 | + condition_indicator = Indicator(indicator_value, value) |
| 89 | + adx_cci_condition = Condition(condition_indicator, condition) |
| 90 | + self.strategy.add_long_condition(adx_cci_condition) |
| 91 | + |
| 92 | + def _process_longDmi(self): |
| 93 | + indicatorA_value = self.request_form.get('longDmi_indicatorA', '') |
| 94 | + condition_value = self.request_form.get('longDmi_condition', '') |
| 95 | + indicatorB_value = self.request_form.get('longDmi_indicatorB', '') |
| 96 | + |
| 97 | + # Check if any of the values are empty; if they are, skip this condition |
| 98 | + if not (indicatorA_value and condition_value and indicatorB_value): |
| 99 | + return |
| 100 | + |
| 101 | + # If all values are non-empty, proceed with creating the Condition object |
| 102 | + indicatorA = Indicator(indicatorA_value) |
| 103 | + indicatorB = Indicator(indicatorB_value) |
| 104 | + dmi_condition = Condition(indicatorA, condition_value, indicatorB) |
| 105 | + print(dmi_condition) |
| 106 | + self.strategy.add_long_condition(dmi_condition) |
| 107 | + |
| 108 | + def _process_longRsiMacd(self, index): |
| 109 | + indicatorA_value = self.request_form[f'longRsiMacd_{index}_indicatorA'] |
| 110 | + condition_value = self.request_form[f'longRsiMacd_{index}_condition'] |
| 111 | + indicatorB_value = self.request_form[f'longRsiMacd_{index}_indicatorB'] |
| 112 | + indicatorB_value_num = self.request_form[f'longRsiMacd_{index}_indicatorBValue'] |
| 113 | + |
| 114 | + # Check if any of the values are empty; if they are, skip this condition |
| 115 | + if not (indicatorA_value and condition_value and indicatorB_value and indicatorB_value_num): |
| 116 | + return |
| 117 | + |
| 118 | + # If all values are non-empty, proceed with creating the Condition object |
| 119 | + indicatorA = Indicator(indicatorA_value) |
| 120 | + indicatorB = Indicator(indicatorB_value, indicatorB_value_num) |
| 121 | + rsi_macd_condition = Condition(indicatorA, condition_value, indicatorB) |
| 122 | + print(rsi_macd_condition) |
| 123 | + self.strategy.add_long_condition(rsi_macd_condition) |
0 commit comments