简体   繁体   中英

Only enter a trade once per bar when calc_on_order_fills=true, Pine Script, Tradingview

I want to add one of the following conditions to avoid my strategy entering a trade on a bar where already a trade was entered. Eg: Last trade was entered at 12 o´clock on an 1 hour chart and was closed by the trailing stop function into this same bar. Now the next entry for a trade should be earliest in the beginning of the next bar ( calc_on_order_fills is set to true )

strategy.closedtrades.entry_bar_index(strategy.closedtrades) != bar_index

strategy.closedtrades.entry_time(strategy.closedtrades) != time

But it doesn´t work. Can anyone please help to solve this issue? And explain to me why it doesn´t work?

EDIT:

//Submit entry orders
if (LongCondition and time>timestamp(2022, 07, 01, 06, 00) and strategy.closedtrades.entry_bar_index(strategy.closedtrades-1) < bar_index)
    strategy.entry(id = "Long", direction = strategy.long, alert_message = "Place Long Order", comment = "Long Entry")


if (ShortCondition and time>timestamp(2022, 07, 01, 06, 00) and strategy.closedtrades.entry_bar_index(strategy.closedtrades-1) < bar_index)
    strategy.entry(id = "Short", direction = strategy.short, alert_message = "Place Short Order", comment = "Short Entry")

Without this: and strategy.closedtrades.entry_bar_index(strategy.closedtrades-1) < bar_index it works, but with several entrys in one bar. And that´s what I try to avoid.

I added this condition:

and (strategy.closedtrades == 0 or strategy.closedtrades.entry_bar_index(strategy.closedtrades-1) < bar_index)

the key part is

strategy.closedtrades == 0 

otherwise, you never get into a trade

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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