I am trying to write code that will close each opened trade at the close of the first bar in which that close would produce a profitable result for that particular trade. Closing on the same bar as the one that initiated the trade is not allowed, but it shouldn't be possible since the entry is at the close of the bar. I'm enabling pyramiding, so it needs to judge each lot as its own trade for the purposes of exiting, not lump them together and close the whole position. If this exit system is working the way it should, it should be impossible for any closed trade to be a loss. Only wins can close. If it's not profitable yet, the trade should stay open until it is.
I'm terrible at coding so if you answer, speak to me like I'm an idiot because as far as coding goes, I am. Thanks for any help anyone can give!
Question answered by @vitruvius - a true hero.
In order for this to work, you need to give unique ids to your trades.
strategy.entry(id, direction, qty, limit, stop, oca_name, oca_type, comment, alert_message, disable_alert) → void
id (series string) The identifier of the order, which corresponds to an entry ID in the strategy's trades after the order fills. If the strategy opens a new position after filling the order, the order's ID becomes the strategy.position_entry_name value. Strategy commands can reference the order ID to cancel or modify pending orders and generate exit orders for specific open trades. The Strategy Tester and the chart display the order ID unless the command specifies a comment value.
One way to do this is to use a counter system. For example, L-1, L-2, L-3
... for long trades.
Then, when you place your close order, you will refer to this entry id.
strategy.close(id, comment, qty, qty_percent, alert_message, immediately, disable_alert) → void
id (series string) The entry identifier of the open trades to close.
This is the basic idea. The implemenatation is up to you. Somehow you need to know the trade id and the profit.
You can use the built-in variables to loop over the open trades, get the profit and trade id. Then you can call strategy.close()
if that specific trade is in profit.
One important note is, you need to set close_entries_rule="ANY"
. Otherwise, earliest exit order must close the earliest entry order.
Here is an example for you:
//@version=6
strategy("My strategy", overlay=true, pyramiding=10, process_orders_on_close=true, close_entries_rule="ANY")
var long_cnt = 1
long_entry_cond = (bar_index % 3) == 0 // Enter a new trade every third bar
if (long_entry_cond)
long_id = "L-" + str.tostring(long_cnt) // Create the entry id
strategy.entry(long_id, strategy.long)
if (strategy.position_size > strategy.position_size[1]) // New trade
long_cnt := long_cnt + 1
for i = 0 to strategy.opentrades - 1
trade_id = strategy.opentrades.entry_id(i)
profit = strategy.opentrades.profit(i)
if (profit > 0) // In profit
strategy.close(trade_id, comment="C-" + trade_id)
s = "Trade: " + trade_id + "\nProfit: " + str.tostring(profit, format.price)
label.new(bar_index, high, s)