Firetron’s Hedge Race

2 174 Views No Comments 2 months ago
  • A long position and a short position are opened at the same time. They run until hitting a stop loss or take profit. After both positions close, a new race begins. In order to make a profit, the take profit must be greater than the stop loss.

    • This topic was modified 2 months, 1 week ago by Firetron. Reason: just a whitespace alignment
    HaasScript Code
    --  ==========================================================================================================
    --    Firetron's Hedge Race
    --
    --    A long position and a short position are opened at the same time. They run until hitting a stop loss or
    --    take profit. After both positions close, a new race begins. In order to make a profit, the take profit
    --    must be greater than the stop loss.
    --
    --    Discord: @FiretronP75
    --    BTC Tips: 14WLDZsrhshcP6qvBarKW8ABACv3iUXEro
    --    ETH Tips: 0xfaaac99fbf11f8f4abc4b3bf079cb2c7a43b5f87
    --  ==========================================================================================================
    
    --  =================
    --    Configuration  
    --  =================
    
    EnableHighSpeedUpdates(true)
    HideOrderSettings()
    HideTradeAmountSettings()
    
    --  =============
    --    Variables  
    --  =============
    
    --  ----------
    --    Inputs  
    --  ----------
    
    -- Basic
    
    local amount = Input('Trade Amount',          100,   'contracts',                           'Basic')
    local fee    = Input('Market Fee Percentage', Fee(), 'for backtests and simulated trading', 'Basic')
    
    -- Winner / Loser Percentages
    
    local stopLoss   = Input('Stop Loss',   1,   'percent loss the loser should quit at',   'Winner / Loser Percentages')
    local takeProfit = Input('Take Profit', 1.5, 'percent profit the winner should win at', 'Winner / Loser Percentages')
    
    --  ----------
    --    Memory  
    --  ----------
    
    local longPositionId     = Load('longPositionId',     '')
    local maxRiskPointAmount = Load('maxRiskPointAmount', 0)
    local shortPositionId    = Load('shortPositionId',    '')
    
    --  ----------
    --    System  
    --  ----------
    
    local currentPrice = CurrentPrice()
    local longAmount   = LongAmount()
    local shortAmount  = ShortAmount()
    
    --  =============
    --    Functions  
    --  =============
    
    --  ----------
    --    Checks  
    --  ----------
    
    -- Check Max Risk Point
    
    function CheckMaxRiskPoint()
    
      local positionList = GetAllOpenPositions()
    
      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
    
    -- Did Breach Long Stop Loss
    
    function DidBreachLongStopLoss()
    
      local parameters = {
        positionId = longPositionId,
        direction  = PositionLong,
      }
    
      return StopLoss(stopLoss, parameters)
    
    end
    
    -- Did Breach Long Take Profit
    
    function DidBreachLongTakeProfit()
    
      local parameters = {
        positionId = longPositionId,
        direction  = PositionLong,
      }
    
      return TakeProfit(takeProfit, parameters)
    
    end
    
    -- Did Breach Short Stop Loss
    
    function DidBreachShortStopLoss()
    
      local parameters = {
        positionId = shortPositionId,
        direction  = PositionShort,
      }
    
      return StopLoss(stopLoss, parameters)
    
    end
    
    -- Did Breach Short Take Profit
    
    function DidBreachShortTakeProfit()
    
      local parameters = {
        positionId = shortPositionId,
        direction  = PositionShort,
      }
    
      return TakeProfit(takeProfit, parameters)
    
    end
    
    -- Is Input Valid
    
    function IsInputValid()
    
      if takeProfit > stopLoss then return true end
    
      LogError('Take Profit must be higher than Stop Loss')
    
      return false
    
    end
    
    --  ------------
    --    Entering  
    --  ------------
    
    -- Try Begin Race
    
    function TryBeginRace()
    
      local didRaceEnd = longAmount == 0 and shortAmount == 0
    
      if not didRaceEnd then return end
    
      GoLong()
      GoShort()
    
    end
    
    -- Go Long
    
    function GoLong()
    
      longPositionId = NewGuid()
    
      local parameters = {
        type       = MarketOrderType,
        note       = 'Long is racing!',
        positionId = longPositionId,
      }
    
      PlaceGoLongOrder(currentPrice, amount, parameters)
    
      Save('longPositionId', longPositionId)
    
    end
    
    -- Go Short
    
    function GoShort()
    
      shortPositionId = NewGuid()
    
      local parameters = {
        type       = MarketOrderType,
        note       = 'Short is racing!',
        positionId = shortPositionId,
      }
    
      PlaceGoShortOrder(currentPrice, amount, parameters)
    
      Save('shortPositionId', shortPositionId)
    
    end
    
    --  -----------
    --    Exiting  
    --  -----------
    
    -- Try Exit Long
    
    function TryExitLong()
    
      if longAmount == 0 then
        return
      elseif DidBreachLongStopLoss() then
        ExitLong('Long has quit!')
      elseif DidBreachLongTakeProfit() then
        ExitLong('Long has won!')
      end
    
    end
    
    -- Try Exit Short
    
    function TryExitShort()
    
      if shortAmount == 0 then
        return
      elseif DidBreachShortStopLoss() then
        ExitShort('Short has quit!')
      elseif DidBreachShortTakeProfit() then
        ExitShort('Short has won!')
      end
    
    end
    
    -- Exit Long
    
    function ExitLong(note)
    
      local parameters = {
        positionId = longPositionId,
        type       = MarketOrderType,
        note       = note,
      }
    
      PlaceExitPositionOrder(parameters);
    
    end
    
    -- Exit Short
    
    function ExitShort(note)
    
      local parameters = {
        positionId = shortPositionId,
        type       = MarketOrderType,
        note       = note,
      }
    
      PlaceExitPositionOrder(parameters);
    
    end
    
    --  =============
    --    Execution  
    --  =============
    
    if IsInputValid() then
    
      SetFee(fee)
      CheckMaxRiskPoint()
      TryBeginRace()
      TryExitLong()
      TryExitShort()
    
    end
    
    --  =============
    --    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