【问题标题】:Close a position after a "x" days (pine editor - tradingview)在 "x" 天后平仓 (pine editor - tradingview)
【发布时间】:2019-11-15 16:11:56
【问题描述】:

我是新手,我已经制定了交易策略,但我无法在一定数量的蜡烛上关闭操作。 我使用了“barssince”这个表达,但它在满足条件后才算数;有时它会发出一个信号,然后它会返回以满足条件,并再次重新计算收盘蜡烛。 如果有人可以帮助我,当然。谢谢。

【问题讨论】:

  • 请提供您如何平仓的代码
  • 而且你应该知道,X 天与 X 支蜡烛之前不一样。有不同的时间范围(如盘中)和不同的时段,因此蜡烛的数量不可能像有天那样多。
  • 谢谢 Michel,我需要 X 支蜡烛。
  • 嗨,克里斯蒂安,请添加一些代码或有关您的问题的更多信息,您的问题太宽泛了。
  • 嗨 sebasaenz,我在这个答案下添加了 mi 代码

标签: pine-script


【解决方案1】:

我写了一个简单的策略,在 20 天内关闭。我认为它显示了如何在 N 天内平仓。

//@version=4
strategy("My Strategy", overlay=true)

entryTime = 0.0
entryTime := entryTime[1]

if not na(strategy.position_size) and strategy.position_size > 0 and na(entryTime)
    entryTime := time

longCondition = dayofweek == dayofweek.monday
if longCondition
    strategy.entry("buy", true)

MILLIS_IN_DAY = 24 * 60 * 60 * 1000
CLOSE_AFTER_DAYS = 20
if not na(entryTime) and time - entryTime >= CLOSE_AFTER_DAYS * MILLIS_IN_DAY
    strategy.close("buy")
    entryTime := na

如果在 N 个柱后收盘:

//@version=4
strategy("My Strategy", overlay=true)

enterIndex = 0.0
enterIndex := enterIndex[1]

inPosition = not na(strategy.position_size) and strategy.position_size > 0
if inPosition and na(enterIndex)
    enterIndex := bar_index

longCondition = dayofweek == dayofweek.monday
if longCondition
    strategy.entry("buy", true)

CLOSE_AFTER_BARS = 20
if not na(enterIndex) and bar_index - enterIndex + 1 >= CLOSE_AFTER_BARS
    strategy.close("buy")
    enterIndex := na

Ver.3:更改了 OP 的脚本

//@version=3
strategy("StoJor", overlay=true)

// enterIndex is index of the bar where we've entered in the position. It's empty at the begin

// put here 0.0 just to give to Pine-Script compiller understanding
// what the type of enterIndex is (float)
enterIndex = 0.0 
enterIndex := enterIndex[1] // here it's becoming 'na' till we've entered to a position

// check that we are not in the position. As an order is filled strategy.position_size becomes a number
inPosition = not na(strategy.position_size) and strategy.position_size != 0
if inPosition and na(enterIndex)
    enterIndex := n // preserve an index of the bar where we entered to current position

// the function checks if we've reached N bars in position
itNthBarInPos(closeAfterBars) => not na(enterIndex) and n - enterIndex + 1 >= closeAfterBars


len = input(25, minval=1, title="Tiempo Stochastic") 
smoothK = input(1, minval=1, title="SmoothK Stochastic")
smoothD = input(1, minval=1, title="SmoothD Stochastic")
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)
min = input(6, minval=1, title="Min")
max = input(96, minval=1, title="Max")

// position's direction. constants for easier using of it:
NONE = 0
SHORT = -1
LONG = 1

direction = NONE
direction := direction[1]

// I don't know Spanish, but suppose the minutos_cierre input meand close after 'minutos_cierre' bars
minutos_cierre = input(3, title='Minutos cierre', minval=1)

STOLONG = crossunder(k, min)
STOSHORT = crossover(k, max)
if STOLONG
    strategy.entry("STOLONG", strategy.long)
    direction := LONG

if itNthBarInPos(minutos_cierre) and direction == LONG
    strategy.close(id = "STOLONG")
    enterIndex := na // set na to enterIndex to mark that we've exited from the position
    direction := NONE

if STOSHORT
    strategy.entry("EMASHORT", strategy.short)
    direction := SHORT

if itNthBarInPos(minutos_cierre) and direction == SHORT
    strategy.close(id = "EMASHORT")
    enterIndex := na // set na to enterIndex to mark that we've exited from the position
    direction := NONE

【讨论】:

  • 如果对您有帮助,请在我的答案左侧标记灰色标记。
【解决方案2】:

感谢米歇尔的回复。 代码是:

//@version=3
strategy("StoJor", overlay=true)

len = input(25, minval=1, title="Tiempo Stochastic") 
smoothK = input(1, minval=1, title="SmoothK Stochastic")
smoothD = input(1, minval=1, title="SmoothD Stochastic")
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)
min = input(6, minval=1, title="Min")
max = input(96, minval=1, title="Max")
minutos_cierre = input(3, title='Minutos cierre', minval=1)
STOLONG = crossunder(k, min)
STOSHORT = crossover(k, max)
strategy.entry("STOLONG", strategy.long, when=STOLONG)
strategy.close(id = "STOLONG", when = barssince(STOLONG) >= minutos_cierre)
strategy.entry("EMASHORT", strategy.short, when=STOSHORT)
strategy.close(id = "EMASHORT", when = barssince(STOSHORT) >= minutos_cierre)

我需要在下一根蜡烛的第四个关闭。 我很欣赏你的代码,但由于我只是在学习,我还不能完全理解它

【讨论】:

  • 请不要将答案发布为 cmets。只需编辑您的问题即可添加更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2020-02-10
相关资源
最近更新 更多