[pshaiBot] Native Scalper

stable
By pshai in Trading Bots Published June 2020 👁 2,012 views 💬 0 comments

Description

This is an example bot using HaasScript's new native orders! Hope you will find it educational.
HaasScript
EnableHighSpeedUpdates(true)
HideOrderSettings()

local tp = Input('1. Take-Profit %', 0.2)
local sl = Input('2. Stop-Loss %', 0.1)
local rsi_len = Input('3. RSI Length', 9)
local rsi_os = Input('4. RSI Oversold', 30)
local rsi_ob = Input('4. RSI Overbought', 70)

local cp = CurrentPrice()
local c = ClosePrices()
local rsi = RSI(c, rsi_len)

local bot_pos = GetPositionDirection()
local tpid = Load('tpid', '')
local slid = Load('slid', '')
local target = Load('target', 0)
local stop = Load('stop', 0)


if tpid != '' and IsOrderFilled(tpid) then
    CancelOrder(slid)
    tpid = ''
    slid = ''
end
if slid != '' and IsOrderFilled(slid) then
    CancelOrder(tpid)
    tpid = ''
    slid = ''
end

if bot_pos == NoPosition and IsAnyOrderOpen() == false then
    if rsi < rsi_os then
        PlaceGoLongOrder(cp.bid, TradeAmount(), {note='LE', type=MarketOrderType})
        target = Round(AddPerc(cp.bid, tp), 0)
        stop = Round(SubPerc(cp.bid, sl), 0)

    elseif rsi > rsi_ob then
        PlaceGoShortOrder(cp.ask, TradeAmount(), {note='SE', type=MarketOrderType})
        target = Round(SubPerc(cp.ask, tp), 0)
        stop = Round(AddPerc(cp.ask, sl), 0)
    end
else
    if IsAnyOrderOpen() == false then
        if bot_pos == PositionLong then
            tpid = PlaceExitLongOrder(target, GetPositionAmount(), {note='L-TP', type=TakeProfitLimitOrderType, timeout=999999, price2=target})
            slid = PlaceExitLongOrder(stop, GetPositionAmount(), {note='L-SL', type=StopLimitOrderType, timeout=999999, price2=stop})
        else
            tpid = PlaceExitShortOrder(target, GetPositionAmount(), {note='S-TP', type=TakeProfitLimitOrderType, timeout=999999, price2=target})
            slid = PlaceExitShortOrder(stop, GetPositionAmount(), {note='S-SL', type=StopLimitOrderType, timeout=999999, price2=stop})
        end
    end
end 

if tpid != '' and IsOrderOpen(tpid) then
    Plot(0, 'tp', OrderContainer(tpid).price, {id=tpid, c=Green})
end

if slid != '' and IsOrderOpen(slid) then
    Plot(0, 'sl', OrderContainer(slid).price, {id=slid, c=Red})
end

Save('tpid', tpid)
Save('slid', slid)
Save('target', target)
Save('stop', stop)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!