[CMD] VPM (HaasScript Version)

stable
By Strooth in Miscellaneous Published May 2021 👁 2,065 views 💬 3 comments

Description

Written in HaasScript but taken from the original visual editor version here VPM3 Manage and fix positions from Haas Script Bots as per the original. Just added some additional leverage and market options so this can be used on multi market bot scripts like a crypto index bot for example. to use with defaults or see the parameters for more advanced scripting use

CC_VPM_HS()
-- Author - Strooth - Find me on discord - strooth#4739 -- Feel free to donate to support my work or if my script helped you in any way <3 -- BTC Adress: 33MsEAbA8tg7SpohgnCpSrmPTBih2UkhxQ -- v4 version here VPM4
HaasScript
-- Author - Strooth - Find me on discord - strooth#4739
-- Feel free to donate to support my work or if my script helped you in any way &lt;3
-- BTC Adress: 33MsEAbA8tg7SpohgnCpSrmPTBih2UkhxQ
--
DefineCommand('VPM_HS', 'VPM-HS ::: create_adjust_close::: | Manage Virtual Positions in running HaasBots.')
local leverages = {'CrossMargin', '1x', '2x', '3x'}
for i=5, 125, 5 do 
    leverages[#leverages+1] = i..'x'
end 
local group = 'Virtual Position Management'
local CreateVPosition_execute = DefineParameter(BooleanType, 'CreateVPosition_execute', 'Check box and hit save to execute adjustment. Clear inputfields uncheck execute checkbox and save again ', false, Input('        Position Create', false, 'Check box and hit save to execute and create VPosition. Clear inputfields uncheck execute checkbox and save again', group), 'true/false')
local AdjustVPosition_execute = DefineParameter(BooleanType, 'AdjustVPosition_execute', 'Check box and hit save to execute adjustment. Clear inputfields uncheck execute checkbox and save again ', false, Input('       Position Adjust', false, 'Check box and hit save to execute and create VPosition. Clear inputfields uncheck execute checkbox and save again', group), 'true/false')
local CloseVPosition_execute = DefineParameter(BooleanType, 'CloseVPosition_execute', 'Check box and hit save to execute adjustment. Clear inputfields uncheck execute checkbox and save again ', false, Input('      Position Close', false, 'Check box and hit save to execute and create VPosition. Clear inputfields uncheck execute checkbox and save again', group), 'true/false')
local PositionPrice = DefineParameter(NumberType, 'PositionPrice', 'Price to create, close or adjust VP', false, Input('   Position Price:', 0, 'Price to create, close or adjust VP', group), 'Number')
local PositionAmount = DefineParameter(NumberType, 'PositionAmount', 'Amount to adjust: negative value reduces, positive value adds - also required at creating VP', false, Input('   Position Amount:', 0, 'adjustVPosition amount', group), 'Number')
local PId = DefineParameter(StringType, 'PId', 'Required to Adjust or Close,  empty field to Create position    (if empty, null: a new unique positionId is created for the position: NewGuid  )', false, Input('  Position Id', 'Empty=NewGuid', 'Required to Adjust or Close,  empty field to Create position    (if empty, null: a new unique positionId is created for the position: NewGuid  )', group), 'PositionContainer, Input, Load, string')
local PositionLeverage = DefineParameter(StringType, 'PositionLeverage', 'Required to Create', false, InputOptions('   Position Leverage:', leverages[1],leverages, 'Required to Create position    (0 for cross margin or -1 for default  )', group), 'String/Input')
local PositionMarket = DefineParameter(StringType, 'PositionMarket', 'Required to Create', false, InputMarket('   Position Market:', PriceMarket(), 'Required to Create position', group), 'String')
local PositionDirection = DefineParameter(EnumType, 'PositionDirection', 'direction of the VPosition ', false, InputConstant('Position Direction', NoPosition, 'direction of the VPosition', group), 'PositionLong, PositionShort')
local executedOnce = Load('executedOnce', false)
PositionLeverage =Parse( PositionLeverage == 'CrossMargin' and 0 or StringSplit(PositionLeverage, 'x')[1], NumberType)
local state1 = And(CreateVPosition_execute, Or(AdjustVPosition_execute, CloseVPosition_execute))
local state2 = And(CloseVPosition_execute, Or(AdjustVPosition_execute, CreateVPosition_execute))
local state3 = And(AdjustVPosition_execute, Or(CloseVPosition_execute, CreateVPosition_execute))
local state4 = And(CreateVPosition_execute, IsFalse(AdjustVPosition_execute, CloseVPosition_execute))
local state5 = And(CloseVPosition_execute, IsFalse(AdjustVPosition_execute, CreateVPosition_execute))
local state6 = And(AdjustVPosition_execute, IsFalse(CloseVPosition_execute, CreateVPosition_execute))
local state7 = IsFalse(CloseVPosition_execute, CreateVPosition_execute, AdjustVPosition_execute)
if Or(state1, state2, state3) then
    LogWarning('Please Only Tick One Position Management Option at a time, untick them all when finished and save again')
    return 
elseif Or(state4, state5, state6) and executedOnce == true then 
    LogWarning('Simulated Order has been executed. Please clear inputfields, uncheck execute checkbox and save again.')
    return 
elseif state7 and executedOnce == true then 
    Save('executeOnce', false)
    return
elseif Or(state4, state5, state6) and executedOnce == false then 
    local cancreate = And(IsFalse(PositionPrice == 0),IsFalse(PositionAmount == 0),IsFalse(PositionDirection == NoPosition),IsFalse(PId == 'Empty=NewGuid'))
    local canclose = And(IsFalse(PositionPrice == 0),IsFalse(PositionAmount == 0),IsTrue(PositionDirection == NoPosition),IsFalse(PId == 'Empty=NewGuid'))
    local canadjust = And(IsFalse(PositionPrice == 0),IsFalse(PositionAmount == 0),IsFalse(PId == 'Empty=NewGuid'))
    PId = IfNull(PId, NewGuid())
    if IsFalse(cancreate,canclose, canadjust) then 
        LogWarning('Incorrect settings specified in Virtual Position Management')
        return 
    elseif cancreate and state4 then 
        CreatePosition(PositionDirection, PositionPrice, PositionAmount, PositionMarket, PositionLeverage, PId)
        executedOnce = true
        Save('executedOnce', executedOnce)
        return
    elseif canadjust and state6 then 
        AdjustVPosition(PositionPrice, PositionAmount, PId)
        executedOnce = true
        Save('executedOnce', executedOnce)
        return
    elseif canclose and state5 then 
        AdjustVPosition(PositionPrice, PositionAmount, PId)
        executedOnce = true
        Save('executedOnce', executedOnce)
        return
    end 
    return
end 
DefineOutput(VoidType)

3 Comments

Sign in to leave a comment.

T
TooT almost 5 years ago

Hi Strooth,

I got these massage :

ERROR: Command script is invalid and can not be used. ??

W
wMNGTh over 4 years ago

Have you tested this on HO v4?

S
Strooth over 4 years ago

v4 version here
https://www.haasscripts.com/t/cmd-vpm-haasscript-version-v4/