英文:
Pine Script strategy.entry order that follows a moving average
问题
我正在尝试在一组条件满足时以移动平均为基础下订单,这部分正常运作。但我希望在每个柱状图关闭时更新订单以跟随平均上下波动。对于Pine Script相对新手,不确定是否可行。
以下是您的代码,我将不翻译代码部分,只提供注释和说明:
// 设置条件
setupSqueeze = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid ? 1 : 0
setupSqueezeSum = math.sum(setupSqueeze, 8)
// 突破条件
breakOut = BB_lower < KC_lower_low or BB_upper > KC_upper_low ? 1 : 0
breakOutSum = math.sum(breakOut, 2)
// 交易条件
condition1 = setupSqueezeSum >= 2 and breakOutSum >= 2
if (condition1 == true and close > ema4 and strategy.position_size == 0)
strategy.entry("Long", strategy.long, limit = ema3)
strategy.exit("Long Stop", from_entry = "Long", loss = 2000, profit = 4000)
if (condition1 == true and close < ema4 and strategy.position_size == 0)
strategy.entry("Short", strategy.short, limit = ema3)
strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
如果您有任何进一步的问题或需要帮助,请告诉我。
英文:
I'm trying to place a limit order at a moving average when a set of conditions are meant, which is working fine. But I would like the order to be updated at the close of every bar to follow the average up/down. Pretty new to Pine Script and don't know if this is possible.
Here is my code:
//SETUP
setupSqueeze = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid ? 1 : 0
setupSqueezeSum = math.sum(setupSqueeze, 8)
//-------------------------------------------------------------------------------------------------------------------------------------
//BREAKOUT
breakOut = BB_lower < KC_lower_low or BB_upper > KC_upper_low ? 1 : 0
breakOutSum = math.sum(breakOut, 2)
//-------------------------------------------------------------------------------------------------------------------------------------
//TRADING CONDITIONS
condition1 = setupSqueezeSum >= 2 and breakOutSum >= 2
//-------------------------------------------------------------------------------------------------------------------------------------
if (condition1 == true and close > ema4 and strategy.position_size == 0)
strategy.entry("Long", strategy.long, limit = ema3)
strategy.exit("Long Stop", from_entry = "Long", loss = 2000, profit = 4000)
if (condition1 == true and close < ema4 and strategy.position_size == 0)
strategy.entry("Short", strategy.short, limit = ema3)
strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
答案1
得分: 0
你可以使用 strategy.opentrade 来测试是否有交易正在进行。
你可以使用 id_entry 来确定当前交易是做多还是做空。
然后根据你的需求调整退出参数:
if strategy.opentrade > 0
if strategy.opentrades.entry_id(strategy.opentrades-1)=="Long"
strategy.exit("Long Stop", from_entry = "Long", loss = NewValueLoss, profit = NewValueProfit)
else if strategy.opentrades.entry_id(strategy.opentrades-1)=="Short"
strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
英文:
You can use strategy.opentrade to test if a trade is on.
You can use id_entry to know if the actual trade is a long or a short.
Then adjust the exit parameters to your wish :
if strategy.opentrade > 0
if strategy.opentrades.entry_id(strategy.opentrades-1)=="Long"
strategy.exit("Long Stop", from_entry = "Long", loss = NewValueLoss, profit = NewValueProfit)
else if strategy.opentrades.entry_id(strategy.opentrades-1)=="Short"
strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论