首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何进行长/空交替交易

如何进行长/空交替交易
EN

Stack Overflow用户
提问于 2021-11-19 01:58:32
回答 1查看 180关注 0票数 1

我正在写一份策略,我需要避免重复两次多头交易或连续两次短期交易。这是多空交易交替进行的。我试着使用strategy.closedtrades.size,但是没有用,或者我漏掉了一些东西。

我也加了金字塔,但没什么用。

代码语言:javascript
复制
    // Davydov Strategy
    //@version=5
    strategy("Davydov Strategy", overlay=true, pyramiding=0)
    
    
    // Declaring stop loss (SL) take profit (TP)
    SL = input.float(0.5, "Stop loss", minval=0.1, maxval=100, step=0.1)
    TP = input.float(1, "Take profit", minval=0.1, maxval=100,step=0.1)
    
    // Interrupt the Long-Long / Short-Short cycle
    var count = 0
    longLong = strategy.closedtrades.size(0)>=0
    if longLong
    count := count + 1
    plot(count)
    
    // Condition Long
    
    // Price calculation for stop loss and take profit (Long)
    longStop = strategy.position_avg_price*(100-SL)/100
    longProfit = strategy.position_avg_price*(100+TP)/100
    
    // Condition check
    if trend_strength > 0
        if Greenbar1 and Greenbar2 == 1 ? RsiMa2 - 50 : na
            if direction < 0 ? supertrend : na
                strategy.entry("Long", strategy.long, na)
    
        // Conditions for closing a deal
    strategy.exit("Close","Long", stop=longStop, limit=longProfit, 
    when=strategy.position_size>=0)
    
    // Condition Short
    
        //Price calculation for stop loss and take profit
    shortStop = strategy.position_avg_price*(100+SL)/100
    shortProfit = strategy.position_avg_price*(100-TP)/100
    
        //  Condition check
    if trend_strength < 0
         if Redbar1 and Redbar2 == 1 ? RsiMa2 - 50 : na
            if direction < 0? na : supertrend
                strategy.entry("Short", strategy.short, na)
    
        // Conditions for closing a deal    
    strategy.exit("exit","Short", stop=shortStop,limit=shortProfit, 
    when=strategy.position_size<=0)


  [1]: https://i.stack.imgur.com/WuCXU.png
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-26 15:19:19

getLastPosSign()是您需要的助手函数:

代码语言:javascript
复制
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=5
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

getLastPosSign() =>
    strategy.closedtrades > 0 ? math.sign(strategy.closedtrades.size(strategy.closedtrades-1)) : na

plot( getLastPosSign(), style = plot.style_linebr )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70028933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档