[pshaiCmd] Get Orderbook Impact

stable
By pshai in Insurances Published January 2021 👁 2,518 views 💬 0 comments

Description

Found another one... Old script, never got published anywhere iinm. The command simply "Calculates the impact on orderbook based on trade amount, returns the price impact in percentages." Hope you find good use for it! ~pshai
HaasScript
DefineCommand("GetOrderbookImpact", "Calculates the impact on orderbook based on trade amount, returns the price impact in percentages.")

local tradeAmount = DefineParameter(NumberType, 'tradeAmount', 'The trade amount to calculate with. Default is TradeAmount()', false, TradeAmount(), 'TradeAmount, MaxLongAmount, MaxShortAmount, LongAmount, ShortAmount')
local isLong = DefineParameter(BooleanType, 'isLong', 'Whether or not we are checking for a long or short entry/exit', false, true, 'True, False')

local ob = GetOrderbook()
local askPrices = ob.askPrices
local askAmounts = ob.askAmounts
local bidPrices = ob.bidPrices
local bidAmounts = ob.bidAmounts

local i = 1
local lastPrice = 0
while tradeAmount > 0 do
    tradeAmount = tradeAmount - (isLong and bidAmounts[i] or askAmounts[i])
    lastPrice = (isLong and bidPrices[i] or askPrices[i])
    i = i + 1
end

local impact = 0
if isLong then
    impact = bidPrices[1] / lastPrice - 1
else
    impact = lastPrice / askPrices[1] - 1
end

DefineOutput(NumberType, impact * 100, 'The amount of impact in percentages.', 'IsBiggerThan, IsSmallerThan, IsBiggerOrSmallerThan')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!