user13611579
user13611579

Reputation: 21

PineScript: Close a daytrade position at specific time of day

Using TradingView's Pinescript, how can I program a backtest strategy to (on 1 Day Bar Resolution):

1) Enter a long position at market open using a market order, if the previous day's close marked a buy condition. (the buy condition is, RSI (7) is above 50, MACD line is above signal line)

2) Place a stoploss at 1X the Average Day Range (14)

3) Place a sell order at 15:55:00 Eastern Time to close the whole position before market close.

4) The stoploss trigger cancels the other sell order, and vice versa (e.g. it's an OCO order)

5) Repeat every time buy condition is present after close.

In other words, with this strategy, I hope to enter a day trade at market open with a 1X ADR stoploss, and close the trade right before close. This will avoid the after hours gaps.

Thanks for being generous with your time!

Edit:
here's what I have so far (as requested)
    //@version=4
    strategy("Trendability Strategy", overlay=true)
    [macdLine, signalLine, histLine] = macd(close, 12, 26, 9)
    rsiLine = rsi(close, 7)
    stochLine = sma(sma(stoch(close, high, low, 14),3),3)
    signal = histLine > -0.05 and rsiLine > 40 and stochLine > 40 ? "buy" : 
    histLine <= -0.05 and rsiLine <= 40 and stochLine <= 40 ? "sell" : "none"
    palette = signal == "buy" ? color.lime : signal == "sell" ? color.red : 
    color.black
    plotbar(open, high, low, close, color=palette)

    strategy.entry("Long", strategy.long, when = signal == "buy") 
    strategy.close("Long", when = signal == "none") 

Upvotes: 2

Views: 10115

Answers (3)

Viraj Parab
Viraj Parab

Reputation: 91

Probably this will help

// close all entries after timeframe
intraday_sess = "0920-1500"
t = time(timeframe.period, intraday_sess)
sess_over = not na(t[1]) and na(t)
strategy.close_all(when = sess_over)

Upvotes: 2

Fill.i.Pillow
Fill.i.Pillow

Reputation: 46

//@version=5 
var close_hour = 15
var close_minutes = 56   
var new_day = dayofmonth
var new_month = month
var new_year = year
var close_trades_at_time_of_day = na

if dayofmonth != dayofmonth[1]
    new_day := dayofmonth
if month != month[1]
    new_month := month
if year != year[1]
    new_year := year
close_trades_at_time_of_day := timestamp("GMT-4",new_year,new_month,new_day,close_hour,close_minutes)

strategy.close_all(time == close_trades_at_time_of_day) 

This kind of works, you would need to adjust close_hours and close_minutes depending on your chart's timeframe and your needs.

Upvotes: 1

KSK2202
KSK2202

Reputation: 21

I’m not sure if PineScript has this capability. I do have the python code from Quantopian. I’m not sure if it would be of any help. Most brokers have an option to close all positions (or some)for day traders, so I would think this would be pretty standard function.

def initialize(context):
    # Algorithm will call myfunc every day 15 minutes before the market closes
    schedule_function(
        myfunc,
        date_rules.every_day(),
        time_rules.market_close(minutes=15)
    )

def myfunc(context,data):
    pass

Here is a sample from the user manual if you just want to avoid after-hours trading

strategy("closeEntry Demo", overlay=false)
strategy.entry("buy", true, when = open > close)
strategy.close("buy", when = open < close, qty_percent = 50, comment = "close buy entry for 50%")
plot(strategy.position_size)

Upvotes: 2

Related Questions