有谁知道我如何在某个日期之后绘制定义变量的每次出现?因此,在某个时间帧输入之前忽略所有事件。我能够找到这个:How to plot only the last x period 但是,这似乎只在输入日期绘制了变量的一次出现。
例如和简单如何在用户输入日期之后绘制每个十字星:
//@version=5
indicator('Plot all Doji after input date and ignore before input date', overlay=true)
monthsBack = input.int(3, minval=0)
doji = close == open
targetDate = time >= timestamp(year(timenow), month(timenow) - monthsBack, 1, 0, 0, 0)
beginMonth = not targetDate[1] and targetDate
var float valueToPlot = doji
if beginMonth
valueToPlot := high
valueToPlot
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na, transp=90)
更新:
哇,谢谢你,我没想到会有这么彻底的答案。阅读您的回复后,现在对我来说很有意义。
我主要是一名交易员,而不是程序员,所以我很高兴看到我的交易策略开始以更自动化的方式结合在一起。这个脚本只是我最有利可图的交易策略的一部分,它将帮助我节省大量用于识别水平的时间。这也将帮助我转移到一个新的符号,其级别准备好更快,因为我在过去两年一直在手绘我的级别,这很耗时。
我应该在我原来的帖子中说“测试蜡烛”而不是“十字星”。我只是想为蜡烛类型使用占位符,因为为我正在寻找的实际条件编写代码会有些复杂。但是,我想确保我能够在开始之前绘制我需要看到的内容。
截屏
这是我通常如何手绘价格水平的屏幕截图。现在自动完成一个脚本!
这是我在 shanem 的帮助下得到的脚本,它仍在进行中,但要到达那里:
//@version=5
// with help from shanem it works!
indicator("Identify [a specific kind of candle] in past N months", shorttitle = "Auto Price Levels", overlay = true)
// 1) Define inputs used in the script
monthsBack = input.int(8, minval=0, title="How many months back should we scan for [candle type]?")
// 2) Declare variables used in the script, and set defaults (once per chart)
var tn = timenow
var firstDate = timestamp(year(tn), month(tn) - monthsBack, 1, 0, 0, 0)
var testCandle = false
var okayToPlot = false
// 3) Calculations.
okayToPlot := time > firstDate
// A note about the candle type. For the purpose of this script I plugged in a quick candle description to limit the number of
// candles that would appear on the chart, and to reduce the amount of code to deal with to focus on the 'plot' code. I first
// wanted to see if it was possible to plot what I needed to see before I set out to create code for the specifi candle I'm
// looking for. I will either fully describe the candle in the future script, or import from a Pine Library since it is somewhat complex.
testCandle := if close == open and barstate.isconfirmed and session.ismarket
close
// 4) Output / Plotting
// Plot a price line on the close of each occurance of the [candle type].
// Also, highlight the high and low of the [candle type] and extend right.
plot(okayToPlot and testCandle ? close : na, style = plot.style_circles, linewidth = 3, color = color.rgb(245, 66, 221))
if testCandle and okayToPlot
line.new(bar_index - 1, close, bar_index, close, color = color.rgb(255, 164, 23), extend = extend.right, width = 1)
box.new(bar_index - 1, high, bar_index, low, border_color = na, bgcolor = color.rgb(135, 135, 135, 80), extend = extend.right)
// To do next, working on now:
// 1. Define the candle and limit the timeframe (or resolution) the price level is calculated on.
// Perhaps 30 minute. But, allow the price levels to be seen on all other timeframes.
// Seen on 1m, 5m, 4hr, daily, etc., but derived from 30m (just example).
// 2. Alerts. Send JSON payload via webhook if current price in range. Found this and modified version works good:
// User "wlhm" - https://www.tradingview.com/script/FPq2xKyZ-DiscordWebhookFunction/