-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleEmaStrategy.pine
More file actions
188 lines (155 loc) · 8.64 KB
/
simpleEmaStrategy.pine
File metadata and controls
188 lines (155 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CiclicaTrading - 2023 Strambatax (Alessandro Arrabito)
// https://www.youtube.com/watch?v=g7TNcMseFYE
// ████████ ██ ██
// ██░██░░░ ██ ██
// ████████░ ██
// ██ ██ ██ ██
// ████████ ██ ██
//@version=5
strategy("Kondor_Ema", overlay=true, default_qty_type=strategy.percent_of_equity, initial_capital = 1000,
default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.061)
// Campi di input nella scheda dei settting
int emaFastLen = input.int(7, "Ema Fast", step = 1, group = "Ma settings")
int emaSlowLen = input.int(25, "Ema Slow", step = 1, group = "Ma settings")
int trendMaLen = input.int(200, "Trend Ma", step = 1, group = "Ma settings")
// offset del trailing stop espresso in percentuale
bool enableTrailStopOnTp = input.bool(false, "Enable Trail stop on take profit", group = "strategy settings")
float trailStopOffsetPrc = input.float(1.0, "Trail offset Prc", step=0.1, group = "strategy settings") / 100
bool enableTrailStopLossAtr = input.bool(false, "Trail stop loss by Atr", group = "strategy settings")
// risk reward 1:X quindi si imposta X cioè il valore di rendimento.
// default 1.0 => rischio = rendimento, se imporsto 2.0 => rendimento doppio rispetto al rischio.
bool enablePartialTp = input.bool(false, "Enable partial take profit", group = "strategy settings")
float riskReward = input.float(1.5, "Risk reward", step=0.1, group = "strategy settings")
bool breakEvenEnabled = input.bool(true, "Break Even Enabled", tooltip = "if enabled move stop to breakevent if price reach partial take profit", group = "strategy settings")
// periodo atr
int atrLen = input.int(11, "atr len", group = "atr settings")
// moltiplicatore per definire il livello di stop loss
float atrMultiplier = input.float(3.0, "atr Multiplier", step=0.1, group = "atr settings")
bool atrDraw = input.bool(false, "atr band draw", group = "atr settings")
string botLongEntryCmd = input.text_area("", "wundertrading entry long command", group = "bot")
string botLongExitCmd = input.text_area("", "wundertrading exit long command", group = "bot")
string botShortEntryCmd = input.text_area("", "wundertrading entry short command", group = "bot")
string botShortExitCmd = input.text_area("", "wundertrading exit short command", group = "bot")
// Calcolo delle medie mobili esponenziali
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
trendMa = ta.ema(close, trendMaLen)
// Funzioni grafiche per disegnare le medie mobili
plot(emaFast, color = color.yellow, linewidth = 2)
plot(emaSlow, color = color.blue, linewidth = 2)
plot(trendMa, color = color.gray, linewidth = 2)
// Condizioni di ingresso
bool emaCrossLong = ta.crossover(emaFast, emaSlow)
bool emaCrossShort = ta.crossunder(emaFast, emaSlow)
bool entryLong = emaCrossLong and emaSlow > trendMa
bool entryShort = emaCrossShort and emaSlow < trendMa
//// Variabili che mantengono i livelli del trade in corso.
var float stopLossLevel = na
var float takeProfitLevel = na
var float partialTakeProfitLevel = na
var float entryLevel = na
var float beLevel = na
// Grafica: disegno dei livelli del trade tramite funzioni plot e fill.
entryLine = plot(entryLevel, color=color.yellow, style = plot.style_linebr)
pTpLine = plot(partialTakeProfitLevel, color=color.fuchsia, style = plot.style_linebr, linewidth = 2)
tpLine = plot(takeProfitLevel, color=color.green, style = plot.style_linebr)
slLine = plot(stopLossLevel, color=color.red, style = plot.style_linebr)
fill(entryLine, tpLine, color.new(color.green, 50))
fill(entryLine, slLine, color.new(color.red, 50))
float atr = ta.atr(atrLen)
float atrOffset = atr*atrMultiplier
plot(atrDraw ? close + atrOffset : na)
plot(atrDraw ? close - atrOffset : na)
getTickValue() =>
syminfo.mintick * syminfo.pointvalue
trailStopOffset = close * trailStopOffsetPrc / getTickValue()
// Questa funzione aggiorna i livelli di uscita con trail e senza trail.
updateExits(direction) =>
if direction == "long"
if enableTrailStopOnTp
strategy.exit("long_exit_id", "long_id", stop = stopLossLevel, trail_price = takeProfitLevel, trail_offset = trailStopOffset)
else
strategy.exit("long_exit_id", "long_id", stop = stopLossLevel, limit = takeProfitLevel, alert_message = botLongExitCmd)
else if direction == "short"
if enableTrailStopOnTp
strategy.exit("short_exit_id", "short_id", stop = stopLossLevel, trail_price = takeProfitLevel, trail_offset = trailStopOffset)
else
strategy.exit("short_exit_id", "short_id", stop = stopLossLevel, limit = takeProfitLevel, alert_message = botShortExitCmd)
// blocco di apertura dei trade long e short
// se si verificano le condizioni.
if entryLong and strategy.position_size == 0
entryLevel := close
// stop loss proporzionale al livello di atr
stopLossLevel := close - atrOffset
takeProfitLevel := close + atrOffset*riskReward
partialTakeProfitLevel := enablePartialTp ? close + atrOffset*riskReward/2 : na
beLevel := close + atrOffset*riskReward/2
strategy.entry("long_id", strategy.long, alert_message = botLongEntryCmd)
if not na(partialTakeProfitLevel)
strategy.exit("p1_long_exit_id", "long_id", stop = stopLossLevel, limit = partialTakeProfitLevel, qty_percent = 50)
updateExits("long")
if entryShort and strategy.position_size == 0
entryLevel := close
stopLossLevel := close + atrOffset
takeProfitLevel := close - atrOffset*riskReward
partialTakeProfitLevel := enablePartialTp ? close - atrOffset*riskReward/2 : na
beLevel := close - atrOffset*riskReward/2
strategy.entry("short_id", strategy.short, alert_message = botShortEntryCmd)
if not na(partialTakeProfitLevel)
strategy.exit("p1_short_exit_id", "short_id", stop = stopLossLevel, limit = partialTakeProfitLevel, qty_percent = 50)
updateExits("short")
// blocco per abilitira la chiusura a breakeven
if breakEvenEnabled and strategy.position_size > 0 and high > beLevel and barstate.isconfirmed
stopLossLevel := entryLevel
updateExits("long")
if breakEvenEnabled and strategy.position_size < 0 and low < beLevel and barstate.isconfirmed
stopLossLevel := entryLevel
updateExits("short")
// trailing stop loss atr
if strategy.position_size > 0 and barstate.isconfirmed and enableTrailStopLossAtr
//if atrOffset > stopLossLevel
if close - atrOffset > stopLossLevel
stopLossLevel := close - atrOffset
if not na(partialTakeProfitLevel)
strategy.exit("p1_long_exit_id", "long_id", stop = stopLossLevel, limit = partialTakeProfitLevel, qty_percent = 50)
updateExits("long")
if strategy.position_size < 0 and barstate.isconfirmed and enableTrailStopLossAtr
//if atrOffset < stopLossLevel
if close + atrOffset < stopLossLevel
stopLossLevel := close + atrOffset
if not na(partialTakeProfitLevel)
strategy.exit("p1_short_exit_id", "short_id", stop = stopLossLevel, limit = partialTakeProfitLevel, qty_percent = 50)
updateExits("short")
// Reset delle variabili in chiusura del trade.
// Come accennato nel video, il seguente snippet di codice non gestisce il caso di un trade che
// inizia e finisce durante la stessa candela. Per questo lascio nel commento la soluzione.
// condizioneDiUscita = strategy.position_size == 0 and strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index
if strategy.position_size == 0 and strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index
stopLossLevel := na
takeProfitLevel := na
partialTakeProfitLevel := na
entryLevel := na
beLevel := na
var float totProfit = 0
if ta.change(strategy.closedtrades)
tradeIndex = strategy.closedtrades -1
tradeId = strategy.closedtrades.exit_id(tradeIndex)
if tradeId == "long_exit_id" or tradeId == "short_exit_id"
p2 = strategy.closedtrades.profit(tradeIndex)
p1 = strategy.closedtrades.profit(tradeIndex - 1)
label.new(bar_index, close, str.format("{0, number, #.##}", p1+p2), style = label.style_label_lower_left, color = p1+p2 > 0 ? color.green : color.red)
totProfit += p1 + p2
// tabella riepilogo dati strategia
var tableInfo = table.new(position.top_right, 2,2)
tableInfo.set_bgcolor(color.white)
table.cell(tableInfo, 0, 0, "Info")
table.cell(tableInfo, 0, 1, "Profit")
table.cell(tableInfo, 1, 1, str.format("{0, number, #.##}", totProfit))
// Settings
// FTMUSDT TF 1H
// ema 7, 23, 200
// enable partial tp
// RR 1,6
// enable breakeven
// ATR len 3 mul 3.1