[CMD] Position Sync with Exchange

stable
By Strooth in Miscellaneous Published June 2021 👁 1,243 views 💬 1 comments

Description

A custom command you can easily add to any script to sync open positions with exchange on start for example. Tested working with Binance, not sure about others. Simple Usage - give the command the position Id's for the positions you want to sync and save the id's, done. local shortPosId = Load('shortPosId', NewGuid()) local longPosId = Load('longPosId', NewGuid()) if Load('syncdone', true) then CC_SyncPositions(true, shortPosId, longPosId) Save('syncdone', false) end Save('shortPosId', shortPosId) Save('longPosId', longPosId)
HaasScript
-- Author: -- Discord:  @strooth#4739
DefineCommand('SyncPositions', 'Sync Positions with the exchange')
local enable = DefineParameter(BooleanType, 'enable', 'default=false', false, true, 'true/false')
local short = DefineParameter(DynamicType, 'Short PositionId', 'unique positionId', false, '', 'NewGuid()')
local long = DefineParameter(DynamicType, 'Long PositionId', 'unique positionId', false, '', 'NewGuid()')
-- check UPC
if enable then
    local upc_short = UserPositionContainer({direction = PositionShort})
    if short != '' and Load('syncedshort', true) then 
        local short = PositionContainer(short)
        if short.amount == 0 and short.enterPrice == 0 then 
            if upc_short.isShort and upc_short.amount > 0 then
                CreatePosition(PositionShort, upc_short.enterPrice, upc_short.amount, {positionId = short})
                Save('syncedshort', false)
            end
        elseif short.amount != upc_short.amount then 
            CloseVPosition(short.enterPrice, short)
            CreatePosition(PositionShort, upc_short.enterPrice, upc_short.amount, {positionId = short})
            Save('syncedshort', false)
        end 
    elseif Load('syncedshort', true) then 
        if upc_short.isShort and upc_short.amount > 0 then
            short = CreatePosition(PositionShort, upc_short.enterPrice, upc_short.amount, {positionId = short})
            Save('syncedshort', false)
        end
    end 
    local upc_long = UserPositionContainer({direction = PositionLong})
    if long != '' and Load('syncedlong', true) then 
        local long = PositionContainer(long)
        if long.amount == 0 and long.enterPrice == 0 then  
            if upc_long.isLong and upc_long.amount > 0 then
                CreatePosition(PositionLong, upc_long.enterPrice, upc_long.amount, {positionId = long})
                Save('syncedlong', false)
            end
        elseif long.amount != upc_long.amount then 
            CloseVPosition(long.enterPrice, long)
            CreatePosition(PositionLong, upc_long.enterPrice, upc_long.amount, {positionId = long})
            Save('syncedlong', false)
        end 
    elseif Load('syncedlong', true) then 
        if upc_long.isLong and upc_long.amount > 0 then
            long = CreatePosition(PositionLong, upc_long.enterPrice, upc_long.amount, {positionId = long})
            Save('syncedlong', false)
        end 
    end 
    
end

DefineOutput(ListDynamicType, {short, long}, 'positionIds')
DefineOutputIndex(1, DynamicType, 'short', 'short positionId')
DefineOutputIndex(2, DynamicType, 'long', 'long positionId')

1 Comment

Sign in to leave a comment.

J
Justin almost 5 years ago

this is great, Can it modify to work with hedge mode? so it can sync with SMM