简体   繁体   English

有没有办法在 Pine Script 中获得当天第一根和第二根蜡烛的 OHLC

[英]Is there a way to get the OHLC of first and second candle of the day in Pine Script

I want the OHLC data of the first and second candle of the day in 5 minute time frame.我想要 5 分钟时间范围内当天第一根和第二根蜡烛的 OHLC 数据。 That is, OHLC of 1st 5 min candle and OHLC of 2nd 5 min candle.即第一个 5 分钟蜡烛的 OHLC 和第二个 5 分钟蜡烛的 OHLC。 I have tried the following.我尝试了以下方法。

is_newbar(res) =>
    t = time(res)
    change(t) != 0 ? 1 : 0

is_newbar1(res) =>
    t = time(res)
    change(t[1]) != 0 ? 1 : 0

newbar = is_newbar("D")
newbar1 = is_newbar1("D")

var float s1 = na
var float s2 = na
var float s3 = na
var float s4 = na
if newbar
    s1 := low
    s2 := high
    s3 := close
    s4 := open

var float s11 = na
var float s12 = na
var float s13 = na
var float s14 = na
if newbar1
    s11 := low
    s12 := high
    s13 := close
    s14 := open

After getting these values I am coding for range breakouts like if s13>s3 then do something.获得这些值后,我正在为范围突破编码,例如 if s13>s3 then do something. The problem is, in real-time it not only takes the 1st two candle values, but considers every candle OHLC and tries to match the if condition everytime.问题是,它不仅实时获取前两个蜡烛值,而且考虑每根蜡烛 OHLC 并尝试每次都匹配 if 条件。 I want it to execute once and display the result.我希望它执行一次并显示结果。 Please help me out.请帮帮我。

Hi @Prasanna Kumar S R,嗨@Prasanna Kumar S R,

Instead of using an if and save the value into a variable, try to use "valuewhen".不要使用 if 并将值保存到变量中,而是尝试使用“valuewhen”。 I have been working on a strategy where I have to save the first handle high.我一直在制定一个策略,我必须将第一个手柄保存在高位。 I used the following code '''d = valuewhen( (is_first), high, 0 )''' when i plot the "d" its constant till end of the day.我在 plot 中使用了以下代码 '''d = valuewhen( (is_first), high, 0 )''' ,“d”一直保持不变,直到一天结束。

Here is what I usually use for selling and buying at a specific time.这是我通常用于在特定时间进行买卖的东西。 I used this script for backtesting a BTST strategy.我使用这个脚本来回测 BTST 策略。 Since you want to trigger the buy/sell actions at a specific time you can actually provide the hour and minutes in the scripts.由于您想在特定时间触发买入/卖出操作,您实际上可以在脚本中提供小时和分钟。 All you need to make sure is that there is a close point at your desired point, basically, keep the resolution smaller than 5 mins and can give a candle at 5 min interval.您只需要确保在您想要的点上有一个接近点,基本上,保持分辨率小于 5 分钟,并且可以每隔 5 分钟给出一个蜡烛。 (in your case it is 1 min and 5 mins candles) (在你的情况下是 1 分钟和 5 分钟的蜡烛)

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SaiyadRasikh
//@version=4
strategy("Stackoverflow_Answer", overlay = true)

// Allowing only Long entries
strategy.risk.allow_entry_in(strategy.direction.long)

//Considering Nifty as the Index
var s_close = 0.0

if (hour(time) == 09 and minute(time) == 20)
    s_close := close
    
buy = s_close > s_close[1] //Condition where second candleis close is higher than first candle close, Then enter the trade
sell = s_close < s_close[1]

strategy.entry("Long", strategy.long, when = buy,comment="Buy")
strategy.entry("Short", strategy.short, when = sell,comment="Sell")

You will get something like this with the above code as a plot.上面的代码是 plot,你会得到类似的东西。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM