Move snippets and strategies folders to misc directory
This commit is contained in:
304
misc/snippets/Bull_Bear_Flag_Finder_v6
Normal file
304
misc/snippets/Bull_Bear_Flag_Finder_v6
Normal file
@@ -0,0 +1,304 @@
|
||||
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
|
||||
// © Amphibiantrading
|
||||
|
||||
//@version=6
|
||||
indicator('Flag Finder', overlay = true, max_lines_count = 500)
|
||||
|
||||
//inputs
|
||||
//user controlled settings for the indicator, separated into 4 different groups
|
||||
var g1 = 'Bull Flag Criteria'
|
||||
|
||||
max_depth = input.float(7.5, 'Max Flag Depth', step = .25, minval = .25, group = g1, tooltip = 'Maximum pullback allowed from flag high to flag low')
|
||||
max_flag = input.int(10, 'Max Flag Length', group = g1, tooltip = 'The maximum number of bars the flag can last')
|
||||
min_flag = input.int(4, 'Min Flag Length', group = g1, tooltip = 'The minimum number of bars required for the flag')
|
||||
poleMin = input.float(20.0, 'Prior Uptrend Minimum', step = .50, minval = .25, group = g1, tooltip = 'The minimum percentage gain required before a flag forms')
|
||||
poleMaxLen = input.int(15, 'Max Flag Pole Length', minval = 1, group = g1, tooltip = 'The maximum number of bars the flagpole can be')
|
||||
poleMinLen = input.int(5, 'Min Flag Pole Length', minval = 1, group = g1, tooltip = 'The minimum number of bars required for the flag pole')
|
||||
|
||||
var g2 = 'Bear Flag Criteria'
|
||||
|
||||
max_rally = input.float(10, 'Max Flag Rally', step = .25, minval = .25, group = g2, tooltip = 'Maximum rally allowed from flag low to flag low')
|
||||
max_flagBear = input.int(10, 'Max Flag Length', group = g2, tooltip = 'The maximum number of bars the flag can last')
|
||||
min_flagBear = input.int(4, 'Min Flag Length', group = g2, tooltip = 'The minimum number of bars required for the flag')
|
||||
poleMinBear = input.float(15.0, 'Prior Downtrend Minimum', step = .50, minval = .25, group = g2, tooltip = 'The minimum percentage loss required before a flag forms')
|
||||
poleMaxLenBear = input.int(15, 'Max Flag Pole Length', minval = 1, group = g2, tooltip = 'The maximum number of bars the flagpole can be')
|
||||
poleMinLenBear = input.int(3, 'Min Flag Pole Length', minval = 1, group = g2, tooltip = 'The minimum number of bars required for the flag pole')
|
||||
|
||||
var g3 = 'Bull Flag Display Options'
|
||||
|
||||
showF = input.bool(true, 'Show Bull Flags', group = g3)
|
||||
showBO = input.bool(true, 'Show Bull Flag Breakouts', group = g3)
|
||||
lineColor = input.color(color.green, 'Bull Line Color', group = g3)
|
||||
lineWidth = input.int(2, 'Bull Line Width', minval = 1, maxval = 5, group = g3)
|
||||
|
||||
var g4 = 'Bear Flag Display Options'
|
||||
|
||||
showBF = input.bool(true, 'Show Bear Flags', group = g4)
|
||||
showBD = input.bool(true, 'Show Bear Flag Breakdowns', group = g4)
|
||||
lineColorBear = input.color(color.red, 'Bear Flag Line Color', group = g4)
|
||||
lineWidthBear = input.int(2, 'Bear Flag Line Width', minval = 1, maxval = 5, group = g4)
|
||||
|
||||
//variables
|
||||
//declare starting variables used for identfying flags
|
||||
var baseHigh = high
|
||||
var startIndex = 0
|
||||
var flagLength = 0
|
||||
var baseLow = low
|
||||
var lowIndex = 0
|
||||
var flagBool = false
|
||||
var poleLow = 0.0
|
||||
var poleLowIndex = 0
|
||||
var line flagHLine = na
|
||||
var line flagLLine = na
|
||||
var line flagPLine = na
|
||||
|
||||
// bear flag variables
|
||||
var flagLowBear = high
|
||||
var startIndexBear = 0
|
||||
var flagLengthBear = 0
|
||||
var flagHigh = low
|
||||
var highIndex = 0
|
||||
var flagBoolBear = false
|
||||
var poleHigh = 0.0
|
||||
var polehighIndex = 0
|
||||
var line flagBearHLine = na
|
||||
var line flagBearLLine = na
|
||||
var line flagBearPLine = na
|
||||
|
||||
//find bull flag highs
|
||||
// check to see if the current bars price is higher than the previous base high or na and then sets the variables needed for flag detection
|
||||
if high > baseHigh or na(baseHigh)
|
||||
baseHigh := high
|
||||
startIndex := bar_index
|
||||
flagLength := 0
|
||||
baseLow := low
|
||||
lowIndex := bar_index
|
||||
lowIndex
|
||||
// check to see if the low of the current bar is lower than the base low, if it is set the base low to the low
|
||||
if high <= baseHigh and low < baseLow
|
||||
baseLow := low
|
||||
lowIndex := bar_index
|
||||
lowIndex
|
||||
// moves the base low from the base high bar_index to prevent the high and low being the same bar
|
||||
if high <= baseHigh and lowIndex == startIndex
|
||||
baseLow := low
|
||||
lowIndex := bar_index
|
||||
lowIndex
|
||||
|
||||
//find bear flag lows
|
||||
// check to see if the current bars price is lower than the previous flag low or na and then sets the variables needed for flag detection
|
||||
if low < flagLowBear or na(flagLowBear)
|
||||
flagLowBear := low
|
||||
startIndexBear := bar_index
|
||||
flagLengthBear := 0
|
||||
flagHigh := high
|
||||
highIndex := bar_index
|
||||
highIndex
|
||||
// check to see if the high of the current bar is higher than the flag high, if it is set the flag high to the high
|
||||
if low >= flagLowBear and high > flagHigh
|
||||
flagHigh := high
|
||||
highIndex := bar_index
|
||||
highIndex
|
||||
// moves the flag high from the flag low bar_index to prevent the high and low being the same bar
|
||||
if low >= flagLowBear and highIndex == startIndexBear
|
||||
flagHigh := high
|
||||
highIndex := bar_index
|
||||
highIndex
|
||||
|
||||
//calulations bullish
|
||||
findDepth = math.abs((baseLow / baseHigh - 1) * 100) //calculate the depth of the flag
|
||||
poleDepth = (baseHigh / poleLow - 1) * 100 // calculate the low of the flag pole to the base high
|
||||
lower_close = close < close[1] // defines a lower close
|
||||
|
||||
//calculations bearish
|
||||
findRally = math.abs((flagHigh / flagLowBear - 1) * 100) //calculate the rally of the flag
|
||||
poleDepthBear = (flagLowBear / poleHigh - 1) * 100 // calculate the high of the flag pole to the low high
|
||||
higher_close = close > close[1] // defines a higher close
|
||||
|
||||
//start the counters
|
||||
// begins starting bars once a high is less than the flag high
|
||||
if high < baseHigh and findDepth <= max_depth or high == baseHigh and lower_close
|
||||
flagLength := flagLength + 1
|
||||
flagLength
|
||||
else
|
||||
flagLength := 0
|
||||
flagLength
|
||||
|
||||
// begins starting bars once a low is higher than the flag low
|
||||
if low > flagLowBear and findRally <= max_rally or low == flagLowBear and higher_close
|
||||
flagLengthBear := flagLengthBear + 1
|
||||
flagLengthBear
|
||||
else
|
||||
flagLengthBear := 0
|
||||
flagLengthBear
|
||||
|
||||
// check for prior uptrend / downtrend to meet requirements
|
||||
// loops through all the bars from the minimum pole length to the maximum pole length to check if the prior uptrend requirements are met and sets the variables to their new values
|
||||
if high == baseHigh
|
||||
for i = poleMinLen to poleMaxLen by 1
|
||||
if (high / low[i] - 1) * 100 >= poleMin
|
||||
flagBool := true
|
||||
poleLow := low[i]
|
||||
poleLowIndex := bar_index[i]
|
||||
break
|
||||
|
||||
// loops through all the bars from the minimum pole length to the maximum pole length to check if the prior downtrend requirements are met and sets the variables to their new values
|
||||
if low == flagLowBear
|
||||
for i = poleMinLenBear to poleMaxLenBear by 1
|
||||
if math.abs((low / high[i] - 1) * 100) >= poleMinBear
|
||||
flagBoolBear := true
|
||||
poleHigh := high[i]
|
||||
polehighIndex := bar_index[i]
|
||||
break
|
||||
|
||||
|
||||
// reset variables if criteria for a flag is broken
|
||||
// if the depth of the bull flag is greater than the maximum depth limit or the flag lasts longer than the maximum length everything is reset to beging looking for a new flag
|
||||
if findDepth >= max_depth or flagLength > max_flag
|
||||
flagBool := false
|
||||
flagLength := 0
|
||||
baseHigh := na
|
||||
startIndex := na
|
||||
lowIndex := na
|
||||
baseLow := na
|
||||
baseLow
|
||||
|
||||
// if the rally of the bear flag is greater than the maximum rally limit or the flag lasts longer than the maximum length everything is reset to beging looking for a new flag
|
||||
if findRally >= max_rally or flagLengthBear > max_flagBear
|
||||
flagBoolBear := false
|
||||
flagLengthBear := 0
|
||||
flagLowBear := na
|
||||
startIndexBear := na
|
||||
highIndex := na
|
||||
flagHigh := na
|
||||
flagHigh
|
||||
|
||||
// reset the variables and begin looking for a new bull flag if price continues higher before the minimum flag length requirement is met
|
||||
if high > baseHigh[1] and flagLength < min_flag
|
||||
baseHigh := high
|
||||
flagLength := 0
|
||||
startIndex := bar_index
|
||||
lowIndex := bar_index
|
||||
baseLow := low
|
||||
baseLow
|
||||
|
||||
// reset the variables and begin looking for a new bear flag if price continues lower before the minimum bear flag length requirement is met
|
||||
if low < flagLowBear[1] and flagLengthBear < min_flagBear
|
||||
flagLowBear := low
|
||||
flagLengthBear := 0
|
||||
startIndexBear := bar_index
|
||||
highIndex := bar_index
|
||||
flagHigh := high
|
||||
flagHigh
|
||||
|
||||
|
||||
//define the flags
|
||||
// if all requirements are met a bull flag is true, flagBool is true, flag length is less than maximum set length and more than miminum required. The prior run up also meets the requirements for length and price
|
||||
flag = flagBool == true and flagLength < max_flag and findDepth < max_depth and flagLength >= min_flag and startIndex - poleLowIndex <= poleMaxLen
|
||||
|
||||
// if all requirements are met a bear flag is true, flagBoolBear is true, flag length is less than maximum set length and more than miminum required. The prior downtrend also meets the requirements for length and price
|
||||
bearFlag = flagBoolBear == true and flagLengthBear < max_flagBear and findRally < max_rally and flagLengthBear >= min_flagBear and startIndexBear - polehighIndex <= poleMaxLen
|
||||
|
||||
|
||||
//define flags, breakouts, breadowns
|
||||
// a breakout is defined as the high going above the flag high and the length of the flag is greater than the minimum requirement. The flag boolean must also be true
|
||||
breakout = high > baseHigh[1] and flagLength >= min_flag and flagBool == true
|
||||
//a breakdown is defined as the depth of the flag being larger than user set parameter or the length of the flag exceeded the maximum flag length
|
||||
breakdown = findDepth >= max_depth or flagLength > max_flag
|
||||
// a separate variable to have breakouts only plot once using a plotshape
|
||||
plotBO = flag[1] and high > baseHigh[1] and flagLength[1] >= min_flag and flagBool == true
|
||||
|
||||
// a bear flag breakout is defined as the low going below the flag low and the length of the flag is greater than the minimum requirement. The flag boolean must also be true
|
||||
breakoutBear = low < flagLowBear[1] and flagLengthBear >= min_flagBear and flagBoolBear == true
|
||||
//a breakdown is defined as the rally of the flag being larger than user set parameter or the length of the flag exceeded the maximum flag length
|
||||
breakdownBear = findRally >= max_rally or flagLengthBear > max_flagBear
|
||||
// a separate variable to have breakouts only plot once using a plotshape
|
||||
plotBD = bearFlag[1] and low < flagLowBear[1] and flagLengthBear[1] >= min_flagBear and flagBoolBear == true
|
||||
|
||||
// if a bul flag is detected and the user has bull flags selected from the settings menu draw it on the chart
|
||||
if flag and showF
|
||||
//if a flag was detected on the previous bar, delete those lines and allow for new lines to be drawn
|
||||
if flag[1]
|
||||
line.delete(flagHLine[1])
|
||||
line.delete(flagLLine[1])
|
||||
line.delete(flagPLine[1])
|
||||
//draw new lines
|
||||
flagHLine := line.new(startIndex, baseHigh, bar_index, baseHigh, color = lineColor, width = lineWidth)
|
||||
flagLLine := line.new(startIndex, baseLow, bar_index, baseLow, color = lineColor, width = lineWidth)
|
||||
flagPLine := line.new(poleLowIndex + 1, poleLow, startIndex, baseLow, color = lineColor, width = lineWidth)
|
||||
flagPLine
|
||||
|
||||
// if a bear flag is detected and the user has bear flags selected from the settings menu draw it on the chart
|
||||
if bearFlag and showBF
|
||||
//if a bear flag was detected on the previous bar, delete those lines and allow for new lines to be drawn
|
||||
if bearFlag[1]
|
||||
line.delete(flagBearHLine[1])
|
||||
line.delete(flagBearLLine[1])
|
||||
line.delete(flagBearPLine[1])
|
||||
//draw new lines
|
||||
flagBearHLine := line.new(startIndexBear, flagHigh, bar_index, flagHigh, color = lineColorBear, width = lineWidthBear)
|
||||
flagBearLLine := line.new(startIndexBear, flagLowBear, bar_index, flagLowBear, color = lineColorBear, width = lineWidthBear)
|
||||
flagBearPLine := line.new(polehighIndex + 1, poleHigh, startIndexBear, flagHigh, color = lineColorBear, width = lineWidthBear)
|
||||
flagBearPLine
|
||||
|
||||
//reset variables if a breakout occurs
|
||||
if breakout // bull flag - high gets above flag high
|
||||
flagLength := 0
|
||||
baseHigh := high
|
||||
startIndex := bar_index
|
||||
lowIndex := bar_index
|
||||
baseLow := low
|
||||
baseLow
|
||||
|
||||
if breakoutBear // bear flag - low gets below flag low
|
||||
flagLengthBear := 0
|
||||
flagLowBear := low
|
||||
startIndexBear := bar_index
|
||||
highIndex := bar_index
|
||||
flagHigh := high
|
||||
flagHigh
|
||||
|
||||
//reset the variables and highs from a failed bull flag. This allows stocks below previous highs to find new flags
|
||||
highest = ta.highest(high, 10) // built in variable that finds the highest high lookingback the past 10 bars
|
||||
if breakdown or flagLength == max_flag
|
||||
flagBool := false
|
||||
baseHigh := highest
|
||||
startIndex := bar_index
|
||||
lowIndex := bar_index
|
||||
baseLow := low
|
||||
baseLow
|
||||
|
||||
//reset the variables and lows from a failed bear flag. This allows stocks above previous lows to find new flags
|
||||
lowest = ta.lowest(low, 10) // built in variable that finds the lowest low lookingback the past 10 bars
|
||||
if breakdownBear or flagLengthBear == max_flagBear
|
||||
flagBoolBear := false
|
||||
flagLowBear := lowest
|
||||
startIndexBear := bar_index
|
||||
highIndex := bar_index
|
||||
flagHigh := high
|
||||
flagHigh
|
||||
|
||||
// if a flag breakdowns remove the lines from the chart
|
||||
if breakdown and flag[1]
|
||||
line.delete(flagHLine)
|
||||
line.delete(flagLLine)
|
||||
line.delete(flagPLine)
|
||||
|
||||
if breakdownBear and bearFlag[1]
|
||||
line.delete(flagBearHLine)
|
||||
line.delete(flagBearLLine)
|
||||
line.delete(flagBearPLine)
|
||||
|
||||
//plot breakouts
|
||||
// use a plotshape to check if there is a breakout and the show breakout option is selected. If both requirements are met plot a shape at the breakout bar
|
||||
plotshape(plotBO and showBO and showF, 'Breakout', shape.triangleup, location.belowbar, color.green, display = display.pane)
|
||||
|
||||
// use a plotshape to check if there is a breakout and the show breakout option is selected. If both requirements are met plot a shape at the breakout bar
|
||||
plotshape(plotBD and showBD and showBF, 'Breakout', shape.triangledown, location.abovebar, color.red, display = display.pane)
|
||||
|
||||
//alerts
|
||||
// add alerts for two conditions, a breakout and when a new flag is formed meeting all requirements.
|
||||
alertcondition(plotBO, 'Bull Flag Breakout', '{{ticker}} Breaking Out from Bull Flag')
|
||||
alertcondition(flag, 'New Bull Flag', '{{ticker}} has formed a bull flag')
|
||||
alertcondition(plotBD, 'Bear Flag Breakout', '{{ticker}} Breaking Out from Bear Flag')
|
||||
alertcondition(bearFlag, 'New Bear Flag', '{{ticker}} has formed a bear flag')
|
||||
74
misc/snippets/box_label_demo_v6.pine
Normal file
74
misc/snippets/box_label_demo_v6.pine
Normal file
@@ -0,0 +1,74 @@
|
||||
//@version=6
|
||||
indicator("Label Example", overlay=true)
|
||||
|
||||
// Calculate a condition for when to create a label
|
||||
condition = close > math.avg(close, 20)
|
||||
|
||||
// Create a new label if the condition is true, update the label's text with the current close price
|
||||
//label_id = label.new(title="Price Label", x=bar_index, y=close, text=str.tostring(close), color=color.green, style=label.style.plain)
|
||||
|
||||
// Create an array to store boxes and labels
|
||||
var box[] boxArray = array.new_box()
|
||||
var label[] labelArray = array.new_label()
|
||||
|
||||
labelDemo() =>
|
||||
// Remove a specific label by index
|
||||
if (array.size(labelArray) > 0)
|
||||
label.delete(array.get(labelArray, 0))
|
||||
array.remove(labelArray, 0)
|
||||
|
||||
label newLabel = label.new(x=bar_index, y=close, text='H:' + str.tostring(high, '#.##') + '\nL:' + str.tostring(low, '#.##'), xloc=xloc.bar_index, yloc=yloc.price, color=color.blue, style=label.style_label_down, textcolor=color.white, size=size.normal, textalign=text.align_center, tooltip='SIZE: ' + str.tostring(high-low, '#.##'))
|
||||
|
||||
// Add new labels to array
|
||||
array.push(labelArray, newLabel)
|
||||
|
||||
labelDemo()
|
||||
|
||||
// Input parameters
|
||||
var float buyLimitPrice = input.price(title="Buy Limit Price", defval=0.0)
|
||||
var float sellLimitPrice = input.price(title="Sell Limit Price", defval=0.0)
|
||||
var int rectangleWidth = input.int(title="Rectangle Width (Price Units)", defval=10, minval=1)
|
||||
var bool showDebugInfo = input.bool(title="Show Debug Information", defval=false)
|
||||
var color buyShadeColor = input.color(title="Buy Limit Shade Color", defval=#4caf4f46)
|
||||
var color sellShadeColor = input.color(title="Sell Limit Shade Color", defval=#ff52523a)
|
||||
var color borderColor = input.color(title="Border Color", defval=color.gray)
|
||||
|
||||
buyLimitPrice := high + 10
|
||||
sellLimitPrice := low - 10
|
||||
|
||||
cleanupBoxes() =>
|
||||
// Remove oldest boxes if exceed max limit
|
||||
while (array.size(boxArray) > 0)
|
||||
box.delete(array.get(boxArray, 0))
|
||||
array.remove(boxArray, 0)
|
||||
|
||||
// Function to draw order levels
|
||||
f_drawOrderLevels() =>
|
||||
cleanupBoxes()
|
||||
var boxes = array.new_box()
|
||||
|
||||
// Buy limit rectangle
|
||||
if (buyLimitPrice > 0)
|
||||
float buyRectTop = high + rectangleWidth //buyLimitPrice + rectangleWidth / 2
|
||||
float buyRectBottom = (high + low) / 2 //buyLimitPrice - rectangleWidth / 2
|
||||
|
||||
box newBox1 = box.new(left=bar_index, top=buyRectTop, right=bar_index + 10, bottom=buyRectBottom, border_color=borderColor, border_width=1, bgcolor=buyShadeColor, text=str.tostring(buyRectTop, '#.##'), text_color=color.white, text_halign=text.align_center, text_valign=text.align_top, text_size = size.small)
|
||||
array.push(boxArray, newBox1)
|
||||
|
||||
if (showDebugInfo)
|
||||
//label.new(x=bar_index, y=close, text='H:' + str.tostring(high, '#.##') + '\nL:' + str.tostring(low, '#.##'), xloc=xloc.bar_index, yloc=yloc.price, color=color.blue, style=label.style_label_down, textcolor=color.white, size=size.normal, textalign=text.align_center, tooltip='SIZE: ' + str.tostring(high-low, '#.##'))
|
||||
label.new(x=bar_index, y=buyLimitPrice, text='Buy Limit\nPrice: ' + str.tostring(buyLimitPrice, format.mintick) + '\nTop: ' + str.tostring(buyRectTop, format.mintick) + '\nBottom: ' + str.tostring(buyRectBottom, format.mintick), color=color.blue)
|
||||
|
||||
// Sell limit rectangle
|
||||
if (sellLimitPrice > 0)
|
||||
float sellRectTop = (high + low) / 2 //sellLimitPrice + rectangleWidth / 2
|
||||
float sellRectBottom = low - rectangleWidth //sellLimitPrice - rectangleWidth / 2
|
||||
|
||||
box newBox2 = box.new(left=bar_index, top=sellRectTop, right=bar_index + 10, bottom=sellRectBottom, border_color=borderColor, border_width=1, bgcolor=sellShadeColor, text=str.tostring(sellRectBottom, '#.##'), text_color=color.white, text_halign=text.align_center, text_valign=text.align_bottom, text_size = size.small)
|
||||
array.push(boxArray, newBox2)
|
||||
|
||||
if (showDebugInfo)
|
||||
label.new(x=bar_index, y=sellLimitPrice, text='Sell Limit\nPrice: ' + str.tostring(sellLimitPrice, format.mintick) + '\nTop: ' + str.tostring(sellRectTop, format.mintick) + '\nBottom: ' + str.tostring(sellRectBottom, format.mintick), color=color.red)
|
||||
|
||||
// Execute the function to draw order levels
|
||||
f_drawOrderLevels()
|
||||
38
misc/snippets/label_debug.pine
Normal file
38
misc/snippets/label_debug.pine
Normal file
@@ -0,0 +1,38 @@
|
||||
//@version=6
|
||||
indicator("Removing key-value pairs demo")
|
||||
|
||||
//@function Returns a label to display the keys and values from a map.
|
||||
method debugLabel(
|
||||
map<string, int> this, int barIndex = bar_index,
|
||||
color bgColor = color.blue, string note = ""
|
||||
) =>
|
||||
//@variable A string representing the size, keys, and values in `this` map.
|
||||
string repr = str.format(
|
||||
"{0}\nSize: {1}\nKeys: {2}\nValues: {3}",
|
||||
note, this.size(), str.tostring(this.keys()), str.tostring(this.values())
|
||||
)
|
||||
label.new(
|
||||
barIndex, 0, repr, color = bgColor, style = label.style_label_center,
|
||||
textcolor = color.white, size = size.small
|
||||
)
|
||||
|
||||
if bar_index == last_bar_index - 1
|
||||
//@variable A map containing `string` keys and `int` values.
|
||||
m = map.new<string, int>()
|
||||
|
||||
// Put key-value pairs into `m`.
|
||||
for [i, key] in array.from("A", "B", "C", "D", "E")
|
||||
m.put(key, i)
|
||||
m.debugLabel(bar_index, color.green, "Added pairs")
|
||||
|
||||
//@variable An array of keys to remove from `m`.
|
||||
array<string> removeKeys = array.from("B", "B", "D", "F", "a")
|
||||
|
||||
// Remove each `key` in `removeKeys` from `m`.
|
||||
for key in removeKeys
|
||||
m.remove(key)
|
||||
m.debugLabel(bar_index + 15, color.red, "Removed pairs")
|
||||
|
||||
// Remove all remaining keys from `m`.
|
||||
m.clear()
|
||||
m.debugLabel(bar_index + 30, color.purple, "Cleared the map")
|
||||
24
misc/snippets/label_demo.pine
Normal file
24
misc/snippets/label_demo.pine
Normal file
@@ -0,0 +1,24 @@
|
||||
//@version=6
|
||||
indicator("Label Example", overlay=true)
|
||||
|
||||
// Calculate a condition for when to create a label
|
||||
condition = close > math.avg(close, 20)
|
||||
|
||||
// Create a new label if the condition is true, update the label's text with the current close price
|
||||
//label_id = label.new(title="Price Label", x=bar_index, y=close, text=str.tostring(close), color=color.green, style=label.style.plain)
|
||||
|
||||
// Create an array to store labels
|
||||
var label[] labelArray = array.new_label()
|
||||
|
||||
labelDemo() =>
|
||||
// Remove a specific label by index
|
||||
if (array.size(labelArray) > 0)
|
||||
label.delete(array.get(labelArray, 0))
|
||||
array.remove(labelArray, 0)
|
||||
|
||||
label newLabel = label.new(x=bar_index, y=close, text='H:' + str.tostring(high, '#.##') + '\nL:' + str.tostring(low, '#.##'), xloc=xloc.bar_index, yloc=yloc.price, color=color.blue, style=label.style_label_down, textcolor=color.white, size=size.normal, textalign=text.align_center, tooltip='SIZE: ' + str.tostring(high-low, '#.##'))
|
||||
|
||||
// Add new labels to array
|
||||
array.push(labelArray, newLabel)
|
||||
|
||||
labelDemo()
|
||||
39
misc/snippets/shooting-star_doji_candle_finder_v6.pine
Normal file
39
misc/snippets/shooting-star_doji_candle_finder_v6.pine
Normal file
@@ -0,0 +1,39 @@
|
||||
//@version=6
|
||||
indicator("Shooting Star and Doji Detector", overlay=true)
|
||||
|
||||
// Function to determine if a candle is a shooting star
|
||||
is_shooting_star(open, high, close, low) =>
|
||||
body_length = math.abs(close - open)
|
||||
upper_shadow = high - math.max(open, close)
|
||||
lower_shadow = math.min(open, close) - low
|
||||
|
||||
// Shooting star conditions:
|
||||
// 1. Small body (less than 1/3 of total candle length)
|
||||
// 2. Upper shadow at least 2x the body length
|
||||
// 3. Lower shadow is very small
|
||||
body_length <= (high - low) / 3 and upper_shadow >= body_length * 2 and lower_shadow <= body_length
|
||||
|
||||
// Function to determine if a candle is a doji
|
||||
is_doji(open, high, close, low) =>
|
||||
body_length = math.abs(close - open)
|
||||
total_range = high - low
|
||||
|
||||
// Doji conditions:
|
||||
// 1. Body is very small (less than 10% of total candle range)
|
||||
body_length <= total_range * 0.1
|
||||
|
||||
// Plot shooting star red mark
|
||||
plotchar(is_shooting_star(open, high, close, low),
|
||||
title="Shooting Star",
|
||||
char="+",
|
||||
location=location.abovebar,
|
||||
color=color.red,
|
||||
size=size.small)
|
||||
|
||||
// Plot doji green mark
|
||||
plotchar(is_doji(open, high, close, low),
|
||||
title="Doji",
|
||||
char="+",
|
||||
location=location.abovebar,
|
||||
color=color.green,
|
||||
size=size.small)
|
||||
50
misc/snippets/three-strike_detector_v6.pine
Normal file
50
misc/snippets/three-strike_detector_v6.pine
Normal file
@@ -0,0 +1,50 @@
|
||||
//@version=6
|
||||
indicator("Three Strike Detector", overlay=true)
|
||||
|
||||
// Function to define a long candle
|
||||
// Function to define a long candle
|
||||
is_long_candle(open, high, close, low, min_length) =>
|
||||
body_length = math.abs(close - open)
|
||||
total_range = high - low
|
||||
|
||||
// Long candle condition:
|
||||
// 1. Body is at least min_length of the total range
|
||||
body_length >= total_range * min_length
|
||||
|
||||
// Function to detect the three strike pattern
|
||||
is_three_strike(open, high, close, low) =>
|
||||
// Define minimum body length for long candles
|
||||
min_long_body = 0.5 // Adjust this value as needed
|
||||
|
||||
// Check for bullish three strike
|
||||
isBullishLong1 = is_long_candle(open[1], high[1], close[1], low[1], min_long_body) and close[1] < close[2]
|
||||
isBullishLong2 = is_long_candle(open[2], high[2], close[2], low[2], min_long_body) and close[2] < close[3]
|
||||
isBullishLong3 = is_long_candle(open[3], high[3], close[3], low[3], min_long_body)
|
||||
isBullishEngulfing = open[4] < low[3] and close[4] > high[1]
|
||||
isBullishThreeStrike = isBullishLong1 and isBullishLong2 and isBullishLong3 and isBullishEngulfing
|
||||
|
||||
// Check for bearish three strike
|
||||
isBearishLong1 = is_long_candle(open[1], high[1], close[1], low[1], min_long_body) and close[1] > close[2]
|
||||
isBearishLong2 = is_long_candle(open[2], high[2], close[2], low[2], min_long_body) and close[2] > close[3]
|
||||
isBearishLong3 = is_long_candle(open[3], high[3], close[3], low[3], min_long_body)
|
||||
isBearishEngulfing = open[4] > high[3] and close[4] < low[1]
|
||||
isBearishThreeStrike = isBearishLong1 and isBearishLong2 and isBearishLong3 and isBearishEngulfing
|
||||
|
||||
[isBullishThreeStrike, isBearishThreeStrike]
|
||||
|
||||
[isBullTS, isBearTS] = is_three_strike(open, high, close, low)
|
||||
|
||||
// Plot the three strike pattern with an arrow
|
||||
plotchar(isBullTS,
|
||||
title="Three Strike",
|
||||
char="3",
|
||||
location=location.belowbar,
|
||||
color=color.green,
|
||||
size=size.small)
|
||||
|
||||
plotchar(isBearTS,
|
||||
title="Three Strike",
|
||||
char="3",
|
||||
location=location.belowbar,
|
||||
color=color.red,
|
||||
size=size.small)
|
||||
74
misc/strategies/Candle_Bear-Bull_v6.pine
Executable file
74
misc/strategies/Candle_Bear-Bull_v6.pine
Executable file
@@ -0,0 +1,74 @@
|
||||
//@version=6
|
||||
strategy('Hammer Candle Strategy', overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
|
||||
|
||||
// Inputs for customization
|
||||
hammertBodyRatio = input.float(0.3, title = 'Max Body Size Ratio', minval = 0.1, maxval = 0.5, step = 0.1)
|
||||
hammerWickRatio = input.float(2.0, title = 'Min Wick to Body Ratio', minval = 1.0, maxval = 5.0, step = 0.1)
|
||||
|
||||
// Function to determine if a candle is bullish or bearish
|
||||
is_candle_bullish(index) =>
|
||||
close[index] > open[index]
|
||||
|
||||
is_candle_bearish(index) =>
|
||||
close[index] < open[index]
|
||||
|
||||
// Hammer Candle Detection Function
|
||||
//isHammer() =>
|
||||
string dbg = ''
|
||||
bodySize = math.abs(close - open)
|
||||
totalRange = high - low
|
||||
|
||||
// Calculate upper and lower wicks
|
||||
upperWick = high - math.max(close, open)
|
||||
lowerWick = math.min(close, open) - low
|
||||
|
||||
// Check if body is small (less than 30% of total candle range)
|
||||
isSmallBody = bodySize <= totalRange * hammertBodyRatio
|
||||
//plotchar(isSmallBody, title="lw", char='-', location=location.abovebar, color=#40fbf5, size=size.small)
|
||||
|
||||
// Lower/Upper wick should be at least 2x the body size and significantly long
|
||||
bool isLongUpperWick = upperWick >= totalRange * 0.5 and upperWick >= bodySize * hammerWickRatio
|
||||
bool isLongLowerWick = lowerWick >= totalRange * 0.5 and lowerWick >= bodySize * hammerWickRatio
|
||||
//plotchar(isLongUpperWick, title="LW", char='L', location=location.abovebar, color=color.fuchsia, size=size.small)
|
||||
//plotchar(isLongLowerWick, title="lw", char='l', location=location.belowbar, color=color.fuchsia, size=size.small)
|
||||
|
||||
// Minimal lower/upper wick
|
||||
bool isShortUpperWick = upperWick <= bodySize * 0.5 or upperWick <= totalRange * 0.2
|
||||
bool isShortLowerWick = lowerWick <= bodySize * 0.5 or lowerWick <= totalRange * 0.2
|
||||
//plotchar(isShortUpperWick, title="Bullish Candle", char='S', location=location.abovebar, color=color.fuchsia, size=size.small)
|
||||
//plotchar(isShortLowerWick, title="Bullish Candle", char='s', location=location.belowbar, color=color.fuchsia, size=size.small)
|
||||
|
||||
// Bullish Hammer (Green/White)
|
||||
isBullishHammer = isSmallBody and isShortUpperWick and isLongLowerWick //and is_candle_bearish(1) and is_candle_bearish(2) // and close > open
|
||||
|
||||
// Bearish Hammer (Red/Black)
|
||||
isBearishHammer = isSmallBody and isLongUpperWick and isShortLowerWick // and is_candle_bullish(1) and is_candle_bullish(2) // and close < open
|
||||
if isBearishHammer
|
||||
dbg := 'TEST: ' + str.tostring(isShortUpperWick) + ' | ' + str.tostring(upperWick) + ' | ' + str.tostring(bodySize) + ' | ' + str.tostring(totalRange) + ' | '
|
||||
dbg
|
||||
|
||||
// [isBullishHammer, isBearishHammer, dbg]
|
||||
|
||||
//[bullish, bearish, deb] = isHammer()
|
||||
bullish = isBullishHammer
|
||||
bearish = isBearishHammer
|
||||
// Plot a '*' when close is above open (bullish candle)
|
||||
plotchar(bullish, title = 'Bullish Candle', char = '*', location = location.belowbar, color = color.green, size = size.small)
|
||||
|
||||
// Plot an '*' when close is below open (bearish candle)
|
||||
plotchar(bearish, title = 'Bearish Candle', char = '*', location = location.abovebar, color = color.red, size = size.small)
|
||||
|
||||
bullish := isBullishHammer and is_candle_bullish(1) and is_candle_bullish(2)
|
||||
bearish := isBearishHammer and is_candle_bullish(1) and is_candle_bullish(2)
|
||||
|
||||
// Plot hammer candle markers
|
||||
plotshape(bullish, title = 'Bullish Hammer', location = location.belowbar, style = shape.triangleup, size = size.small, color = color.green)
|
||||
|
||||
plotshape(bearish, title = 'Bearish Hammer', location = location.abovebar, style = shape.triangledown, size = size.small, color = color.red)
|
||||
|
||||
// Trading logic
|
||||
if bullish
|
||||
strategy.entry('Long', strategy.long)
|
||||
|
||||
if bearish
|
||||
strategy.entry('Short', strategy.short)
|
||||
18
misc/strategies/High_Volume_Bars_Strategy.pine
Normal file
18
misc/strategies/High_Volume_Bars_Strategy.pine
Normal file
@@ -0,0 +1,18 @@
|
||||
// Description: This script identifies high-volume bars that close up or down as potential indicators of bullish or bearish institutional activity, respectively. It generates buy and sell signals based on these conditions.
|
||||
//@version=4
|
||||
study("High Volume Bars Strategy", shorttitle="HVBS", overlay=true)
|
||||
|
||||
// Input parameters
|
||||
length = input(14, title="Length")
|
||||
multiplier = input(2, title="Multiplier")
|
||||
|
||||
// Calculate average volume
|
||||
averageVolume = sma(volume, length)
|
||||
|
||||
// Generate signals
|
||||
buySignal = crossover(volume, averageVolume * multiplier) and close > open
|
||||
sellSignal = crossover(volume, averageVolume * multiplier) and close < open
|
||||
|
||||
// Plot buy and sell arrows
|
||||
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
|
||||
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
|
||||
96
misc/strategies/Strategy_Limit_DATE_TIME.pine
Executable file
96
misc/strategies/Strategy_Limit_DATE_TIME.pine
Executable file
@@ -0,0 +1,96 @@
|
||||
//@version=5
|
||||
strategy("Test Candle Close Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)
|
||||
|
||||
// Input parameters for additional customization
|
||||
riskPerTrade = input.float(1, title="Risk % per Trade", minval=0.1, maxval=5, step=0.1)
|
||||
stopLossPercent = input.float(0.5, title="Stop Loss %", minval=0.1, maxval=2, step=0.1)
|
||||
takeProfitPercent = input.float(1.5, title="Take Profit %", minval=0.5, maxval=5, step=0.1)
|
||||
|
||||
var string userKey = input.string("Xbwln5mblaaEBPDnFpVuTmbUqNax51CmjOEmdvuXSjc", "API Key")
|
||||
var string userAccount = input.string("Sim101", "Account ID")
|
||||
var string atmName = input.string("EMA_CROSS_50", "ATM Strategy Name")
|
||||
var int numContracts = input.int(1, "Number of Contracts")
|
||||
|
||||
// Precise Start Date and Time inputs
|
||||
startDateTimeX = input.time(timestamp("2024-11-21 00:01"), title="Start Date and Time")
|
||||
var startDateTime = startDateTimeX + (6 * 60 * 60 * 1000) // Adjust to match 6h diff >> UTC to Chicago
|
||||
|
||||
startTime = input.string("00:15", title="Start Time (HH:MM)")
|
||||
endTime = input.string("00:30", title="End Time (HH:MM)")
|
||||
|
||||
// Parse the input times
|
||||
startHour = str.tonumber(str.substring(startTime, 0, 2))
|
||||
startMinute = str.tonumber(str.substring(startTime, 3, 5))
|
||||
endHour = str.tonumber(str.substring(endTime, 0, 2))
|
||||
endMinute = str.tonumber(str.substring(endTime, 3, 5))
|
||||
|
||||
maxOrderCount = input.int(150, "MAX number of orders in the strategy")
|
||||
|
||||
// Order counting
|
||||
var order_count = 0
|
||||
|
||||
// Time conditions and trade conditions
|
||||
isWithinTimeWindow = (hour(time_close) > startHour or
|
||||
(hour(time_close) == startHour and minute(time_close) >= startMinute)) and
|
||||
(hour(time_close) < endHour or
|
||||
(hour(time_close) == endHour and minute(time_close) <= endMinute))
|
||||
|
||||
isAfterStartDateTime = time_close >= startDateTime
|
||||
within_trading_period = order_count <= maxOrderCount and isWithinTimeWindow and isAfterStartDateTime
|
||||
|
||||
// Determine candle direction
|
||||
candleGreen = close > open
|
||||
candleRed = close < open
|
||||
|
||||
// Build alerts
|
||||
alertLong = "key=" + userKey + ";command=PLACE;account=" + userAccount + ";instrument=MNQ 12-24; action=BUY" + ";qty=" + str.tostring(numContracts) + ";order_type=MARKET;tif=DAY;strategy=" + atmName + ";flatten_first=true;"
|
||||
alertShort = "key=" + userKey + ";command=PLACE;account=" + userAccount + ";instrument=MNQ 12-24; action=SELL" + ";qty=" + str.tostring(numContracts) + ";order_type=MARKET;tif=DAY;strategy=" + atmName + ";flatten_first=true;"
|
||||
|
||||
// Entry conditions - using barstate.isconfirmed to ensure we act on closed candles only
|
||||
if (barstate.isconfirmed and within_trading_period)
|
||||
|
||||
// Close any existing positions and enter new ones based on candle color
|
||||
if (candleGreen)
|
||||
alert(alertLong)
|
||||
order_count := order_count + 1
|
||||
|
||||
strategy.close_all()
|
||||
strategy.entry("long", strategy.long, 1)
|
||||
|
||||
if (candleRed)
|
||||
alert(alertShort)
|
||||
order_count := order_count + 1
|
||||
|
||||
strategy.close_all()
|
||||
strategy.entry("short", strategy.short, 1)
|
||||
|
||||
// Calculate stop loss and take profit levels for open positions
|
||||
if (strategy.position_size != 0)
|
||||
longStopPrice = strategy.position_avg_price * (1 - stopLossPercent/100)
|
||||
longTakeProfit = strategy.position_avg_price * (1 + takeProfitPercent/100)
|
||||
shortStopPrice = strategy.position_avg_price * (1 + stopLossPercent/100)
|
||||
shortTakeProfit = strategy.position_avg_price * (1 - takeProfitPercent/100)
|
||||
|
||||
if (strategy.position_size > 0)
|
||||
strategy.exit("Long TP/SL", "Long",
|
||||
stop=longStopPrice,
|
||||
limit=longTakeProfit)
|
||||
|
||||
if (strategy.position_size < 0)
|
||||
strategy.exit("Short TP/SL", "Short",
|
||||
stop=shortStopPrice,
|
||||
limit=shortTakeProfit)
|
||||
|
||||
// Close all positions after 5000 candles
|
||||
if (order_count == maxOrderCount)
|
||||
strategy.close_all()
|
||||
|
||||
// Plotting for visual reference
|
||||
plotshape(candleGreen and barstate.isconfirmed and within_trading_period, title="Long Entry", location=location.belowbar, style=shape.triangleup, size=size.small, color=color.green)
|
||||
plotshape(candleRed and barstate.isconfirmed and within_trading_period, title="Short Entry", location=location.abovebar, style=shape.triangledown, size=size.small, color=color.red)
|
||||
|
||||
// Plot line showing where trading stops
|
||||
plot(order_count == maxOrderCount ? high + (high * 0.001) : na, color=color.red, linewidth=2, style=plot.style_circles, title="Trading End Point")
|
||||
|
||||
// Plot line showing trading status
|
||||
bgcolor(isAfterStartDateTime ? (isWithinTimeWindow ? color.new(color.green, 80) : color.new(color.red, 80)) : na)
|
||||
20
misc/strategies/VWAP_Cross.pine
Normal file
20
misc/strategies/VWAP_Cross.pine
Normal file
@@ -0,0 +1,20 @@
|
||||
// Description: This script implements the VWAP Cross Strategy, which generates buy and sell signals when the price crosses the Volume Weighted Average Price (VWAP) line. This institutional approach is particularly relevant for intraday trading.
|
||||
//@version=4
|
||||
study("VWAP Cross Strategy", shorttitle="VCS", overlay=true)
|
||||
|
||||
// Calculate VWAP
|
||||
vwap_sum = cum(close * volume)
|
||||
volume_sum = cum(volume)
|
||||
vwap = vwap_sum / volume_sum
|
||||
|
||||
// Generate buy and sell signals
|
||||
buySignal = crossover(close, vwap)
|
||||
sellSignal = crossunder(close, vwap)
|
||||
|
||||
// Plot VWAP on the chart
|
||||
plot(vwap, title="VWAP", color=color.orange, linewidth=2)
|
||||
|
||||
// Plot buy and sell arrows
|
||||
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
|
||||
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
|
||||
|
||||
Reference in New Issue
Block a user