[EXAMPLE] Position sizing based on pre-determined (acceptable) loss & stop-loss size
stableDescription
Hey fellas!
Here is an example code snippet to demonstrate how you can calculate your position size based on acceptable loss amount and your stop-loss size. You can test this code by running a backtest on the market of your choice, it will run 2 updates and output the calculated values and the actual loss amount.
The code works "both ways", meaning that you can use this for COIN/USDT and COIN/BTC markets (or whatever the underlying asset is). The code also works in both SPOT and FUTURES.
~~May the profits be with you~~
CC available here: https://www.haasscripts.com/t/pshaicmd-get-dynamic-trade-amount/
Example results on BAT/BTC (SPOT) using 0.01 BTC acceptable loss and 2% stop-loss size:
Losses: -0.0099999216 BTC
Sell order finished. 114678 BAT @ 0.0000042728 BTC (70382447-e3fe-4249-8a6d-553ebafd7fc5) (Simulated Order)
Buy order finished. 114678 BAT @ 0.00000436 BTC (72ebc12d-5911-4bd8-b8b3-2b7711cfe268) (Simulated Order)
Entry Price: 0.00000436 BTC
Stop-Loss: 0.00000427 BTC
Investment Size (margin): 0.5 BTC
Notional Size: 0.5 BTC
Position Size: 114679 BAT
Example results on MANA/USDT (Perpetual) using 100 USDT acceptable loss and 2% stop-loss size,
1x leverage (or spot):
Losses: -99.999984 USDT
ExitLong order finished. 8364.0 MANA @ 0.585844 USDT (f1efa9d7-48ea-43fe-9eeb-249bce51384f) (Simulated Order)
GoLong order finished. 8364.0 MANA @ 0.5978 USDT (1a3675d4-dee9-446c-82e3-765d3d001534) (Simulated Order)
Entry Price: 0.5978 USDT
Stop-Loss: 0.5858 USDT
Investment Size (margin): 5000 USDT
Notional Size: 5000 USDT
Position Size: 8364 MANA
20x leverage:
Losses: -99.999984 USDT
ExitLong order finished. 8364.0 MANA @ 0.585844 USDT (5b6fa71c-0b81-4313-8429-06d5f05b15f4) (Simulated Order)
GoLong order finished. 8364.0 MANA @ 0.5978 USDT (904dce38-f294-46fd-8149-f4b9fca84a86) (Simulated Order)
Entry Price: 0.5978 USDT
Stop-Loss: 0.5858 USDT
Investment Size (margin): 250 USDT
Notional Size: 5000 USDT
Position Size: 8364 MANA
HaasScript
-- Position sizing based on pre-determined (acceptable) loss & stop-loss size
-- Author: pshai
label1 = AmountCurrency()
label2 = MarketType() == SpotTrading and QuoteCurrency() or UnderlyingAsset()
label3 = QuoteCurrency()
if not init then
-- let's imagine a USDT based market and we are buying or entering long
-- parameters:
acceptable_loss = 100 -- USDT
entry_price = CurrentPrice().ask -- we buy the ask price
stop_loss_offset = 2 -- percentage. this can also be calculated with ATR for example
stop_loss_price = SubPerc(entry_price, stop_loss_offset) -- final SL price
stop_loss_per_share = Abs(entry_price - stop_loss_price) -- "share" in this case would be the "coin" we buy
position_size = acceptable_loss / stop_loss_per_share
investment_size = UsedMargin(PriceMarket(), entry_price, position_size, Leverage())
notional_size = investment_size * Leverage()
Log('Position Size: ' .. Round(position_size, AmountDecimals()) .. ' ' .. label1)
Log('Notional Size: ' .. Round(notional_size, QuoteDecimals()) .. ' ' .. label2)
Log('Investment Size (margin): ' .. Round(investment_size, QuoteDecimals()) .. ' ' .. label2)
Log('Stop-Loss: ' .. Round(stop_loss_price, PriceDecimals()) .. ' ' .. label3)
Log('Entry Price: ' .. Round(entry_price, PriceDecimals()) .. ' ' .. label3)
-- create virtual position with this info
pid = CreatePosition(PositionLong, entry_price, position_size)
-- close it at stop-loss price
CloseVPosition(stop_loss_price, pid)
init = true
else
-- get the position
pos = PositionContainer(pid)
-- log results
Log('Losses: ' .. pos.profit .. ' ' .. label2)
-- stop the test
DeactivateBot()
end
0 Comments
Sign in to leave a comment.
No comments yet. Be the first!