Firetron's Mad Flipper

stable
By Firetron in Trading Bots Published May 2020 👁 1,603 views 💬 0 comments

Description

Flips position every time the stop loss or the take profit is hit.
HaasScript
--  ==========================================================================================================
--    Firetron's Mad Flipper
--
--    Flips position every time the stop loss or the take profit is hit.
--
--    Discord:  @FiretronP75
--  ==========================================================================================================

--  =================
--    Configuration  
--  =================

EnableHighSpeedUpdates()
HideOrderSettings()
HideTradeAmountSettings()

--  =============
--    Variables  
--  =============

local direction = {
  long  = 'Go Long',
  short = 'Go Short',
}

--  ----------
--    Inputs  
--  ----------

-- Testing

local fee = Input('Maker Fee Percentage', Fee(), 'for backtests and simulated trading', 'Testing')

-- Script Logic

local startDirection = InputOptions('Start Direction', direction.long, direction, '(First positition to place)', 'Script Logic')

local amount     = Input('Amount',    100, '(Contracts)', 'Script Logic')
local stopLoss   = Input('Stop Loss',   2, '(Percent)',   'Script Logic')
local takeProfit = Input('Take Profit', 2, '(Percent)',   'Script Logic')

--  ----------
--    Memory  
--  ----------

local maxRiskPointAmount = Load('maxRiskPointAmount', 0)

--  ----------
--    System  
--  ----------

local amount       = TradeAmount()
local positionList = GetAllOpenPositions()
local sentiment    = OrderbookSentiment()

--  =============
--    Functions  
--  =============

--  ------------
--    Checking  
--  ------------

-- Check Max Risk Point

function CheckMaxRiskPoint()

  if Count(positionList) == 0 then return end

  local nowRiskPointAmount = 0

  for i = 1, #positionList do

    local positionContainer = positionList[i]

    nowRiskPointAmount = nowRiskPointAmount + positionContainer.profit

  end

  if nowRiskPointAmount >= maxRiskPointAmount then return end

  maxRiskPointAmount = nowRiskPointAmount

  Save('maxRiskPointAmount', maxRiskPointAmount)

end

-- Check If No Position

function CheckNoPosition()

  if Count(positionList) > 0 then return end

  if startDirection == direction.long then GoLong() else GoShort() end

end

-- Check Stop Loss And Take Profit

function CheckStopLossAndTakeProfit()

  if StopLoss(stopLoss) or TakeProfit(takeProfit) then

    DoFlipPosition()

  end

end

--  ------------
--    Entering  
--  ------------

-- Go Long

function GoLong()
  
  local parameters = {
    type = MarketOrderType,
  }

  PlaceGoLongOrder(0, amount, parameters)

end

-- Go Short

function GoShort()

  local parameters = {
    type = MarketOrderType,
  }

  PlaceGoShortOrder(0, amount, parameters)

end

--  =============
--    Execution  
--  =============

CheckMaxRiskPoint()
CheckNoPosition()
CheckStopLossAndTakeProfit()

--  =============
--    Reporting  
--  =============

Finalize(function()

  local maxRiskPointAmountText = maxRiskPointAmount..' '..QuoteCurrency()

  CustomReport('Max. Risk Point Amount', maxRiskPointAmountText, 'Max Risk Point Report')

end)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!