代码之家  ›  专栏  ›  技术社区  ›  Brandon Koo

strategy.short不下订单,strategy.long下订单

  •  0
  • Brandon Koo  · 技术社区  · 2 年前

    我是pinescrpt的新手,遇到了一个我无法解决的问题,真的需要一些帮助。我不知道为什么strategy.order(“Long”,strategy.Long,stop=trailingStopLoss)通过为Long下订单来工作,但strategy.order(“Short”,trategy.Short,stod=trailingStopLossS)不下订单。这是我现在拥有的代码的一部分。

    ///  Long Condition  ///
    
    condition1 = close >= sessionHighPrice and isInSession 
    condition2 =  (ta.crossunder(high, sessionHighPrice) or ta.crossunder(close, sessionHighPrice) or ta.crossunder(low, sessionHighPrice)) and isInSession
    condition3 = lowertimevariable >= sessionHighPrice
    
    // Track whether condition1 has been met in the session
    var bool condition1Met = false
    var bool condition2Met = false
    var bool condition3Met = false
    
    // Check for condition1 and set condition1Met to true when it's met
    if condition1[Timeequal5m] and not inSession
        condition1Met := true
    
    // Check for condition1 and set condition1Met to true when it's met
    if  condition1Met and condition2[Timeequal5m] and not inSession
        condition2Met := true
    
    
    if  condition2Met and condition3[0] and not inSession
        condition3Met := true
    
    var bool crosscondone = false
    trailingStopLoss = 0.0
    stopvalue_long = 0.0
    
    if (strategy.position_size > 0)
            if (high < partialExitPriceper)
                trailingStopLoss := math.max(perstop1, trailingStopLoss[1])
            else if crossoverConditionper and not crosscondone //(high >= partialExitPriceper and high < SetTrailingper)
                trailingStopLoss := math.max(entryprice, trailingStopLoss[1])
                crosscondone := true
            else if (high >= partialExitPriceper)
                stopvalue_long := math.max(high - ((trailingOffset / 100) * entryprice), entryprice)
                trailingStopLoss := math.max(stopvalue_long, trailingStopLoss[1])
    
    
    
    ///   Short Condition  ///
    
    condition1S = close <= sessionLowPrice and isInSession 
    condition2S =  (ta.crossover(low, sessionLowPrice) or ta.crossover(close, sessionLowPrice) or ta.crossover(high, sessionLowPrice)) and isInSession
    condition3S = lowertimevariableS <= sessionLowPrice
    
    // Track whether condition1 has been met in the session
    var bool condition1SMet = false
    var bool condition2SMet = false
    var bool condition3SMet = false
    var bool flag_condition3SMet = false
    
    // Check for condition1 and set condition1Met to true when it's met
    if condition1S[Timeequal5m] and not inSession
        condition1SMet := true
    
    // Check for condition1 and set condition1Met to true when it's met
    if  condition1SMet and condition2S[Timeequal5m] and not inSession
        condition2SMet := true
    
    
    if  condition2SMet and condition3S[0] and not inSession
        condition3SMet := true
    
    var bool crosscondoneS = false
    trailingStopLossS = 0.0
    stopvalue_short = 0.0
    
    if (strategy.position_size < 0)
            if (low > partialExitPriceperS)
                trailingStopLossS := math.min(perstop1S, trailingStopLossS[1])
            else if crossoverConditionperS and not crosscondoneS
                trailingStopLossS := math.min(entryprice, trailingStopLossS[1])
                crosscondoneS := true
            else if (low <= partialExitPriceperS)
                stopvalue_short := math.min(low + ((trailingOffset / 100) * entryprice), entryprice)
                trailingStopLossS := math.min(stopvalue_long, trailingStopLossS[1])
    
    
    
    
    ///   Execute Strategy  ///
    
    if condition1Met and condition2Met and condition3Met and strategy.position_size == 0
        strategy.order("Long", strategy.long, stop = trailingStopLoss)
    
    
    if condition1SMet and condition2SMet and condition3SMet and strategy.position_size != 0
        strategy.order("Short", strategy.short, stop = trailingStopLossS)
    
    
    if strategy.position_size > 0
        if low < trailingStopLoss
            strategy.close("Long", comment = "Stop Loss 100", qty_percent = 100)
    
    if strategy.position_size < 0
         if high > trailingStopLoss
             strategy.close("Short", comment = "Stop Loss 100", qty_percent = 100)
    
    
    
    

    我已经检查了条件,它似乎在短期和长期运行良好,我不确定我的代码是否冲突,这就是为什么我无法输入短期或我应该做什么

    0 回复  |  直到 2 年前
        1
  •  0
  •   vgladkov    2 年前

    strategy.order 函数将订单按所需方向放置,默认情况下尺寸为1。

    在您的代码中 strategy.order(short) 仅当 strategy.position_size != 0 strategy.order(long) 回测中的调用总是第一个,因为 strategy.position_size!=0 只有在战略具有开放贸易的情况下才是正确的。

    因此,您的策略首先打开大小为1的长订单(因为您没有定义订单大小),然后调用 strategy.close() 长期,或 策略.订单(短) 在任何情况下,它只关闭了您当前的位置,因为 策略.订单(短) 默认大小为1的看涨期权,所以你打开的多头+1头寸以空头-1头寸的看涨期权收盘,因为要做空,你应该卖出所有当前合约,而你的头寸大小再次为0。

    所以,如果你想转到你当前位置的对面,你需要创建一个比当前位置大的对面订单,或者使用 strategy.entry() 如上文评论中所述。

    strategy.entry() 通过添加到订单的当前位置大小,自动计算具有定义大小的转到所需方向的位置大小。