admin管理员组文章数量:1245823
I have a fairly short pinescript strategy that just tests how often a day is likely to return positive. The strategy checks to see what day it currently is and whether the current time is within the tested time frame. Occasionally the script activates on the wrong day.
Code for the script:
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at .0/
// © Trading_BB
//@version=5
strategy("Daily Strategy", overlay=true)//, max_bars_back=5000)
enum weekDay
Monday = "Monday"
Tuesday = "Tuesday"
Wednesday = "Wednesday"
Thursday = "Thursday"
Friday = "Friday"
Saturday = "Saturday"
Sunday = "Sunday"
enum direction
Long = "Long"
Short = "Short"
GetTickValue() =>
syminfo.mintick * syminfo.pointvalue
dayOfWeek = input.enum(weekDay.Monday, "Day", group="Entry Conditions")
startDate = input.time(timestamp("Jan 01 2000"), "Start date", group="Entry Conditions")
limit = input.bool(false, "Use limit order", "Use a limit order to enter insted of buying at market.", group="Entry Conditions")
stop = input.float(0.0, "Stop Loss %", tooltip="Percentage of opening price to place stop loss at. E.g. 1% will set the stop loss at 99% of the opening value.", group="Position Management")
profit = input.float(0.0, "Take Profit %", tooltip="Percentage of opening price to take profit loss at. E.g. 1% will set the take profit at 101% of the opening value.", group="Position Management")
dir = input.enum(direction.Long, "Direction", group="Entry Conditions")
trailEntry = input.float(0.0, "Trailing Stop Entry", tooltip="Percentage profit needed before the trailing stop will activate.", group="Position Management")
trailOffset = input.float(0, "Trailing Stop Offset", tooltip="Number of ticks the trailing stop will sit behind price.", group="Position Management")
log.info("{0} Tick Value = {1}", syminfo.ticker, syminfo.currency + " " + str.tostring(GetTickValue()))
lookBack = ta.barssince(time == startDate)
if na(lookBack)
lookBack := bar_index
if bar_index > 4326
lookBack := 4326
if lookBack > 4326
lookBack := 4326
if lookBack >= bar_index and bar_index > 1
lookBack := bar_index - 1
stratDay = 0
if(syminfo.type == "crypto")
stratDay := switch dayOfWeek
weekDay.Monday => dayofweek.sunday
weekDay.Tuesday => dayofweek.monday
weekDay.Wednesday => dayofweek.tuesday
weekDay.Thursday => dayofweek.wednesday
weekDay.Friday => dayofweek.thursday
weekDay.Saturday => dayofweek.friday
weekDay.Sunday => dayofweek.saturday
else if(syminfo.type == "forex" or syminfo.type == "cfd" or syminfo.type == "index")
stratDay := switch dayOfWeek
weekDay.Monday => dayofweek.thursday
weekDay.Tuesday => dayofweek.sunday
weekDay.Wednesday => dayofweek.monday
weekDay.Thursday => dayofweek.tuesday
weekDay.Friday => dayofweek.wednesday
else if(syminfo.type == "fund" or syminfo.type == "stock")
stratDay := switch dayOfWeek
weekDay.Monday => dayofweek.friday
weekDay.Tuesday => dayofweek.monday
weekDay.Wednesday => dayofweek.tuesday
weekDay.Thursday => dayofweek.wednesday
weekDay.Friday => dayofweek.thursday
else if(syminfo.type == "futures")
stratDay := switch dayOfWeek
weekDay.Monday => dayofweek.thursday
weekDay.Tuesday => dayofweek.sunday
weekDay.Wednesday => dayofweek.monday
weekDay.Thursday => dayofweek.tuesday
weekDay.Friday => dayofweek.wednesday
else
stratDay := switch dayOfWeek
weekDay.Sunday => dayofweek.sunday
weekDay.Monday => dayofweek.monday
weekDay.Tuesday => dayofweek.tuesday
weekDay.Wednesday => dayofweek.wednesday
weekDay.Thursday => dayofweek.thursday
weekDay.Friday => dayofweek.friday
weekDay.Saturday => dayofweek.saturday
if ((dayofweek == stratDay) and time >= startDate and barstate.isnew)
if(dir == direction.Long)
todayLimit = switch stratDay
1 => (open / 100) * (100 + profit)
2 => (open / 100) * (100 + profit)
3 => (open / 100) * (100 + profit)
4 => (open / 100) * (100 + profit)
5 => (open / 100) * (100 + profit)
6 => (open / 100) * (100 + profit)
7 => (open / 100) * (100 + profit)
todayStop = switch stratDay
1 => (open / 100) * (100 - stop)
2 => (open / 100) * (100 - stop)
3 => (open / 100) * (100 - stop)
4 => (open / 100) * (100 - stop)
5 => (open / 100) * (100 - stop)
6 => (open / 100) * (100 - stop)
7 => (open / 100) * (100 - stop)
if (stop > 0 and limit)
strategy.entry("long", strategy.long, stop=todayStop, limit=open)
else if(stop > 0)
strategy.entry("long", strategy.long, stop=todayStop)
else if(limit)
strategy.entry("long", strategy.long, limit=open)
else
strategy.entry("long", strategy.long)
if(profit > 0 and trailEntry > 0 and trailOffset > 0)
trailEntry := (open / 100) * (100 + trailEntry)
strategy.exit("Exit long", from_entry="long", limit=todayLimit)
else if (profit > 0)
strategy.exit("Exit long", from_entry="long", limit=todayLimit, trail_price = trailEntry, trail_offset = trailOffset)
else if (trailEntry > 0 and trailOffset > 0)
strategy.exit("Exit long", from_entry="long", trail_price = trailEntry, trail_offset = trailOffset)
else if(dir == direction.Short)
todayLimit = switch stratDay
1 => (open / 100) * (100 - profit)
2 => (open / 100) * (100 - profit)
3 => (open / 100) * (100 - profit)
4 => (open / 100) * (100 - profit)
5 => (open / 100) * (100 - profit)
6 => (open / 100) * (100 - profit)
7 => (open / 100) * (100 - profit)
todayStop = switch stratDay
1 => (open / 100) * (100 + stop)
2 => (open / 100) * (100 + stop)
3 => (open / 100) * (100 + stop)
4 => (open / 100) * (100 + stop)
5 => (open / 100) * (100 + stop)
6 => (open / 100) * (100 + stop)
7 => (open / 100) * (100 + stop)
if(stop > 0)
strategy.entry("short", strategy.short, stop=todayStop)
else
strategy.entry("short", strategy.short)
if(profit > 0 and trailEntry > 0 and trailOffset > 0)
trailEntry := (open / 100) * (100 - trailEntry)
strategy.exit("Exit short", from_entry="short", limit=todayLimit)
else if (profit > 0)
strategy.exit("Exit short", from_entry="short", limit=todayLimit, trail_price = trailEntry, trail_offset = trailOffset)
else if (trailEntry > 0 and trailOffset > 0)
strategy.exit("Exit short", from_entry="short", trail_price = trailEntry, trail_offset = trailOffset)
else if (dayofweek != stratDay)
strategy.close("short")
strategy.close("long")
I have included an image of the script activating on a Thursday when it should only be activating on Mondays. To add some clarity to this I have a bunch of if statements at the start of the script that attempt to assign the correct day when a bar starts and then a later a check to see if a new bar is formed and if the current day is the correct day. This is probably where the problem is however I am not sure how to fix this as having dayofweek == stratDay
and stratDay
just being the current day has all of my activations 1-2 days behind where they should be.
本文标签: pine scriptPinescript strategy occasionally activating on the wrong dayStack Overflow
版权声明:本文标题:pine script - Pinescript strategy occasionally activating on the wrong day - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740217115a2243076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论