Reputation: 1
I have a simple strategy of buying a security above a WMA and then selling when it closes .98*WMA. I run an if/else statement to decide if I will buy at the 50D or 200D. This works as expected.
My problem is - I cannot get the sells to execute correctly when I am buying at the 50D. The script works well with the 200D. When I buy as the security crosses the 50D, the sell executes at the next days close every time, even when price is above the 50D buffer.
I would expect the buy to be above the 50D WMA. The sell should be when the security crosses below the 50D "buffer" as I call it. Which is .98*50DWMA. The bolded part is my if/else that sets movingAvg to either the 50D or 200D. It also sets the buffer to either the 50D buffer or the 200D buffer. The picture shows the sell the day after each 50DWMA buy (Green line) even tho price is above the 50D buffer. The buy signals appear to all be correct. Any ideas?
//@version=5 strategy("S4 QQQ 2.0", process_orders_on_close=true, overlay=true,commission_type=strategy.commission.cash_per_order, commission_value=0, slippage=0, initial_capital = 100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Script Date Range Inputs
StartTime = input.time(defval=timestamp('14 June 2010 05:00 +0000'), group="Script Date Range Settings", title='Start Time')
EndTime = input.time(defval=timestamp('20 Jan 2023 00:00 +0000'), group="Script Date Range Settings", title='End Time')
InDateRange = time>=StartTime and time<=EndTime
QQQ = request.security("QQQ","D",close)
QQQ50 = ta.wma(QQQ,50)
QQQ200 = ta.wma(QQQ,200)
QQQ50Buffer = QQQ50*.98 //50D Buffer
QQQ200Buffer = QQQ200*.98 //200D Buffer
plot(QQQ50, color = color.green)
plot(QQQ200, color = color.white)
plot(QQQ50Buffer, color = color.green, style = plot.style_cross)
plot(QQQ200Buffer, color = color.white, style = plot.style_cross)
moving50AvgFlag = false
movingAvg = QQQ
buffer = QQQ
if (QQQ[1] <= QQQ50[1]) and (QQQ50[1] < QQQ200[1]) //if QQQ is < 50 and the 50 is less than the 200
movingAvg := QQQ50
buffer := QQQ50Buffer
moving50AvgFlag := true
else if ((QQQ[1] <= QQQ200[1]) and moving50AvgFlag == true) //keep using the 50d as long as qqq < 200
movingAvg := QQQ50
buffer := QQQ50Buffer
else //start using the 200d
movingAvg := QQQ200
buffer := QQQ200Buffer
moving50AvgFlag := false**
if QQQ > movingAvg and InDateRange==true //Buying at the WMA
strategy.entry("Buy",strategy.long)
if QQQ < buffer and InDateRange==true //Selling at the buffer
strategy.close("Buy")
Upvotes: 0
Views: 45
Reputation: 2171
You're using the request.security
function wrong.
Let's say you're on a 1 min timeframe and requesting a daily timeframe close
using request.security("QQQ","D",close)
. In that case, you'll have about 390 bars with the same value, since there are about 390 of 1 minute bars in a day. If you're checking for ta.wma(QQQ,50)
, you'll get the wma of 50 bars with the same value, since it is checking the value of the daily bar, but on the 1 minute number of bars.
You can assign a function to the request.security
function directly to get the results you want:
wma50 = ta.wma(close,50)
wma200 = ta.wma(close,200)
wma50Buffer = wma50*.98 //50D Buffer
wma200Buffer = wma200*.98 //200D Buffer
[QQQ50, QQQ200, QQQ50Buffer, QQQ200Buffer] = request.security("QQQ","D",[wma50, wma200, wma50Buffer, wma200Buffer])
Upvotes: 0