[CMD] Sync Positions
stableDescription
Yes it works, Yes Its running on live working bots.
Will sync positions with whats open at the exchange if the bot looses its amount or entry price somehow.
example
if backtest == false then
CC_SyncPositions(sync, LongPositionId, ShortPositionId, PriceMarket(), Leverage(), false, false)
end
-- Feel free to donate to support my work or if my script helped you in any way <3
-- BTC Adress: 33MsEAbA8tg7SpohgnCpSrmPTBih2UkhxQ
HaasScript
-- Author: Strooth
-- Feel free to donate to support my work or if my script helped you in any way <3
-- BTC Adress: 33MsEAbA8tg7SpohgnCpSrmPTBih2UkhxQ
--
DefineCommand('SyncPositions', 'Sync Positions with the exchange, *Note* Make sure leverage is set correctly, if leverage doesnt match it will not find the open positions, and Leverage for Cross Margin must be set to -1')
local enable = DefineParameter(BooleanType, 'enable', 'default=false', true, false, 'true/false')
local short = DefineParameter(DynamicType, 'Short PositionId', 'unique positionId', false, '', 'NewGuid()')
local long = DefineParameter(DynamicType, 'Long PositionId', 'unique positionId', false, '', 'NewGuid()')
local market = DefineParameter(DynamicType, 'market', 'The market used for adjusting positions', true, PriceMarket(), 'PriceMarket()')
local leverage = DefineParameter(DynamicType, 'leverage', 'The leverage used for finding positions, Use -1 for cross margin', true, Leverage(), 'Leverage()')
local checkall = DefineParameter(BooleanType, 'checkall', 'default=false', false, false, 'true/false')
local log = DefineParameter(BooleanType, 'verbose', 'default=false', false, false, 'true/false')
-- check UPC
if log == true then
Log('enable = '..Parse(IfNull(enable, ''), StringType))
end
if enable == true then
local leverages, mainleverage, upc_short, upc_long = {0, 1, 2, 3}
for i=5, 125 do
leverages[#leverages+1] = i
end
if checkall == true then
for i, l in pairs(leverages) do
local temp_upc_short = UserPositionContainer(AccountGuid(), market, l, PositionShort)
local temp_upc_long = UserPositionContainer(AccountGuid(), market, l, PositionLong)
if leverage != l then
if temp_upc_short.isShort then
LogWarning('Cannot Sync Short Position with Exchange, Bot Leverage does not match, please update Leverage to '..l)
Log(Parse(IfNull(temp_upc_short, ''), StringType))
end
if temp_upc_long.isLong then
LogWarning('Cannot Sync Long Position with Exchange, Bot Leverage does not match, please update Leverage to '..l)
Log(Parse(IfNull(temp_upc_long, ''), StringType))
end
else
upc_short = UserPositionContainer(AccountGuid(), market, l, PositionShort)
upc_long = UserPositionContainer(AccountGuid(), market, l, PositionLong)
mainleverage = l
end
end
else
upc_short = UserPositionContainer(AccountGuid(), market, leverage, PositionShort)
upc_long = UserPositionContainer(AccountGuid(), market, leverage, PositionLong)
mainleverage = leverage
end
if long != '' then
local longadj = Load(long..'adjusted', 0)
local caep_l = GetPositionEnterPrice(long, false)
local camt_l = GetPositionAmount(long)
local cvalue_l = Mult(camt_l, caep_l)
local xaep_l = upc_long.enterPrice
local xamt_l = upc_long.amount
local xvalue_l = Mult(xamt_l, xaep_l)
if GetPositionAmount(long) != xamt_l then
local diffvalue_l = Sub(xvalue_l, cvalue_l)
local diffsize_l = Sub(xamt_l, camt_l)
local adjustprice_l = 0
if log == true then
Log('diffvalue_l = '..Parse(IfNull(diffvalue_l, ''), StringType))
Log('diffsize_l = '..Parse(IfNull(diffsize_l, ''), StringType))
end
if diffsize_l != 0 then
adjustprice_l = Div(diffvalue_l,diffsize_l)
for n, pos in pairs(GetAllOpenPositions()) do
if pos.positionId == long then
elseif pos.isLong and pos.positionId != long then
CloseVPosition(pos.enterPrice, pos.positionId)
end
end
if camt_l == 0 then
long = CreatePosition(PositionLong, xaep_l, xamt_l, upc_short.market, mainleverage, long)
if xamt_l == GetPositionAmount(long) then
Save(long..'adjusted', xamt_l)
end
elseif And(diffsize_l > 0, diffsize_l-longadj != 0) or And(diffsize_l < 0, diffsize_l + longadj != 0) then
AdjustVPosition(Switch(adjustprice_l==0, xaep_l, adjustprice_l), diffsize_l, long)
end
end
if log == true then
Log('adjustprice_l = '..Parse(IfNull(adjustprice_l, ''), StringType))
Log('long = '..Parse(IfNull(long, ''), StringType))
end
end
if log == true then
if long != '' then
Log('long = '..Parse(IfNull(long, ''), StringType))
Log('longadj = '..Parse(IfNull(longadj, ''), StringType))
Log('caep_l = '..Parse(IfNull(caep_l, ''), StringType))
Log('camt_l = '..Parse(IfNull(camt_l, ''), StringType))
Log('cvalue_l = '..Parse(IfNull(cvalue_l, ''), StringType))
Log('upc_long = '..Parse(IfNull(upc_long, ''), StringType))
Log('xaep_l = '..Parse(IfNull(xaep_l, ''), StringType))
Log('xamt_l = '..Parse(IfNull(xamt_l, ''), StringType))
Log('xvalue_l = '..Parse(IfNull(xvalue_l, ''), StringType))
end
end
end
if short != '' then
local shortadj = Load(short..'adjusted', 0)
local caep_s = GetPositionEnterPrice(short, false)
local camt_s = GetPositionAmount(short)
local cvalue_s = Mult(camt_s, caep_s)
local xaep_s = upc_short.enterPrice
local xamt_s = upc_short.amount
local xvalue_s = Mult(xamt_s, xaep_s)
if GetPositionAmount(short) != xamt_s then
local diffvalue_s = Sub(xvalue_s, cvalue_s)
local diffsize_s = Sub(xamt_s, camt_s)
local adjustprice_s = 0
if log == true then
Log('diffvalue_s = '..Parse(IfNull(diffvalue_s, ''), StringType))
Log('diffsize_s = '..Parse(IfNull(diffsize_s, ''), StringType))
end
if diffsize_s != 0 then
adjustprice_s = Div(diffvalue_s, diffsize_s)
for n, pos in pairs(GetAllOpenPositions()) do
if pos.positionId == short then
elseif pos.isShort == true and pos.positionId != short then
CloseVPosition(pos.enterPrice, pos.positionId)
end
end
if camt_s == 0 then
short = CreatePosition(PositionShort, xaep_s, xamt_s, upc_short.market, mainleverage, short)
if xamt_s == GetPositionAmount(short) then
Save(short..'adjusted', xamt_s)
end
elseif And(diffsize_s > 0, diffsize_s-shortadj != 0) or And(diffsize_s < 0, diffsize_s + shortadj != 0) then
AdjustVPosition(Switch(adjustprice_s==0, xaep_s, adjustprice_s), diffsize_s, short)
end
end
if log == true then
Log('adjustprice_s = '..Parse(IfNull(adjustprice_s, ''), StringType))
Log('short = '..Parse(IfNull(short, ''), StringType))
end
end
if log == true then
if short != '' then
Log('short = '..Parse(IfNull(short, ''), StringType))
Log('shortadj = '..Parse(IfNull(shortadj, ''), StringType))
Log('caep_s = '..Parse(IfNull(caep_s, ''), StringType))
Log('camt_s = '..Parse(IfNull(camt_s, ''), StringType))
Log('cvalue_s = '..Parse(IfNull(cvalue_s, ''), StringType))
Log('upc_short = '..Parse(IfNull(upc_short, ''), StringType))
Log('xaep_s = '..Parse(IfNull(xaep_s, ''), StringType))
Log('xamt_s = '..Parse(IfNull(xamt_s, ''), StringType))
Log('xvalue_s = '..Parse(IfNull(xvalue_s, ''), StringType))
end
end
end
end
DefineOutput(VoidType)
1 Comment
Sign in to leave a comment.
Please update line 68 of the script to:
elseif pos.isLong == true and pos.positionId != long then
because pos.isLong == true is missing, when you delete the position from exchange it is not updated in the bot.