Y0ung1
Y0ung1

Reputation: 1

Pine script simple code (strategy) - No Trades Placed

Hello guys i'm trying to learn pine script I dont know what is happening I just want to backtest this code but it doesnt work.

When the time is 09:00 until 11:00, it will buy and the stop loss will be 30 points and the take profit will be 60 points the exit will be either when it touches the stop loss or the profit.

//@version=5
strategy('test', overlay=true)

timeCondition = time('0900-1100', 'America/New_York:23456')


if timeCondition
    strategy.entry('Buy', strategy.long,stop=high - 30, limit=high + 60)


strategy.exit('Buy', stop=high - 30, limit=high + 60)

. . . . . . . . . .

Upvotes: 0

Views: 81

Answers (1)

AmphibianTrading
AmphibianTrading

Reputation: 1425

A couple things, you have your stop loss and take profits set to the same price as your entry limits.

Not sure what you want as far as buying between 9:00 and 11. How many buys, need specifics, but this code will buy the New York Open at 9:30 and then sell it at 11:00 or if the stop or target is hit before then. It should give you an idea and you can tweak it to your strategy.

//@version=5
strategy('test', overlay=true)

buyTime = hour(time, 'America/New_York') == 09 and minute(time, 'America/New_York') == 30
sellTime = (hour(time, 'America/New_York') == 11 and minute(time,'America/New_York') == 00)

var float stopLoss = 0.0
var float target = 0.0

if buyTime
    strategy.entry('Buy', strategy.long)
    stopLoss := high - 30
    target := high + 30


if sellTime or high >= target or low <= stopLoss
    strategy.exit('Buy', stop=stopLoss, limit=target)

Upvotes: 0

Related Questions