[CMD] VPM (HaasScript Version - V4)

stable
By Strooth in Miscellaneous Published November 2021 👁 1,676 views 💬 1 comments

Description

Updated version of VPM for V4 using buttons and can still be called from another script using old logic for multi market controller scripts etc. -- 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 --
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.')
-- define variables
local getstate, cancreate, canclose, canadjust, executedOnce, create, adjust, close, Createclicked, Adjustclicked, Closeclicked, CreateVPosition_execute, AdjustVPosition_execute, CloseVPosition_execute, PositionPrice, PositionAmount, PId, PositionLeverage, PositionDirection, PositionMarket
local anyclicked = false 
local leverages = {'CrossMargin', '1x', '2x', '3x'}
for i=5, 125, 5 do 
    leverages[#leverages+1] = i..'x'
end 
-- create input groups 
local exec = '00. Virtual Position Management'
InputGroupHeader(exec)
local group = '01. Virtual Position Management'
InputGroupHeader(group)
-- get inputs/parameters values
CreateVPosition_execute = DefineParameter(DynamicType, 'CreateVPosition_execute', 'Check box and hit save to execute adjustment. Clear inputfields uncheck execute checkbox and save again ', false, false, 'true/false')
AdjustVPosition_execute = DefineParameter(DynamicType, 'AdjustVPosition_execute', 'Check box and hit save to execute adjustment. Clear inputfields uncheck execute checkbox and save again ', false, false, 'true/false')
CloseVPosition_execute = DefineParameter(DynamicType, 'CloseVPosition_execute', 'Check box and hit save to execute adjustment. Clear inputfields uncheck execute checkbox and save again ', false, false, 'true/false')
PositionPrice = DefineParameter(NumberType, 'PositionPrice', 'Price to create, close or adjust VP', false, Input('04. Position Price:', 0, 'Price to create, close or adjust VP', group), 'Number')
PositionAmount = DefineParameter(NumberType, 'PositionAmount', 'Amount to adjust: negative value reduces, positive value adds - also required at creating VP', false, Input('05. Position Amount:', 0, 'adjustVPosition amount', group), 'Number')
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('06. 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')
PositionLeverage = DefineParameter(NumberType, 'PositionLeverage', 'Required to Create', false, InputOptions('07. Position Leverage:', leverages[1],leverages, 'Required to Create position    (0 for cross margin or -1 for default  )', group), 'Number')
PositionDirection = DefineParameter(EnumType, 'PositionDirection', 'direction of the VPosition ', false, InputConstant('08. Position Direction', NoPosition, 'direction of the VPosition', group), 'PositionLong, PositionShort')
PositionMarket = DefineParameter(StringType, 'PositionMarket', 'Required to Create', false, PriceMarket(), 'String')
-- check if inputs/parameters are ok for functions to execute 
cancreate = And(IsFalse(PositionPrice == 0),IsFalse(PositionAmount == 0),IsFalse(PositionDirection == NoPosition),IsFalse(PId == 'Empty=NewGuid'))
canclose = And(IsFalse(PositionPrice == 0),IsFalse(PositionAmount == 0),IsTrue(PositionDirection == NoPosition),IsFalse(PId == 'Empty=NewGuid'))
canadjust = And(IsFalse(PositionPrice == 0),IsFalse(PositionAmount == 0),IsFalse(PId == 'Empty=NewGuid'))
PId = Switch(PId=='Empty=NewGuid', NewGuid(), IfNull(PId, NewGuid()))
executedOnce = Load('executedOnce', false)
PositionLeverage = PositionLeverage == 'CrossMargin' and 0 or StringSplit(PositionLeverage, 'x')[1]

-- get state for running from both another script and running bot 
getstate = function(create, adjust, close)
    local state = 0
    if And(create == true, Or(adjust == true, close == true)) then state = 1 end 
    if And(close == true, Or(adjust == true, create == true)) then state = 1 end 
    if And(adjust == true, Or(close == true, create == true)) then state = 1 end 
    if And(create == true, adjust == false, close == false) then state = 2 end 
    if And(close == true, adjust == false, create == false) then state = 2 end 
    if And(adjust == true, close == false, create == false) then state = 2 end 
    if And(close == false, create == false, adjust == false) then state = 3 end 
    return state 
end 
-- create function for button callback and script init
Createclicked = function(clicked)
    local state = clicked == true and getstate(clicked, false, false) or getstate(CreateVPosition_execute, false, false)
    if state == 2 and Load('executedOnce') == false and cancreate == true then 
        CreatePosition(PositionDirection, PositionPrice, PositionAmount, PositionMarket, PositionLeverage, PId)
        Save('executedOnce', true)
    end 
    anyclicked = clicked
    return VoidDataType
end
-- adjust function for button callback and script init 
Adjustclicked = function(clicked)
    local state = clicked == true and getstate(false, clicked, false) or getstate(false, AdjustVPosition_execute, false)
    if state == 2 and Load('executedOnce') == false and canadjust == true then 
        AdjustVPosition(PositionPrice, PositionAmount, PId)
        Save('executedOnce', true)
    end 
    anyclicked = clicked 
    return VoidDataType
end
-- close function for button callback and script init
Closeclicked = function(clicked)
    local state = clicked == true and getstate(false, false, clicked) or getstate(false, false, CloseVPosition_execute)
    if state == 2 and Load('executedOnce') == false and canclose == true then 
        AdjustVPosition(PositionPrice, PositionAmount, PId)
        Save('executedOnce', true)
    end 
    anyclicked = clicked
    return VoidDataType
end

-- if running live in running bot 
InputButton('01. Position Create', Createclicked(true), 'Enter changes into inputfields and click to execute', exec)
InputButton('02. Position Adjust', Adjustclicked(true), 'Enter changes into inputfields and click to execute', exec)
InputButton('03. Position Close', Closeclicked(true), 'Enter changes into inputfields and click to execute', exec)

-- if running from another script

    local state = getstate(CreateVPosition_execute, AdjustVPosition_execute, CloseVPosition_execute)
    if state == 1 then
        LogWarning('Please Only enable One Position Management Option at a time')
        return 
    elseif state == 2 and executedOnce == true then 
        LogWarning('Simulated Order has been executed. Please clear inputfields')
        return 
    elseif state == 3 and executedOnce == true then 
        Save('executeOnce', false)
        return
    end
    if state == 2 and Load('executeOnce') == false and anyclicked == false then 
        if IsFalse(cancreate,canclose, canadjust) then 
            LogWarning('Incorrect settings specified in Virtual Position Management')
            return 
        elseif cancreate == true and CreateVPosition_execute == true then 
            Createclicked(false)
            return
        elseif canadjust == true and AdjustVPosition_execute == true then 
            Adjustclicked(false)
            return
        elseif canclose == true and CloseVPosition_execute == true then 
            Closeclicked(false)
            return
        end 
        return
    end 

DefineOutput(VoidType)

1 Comment

Sign in to leave a comment.

K
Kobalt almost 4 years ago

Hey,
Great job, love the buttons!.
-gave input menu some some <3.
Maybe you can merge this then will set this to EOL.
[to prevent multiple suggestions for the [non essential or breaking ] VPM dependency in many scripts, trade bots..]

https://www.haasscripts.com/t/vpm4-virtual-position-management-for-hs4-tradeserver-cloud/