繁体   English   中英

我在 pine 脚本中遇到 (strategy.exit) 的问题。 它适用于长途电话但不适用于短途电话

[英]I am facing a problem with (strategy.exit) in pine script. it is working for the long call but not working for short calls

inTrade = strategy.position_size > 0
notInTrade = strategy.position_size <= 0


//Figure out take profit and stop loss price
longExitPrice  = strategy.position_avg_price * (1 + longProfitPerc ) // longProfitPerc=1.5 
longStopPrice = strategy.position_avg_price * (1 - longStopPerc ) // longStopPerc=1
shortExitPrice = strategy.position_avg_price * (1 + shortProfitPerc ) //shortProfitPerc=1.5
shortStopPrice = strategy.position_avg_price * (1 - shortStopPerc ) //shortStopPerc=1



if GL1 and GL2 == 1 and GL3 == 1 and GL4 == 1 and GL5 == 1 and notInTrade //GL1 TO GL5 ARE CONDITIONS
    strategy.entry("long_Entry", strategy.long)

if inTrade
    strategy.exit("Long_Exit", 'long_Entry', limit=longExitPrice, loss=longStopPrice)


if GS1 and GS2 == 0 and GS3 == 0 and GS4 == 0 and GS5 == 0 and notInTrade //GS1 TO GS5 ARE CONDITIONS
    strategy.entry("short_Entry", strategy.short)


if inTrade
    strategy.exit("Short_Exit", 'short_Entry', limit=shortExitPrice, loss=shortStopPrice)


在长期调用的情况下,strategy.exit 会在达到 tp 或 sl 百分比时关闭调用。 但空头呼叫仅由多头呼叫关闭,空头呼叫继续,直到生成新的多头,然后才关闭。 他们以高利润或亏损收盘,而不是由 tp 触发或在代码中设置的 sl此处输入图像描述

您的inTrade只会表明您是否处于多头头寸。 这是因为strategy.position_size将在市场头寸多头时返回> 0 ,而在头寸空头时返回< 0

其次,你的短期退出计算是错误的。 看起来您复制了多头头寸的计算结果,但实际上应该是相反的。

正确的声明应该如下:

inTrade = strategy.position_size != 0
inLongTrade = strategy.position_size > 0
inShortTrade = strategy.position_size < 0
notInTrade = strategy.position_size == 0

shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc ) //shortProfitPerc=1.5
shortStopPrice = strategy.position_avg_price * (1 + shortStopPerc ) //shortStopPerc=1

if inLongTrade
    strategy.exit("Long_Exit", 'long_Entry', limit=longExitPrice, loss=longStopPrice)

if inShortTrade
    strategy.exit("Short_Exit", 'short_Entry', limit=shortExitPrice, loss=shortStopPrice)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM