Firetron’s Mad Flipper

1 144 Views No Comments 2 months ago
  • Flips position every time the stop loss or the take profit is hit.

    HaasScript Code
    --  ==========================================================================================================
    --    Firetron's Mad Flipper
    --
    --    Flips position every time the stop loss or the take profit is hit.
    --
    --    Discord:  @FiretronP75
    --    BTC Tips: 14WLDZsrhshcP6qvBarKW8ABACv3iUXEro
    --    ETH Tips: 0xfaaac99fbf11f8f4abc4b3bf079cb2c7a43b5f87
    --  ==========================================================================================================
    
    --  =================
    --    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)
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free