[Kobalt] VPM V∆adjuster

beta
By Kobalt in Miscellaneous Published August 2022 👁 1,044 views 💬 1 comments

Description

Last version optimised for v4 [[The (InputButtons used) are NOT compatible with V3.]] -before merging with virtual actual order interim managed.. This will be the Virtual adjusting, Never on exchange) component for Virtual matching, reducing or opening (accounting the virtual profits vs realised) To adjust any pre paid offset virtual profits (from enterLong as realised Take Profit on an existing position on exchange and using the wirtual EXIT as an averaging, Take Profit re-enter Short from averaging, price tracking Virtual [ idea for this is it will be used in a passive Meta position Tracking Average Entry Price + size.. vs Virtual to improve the 'real' not modify the current Actual on exchange position [i.e. maybe enhance the tracking of ∆realised unrealizable Virtual] while other parts execute modifications to the position: -used with active component [that switches reduce? can enter, can exit relative to both aep [ from inverse short: only place PlaceGoLongOrder on over sold range, can exit above Short AEP (here any long take profits will realise loss below short ) uses can enterLong or is reduceOnly (shortTP) exitLong(virtualExit, vGo or .. or enterShort (actualGo or virtualExit) ......potential to recursively go fractal haha... and vv for short the virtual exits have priority in the event the take profits, were not a very good move.. but can be postponed to far above short aep if it was a good move to reduce short, close to flatten on exchange , you keep paid option to enter short [above last aep short as a meta gain ... -opening, closing in between short and long aep[your positions spread should -always- be avoided but are a safety buffer to lock profit against loss - any un filled virtual options... <VPM food for further development] in HEDGE you can do either or both or neither.. [;p] very fun way to counter any price action, you can take profit on both sides and drop the side that is subtly reduced, with a trail stop lock on it ] : Not changing ibut possibly influencing results on exchange position changes only to keep or reset score /adjust for 'spillage' can include an enforced floor/ceiling ACTUAL profit virtual (only to match or account against Actual on exchange position(s)
HaasScript
-- Authors: Kobalt -discord: Kobalt#7977  tipjar: BTC bc1qz6gw8hje2scfq4wfmgtlfe9h4tgm7u8qzwev29
--   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', 'VPM for HS 4 include flips Y/N?::: 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 = '   Virtual Position Management [Execute]'
InputGroupHeader(exec)
local group = 'Virtual Position Management [Parameters]'
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('  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('     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('      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('       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('        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('Create Position', Createclicked(true), 'Enter changes into inputfields and click to execute', exec)
InputButton(' Adjust Position', Adjustclicked(true), 'Enter changes into inputfields and click to execute', exec)
InputButton('  Close Position ', 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.

J
JeffVernon almost 4 years ago

Another great add. Thanks!