首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有一种方法允许strategy.risk.max_intraday_filled_orders()函数进入IF语句?

是否有一种方法允许strategy.risk.max_intraday_filled_orders()函数进入IF语句?
EN

Stack Overflow用户
提问于 2021-11-16 01:34:03
回答 1查看 261关注 0票数 0

我每天只想触发一次交易。但是,当我的长条件为真时,无论脚本中是否有strategy.risk.max_intraday_filled_orders()函数,警报总是会激活的。我想知道是否有办法将这个函数合并到if语句中。

完整的代码如下所示:

代码语言:javascript
复制
//@version=4
strategy("Test" , shorttitle="Test - V2", overlay=true, initial_capital =100000, default_qty_value = 8000, pyramiding =0, default_qty_type = strategy.fixed, currency=currency.USD)

//Settings
pc_prefix        = input(title="Symbol Prefix", defval="", type=input.string, group="Settings")

//Generate Alert String
symbol = pc_prefix + syminfo.ticker
pc_entry_alert(direction, sl, tp) =>
    direction + "," + symbol + "," + "sl=" +tostring(sl) + ",tp=" + tostring(tp) 

// Get user input
pipStop          = input(title="Pip Stop Amount", defval = 10.0, type=input.float, step=1, group ="PIP Values")  
pipTP2           = input(title="Pip Take Profit 2", defval = 20.0, type=input.float, step=1, group="PIP Values")  


//Strategy maximum number of intraday trades
strategy.risk.max_intraday_filled_orders(2)


//Getting Previous Session High/Low 

rp_function(_symbol, _res, _src) => security(_symbol, _res, _src[barstate.isrealtime ? 1:0])

dHigh = rp_function(syminfo.ticker, "D", high[1]) 
dLow = rp_function(syminfo.ticker, "D", low[1])

plot(dHigh, color=color.blue)
plot(dLow, color=color.orange)


//Defining the Trade
goLongCondition1 = close> dHigh 


// Entry Condition for setup
validLong = strategy.position_size == 0 and goLongCondition1 



// Calculate our stop distance & size for the current bar
longStopPrice = close - pipStop
longStopDistance = close - longStopPrice
longTargetPrice2 = close + pipTP2
longEntryPrice = validLong? close:na


// Save trade stop & target & position size if a valid setup is detected
var tradeLongStopPrice = 0.0
var tradeLongTargetPrice2 = 0.0
var tradeLongEntryPrice = 0.0



// Detect valid long setups & trigger alert
if validLong
    tradeLongStopPrice := longStopPrice
    tradeLongTargetPrice2 := longTargetPrice2
    tradeLongEntryPrice := longEntryPrice


//Alerts  
if validLong 
    alert_string = pc_entry_alert("buy",tradeLongStopPrice, tradeLongTargetPrice2)
    alert(alert_string, alert.freq_all)
    strategy.entry(id="Long", long=strategy.long)



// Exit trades whenever our stop or target is hit
strategy.exit(id="LTP1", from_entry="Long", limit=tradeLongTargetPrice2, stop=tradeLongStopPrice, when=strategy.position_size > 0, qty_percent = 100)


//Strategy maximum number of intraday trades
strategy.risk.max_intraday_filled_orders(2)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-16 18:23:11

设置警报时,选择“订单只填充”。

否则,每次validLong变成true时,都会收到警告,因为在if块中使用的是alert()函数。

编辑:

您可以使用alert_message属性的strategy.entry()strategy.exit()调用。这样,只有在执行这些strategy调用时才会收到警告消息。

改变这一点:

代码语言:javascript
复制
//Alerts  
if validLong 
    alert_string = pc_entry_alert("buy",tradeLongStopPrice, tradeLongTargetPrice2)
    alert(alert_string, alert.freq_all)
    strategy.entry(id="Long", long=strategy.long)

对此:

代码语言:javascript
复制
alert_string = pc_entry_alert("buy",tradeLongStopPrice, tradeLongTargetPrice2)
strategy.entry(id="Long", long=strategy.long, when=validLong, alert_message=alert_string)

如果您希望在strategy.exit()调用上有不同的消息,您可以定义一个新字符串并使用strategy.exit()alert_message属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69982831

复制
相关文章

相似问题

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