PineScript:在一天中的特定时间关闭日间交易头寸

时间:2020-05-25 07:16:39

标签: pine-script

使用TradingView的Pinescript,我如何编程回测策略以(在1天的柱分辨率下):

1)如果前一天的收盘价标记为买入条件,则使用市场订单在市场开盘价中输入多头头寸。 (购买条件是RSI(7)高于50,MACD线高于信号线)

2)将止损设置为平均日均线(14)的1倍

3)在东部时间15:55:00下达卖单,以在收盘前关闭整个头寸。

4)止损触发器会取消其他卖单,反之亦然(例如,这是OCO订单)

5)收盘后每次出现购买条件时都要重复一次。

换句话说,通过这种策略,我希望以1倍的ADR止损价在开盘市场进行日间交易,并在平仓之前立即平仓。这样可以避免下班后出现空白。

感谢您的慷慨!

Edit:
here's what I have so far (as requested)
    //@version=4
    strategy("Trendability Strategy", overlay=true)
    [macdLine, signalLine, histLine] = macd(close, 12, 26, 9)
    rsiLine = rsi(close, 7)
    stochLine = sma(sma(stoch(close, high, low, 14),3),3)
    signal = histLine > -0.05 and rsiLine > 40 and stochLine > 40 ? "buy" : 
    histLine <= -0.05 and rsiLine <= 40 and stochLine <= 40 ? "sell" : "none"
    palette = signal == "buy" ? color.lime : signal == "sell" ? color.red : 
    color.black
    plotbar(open, high, low, close, color=palette)

    strategy.entry("Long", strategy.long, when = signal == "buy") 
    strategy.close("Long", when = signal == "none") 

1 个答案:

答案 0 :(得分:1)

我不确定PineScript是否具有此功能。我确实有来自Quantopian的python代码。我不确定这是否有帮助。大多数经纪人可以选择关闭日间交易者的所有头寸(或部分头寸),因此我认为这将是非常标准的功能。

def initialize(context):
    # Algorithm will call myfunc every day 15 minutes before the market closes
    schedule_function(
        myfunc,
        date_rules.every_day(),
        time_rules.market_close(minutes=15)
    )

def myfunc(context,data):
    pass

如果您只是想避免在盘后交易,这里是用户手册中的示例

strategy("closeEntry Demo", overlay=false)
strategy.entry("buy", true, when = open > close)
strategy.close("buy", when = open < close, qty_percent = 50, comment = "close buy entry for 50%")
plot(strategy.position_size)