[CMD] Get Order Direction

stable
By Strooth in Miscellaneous Published October 2021 👁 1,011 views 💬 0 comments

Description

Gets the order direction as a signal enum - signallong, siignalshort, signalexitlong, signalexitshort, signalbuy, signalsell Usage - local orderdir = CC_GetOrderDirection(orderId) or local orderdir = CC_GetOrderDirection(orderId, PositionId, PriceMarket())
HaasScript
DefineCommand('GetOrderDirection', 'Checks orderid and returns the direction as a SignalEnum')
local oid = IfNull(DefineParameter(DynamicType, 'orderId', 'The orderId to check', false, '', 'input'), '')
local pid = IfNull(DefineParameter(DynamicType, 'positionId', 'The positionId to check. If not provided the order will be checked for positionId', false, '', 'input'), '')
local market = IfNull(DefineParameter(DynamicType, 'market', 'The market to check, default is PriceMarket()', false, PriceMarket(), 'input'), '')
local out = SignalNone
--oid = NewGuid()
if oid == '' or IsNull(oid) then LogWarning('No orderId provided to the GetOrderDirection Command') 
elseif oid ~= '' and IsNotNull(oid) then 
        local order = OrderContainer(oid)
        local pos = PositionContainer(Switch(Or(pid == '', IsNull(pid)), IfNull(order.positionId, NewGuid()), pid))
        local dir = PositionToBool(GetPositionDirection(pos))
        local ask = Compare(CurrentPrice().ask, order.price)
        local bid = Compare(CurrentPrice().bid, order.price)
        local isAbove = And(bid.isAbove, ask.isAbove)
        local isBelow = And(bid.isBelow, ask.isBelow)
        local mtype = MarketType(market) == LeverageTrading
        if IsTrue(dir.isLong, order.isEnterOrder) then
            if isAbove then 
                out = Switch(mtype, SignalLong, SignalBuy)
            elseif isBelow then 
                out = Switch(mtype, SignalShort, SignalSell)
            end
        elseif IsTrue(dir.isShort, order.isEnterOrder) then
            if isBelow then 
                out = Switch(mtype, SignalShort, SignalSell)
            elseif isAbove then 
                out = Switch(mtype, SignalLong, SignalBuy)
            end
        elseif IsTrue(dir.isNone, order.isEnterOrder) then
            if isAbove then 
                out = Switch(mtype, SignalLong, SignalBuy)
            elseif isBelow then 
                out = Switch(mtype, SignalShort, SignalSell)
            end
        elseif IsTrue(dir.isLong, order.isExitOrder) then
            if isBelow then 
                out = Switch(mtype, SignalExitLong, SignalSell)
            elseif isAbove then 
                out = Switch(mtype, SignalExitShort, SignalBuy)
            end
        elseif IsTrue(dir.isShort, order.isExitOrder) then
            if isAbove then 
                out = Switch(mtype, SignalExitShort, SignalBuy)
            elseif isBelow then 
                out = Switch(mtype, SignalExitLong, SignalSell)
            end
        elseif IsTrue(dir.isNone, order.isExitOrder) then
            if isAbove then 
                out = Switch(mtype, SignalExitShort, SignalBuy)
            elseif isBelow then 
                out = Switch(mtype, SignalExitLong, SignalSell)
            end
        end
end 

DefineOutput(DynamicType, out, 'The direction as a SignalEnum', 'SignalLong, SignalShort, SignalExitLong, SignalExitShort')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!