Strvinmarvin's ReportMaxMargin

stable
By Strvinmarvin in Miscellaneous Published January 2023 👁 1,442 views 💬 0 comments

Description

Adds a custom report on your maximum margin used.
HaasScript
-- Modified by Strvninmarvin on 2021-11-02

--  ============================================================================
--    Strvinmarvin's ReportMaxMargin
--
--    Adds a custom report on your maximum margin used.
--     
--    * Note: Thanks to Firetron, for I have shamelessly ripped off his ReportMaxRiskPoint
--            command and used the template and framework to make this. Great work!
--  ============================================================================
 
--  ========================================================
--    Variables
--  ========================================================
 
--  ------------------------------------
--    Definition
--  ------------------------------------
 
local dDescription
local dName
local dType
 
--  ------------------------------------
--    Function
--  ------------------------------------
 
local Format
local Report
local Track
 
--  ========================================================
--    Command Definition
--  ========================================================
 
dName        = 'ReportMaxMargin'
dDescription = 'Adds a custom report on your highest used margin.'
DefineCommand(dName, dDescription)

local mkt = DefineParameter(StringType, 'market', 'The currency market', false, PriceMarket(), 'InputAccountMarket, InputMarket, PriceMarket')
 
--  ========================================================
--    Functions
--  ========================================================
 
Track = function ()
 
  local current = 0
  local amount = 0
  local pos = {
      ['LONG'] = UserPositionContainer('', mkt, nil, PositionLong),
      ['SHORT'] = UserPositionContainer('', mkt, nil, PositionShort),
  }
  local cp = CurrentPrice(mkt)
  local margin = {
      ['LONG'] = UsedMargin(mkt, cp.bid, pos['LONG'].amount, Leverage()),
      ['SHORT'] = UsedMargin(mkt, cp.ask, pos['SHORT'].amount, Leverage())
  }

  current = margin['LONG'] + margin['SHORT']
  amount = pos['LONG'].amount + pos['SHORT'].amount
 
  if current <= Load('worst'..mkt, 0) then return end
 
  Save('unix'..mkt,  Time())
  Save('worst'..mkt, Round(current, 6))
  Save('amount'..mkt, Round(amount, 6))
 
end
 
--  ----------------
 
Report = function ()
 
  local unix  = Load('unix'..mkt)
  local worst = Load('worst'..mkt, 0)
  local amount = Load('amount'..mkt, 0)
  local group = 'Max Margin Used Report'
  local name  = BaseCurrency(mkt)..' - Max Margin Used'
  local value = IfElse(worst == 0, 'no margin used yet', Format(unix, worst, amount))
 
  CustomReport(name, value, group)
 
end
 
--  ----------------
 
Format = function (unix, worst, amount)
 
  return worst..' '..ProfitLabel()..' on '..CC_FormatDateTime(unix)..' ('..amount..' tokens)'
 
end
 
--  ========================================================
--    Execution
--  ========================================================
 
Track()
 
Finalize(function()
 
  Report()
 
end)
 
--  ========================================================
--    Output Definition
--  ========================================================
 
dType = VoidType
DefineOutput(dType, 0)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!