提问人:PirTrade 提问时间:11/6/2023 最后编辑:PirTrade 更新时间:11/7/2023 访问量:9
解决使用后期定义的变量的代码依赖关系
Solve Code dependency for using a variable which defined at later stage
问:
我有一个 scrit 来帮助进行 ThinkOrSWim 交易。在脚本中,有一个 tradePL 变量正在工作,现在我需要在脚本的前面使用它的值来表示 stopSignal,问题是它尚未定义,并且存在代码依赖性,不允许重新组织其在代码中的位置。下面是显示依赖项的代码:
input MaxTradeLoss = 300;
def StopTrade = if (TradePL >= MaxTradeLoss and TradePL < 0) then 1 else 0;
def BuyStop = if !useStops then 0 else if state == state.long and (haOpen > haClose) and Flat or (currentTime2 <= 0) or StopTrade==1 then 1 else 0 ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if state == state.short and (haOpen < haClose) and Flat or (currentTime2 <= 0) or StopTrade==1 then 1 else 0 ; # insert condition to stop in place of the 0>0
def CurrentPosition; # holds whether flat = 0 long = 1 short = -1
if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
CurrentPosition = 0;
} else {
if CurrentPosition[1] == 0 { # FLAT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellSignal) {
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (SellSignal) {
CurrentPosition = -1;
} else if (BuyStop and useStops) {
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellStop and useStops) {
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}
def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;
def isOrder = if CurrentPosition..
(其余无关紧要)
def orderPrice = if (isOrder and..
(其余无关紧要)
def TradePL = if isLong then Round(((close - orderPrice).....
(其余无关紧要)
如您所见,目标是使用 StopTrade(基于脚本底部的 TradePL)成为 BuyStop/SellStop 条件(在顶部)的一部分,但我无法将其向上移动,因为代码的每个粗体部分都取决于它们上面的部分。TradePL 依赖于 IsLong 和 OrderPrice,后者依赖于 IsOrder,后者依赖于 CurrentPostion 计算,该计算将 BuyStop/SellStop 作为条件之一 - 我想修改它以包括在底部的 TradePL 上中继的条件。
有编码解决方案吗?尝试创建新变量,但这无济于事,因为代码结构和依赖关系将适用,因为每个代码都依赖于他上面的代码。任何帮助都将受到高度赞赏。
当然,错误是顶部的 TradePL 尚未发现:
No such variable: TradePL at 296:21
No such variable: TradePL at 296:49
No such variable: TradePL at 296:21
No such variable: TradePL at 296:49
谢谢!
我尝试在脚本的早期使用 TradePL,但它尚未定义,并且代码依赖项不允许我重新定位代码,新变量也无济于事,因为逻辑和依赖关系将保留。
答: 暂无答案
评论