[CMD] Strooth's Position Controller

stable
By Strooth in Miscellaneous Published March 2021 👁 1,240 views 💬 0 comments

Description

This is a command to easily control positions in unmanaged trading scripts and increment slots if needed. Simply load the command at the head of your script using the CheckPos key and load your position Id's, the name used will then be suffixed for each position opened/closed

-- Position Controller check at head/top of script 
        local cmd = CC_PositionControl().CheckPos
        local shortPosId = Load('shortPosId', cmd("Trade Short", true, false))
        local longPosId = Load('longPosId', cmd("Trade Long", true, true))
In the main go() function of your script before your strategy

            longPosId = cmd(longPosId)
            shortPosId = cmd(shortPosId)
That's all there is to it, example showing it working in the screenshots
HaasScript
DefineIntervalOptimization(CurrentInterval())
DefineCommand('PositionControl', 'Checks and Increments a Position Prefix')
 
local pc_pid = DefineParameter(StringType, 'positionId', 'Position ID', false, 'Position - 0', 'SessionGet')
local pc_new = DefineParameter(BooleanType, 'isNew', '', false, '', 'True, False, IsTrue, IsFalse')
local pc_isLong = DefineParameter(BooleanType, 'isLong', '', false,  '', 'True, False, IsTrue, IsFalse')
local pc_params = DefineParameter(ListDynamicType, 'AllParams', '', false, false, 'All pc_params')
local pc_output
if pc_params ~= false then
  pc_pid        = pc_params.positionId
  pc_new        = pc_params.isNew
  pc_isLong    = pc_params.AllParams
end

local go = function ()
    local pc_upname = function(pc_var)
        local pc_var = Parse(pc_var, StringType)
        local pc_string = ''
        local pc_index = 0
        local pc_namar = {}
        local pc_dlim = '-'
        local pc_name = pc_var 
        local pc_length = pc_string.len(pc_var)
        if IsNull(pc_var) or pc_var == pc_string or pc_var == pc_dlim then 
            LogWarning('name cannot be null') return
            elseif pc_length < 3 then 
                pc_name = pc_var
                pc_index = 0
        elseif 
            Count(StringSplit(pc_var, pc_dlim)) == 2 then             
            pc_namar = StringSplit(pc_var, pc_dlim)
            pc_name = pc_namar[1]
            pc_index = IsNotNull(pc_namar[2]) and Parse(pc_namar[2], NumberType) 
                if pc_index == nil then pc_index = 0 end 
            pc_index = pc_index >= 1 and pc_index or 0
        end 
        pc_name = StringJoin(pc_name, ' ')
        pc_index = StringJoin(' ', pc_index + 1)
        return StringJoin(pc_name, pc_index, pc_dlim) 
    end 

    local pc_corders = function(pc_pid)
            if Load('pc_cordersdone', false) == false then 
                if IsAnyOrderOpen(pc_pid) then CancelAllOrders(pc_pid) 
                    if IsAnyOrderFinished(pc_pid) then
                        if not IsAnyOrderOpen(pc_pid) then 
                            Save('pc_cordersdone', true)
                            return Load('pc_cordersdone')
                        end 
                    end 
                end 
            end 
    end 
    local pc_newpos = function(pc_oldp, pc_newp, pc_dir)
        local pc_price
        pc_corders(pc_oldp)
        pc_price = LastExitPositionPrice()
        CloseVPosition(pc_price, pc_oldp)
        CreatePosition(pc_dir, 0, 0, {positionId = pc_newp})
        return pc_newp 
    end 
local pc_check = function(pc_pid, pc_new, pc_isLong)
    local pc_direction 
    local pc_pamount 
    local pc_newpid = pc_upname(pc_pid)
    pc_pid = pc_new and pc_newpid or pc_pid 
    pc_pamount = GetPositionAmount(pc_pid)
    pc_direction = GetPositionDirection(pc_pid)
    if IsPositionClosed(pc_pid) then 
        return pc_newpos(pc_pid, pc_newpid, pc_direction)
    end 
    if pc_direction == NoPosition then 
        if pc_pamount == 0 then 
            if IsAnyOrderOpen(pc_pid) then
                return pc_pid
            elseif IsAnyOrderFinished(pc_pid) then 
                return pc_newpos(pc_pid, pc_newpid, pc_direction)
            end 
         elseif pc_pamount != 0 then 
            return pc_pid 
        end 
    elseif pc_direction == PositionLong then
        if pc_pamount == 0 then 
            return pc_newpos(pc_pid, pc_newpid, pc_direction)
        elseif pc_pamount != 0 then 
            return pc_pid 
        end
    elseif pc_direction == PositionShort then
        if pc_pamount == 0 then 
            return pc_newpos(pc_pid, pc_newpid, pc_direction)
        elseif pc_pamount != 0 then 
            return pc_pid 
        end
    end 
    if pc_new == true then 
        pc_direction = pc_isLong and PositionLong or PositionShort
        return pc_newpos(pc_pid, pc_newpid, pc_direction)
    else 
        return pc_pid
    end 
end 

return {
CheckPos = pc_check,
NewPos = pc_newpos,
NewName = pc_upname,
CheckOrders = pc_corders,
}

end 
pc_output = go()

DefineOutput(ListDynamicType, pc_output, 'Position Control')
DefineOutputIndex( 1, DynamicType, 'CheckPos', 'CheckPos')
DefineOutputIndex( 2, DynamicType, 'NewPos', 'NewPos')
DefineOutputIndex( 3, DynamicType, 'NewName', 'NewName')
DefineOutputIndex( 4, DynamicType, 'CheckOrders', 'CheckOrders')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!