英文:
two separate strategies working independently within one strategy
问题
我正在分享我的示例代码。问题如下:它按顺序执行每个操作。如图所示,第2个策略开启做空,然后使用第1个策略切换到做多,再次使用第1个策略切换到做空。而我希望每个策略能够独立运行。
英文:
I am sharing my sample code. the problem is as follows: it does each operation sequentially. as seen in the picture, the 2nd strategy opens shorts, returns to long with the 1st strategy, returns to shorts again with the 1st strategy. whereas I want each strategy to work independently.
//@version=5
strategy("2 in 1 sample", overlay=true, close_entries_rule="ANY")
stopPer = input.float(2.0, title='Stop Loss %' ) / 100
takePer = input.float(4.0, title='Take Profit %' ) / 100
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)
//strategy 1
s1eg1 = ta.ema(close, 5)
s1eg2 = ta.ema(close, 32)
long1 = ta.crossover(s1eg1, s1eg2)
short1 = ta.crossunder(s1eg1, s1eg2)
if long1
strategy.entry("LONG1", strategy.long)
if short1
strategy.entry("SHORT1", strategy.short)
if strategy.position_size > 0
strategy.exit(id="Close Long1", from_entry="LONG1", stop=longStop, limit=longTake)
if strategy.position_size < 0
strategy.exit(id="Close Short1", from_entry="SHORT1", stop=shortStop, limit=shortTake)
//strategy 2
s2eg1 = ta.ema(close, 10)
s2eg2 = ta.ema(close, 20)
long2 = ta.crossover(s2eg1, s2eg2)
short2 = ta.crossunder(s2eg1, s2eg2)
if long2
strategy.entry("LONG2", strategy.long)
if short2
strategy.entry("SHORT2", strategy.short)
if strategy.position_size > 0
strategy.exit(id="Close Long2", from_entry="LONG2", stop=longStop, limit=longTake)
if strategy.position_size < 0
strategy.exit(id="Close Short2", from_entry="SHORT2", stop=shortStop, limit=shortTake)
it does each operation sequentially. as seen in the picture, the 2nd strategy opens shorts, returns to long with the 1st strategy, returns to shorts again with the 1st strategy. whereas I want each strategy to work independently.
答案1
得分: 0
Hedging is not supported on Tradingview. So, you cannot have one long and short positions open at the same time. The opposite direction entry will close the other one.
If you really want, you can do it in an indicator. In that case, you need to simulate all the functionalities and statistics of a strategy which is not an easy job.
英文:
Hedging is not supported on Tradingview. So, you cannot have one long and short positions open at the same time. The opposite direction entry will close the other one.
If you really want, you can do it in an indicator. In that case, you need to simulate all the functionalities and statistics of a strategy which is not an easy job.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论