Handling leverage
Leverage is a powerful tool that can help you amplify your trading profits, but it’s important to use it carefully and responsibly to avoid excessive risk. In HaasScript, you can request and use leverage information using the following commands:
Leverage()
The Leverage()
command returns the main account leverage setting as a number. This command is useful if you want to get the bot’s default leverage setting.
leverage = Leverage()
Log("Main leverage setting: " .. leverage)
GetLeverage([string market, string accountId])
The GetLeverage()
command returns the leverage of the current (or optional input) market as a number. If the function fails, it returns -1. This command is useful if you want to get the leverage for a specific market.
leverage = GetLeverage( PriceMarket() )
if leverage == -1 then
Log("Failed to get leverage for market")
else
Log("Leverage for market: " .. leverage)
end
You can also optionally provide an accountId
parameter to get the leverage for a specific account.
GetMaxLeverage([string market])
The GetMaxLeverage()
command returns the highest possible leverage for the current (or optional input) market as a number. If the function fails, it returns -1.
maxLeverage = GetMaxLeverage( PriceMarket() )
if maxLeverage == -1 then
Log("Failed to get max leverage for market")
else
Log("Max leverage for market: " .. maxLeverage)
end
SetLeverage(number leverage, [string market, string accountId])
The SetLeverage()
command sets the leverage for the current (or optional input) market. This command returns true
if the leverage was set successfully, and false
otherwise.
success = SetLeverage(10, PriceMarket())
if success then
Log("Leverage set successfully")
else
Log("Failed to set leverage")
end
You can also optionally provide an accountId
parameter to set the leverage for a specific account.
UsedMargin(string market, number price, number amount, number leverage)
The UsedMargin()
command calculates the used margin for a specific position. You need to provide the market, the price, the amount, and the leverage as parameters (0 for cross-leverage).
local margin = UsedMargin(PriceMarket(), 50000, 1, 10)
Log("Used margin: " .. margin)
With these commands, you can easily handle leverage in your HaasScript trading bots and scripts. Just remember to use leverage carefully and responsibly to avoid excessive risk.