pine-script - 如果最后一个订单相反,我可以将交易视图策略限制为仅做空/做多吗
问题描述
我一直在用 pinescript 编写我的策略,除了一件我无法解决的事情之外,一切都很好。
有没有办法告诉策略只有在最后一个条目是空头时才做多,反之亦然?
编辑:很抱歉之前没有在下面添加正确的代码,但这是因为我从来没有看到它不允许我保存它,因为我在帖子中没有足够的信息。
我目前的代码是:
//@version=4
strategy("My Script", overlay = true)
ma = ema(close, 200)
// Base condition
aboveMa = close > ma
belowMa = close < ma
//long conditions
threeAboveMa = aboveMa[1] and aboveMa[2] and aboveMa[3]
first3AboveMa = not aboveMa[4] and threeAboveMa
//short conditions
threeBelowMa = belowMa[1] and belowMa[2] and belowMa[3]
first3BelowMa = not belowMa[4] and threeBelowMa
// Debugging
plot(ma)
plotchar(aboveMa, "aboveMa", ".", location.top)
plotchar(threeAboveMa, "threeAboveMa", "•", location.top)
plotchar(first3AboveMa, "first3AboveMa", "▲", location.top)
// Debugging
plot(ma)
plotchar(belowMa, "aboveMa", ".", location.bottom)
plotchar(threeBelowMa, "threeAboveMa", "•", location.bottom)
plotchar(first3BelowMa, "first3AboveMa", "▲", location.bottom)
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(8.0, title='Stop Loss %', type=input.float) / 100
takePer = input(8.0, title='Take Profit %', type=input.float) / 100
// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)
//TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - trailStop)
max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + trailStop)
min(stopValue, shortStopPrice[1])
else
999999
//Long Entry
entrylong() => threeAboveMa
exitlong() => first3AboveMa
//Short Entry
entryshort() => threeBelowMa
exitshort() => first3AboveMa
if (threeAboveMa)
strategy.entry(id = "long_ma", long = true, when = entrylong())
if (threeBelowMa)
strategy.entry(id = "short_ma", long = false, when = entryshort())
if (threeAboveMa)
strategy.exit(id = "exit_long", stop=longStop, limit=longTake)
if (threeBelowMa)
strategy.exit(id = "exit_short", stop=shortStop, limit=shortTake)
//strategy.entry(id = "long_ma", long = true, when = entry())
//strategy.close(id = "long_ma", when = exit())
解决方案
推荐阅读
- python - Python defaultdict(lambda: None) 没有 lambda
- css - 如何使用 calc() 获取该元素的当前高度?
- ios - 冷启动 iOS 14 时未处理 iOS Firebase 动态链接
- discord - 将消息内容保存到列表 discord.py
- javascript - 如何使用Javascript在网页中上传图像?
- python - 在字符串列表中查找最长的公共前缀字符串
- python - 同一时间段内使用 WMD 的文本相似度
- list - Flutter/Dart:不更改列表元素的并发修改异常
- c++ - 如何让我的程序在达到 10000 时退出 for 循环?C++
- directx - 支持 RTX 2060 和 D3D12 的 mGPU(多 GPU)AFR 可能吗?