Spot Market Trading Bot Template with Bot Inheritance

stable
By romdisc in Trading Bots Published April 2024 👁 1,546 views 💬 0 comments

Description

Hi, once again me, HaHa. I like you to inspire your thinking what is possible with HaasScript. I like modularizing code as much as much as I can, as a Software Developer and so I do it with HaasScripts. I developed a Spot Market Trading Bot Template that can ease your development very much. You call it in your code like this: (easy peasy) local bot = CC_SpotBot('Alfonso #002') --- here you can add optional parts parts bot:run() Here, you can see an example: -- Author: romdisc -- test bot local bot = CC_SpotBot('My Spot Bot #001') bot.inputs = function(bot_name) local dict = {} InputGroupHeader(bot_name .. ' - Settings') dict.period = Input('Period', 14) dict.buyLevel = Input('Buy level', 30) dict.sellLevel = Input('Sell level', 70) return dict end bot.indicators = function(inputs) local dict = {} dict.rsi = RSI(ClosePrices(), inputs.period) return dict end bot.signal = function(signal, indicators, inputs) signal = IfNull(signal, SignalNone) if indicators.rsi < inputs.buyLevel then signal = SignalBuy elseif indicators.rsi > inputs.sellLevel then signal = SignalSell else signal = SignalNone end return signal end bot.plot = function(indicators, inputs) Plot(1, 'RSI', indicators.rsi, SkyBlue) PlotHorizontalLine(1, 'Buy', Cyan, inputs.sellLevel, Dotted) PlotHorizontalLine(1, 'Buy', Cyan, inputs.buyLevel, Dotted) end bot:run() This bot template is very flexible. You can inherit your own version out of the super class SpotBot maybe for Future Trading or some other customazations. DefineCommand('MyCustomBot', 'My Custom Bot inheritins SpotBot') local bot_name = DefineParameter(StringType, 'Bot', 'Bot Name', true, 'Bot') local Bot = CC_SpotBot(bot_name) -- override default behavour of your parent strategy and/or any other function Bot.strategy = function(signal, indicators, inputs) -- your different strategy algorithm (possibly your future strategy) end DefineOutput(ListDynamicType, Bot(bot_name), 'Bot Template') Happy Coding !!! - Cheers!!!
HaasScript
-- Module:  Bot Template
-- Name:    Spot Bot (Base Bot)
-- Author:  romdisc
-- Date:    04/09/2024
-- Update:  04/09/2024

-- Description
-- ===========
-- 
-- Implements a simple high speed bot for spot market trading
--
-- Instantiation    : local bot = CC_SpotBot('name of bot')
-- 
-- implement these functions (all of them are optional):
-- bot.inputs       = function(bot_name) returns a table with the configured inputs
-- bot.safety       = function(indicators, inputs) returns false by default
-- bot.indicators   = function(inputs) returns a table with the configured indicators
-- bot.signal       = function(indicators, inputs, old_signal) 
-- bot.plot         = function(indicators, inputs) 
--
-- Starting the bot : bot:run()

DefineCommand('SpotBot', 'Spot Bot')

local bot_name = DefineParameter(StringType, 'Bot', 'Bot Name', true, 'Bot')

local Bot = function(bot_name)

    EnableHighSpeedUpdates(true)

    local dict = {}

    dict._bot_name = 'Bot '..bot_name

    InputGroupHeader(dict._bot_name)
    local use_optimized_for_interval = Input('Optimized for Interval', false)

    dict._use_optimized_for_interval = use_optimized_for_interval
    dict._bot = function(self)
        local inputs        = self.inputs(bot_name)
        local indicators    = self.indicators(inputs)
        if self.safety(indicators, inputs)  then
            self.strategy(SignalExitPosition, indicators, inputs)
        else
            local signal        = Load('signal', SignalNone)
            signal              = self.signal(signal, indicators, inputs)
            if signal == SignalNone then Delete('signal') else Save('signal', signal) end
            self.strategy(signal, indicators, inputs)
        end
        self.plot(indicators, inputs)
    end

    dict.run = function(self)
        if self._use_optimized_for_interval  
            then    OptimizedForInterval(CurrentInterval(), function() self:_bot() end)
            else    self:_bot() end
    end

    dict.inputs     = function(bot_name) return {} end
    dict.indicators = function(inputs) Log("Didn't implement indicators function"); return {} end
    dict.safety      = function(indicators, inputs) return false end 
    dict.signal     = function(signal, indicators, inputs) Log("Didn't implemnt signal"); return SignalNone end
    dict.strategy   = function(signal, indicators, inputs)
        if      signal == SignalBuy  then DoBuy()
        elseif  signal == SignalSell or
                signal == SignalExitPosition then DoSell() end
    end
    dict.plot = function(indicators, inputs) Log ("Didn't implement plot function") end

    return dict
end

DefineOutput(ListDynamicType, Bot(bot_name), 'Bot Template')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!