Question about Position Sizing, Leverage and Risk Managment

Hi everyone,

I am new to trading and learning Pinescript (Tradingview’s coding language).

I am backtesting strategies and interested in algorithmic trading. In my strategies, I mostly use 1:2 Risk Ratio. Risk; %2 of my capital. So, if I have 1000$, that means every winning trade will be 40$ (4%) and every losing trade will be 20$ (2%). But, imagine that an entry position comes with a stop loss of -0.23% and take profit at +0.46%. If I want a flat -%2+%4, I need leverage. I can calculate how much leverage I need with this standard formula;

40$ (%2 Risk) / ,23 (Stoploss) : 1.739 (the leverage level that should be taken)
1.739 x 1000 $: 1739 $ (the size I should enter to this position in order to reach my target)

I wanted to ask; is there any possibility of adapting the leverage in every trade individually with a script? I imagine something that always calculates the last capital and calculates my -%2+%4 target for the new entry position. And of course, since I want my trades to be automated, is it possible to execute trades with my wishing formula?

I will be very pleased to hear your answers!

Though some of the points in this strategy are not clear for me, let me assume that the stop loss/ take profit orders you are referring to are both market orders (stop loss market order and take profit market order). That is, in this example, if the price turns to 0.23% lower than the entry price, a stop loss order is placed into the order book and gets filled with market price. So the final gain/ loss may be slightly different from the expectation.

It is possible to utilise the API service to adjust the leverage level; however, there are some challenges when applying to the aforementioned strategy. First, the leverage is set up per symbol instead of trade. Second, the leverage level is restricted to be integer value.
Ref. POST /fapi/v1/leverage

Hi ishuen, thank you so much for your answer!

I understand that the leverage is restricted to be an integer value. In order to reach a perfect amount of leverage, I can add more math to the formula and I would be able to find the nearest way how I like to have my strategy.

I would like to ask the last question and will be very grateful to hear your answer; What did you mean exactly when you say ‘‘First, the leverage is set up per symbol instead of trade.’’ I don’t understand well enough what is a ‘‘symbol’’ in this sentence.

Looking forward to hearing from you!
Best wishes,

Symbol is one of the mandatory parameter in the request payload. It represent which kind of the trading pair to apply the leverage level. In addition, this endpoint cannot specify which order or trade to have what leverage level. This means if there are multiple open orders under the same symbol, for instance, BTCUSDT. It’s not allowed to have 1 order with leverage level = 10 and another one with leverage level = 20.
I’m not so sure if there is a scenario that you have to place multiple orders such as 1 take profit order and 1 stop loss order with different leverage level at the same time. Just to let you know if this is the case, you may have to think about another workaround.