[pshaiBot] Simple Position Guard
stableDescription
Simple Position Guard is here to help you trade leverage with more confidence!
It will see whether or not you have a position in the exchange and track it with trailing stop-loss and take-profit, with settings of your choice.
Known issues:
- Manually closing a position will be spotted by the bot, but the virtual position information the bot holds cannot be removed. To fix this, just clear the bot's history.
The bot will NOT WORK in spot-markets! It is for leverage markets only!
HaasScript
-- Simple Position Guard
-- by pshai
EnableHighSpeedUpdates()
-- Inputs
local slPrc = Input('Trailing Stop-Loss %', 1)
local tpPrc = Input('Take-Profit %', 2)
-- User position info and variables
local cont = UserPositionContainer()
local hasPosition = Load('hp', false)
local posId = Load('pid', '')
local cp = CurrentPrice()
local posDir = IfElse(posId != '', GetPositionDirection(posId), NoPosition)
local posRoi = IfElse(posId != '', GetPositionROI(posId), 0)
local posEp = IfElse(posId != '', GetPositionEnterPrice(posId), 0)
local posAmt = IfElse(posId != '', GetPositionAmount(posId), 0)
-- Check if we have a user position
if not hasPosition and (cont.isLong or cont.isShort) then
LogWarning('-- Found a position in account')
hasPosition = true
-- Create a virtual position of it,
-- so the bot is able to use stop-loss
-- and take-profit commands.
posId = CreatePosition(
IfElse(cont.isLong, PositionLong, PositionShort),
cont.enterPrice,
cont.amount
)
elseif hasPosition and posAmt != cont.amount and cont.amount > 0 then
LogWarning('-- Updating position information')
-- Calculate difference
local amt = Max(cont.amount, posAmt) - Min(cont.amount, posAmt)
posId = CreatePosition(
IfElse(cont.isLong, PositionLong, PositionShort),
cont.enterPrice,
amt,
{
positionId = posId
}
)
elseif hasPosition and cont.amount == 0 then
LogWarning('-- Position closed manually, tracking stopped...')
LogWarning('-- Remember to CLEAR HISTORY!')
CreatePosition(
NoPosition,
0,
0,
{
positionId = posId
}
)
posId = ''
hasPosition = false
end
-- Update virtual position information just
-- so we dont have to wait for the next update.
if posId != '' then
posDir = GetPositionDirection(posId)
posRoi = GetPositionROI(posId)
posEp = GetPositionEnterPrice(posId)
posAmt = GetPositionAmount(posId)
end
-- Check if we should be tracking the virtual position
if hasPosition and posDir != NoPosition then
LogWarning('-- Tracking Position: '..IfElse(posDir==PositionLong, 'Long', 'Short'))
LogWarning('-- Position Size: '..posAmt)
LogWarning('-- Current ROI%: '..posRoi)
LogWarning('-- Price Change %: '..Round(Delta(cp.close, posEp), 4))
if not IsAnyOrderOpen(posId) then
if TrailingStopLoss(slPrc, posId) then
LogWarning('-- STOP-LOSS HIT!')
DoExitPosition('SL ['..Round(posRoi, 3)..'%]')
hasPosition = false -- reset variable
posId = ''
elseif TakeProfit(tpPrc, posId) then
LogWarning('-- TAKE-PROFIT HIT!')
DoExitPosition('TP ['..Round(posRoi, 3)..'%]')
hasPosition = false -- reset variable
posId = ''
end
end
LogWarning('------------------------------------------------')
end
Save('hp', hasPosition)
Save('pid', posId)
2 Comments
Sign in to leave a comment.
Great script. FYI for other users... I had one issue with my setup. When I ran this script with a "Main Interval" of 1 minute and had the "Main Chart" at 1 minute the position information was not saving and the position kept increasing on each check. For example if the actual Bitmex XBT position was 34 contracts on the next passes the virtual position is 34, then 68, then 102, and so on. This all happened within minutes of starting the script. When I changed the Main Interval to 5 minutes, cleared the log, and restarted the script the virtual position remained at 34 and everything worked very well.
Hey zpeach, thanks for the tip!