Firetron's Hedged Flash Crash
stableDescription
Tries to catch flashes up and flashes down at the same time. Places orders when price changes a certain amount within a certain time frame. One long and one short may be open at the same time. Position direction is chosen based on your choice of reverting or trending.
HaasScript
-- ==========================================================================================================
-- Firetron's Hedged Flash Crash
--
-- Tries to catch flashes up and flashes down at the same time. Places orders when price changes a certain
-- amount within a certain time frame. One long and one short may be open at the same time. Position
-- direction is chosen based on your choice of reverting or trending.
--
-- Discord: @FiretronP75
-- ==========================================================================================================
-- =================
-- Configuration
-- =================
EnableHighSpeedUpdates(true)
HideOrderSettings()
HideTradeAmountSettings()
-- =============
-- Variables
-- =============
local modeType = {
reverting = 'reverting',
trending = 'trending',
}
-- ----------
-- Inputs
-- ----------
-- Testing
local fee = Input('Fee Percentage', Fee(), 'for backtests and simulated trading', 'Testing')
-- Script Logic
local downTrigger = Input( '1) If price goes down by', -3, '(Percent)', 'Script Logic')
local upTrigger = Input( '2) or price goes up by', 3, '(Percent)', 'Script Logic')
local interval = InputInterval('3) Within an interval of', 60, '(Candle)', 'Script Logic')
local amount = Input( '4) Place an order for', 100, '(Contracts)', 'Script Logic')
local mode = InputOptions( '5) in hope of market', modeType.reverting, modeType, '(Relative to the price change direction)', 'Script Logic')
local takeProfit = Input( '6) With a take profit of', 2, '(Percent)', 'Script Logic')
local stopLoss = Input( '7) and a stop loss of', 1, '(Percent)', 'Script Logic')
local coolDown = InputInterval('8) and cool down of', 60, 'before placing in same direction again', 'Script Logic')
-- ----------
-- Memory
-- ----------
local maxRiskPointAmount = Load('maxRiskPointAmount', 0)
local longPositionId = Load('longPositionId', '')
local shortPositionId = Load('shortPositionId', '')
local longStopLossTime = Load('longStopLossTime', 0)
local shortStopLossTime = Load('shortStopLossTime', 0)
-- ----------
-- System
-- ----------
local currentPrice = CurrentPrice()
local highPriceList = HighPrices(interval, true)
local lowPriceList = LowPrices( interval, true)
local longContainer = PositionContainer(longPositionId)
local shortContainer = PositionContainer(shortPositionId)
-- --------------
-- Calculated
-- --------------
local highPrice = ArrayGet(highPriceList, 1)
local lowPrice = ArrayGet(lowPriceList, 1)
local downChange = PercentageChange(highPrice, currentPrice.bid)
local upChange = PercentageChange(lowPrice, currentPrice.ask)
local downWasTriggered = downChange <= downTrigger
local upWasTriggered = upChange >= upTrigger
-- =============
-- Functions
-- =============
-- ------------
-- Checking
-- ------------
-- 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
-- Check Long
function CheckLong()
if longPositionId == '' or IsPositionClosed(longPositionId) then
if longStopLossTime > Time() then
return
end
if downWasTriggered and mode == modeType.reverting then
GoLong('reverting the jump down')
end
if upWasTriggered and mode == modeType.trending then
GoLong('following the trend up')
end
elseif TakeProfit(takeProfit, longPositionId, PositionLong) then
ExitLong('take profit')
elseif StopLoss(stopLoss, longPositionId, PositionLong) then
ExitLong('stop loss')
longStopLossTime = Time()
longStopLossTime = AdjustTimestamp(longStopLossTime, 0, coolDown)
Save('longStopLossTime', longStopLossTime)
end
end
-- Check Short
function CheckShort()
if shortPositionId == '' or IsPositionClosed(shortPositionId) then
if shortStopLossTime > Time() then
return
end
if downWasTriggered and mode == modeType.trending then
GoShort('following the trend down')
end
if upWasTriggered and mode == modeType.reverting then
GoShort('reverting the jump up')
end
elseif TakeProfit(takeProfit, shortPositionId, PositionShort) then
ExitShort('take profit')
elseif StopLoss(stopLoss, shortPositionId, PositionShort) then
ExitShort('stop loss')
shortStopLossTime = Time()
shortStopLossTime = AdjustTimestamp(shortStopLossTime, 0, coolDown)
Save('shortStopLossTime', shortStopLossTime)
end
end
-- -----------
-- Exiting
-- -----------
-- Exit Long
function ExitLong(note)
local parameters = {
positionId = longPositionId,
price = currentPrice,
type = MarketOrderType,
note = note,
}
PlaceExitPositionOrder(parameters);
end
-- Exit Short
function ExitShort(note)
local parameters = {
positionId = shortPositionId,
price = currentPrice,
type = MarketOrderType,
note = note,
}
PlaceExitPositionOrder(parameters);
end
-- ------------
-- Entering
-- ------------
-- Go Long
function GoLong(note)
longPositionId = NewGuid()
local parameters = {
type = MarketOrderType,
note = note,
positionId = longPositionId,
}
PlaceGoLongOrder(currentPrice, amount, parameters)
Save('longPositionId', longPositionId)
end
-- Go Short
function GoShort(note)
shortPositionId = NewGuid()
local parameters = {
type = MarketOrderType,
note = note,
positionId = shortPositionId,
}
PlaceGoShortOrder(currentPrice, amount, parameters)
Save('shortPositionId', shortPositionId)
end
-- =============
-- Execution
-- =============
SetFee(fee)
CheckMaxRiskPoint()
CheckLong()
CheckShort()
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!