Session criteria is ignored.

huangapple go评论58阅读模式
英文:

Session criteria is ignored

问题

我想使用会话标准来限制策略在其活动时间内。

据我所知,应该使用Session函数来实现,但我的逻辑似乎有问题。

i_Handelszeit = input.session(title="Haupthandelszeit", defval="0700-2000")

// 检查过滤器
isInSession(_sess) => not na(time(timeframe.period, i_Handelszeit))
f_dateFilter = time >= i_startTime and time <= i_endTime and isInSession(i_Handelszeit)

// 检查买入/卖出条件
buyCondition = close > ma1 and close < ma2 and strategy.position_size == 0 and f_dateFilter

----------------------------------
这里是完整的代码... 不是我的财产。

// 此源代码受 Mozilla Public License 2.0 的条款约束,网址为 https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=5
strategy("Simple Pullback Strategy",
     overlay=true,
     initial_capital=50000,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=100, // 每笔交易投资余额的100%
     commission_type=strategy.commission.cash_per_contract,
     commission_value=0.005) // 互动经纪费率

// 获取用户输入
i_ma1 = input.int(title="MA 1 长度", defval=200, step=10, group="Strategy Parameters", tooltip="长期移动平均线")
i_ma2 = input.int(title="MA 2 长度", defval=10, step=10, group="Strategy Parameters", tooltip="短期移动平均线")
i_stopPercent = input.float(title="止损百分比", defval=0.10, step=0.1, group="Strategy Parameters", tooltip="安全止损百分比下降")

i_lowerClose = input.bool(title="低收盘价退出", defval=false, group="Strategy Parameters", tooltip="在上方MA2之上退出前等待低收盘价")
i_startTime = input.time(title="开始筛选器", defval=timestamp("01 Jan 1995 13:30 +0000"), group="Time Filter", tooltip="开始搜索设置的日期和时间")
i_endTime = input.time(title="结束筛选器", defval=timestamp("1 Jan 2099 19:30 +0000"), group="Time Filter", tooltip="停止搜索设置的日期和时间")
i_Handelszeit = input.session(title="Haupthandelszeit", defval="0700-2000")

// 获取指标值
ma1 = ta.sma(close, i_ma1)
ma2 = ta.sma(close, i_ma2)

// 检查过滤器
isInSession(_sess) => not na(time(timeframe.period, i_Handelszeit))
f_dateFilter = time >= i_startTime and time <= i_endTime and isInSession(i_Handelszeit)

// 检查买入/卖出条件
var float buyPrice = 0
buyCondition = close > ma1 and close < ma2 and strategy.position_size == 0 and f_dateFilter
sellCondition = close > ma2 and strategy.position_size > 0 and (not i_lowerClose or close < low[1])
stopDistance = strategy.position_size > 0 ? ((buyPrice - close) / close) : na
stopPrice = strategy.position_size > 0 ? buyPrice - (buyPrice * i_stopPercent) : na
stopCondition = strategy.position_size > 0 and stopDistance > i_stopPercent

// 进入持仓
if buyCondition
    strategy.entry(id="Long", direction=strategy.long)

if buyCondition[1]
    buyPrice := open

// 退出持仓
if sellCondition or stopCondition
    strategy.close(id="Long", comment="Exit" + (stopCondition ? "SL=true" : ""))
    buyPrice := na

// 绘制漂亮的颜色
plot(buyPrice, color=color.lime, style=plot.style_linebr)
plot(stopPrice, color=color.red, style=plot.style_linebr, offset=-1)
plot(ma1, color=color.blue)
plot(ma2, color=color.orange)

这段代码工作正常,但未限制策略。 在图像中,您可以看到策略仍在上午7点至晚上8点之间工作。 它被忽略了。

英文:

I want to use the Session Criteria for limiting the Strategy in her active time.

As I know it should be working with the Session function, but something with my logic is not right.

i_Handelszeit   = input.session(title=&quot;Haupthandelszeit&quot;, defval = &quot;0700-2000&quot;)

// Check filter(s)
isInSession(_sess) =&gt; not na(time(timeframe.period, i_Handelszeit))
f_dateFilter = time &gt;= i_startTime and time &lt;= i_endTime  and isInSession(i_Handelszeit)

// Check buy/sell conditions
buyCondition    = close &gt; ma1 and close &lt; ma2 and strategy.position_size == 0 and f_dateFilter 


----------------------------------
Here the full code ... not my proberty. 

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// &#169; ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=5
strategy(&quot;Simple Pullback Strategy&quot;, 
     overlay=true, 
     initial_capital=50000,
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100, // 100% of balance invested on each trade
     commission_type=strategy.commission.cash_per_contract, 
     commission_value=0.005) // Interactive Brokers rate

