如何在蜡烛开盘时开仓,并在蜡烛收盘时平仓?

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

How to open order at candle open and close at candle close?

问题

以下是您要翻译的部分:

我正在尝试编写一个Pine Script策略,根据条件进行多单交易,并在蜡烛结束时关闭订单,除非触发了止损条件。

为此,我使用了策略配置选项"calc_on_order_fills = true",以便在订单被填充时重新计算策略(在蜡烛开始时),以便可以在蜡烛结束时执行关闭订单(关闭订单会延迟到蜡烛的持续时间)。

订单正确打开,但逻辑不按预期工作,因为关闭订单只会在执行多单后的第二个蜡烛的开盘时执行(而不是在同一蜡烛的收盘时执行)。
我不确定我漏掉了什么,以下是代码(为清晰起见,已删除了多单执行部分):

英文:

I am trying to write a Pine Script strategy that passes a long order based on a condition and closes the order at the end of the candle, whatever happens unless a stop loss is reached.

For that I use the strategy configuration option "calc_on_order_fills = true" so that the strategy is recalculated when the order is filled (at the beginning of the candle) and so that the close order can be executed at the end of the candle (the close order is delayed by the duration of the candle).

Orders are correctly opened but the logic does not work as intended as close order would only execute at the open 2 candles after the long was executed (instead of at the close of the same candle).
I am not sure what I am missing, and here is the code (long execution removed for clarity as they are working as intended):

//@version=5
strategy(title="Keltner Strategy v3", shorttitle="KS3", overlay=true, close_entries_rule="ANY", calc_on_order_fills = true)

// Create inputs that configure the backtest's date range
useDateFilter = input.bool(true, title="Begin Backtest at Start Date",
     group="Backtest Time Period")
backtestStartDate = input.time(timestamp("1 Jan 2023"), 
     title="Start Date", group="Backtest Time Period",
     tooltip="This start date is in the time zone of the exchange " + 
     "where the chart's instrument trades. It doesn't use the time " + 
     "zone of the chart or of your computer.")

// See if current bar happens on, or later than, the start date
inTradeWindow = not useDateFilter or time >= backtestStartDate

// Converting the time frame in seconds for delaying close orders
timeFrameSeconds = timeframe.in_seconds()

// Condition to check if a candle closes outside the channel and RSI is > 70 or < 30
bearishSignal = src > open and src > 1.0025 * upperBand and rsiVal > 70
bullishSignal = src < open and src < 0.9975 * lowerBand and rsiVal < 30

if (inTradeWindow and bearishSignal and strategy.opentrades == 0)
    stopLoss1 = close * 1.011
    strategy.entry("sell1", strategy.short, qty = 1)
    label.new(bar_index, high, text="EXE")
    strategy.exit("Exit sell1", "sell1", stop=stopLoss1)

if (inTradeWindow and bearishSignal and strategy.opentrades >= 1)
    stopLoss2 = close * 1.011
    strategy.entry("sell2", strategy.short, qty = 1)
    label.new(bar_index, high, text="EXE")
    strategy.exit("Exit sell2", "sell2", stop=stopLoss2)

for i = 0 to strategy.opentrades-1
    if (time - strategy.opentrades.entry_time(i) >= timeFrameSeconds*1000)
        entry = strategy.opentrades.entry_id(i)
        strategy.close(entry)

答案1

得分: 0

如果您想在每根K线关闭时关闭您的订单,您应该使您的脚本仅在K线关闭时执行,并以以下方式开始您的脚本:

strategy.close_all()

这样,在脚本开始时,如果有订单已打开,它将被关闭,然后您的脚本将决定是否应该开启一个订单。

注意:您当前的脚本不完整,并会生成错误。

英文:

if you want to close your order on each close of the bar, you should make your script execute only on the close bar and begin your script with :

strategy.close_all()  

This way, at the beginning of the script if an order is open, it will be closed and then your script will decide if it should open an order or not.

NB : your actual script isn't complete and generate errors.

答案2

得分: 0

我找到了解决方案,代码如下所示。
它基本上在蜡烛收盘时开仓,并在下一个蜡烛收盘时平仓。这在加密货币市场是可能的,因为收盘价和开盘价之间没有太大的差距。

英文:

I found my own solution to the problem, see code as follow.
It basically opens the trade at the close of a candle and closes it at the next candle close. This is possible mostly in crypto as there is no wide gaps between a close and an open.

//@version=5
strategy(title="Keltner Strategy v3", shorttitle="KS3", overlay=true, close_entries_rule="ANY", process_orders_on_close = true)

// Create inputs that configure the backtest's date range
useDateFilter = input.bool(true, title="Begin Backtest at Start Date",
     group="Backtest Time Period")
backtestStartDate = input.time(timestamp("1 Jan 2023"), 
     title="Start Date", group="Backtest Time Period",
     tooltip="This start date is in the time zone of the exchange " + 
     "where the chart's instrument trades. It doesn't use the time " + 
     "zone of the chart or of your computer.")

// See if current bar happens on, or later than, the start date
inTradeWindow = not useDateFilter or time >= backtestStartDate

// 
timeFrameSeconds = timeframe.in_seconds()

// Condition to check if a candle closes outside the channel and RSI is > 70 or < 30
bearishSignal = src > open and src > 1.0025 * upperBand and rsiVal > 70
bullishSignal = src < open and src < 0.9975 * lowerBand and rsiVal < 30

for i = 0 to strategy.opentrades-1
    if time - strategy.opentrades.entry_time(i) >= 3590*1000
        entry = strategy.opentrades.entry_id(i)
        strategy.close(entry)

if (inTradeWindow and bearishSignal and strategy.opentrades == 0)
    stopLoss1 = close * 1.011
    strategy.entry("sell1", strategy.short, qty = 1)
    label.new(bar_index, high, text="SELL_1")
    strategy.exit("Exit sell1", "sell1", stop=stopLoss1)

if (inTradeWindow and bearishSignal and strategy.opentrades >= 1)
    stopLoss2 = close * 1.011
    strategy.entry("sell2", strategy.short, qty = 1)
    label.new(bar_index, high, text="SELL_2")
    strategy.exit("Exit sell2", "sell2", stop=stopLoss2)

huangapple
  • 本文由 发表于 2023年5月30日 05:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360376.html
匿名

发表评论

匿名网友

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

确定