Simple PineScript v5 strategy (revision and correction) -- 2
Budget: €30 – €250 EUR
I am trying to create a simple strategy (buying or selling when a defined 2 bar candlestick pattern is detected), using as entry price the middle point of the candestick pattern, as Stop Loss the lowest point of the pattern (or the highest point when going short) and as Take Profit the highest point of the candlestick pattern (or the lowest point when going short).
With the particularity of having to cancel not filled orders if the order is not filled 3 bars after the order is created.
And finally the Take Profit has to trail the same distance (as from the entry price to the activation point), in order to take maximum profit in the winning cases when the price goes in the direction of the trade (trail in levels, if level is reached trail to the next, if reverses then exit in the highest level reached).
The code to be corrected to work properly:
//@version=5
strategy(title = 'Simple Candlestick Pattern Strategy with SL & Trailing TP',
shorttitle = 'Simple Candlestick Pattern Strategy',
overlay = true,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 100,
initial_capital = 1000,
commission_type=strategy.commission.percent,
commission_value=0.03)
// Candlestick Pattern
bullish_pattern = open[0] < close[0] and open[1] > close[1] and low[0] > low[1] and high[0] > high[1]
bearish_pattern = open[0] > close[0] and open[1] < close[1] and low[0] < low[1] and high[0] < high[1]
// Plotting Candlestick Pattern
plotshape(bullish_pattern,text='', style=shape.arrowup, location=location.belowbar, color=color.lime, textcolor=color.lime, title = 'Pattern')
plotshape(bearish_pattern,text='', style=shape.arrowdown, location=location.abovebar, color=color.red, textcolor=color.red, title = 'Pattern')
// Entry Variables
bool openLongPosition = bullish_pattern
bool openShortPosition = bearish_pattern
var float lowest = low
if low[1] < low[0]
lowest := low[1] // calculates the lowest point in the candlestick pattern
var float highest = high
if high[1] > high[0]
highest := high[1] // calculates the highest point in the candlestick pattern
float midrange = (highest-lowest)/2 // the middle point in the candlestick pattern
// Entry Orders
strategy.entry('Long', strategy.long, stop = math.avg(lowest, highest), limit = math.avg(lowest, highest) + (1 * syminfo.mintick) , when = openLongPosition)
strategy.entry('Short', strategy.short, stop = math.avg(lowest, highest), limit = math.avg(lowest, highest) - (1 * syminfo.mintick), when = openShortPosition)
// Cancel Not Executed Orders after 3 bars >>> Note: I think this is not working well
strategy.cancel('Long', when = ta.barssince(openLongPosition) > 3 and strategy.position_size == 0)
strategy.cancel('Short', when = ta.barssince(openShortPosition) > 3 and not strategy.position_size == 0)
// Stop Loss
strategy.exit('Long SL', from_entry = 'Long', stop = lowest)
strategy.exit('Short SL', from_entry = 'Short', stop = highest)
// Trailing Take Profit
strategy.exit('Long TP', from_entry = 'Long', trail_price = highest, trail_offset = strategy.position_avg_price + (math.avg(lowest, highest)/2))
strategy.exit('Short TP', from_entry = 'Short', trail_price = lowest, trail_offset = strategy.position_avg_price - (math.avg(lowest, highest)/2))
With the particularity of having to cancel not filled orders if the order is not filled 3 bars after the order is created.
And finally the Take Profit has to trail the same distance (as from the entry price to the activation point), in order to take maximum profit in the winning cases when the price goes in the direction of the trade (trail in levels, if level is reached trail to the next, if reverses then exit in the highest level reached).
The code to be corrected to work properly:
//@version=5
strategy(title = 'Simple Candlestick Pattern Strategy with SL & Trailing TP',
shorttitle = 'Simple Candlestick Pattern Strategy',
overlay = true,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 100,
initial_capital = 1000,
commission_type=strategy.commission.percent,
commission_value=0.03)
// Candlestick Pattern
bullish_pattern = open[0] < close[0] and open[1] > close[1] and low[0] > low[1] and high[0] > high[1]
bearish_pattern = open[0] > close[0] and open[1] < close[1] and low[0] < low[1] and high[0] < high[1]
// Plotting Candlestick Pattern
plotshape(bullish_pattern,text='', style=shape.arrowup, location=location.belowbar, color=color.lime, textcolor=color.lime, title = 'Pattern')
plotshape(bearish_pattern,text='', style=shape.arrowdown, location=location.abovebar, color=color.red, textcolor=color.red, title = 'Pattern')
// Entry Variables
bool openLongPosition = bullish_pattern
bool openShortPosition = bearish_pattern
var float lowest = low
if low[1] < low[0]
lowest := low[1] // calculates the lowest point in the candlestick pattern
var float highest = high
if high[1] > high[0]
highest := high[1] // calculates the highest point in the candlestick pattern
float midrange = (highest-lowest)/2 // the middle point in the candlestick pattern
// Entry Orders
strategy.entry('Long', strategy.long, stop = math.avg(lowest, highest), limit = math.avg(lowest, highest) + (1 * syminfo.mintick) , when = openLongPosition)
strategy.entry('Short', strategy.short, stop = math.avg(lowest, highest), limit = math.avg(lowest, highest) - (1 * syminfo.mintick), when = openShortPosition)
// Cancel Not Executed Orders after 3 bars >>> Note: I think this is not working well
strategy.cancel('Long', when = ta.barssince(openLongPosition) > 3 and strategy.position_size == 0)
strategy.cancel('Short', when = ta.barssince(openShortPosition) > 3 and not strategy.position_size == 0)
// Stop Loss
strategy.exit('Long SL', from_entry = 'Long', stop = lowest)
strategy.exit('Short SL', from_entry = 'Short', stop = highest)
// Trailing Take Profit
strategy.exit('Long TP', from_entry = 'Long', trail_price = highest, trail_offset = strategy.position_avg_price + (math.avg(lowest, highest)/2))
strategy.exit('Short TP', from_entry = 'Short', trail_price = lowest, trail_offset = strategy.position_avg_price - (math.avg(lowest, highest)/2))