Reputation: 3192
I am trying to capture the number found in the Profit column (percentage) from the List of Trades tab in the Strategy Tester.
This metric is defined by the TradingView Support page as:
Profit
The profit per trade and the gain/loss percentage of that trade.
Here is the closest I have been able to get:
pos_size_change = strategy.position_size != strategy.position_size[1]
equity_1 = valuewhen(pos_size_change,strategy.equity,1)
equity_0 = valuewhen(pos_size_change,strategy.equity,0)
pct_equity_change = (equity_0/equity_1-1)*100
plot(pct_equity_change)
The numbers I get when plotting follow the Strategy Tester closely, but they are not identical (sometimes they are).
E.g.:
Upvotes: 0
Views: 951
Reputation: 2310
Here is a function to return the profit of a given trade. We can substitute in a trade number with strategy.clossedtrades, which will return the last trade until a new one is complete, or you can ask of it a trade number as you please.
tradePercentProfit(trade_num) =>
endP = strategy.closedtrades.entry_price(trade_num-1) * math.abs(strategy.closedtrades.size(trade_num-1))
prof = strategy.closedtrades.profit(trade_num-1)
profitInPercent = prof / endP *100
perc = math.round(tradePercentProfit(strategy.closedtrades), 2)
plot(tradePercentProfit(strategy.closedtrades))
Cheers and best of luck with your trading and coding
Upvotes: 2
Reputation: 36
It certainly looks like TradingView calculation is right.
Percentage = 100*(Sold price - bought price)/bought price
So, if you do 100*(18.471-18.707)/18.707 = 1.261
I am not sure what is your commission setting. It is possible that TradingView calculation isn’t considering commission.
Also, I think your method isn’t right to use entire equity.
Even if traded equity is 100%, there are some small issues is strategy calculation such as commission is not considered for calculating trade-able quantity.
Upvotes: 1