Firetron's Hedge Race
stableDescription
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.
HaasScript
-- ==========================================================================================================
-- 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
-- ==========================================================================================================
-- =================
-- 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)
3 Comments
Sign in to leave a comment.
Hi @Firetron - What does this error mean
13 Feb 2021 18:08:54 WARNING: No order handling command detected. Use the command in the Order Handling category to check for open, cancel or filled orders.
Seems like this results in the bot losing reference to open positions -
61. 13 Feb 2021 19:14:25 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.60. 13 Feb 2021 19:14:00 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.59. 13 Feb 2021 19:13:45 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.58. 13 Feb 2021 19:13:25 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.57. 13 Feb 2021 19:13:00 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.56. 13 Feb 2021 19:12:45 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.55. 13 Feb 2021 19:12:30 WARNING: PlaceExitShortOrder(): Blocking order. 0 contract(s) is below exchange specifications.
That will need a discussion. Ask the Discord group.