[pshaiTool] Buy & Sell buttons

stable
By pshai in Other Published March 2024 👁 1,825 views 💬 0 comments

Description

From GPT: The [pshaiTool] Buy & Sell buttons script is an advanced trading tool designed to streamline the process of executing buy and sell orders directly from a trading platform interface. Created by pshai, this tool enhances user experience by providing a simplified, high-speed method for managing trading positions, including opening long and short positions, as well as exiting these positions with precision. Key Features: * Order Customization: Users can specify order size and select from various order size types, such as Base, Quote, % of Total Wallet, or % of Available Wallet, catering to diverse trading strategies and risk management preferences. * Advanced Order Types: Supports different order types, including limit orders, with additional settings for maximum slippage in ticks to protect against unfavorable price movements. * Position Management: Automatically detects open positions not initiated by the bot, allowing seamless integration and management of user-initiated trades alongside bot activities. * Market Compatibility: Adaptable to both spot trading and other market types, ensuring wide applicability across different trading platforms. * Price and Amount Calculation: Dynamically calculates the order price and amount based on current market conditions, user preferences, and specific strategy requirements, ensuring optimized order execution. * Convenient Interface: Features dedicated buttons for quick actions - opening long (buy) and short (sell) positions, closing long and short positions, and canceling all or specific orders, providing a user-friendly interface for rapid trading decisions. * Real-time Reporting: Includes custom reports for long and short positions' profit and loss (PnL), offering immediate feedback on trading performance and financial outcomes. This tool is particularly suitable for traders looking for efficient, automated assistance in managing their trading activities on the fly, combining the flexibility of manual trading with the precision and speed of algorithmic strategies. Screenshots available: https://discord.com/channels/269316665483722764/1221141919300583486/1221141919300583486
HaasScript
-- [pshaiTool] Buy & Sell buttons
-- Author: pshai


if not init then
    EnableHighSpeedUpdates(true)
    HideTradeAmountSettings()
    HideOrderSettings()

    order_size_types = {
        'Base',
        'Quote',
        '% of Total Wallet',
        '% of Available Wallet',
    }

    lpid = NewGuid()
    spid = NewGuid()

    init = true
end

local order_size = Input('Order size', 1)
local order_size_type = InputOptions('Order size type', order_size_types[1], order_size_types)
local order_type = InputOrderType('Order type', LimitOrderType)
local max_slippage_ticks = Input('Max Slippage in Ticks', 3, 'Only affects limit orders')

-- check for open positions the bot doesnt know
local lpos = PositionContainer(lpid)
local spos = PositionContainer(spid)
local ulpos = UserPositionContainer({direction = PositionLong})
local uspos = UserPositionContainer({direction = PositionShort})

if lpos.amount == 0 and lpos.enterPrice == 0 and ulpos.amount > 0 then
    CreatePosition(PositionLong, ulpos.enterPrice, ulpos.amount, {positionId = lpid})
end

if spos.amount == 0 and spos.enterPrice == 0 and uspos.amount > 0 then
    CreatePosition(PositionShort, uspos.enterPrice, uspos.amount, {positionId = spid})
end

isSpot = MarketType() == SpotTrading

function getPrice(isBuy)
    local cp = CurrentPrice()
    local slip = max_slippage_ticks * PriceStep()
    return isBuy and (cp.bid + slip) or (cp.ask - slip)
end

function getAmount(isBuy, isExit)
    if not isExit then
        if order_size_type == order_size_types[1] then -- Base
            return order_size
        elseif order_size_type == order_size_types[2] then -- Quote
            return order_size / getPrice(false)
        elseif order_size_type == order_size_types[3] then -- % total wallet
            local wallet = Balance().total
            return (order_size / 100) * wallet
        elseif order_size_type == order_size_types[4] then -- % avbl wallet
            local wallet = Balance().available
            return (order_size / 100) * wallet
        else
            LogError('Unknown order size type: ' .. order_size_type)
            return nil
        end
    end

    if isBuy then
        return GetPositionAmount(lpid)
    end

    return GetPositionAmount(spid)
end

function buy()
    local cmd = isSpot and PlaceBuyOrder or PlaceGoLongOrder
    local price = getPrice(true)
    local amount = getAmount(true, false)
    local type = order_type

    if type != MarketOrderType then
        price = price - PriceStep() * 5
    end

    cmd(price, amount, {type = type, positionId = lpid})
end

function sell()
    local cmd = isSpot and PlaceSellOrder or PlaceGoShortOrder
    local price = getPrice(false)
    local amount = getAmount(false, false)
    local type = order_type

    if type != MarketOrderType then
        price = price + PriceStep() * 5
    end

    cmd(price, amount, {type = type, positionId = spid})
end

function exitbuy()
    local cmd = isSpot and PlaceSellOrder or PlaceExitLongOrder
    local price = getPrice(false)
    local amount = getAmount(true, true)
    local type = order_type

    if type != MarketOrderType then
        price = price + PriceStep() * 5
    end

    if lpos.amount > 0 then
        cmd(price, amount, {type = type, positionId = lpid})
    else
        CloseVPosition(price, lpid)
    end
end

function exitsell()
    local cmd = isSpot and PlaceBuyOrder or PlaceExitShortOrder
    local price = getPrice(true)
    local amount = getAmount(false, true)
    local type = order_type

    if type != MarketOrderType then
        price = price - PriceStep() * 5
    end

    if spos.amount > 0 then
        cmd(price, amount, {type = type, positionId = spid})
    else
        CloseVPosition(price, spid)
    end
end

InputButton('Open Long', buy, '', 'LONG')
InputButton('Open Short', sell, '', 'SHORT')
InputButton('Close Long', exitbuy, '', 'LONG')
InputButton('Close Short', exitsell, '', 'SHORT')
InputButton('Cancel All Orders', || CancelAllOrders(), '', 'CANCEL')
InputButton('Cancel Long Orders', || CancelAllOrders(lpid), '', 'CANCEL')
InputButton('Cancel Short Orders', || CancelAllOrders(spid), '', 'CANCEL')


CustomReport('Long Pnl', Truncate(CC_GetPositionUPnL(lpid), 4)..' '..QuoteCurrency())
CustomReport('Short Pnl', Truncate(CC_GetPositionUPnL(spid), 4)..' '..QuoteCurrency())

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!