0
votes

I've written some simple code to always set my risk at 1% no matter where my stop loss is set. Here's what it looks like when I open a short position.

SL = 0.002
strategy.entry("open short", false, (strategy.equity*.01)/((high+SL)-close))
strategy.exit("exit short", "open short", stop = high + SL)

This seems to work when I apply it to the chart, as all of the position sizes line up with my math. However, the profit number is wrong. For instance, I have my starting equity set to $1000 and here is the first trade on the chart.

Strategy Tester Wrong Profit

You can see, entry price is 0.63835 and exit is 0.6412, with a difference of 0.00285. That difference multiplied by the amount of contracts (3508) gives me 9.9978, which is the max amount that should have been lost on the trade. This lines up with my 1% stop loss, which should be $10. However, you can see that the strategy tester shows I lost $14.91.

Am I misunderstanding how profit is calculated? Or is this a problem with the strategy tester?

1
May be you specify the commission? - Andrey D
Im having the same issue. Were you able to find out what was causing it? - Jesse G
I never found a solution, but I think I was misunderstanding how profit is calculated in Forex. Basically, the second currency in each currency pair is the "quote currency", meaning any profit made on a trade has to be converted from that currency to your account currency. Because I had my account currency set to USD, any pairs ending with USD would be calculated corrrectly, but other pairs wouldn't. The solution would be to calculate the distance between your entry and stop loss, convert that number to USD, and then calculate your contract size. Just a guess, but I think that was the problem. - Ian Marshall

1 Answers

0
votes

Or the problem may be (which is something I am trying to solve), that strategy.equity also calculates open positions as part of the equity.

You can refer to "Sizing TradingView orders based on a percentage of the strategy's equity " where I got the following explanation:

The strategy equity is the sum of the initial capital, net profit, and unrealised profit of open positions (see TradingView, n.d.). So a strategy that started trading with 10,000 and realised a profit of 1,345, while the open profit/loss is -450, has an equity of 10,895 (10,000 + 1,345 + -450).