[pshaiCmd] Get Orderbook Impact

0 46 Views No Comments 2 weeks ago
  • 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 Code
    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')
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free