Simple buy (RSI based) and sell (only with profit)

stable
By Shoe in Trading Bots Published September 2020 👁 1,669 views 💬 0 comments

Description

The script buys (upon a specific amount) on parametrized RSI levels and sells if a certain profit is reached.
HaasScript
local openPositions = GetAllOpenPositions()
local maxInvestment = Input('Investment Max', 3000,'','Investment')
local investPerOrder = Input('Investment per Order', 100,'','Investment')
local maxOpenPositionCount = maxInvestment / investPerOrder
local minsUntilNextOrder = Round(Input('h until next Order', 0.5,'','Time Management') * 60, 0)

local lastOrderTime = Load('lastOrderTime', AdjustTimestamp(Time(), {addYears = -1}))

local availableOrderCount = maxOpenPositionCount - Count(openPositions)

-- Create an input for RSI length
local rsiLength = Input('RSI Period', 7)
local upperLimit = Input('RSI Upper Limit', 70)
local lowerLimit = Input('RSI Lower Limit', 30)

local c = ClosePrices()

local MyRSI = RSI(c,rsiLength)

-- == Charts ==
PlotHorizontalLine(1, '', DarkGray, upperLimit) -- Horizontal lines
PlotHorizontalLine(1, '', DarkGray, lowerLimit)
Plot(1, 'RSI', MyRSI, Aqua)
Plot(2, 'Open Positions', Count(openPositions), Green)
Plot(2, 'Max Order Count', maxOpenPositionCount, DarkGreen)

-- == Trading Logic
if Count(openPositions) > 0 then
    for key,positionContainer in pairs(openPositions) do
        if positionContainer.isLong 
        and positionContainer.enterPrice * 1.007 < CurrentPrice().ask
        then
            PlaceExitPositionOrder(positionContainer.positionId, {type = MarketOrderType})
        end
    end
end

Log("availableOrderCount: " .. availableOrderCount .. " / maxOpenPositionCount: " .. maxOpenPositionCount)
Log("lastOrderTime: " .. lastOrderTime .. " / minsUntilNextOrder: " .. minsUntilNextOrder)
Log("Time(): " .. Time() .. " / AdjustTimestamp(lastOrderTime, {addMinutes = minsUntilNextOrder}): " .. AdjustTimestamp(lastOrderTime, {addMinutes = minsUntilNextOrder}))
Log("MyRSI: " .. MyRSI .. " / lowerLimit: " .. lowerLimit)

if (
        availableOrderCount >= 1
        and Time() > AdjustTimestamp(lastOrderTime, {addMinutes = minsUntilNextOrder})
    )
    and MyRSI < lowerLimit
    then
        PlaceBuyOrder(CurrentPrice(), investPerOrder / CurrentPrice().ask, {positionId = NewGuid(), note = "Regular"})
        Save('lastOrderTime', Time())
end

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!