Skip to content

Commit 4fb8c4f

Browse files
committed
First Commit
0 parents  commit 4fb8c4f

25 files changed

+1562
-0
lines changed

.gitignore

Whitespace-only changes.

.idea/.gitignore

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/mi_proyecto.iml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Whitespace-only changes.

__init__.py

Whitespace-only changes.

main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pinescriptgen_core.flask_app import app
2+
3+
# Punto de entrada principal para la app.
4+
# No se debe iniciar la app desde ningun otro archivo que no sea este
5+
6+
7+
if __name__ == '__main__':
8+
app.run(debug=True, port=251)

pinescript_reference

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//@version=5
2+
strategy("Optimizer v1", overlay=true)
3+
4+
// Conditions LONG EMA, SMA, PRICE
5+
ema70 = ta.ema(close, 70)
6+
ema100 = ta.ema(close, 100)
7+
8+
cond_EMA1 = 0
9+
if ema70 > ema100 and ta.crossover(close, ema70)
10+
cond_EMA1 := 1
11+
12+
// Conditions LONG PRICE
13+
sma70 = ta.sma(close, 70)
14+
cond_EMA2 = 0
15+
if close > sma70
16+
cond_EMA2 := 1
17+
18+
// Conditions LONG ADX
19+
adxlen = 14
20+
dilen = 14
21+
dirmov(len) =>
22+
up = ta.change(high)
23+
down = -ta.change(low)
24+
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
25+
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
26+
truerange = ta.rma(ta.tr, len)
27+
plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
28+
minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
29+
[plus, minus]
30+
adx(dilen, adxlen) =>
31+
[plus, minus] = dirmov(dilen)
32+
sum = plus + minus
33+
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
34+
adx
35+
sig = adx(dilen, adxlen)
36+
cond_dmi = 0
37+
if sig > 15
38+
cond_dmi := 1
39+
//Conditions LONG CCI
40+
41+
cci = ta.cci(close, 14) /// CCI LENGTH AGREGAR AL CODIGO
42+
cond_cci = 0
43+
if cci > 30
44+
cond_cci := 1
45+
46+
// Conditions LONG DMI, MACD , RSI'
47+
len = 14
48+
up = ta.change(high)
49+
down = -ta.change(low)
50+
truerange = ta.rma(ta.tr, len)
51+
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
52+
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
53+
54+
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
55+
cond_macd = 0
56+
if histLine > 0
57+
cond_macd := 1
58+
59+
60+
if cond_EMA1 and cond_EMA2 and cond_dmi and cond_cci and cond_macd
61+
strategy.entry("Long", strategy.long)
62+
63+
// B3 🟡Take Profit
64+
tp1_lot = 25 * strategy.position_size * 0.01
65+
tp2_lot = 25 * strategy.position_size * 0.01
66+
tp3_lot = 100 * strategy.position_size * 0.01
67+
stop_loss_price = 0.0
68+
tp1 = 0.0
69+
tp2 = 0.0
70+
tp3 = 0.0
71+
if strategy.position_size > 0// B4 🟡Stop Loss %
72+
stop_loss_percent = 3
73+
stop_loss_price := strategy.position_avg_price * 0.97 //(1 - stop_loss_percent / 100)
74+
tp1 := strategy.position_avg_price * (1.01)
75+
tp2 := strategy.position_avg_price * (1.02)
76+
tp3 := strategy.position_avg_price * (1.03)
77+
// strategy.exit("Stop Loss", "Long", stop=stop_loss_price)
78+
strategy.exit("TP1", "Long", qty=tp1_lot, limit=tp1, stop=stop_loss_price)
79+
strategy.exit("TP2", "Long", qty=tp2_lot, limit=tp2, stop=stop_loss_price)
80+
strategy.exit("TP3", "Long", qty=tp3_lot, limit=tp3, stop=stop_loss_price)
81+
82+
83+
84+
85+
86+
plot(stop_loss_price ? stop_loss_price : na, "stop_loss_price", color = color.red, style = plot.style_circles)
87+
88+
plot(tp1 ? tp1 : na, "tp1", color = color.green, style = plot.style_circles)
89+
plot(tp2 ? tp2 : na, "tp2", color = color.green, style = plot.style_circles)
90+
plot(tp3 ? tp3 : na, "tp3", color = color.green, style = plot.style_circles)

pinescriptgen_core/__init__.py

Whitespace-only changes.

pinescriptgen_core/conditions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from abc import ABC
2+
from dataclasses import dataclass
3+
from typing import Optional
4+
import logging
5+
from pinescriptgen_core.indicators import AbstractIndicator
6+
7+
logger = logging.getLogger(__name__)
8+
9+
10+
@dataclass
11+
class AbstractCondition:
12+
""" Abstract Base Class para Condition"""
13+
14+
15+
@dataclass
16+
class Condition(AbstractCondition):
17+
"""Concrete Condition"""
18+
condition: Optional[str]
19+
indicatorA: Optional[AbstractIndicator] = None
20+
indicatorB: Optional[AbstractIndicator] = None

pinescriptgen_core/flask_app.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from flask import Flask, request, render_template
2+
from pinescriptgen_core.pinescriptgenerator import PineScriptGenerator
3+
from pinescriptgen_core.formhandler import FormHandler
4+
import logging
5+
6+
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(levelname)s: %(message)s')
7+
8+
app = Flask(__name__)
9+
10+
11+
@app.route('/', methods=['GET', 'POST'])
12+
def index():
13+
if request.method == 'POST':
14+
form_handler = FormHandler(request.form)
15+
form_handler.handle_form_input()
16+
strategy = form_handler.strategy
17+
18+
pinescript_code = PineScriptGenerator.generate(strategy)
19+
20+
return render_template('generated.html', pine_code=pinescript_code)
21+
22+
return render_template('index.html')
23+
24+

pinescriptgen_core/formhandler.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)