im struglin to get the profit percent for a chosen number of last closed trades, any help is apreciated.
Thanks in advance.
im struglin to get the profit percent for a chosen number of last closed trades, any help is apreciated.
Thanks in advance.
You can calculate a specific trades profit by using the closed trade built ins. Here is a custom function for you
strategy_closedtrades_percent_profit(trade_num) =>
posVal = strategy.closedtrades.entry_price(trade_num) * math.abs(strategy.closedtrades.size(trade_num))
profit = strategy.closedtrades.profit(trade_num)
profitInPercent = profit / posVal *100
Cheers and best of luck with your coding and trading
The answer of @Bjorgum shows the first trade only. You have to calculate trade_num
differently so you count from the back (last trade closed). This will do it:
total_trades = strategy.closedtrades - 1
trade_num = 1
show_trade = total_trades - trade_num
posVal = strategy.closedtrades.entry_price(trade_num) * math.abs(strategy.closedtrades.size(trade_num))
profit = strategy.closedtrades.profit(trade_num)
profitInPercent = profit / posVal *100