// Get user input
i_ma1           = input.int(title=&quot;MA 1 Length&quot;, defval=200, step=10, group=&quot;Strategy Parameters&quot;, tooltip=&quot;Long-term MA&quot;)
i_ma2           = input.int(title=&quot;MA 2 Length&quot;, defval=10, step=10, group=&quot;Strategy Parameters&quot;, tooltip=&quot;Short-term MA&quot;)
i_stopPercent   = input.float(title=&quot;Stop Loss Percent&quot;, defval=0.10, step=0.1, group=&quot;Strategy Parameters&quot;, tooltip=&quot;Failsafe Stop Loss Percent Decline&quot;)

i_lowerClose    = input.bool(title=&quot;Exit On Lower Close&quot;, defval=false, group=&quot;Strategy Parameters&quot;, tooltip=&quot;Wait for a lower-close before exiting above MA2&quot;)
i_startTime     = input.time(title=&quot;Start Filter&quot;, defval=timestamp(&quot;01 Jan 1995 13:30 +0000&quot;), group=&quot;Time Filter&quot;, tooltip=&quot;Start date &amp; time to begin searching for setups&quot;)
i_endTime       = input.time(title=&quot;End Filter&quot;, defval=timestamp(&quot;1 Jan 2099 19:30 +0000&quot;), group=&quot;Time Filter&quot;, tooltip=&quot;End date &amp; time to stop searching for setups&quot;)
i_Handelszeit   = input.session(title=&quot;Haupthandelszeit&quot;, defval = &quot;0700-2000&quot;)


// Get indicator values
ma1 = ta.sma(close, i_ma1)
ma2 = ta.sma(close, i_ma2)

// Check filter(s)
isInSession(_sess) =&gt; not na(time(timeframe.period, i_Handelszeit))
f_dateFilter = time &gt;= i_startTime and time &lt;= i_endTime  and isInSession(i_Handelszeit)



// Check buy/sell conditions
var float buyPrice = 0
buyCondition    = close &gt; ma1 and close &lt; ma2 and strategy.position_size == 0 and f_dateFilter 
sellCondition   = close &gt; ma2 and strategy.position_size &gt; 0 and (not i_lowerClose or close &lt; low[1])
stopDistance    = strategy.position_size &gt; 0 ? ((buyPrice - close) / close) : na
stopPrice       = strategy.position_size &gt; 0 ? buyPrice - (buyPrice * i_stopPercent) : na
stopCondition   = strategy.position_size &gt; 0 and stopDistance &gt; i_stopPercent

// Enter positions
if buyCondition
    strategy.entry(id=&quot;Long&quot;, direction=strategy.long)

if buyCondition[1]
    buyPrice := open

// Exit positions
if sellCondition or stopCondition
    strategy.close(id=&quot;Long&quot;, comment=&quot;Exit&quot; + (stopCondition ? &quot;SL=true&quot; : &quot;&quot;))
    buyPrice := na

// Draw pretty colors
plot(buyPrice, color=color.lime, style=plot.style_linebr)
plot(stopPrice, color=color.red, style=plot.style_linebr, offset=-1)
plot(ma1, color=color.blue)
plot(ma2, color=color.orange)

This code is working but is not limiting the strategy.
enter image description here
In the Image you can see that the Strategy is still working between 20 o'clock and 7 o'clock in the morning.
It is ignored.

答案1

得分: 0

It works fine. However, it will work based on your exchange's timezone and not your local timezone.
(它运行正常。但是,它将根据您的交易所时区而不是本地时区运行。)

I added a bgcolor() to see what's happening.
(我添加了一个 bgcolor() 来查看发生了什么。)

If you want to use a custom timezone, you can use the third argument of time() which is timezone.
(如果您想使用自定义时区,可以使用 time() 的第三个参数,即 timezone。)

> timezone (series string) Timezone of the session argument. Can only
> be used when a session is specified. Optional. The default is
> syminfo.timezone. Can be specified in GMT notation (e.g. "GMT-5") or
> as an IANA time zone database name (e.g. "America/New_York").
(> timezone (series string) session 参数的时区。仅在指定 session 时才能使用。可选。默认值为 syminfo.timezone。可以使用 GMT 标记(例如 "GMT-5")或 IANA 时区数据库名称(例如 "America/New_York")来指定。)

英文:

It works fine. However, it will work based on your exchange's timezone and not your local timezone.

I added a bgcolor() to see what's happening.

Session criteria is ignored.

If you want to use a custom timezone, you can use the third argument of time() which is timezone.

time(timeframe, session, timezone) → series int

> timezone (series string) Timezone of the session argument. Can only
> be used when a session is specified. Optional. The default is
> syminfo.timezone. Can be specified in GMT notation (e.g. "GMT-5") or
> as an IANA time zone database name (e.g. "America/New_York").

huangapple
  • 本文由 发表于 2023年6月29日 19:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定