2

im struglin to get the profit percent for a chosen number of last closed trades, any help is apreciated.

Thanks in advance.

2 Answers2

0

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

Bjorgum
  • 2,054
  • 2
  • 4
  • 11
0

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
dorien
  • 5,265
  • 10
  • 57
  • 116