Related
I have an indicator working the way I want, but I'm trying to convert it to a strategy for backtesting.
I have bollinger bands plotted on the 15 minute timeframe, and on the one hour. This works in the indicator, but it does not work properly in the strategy.
The error I get is:
The strategy function does not have an argument with the name timeframe or timeframe_gaps
I tried removing that in the strategy, but now the 1 hr bollinger bands are incorrect:
strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)
Here is the full code of the strategy:
//#version=5
strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)
entryPrice = close
// Set input parameters
length = input.int(20, minval=1)
mult = input.float(2.5, minval=0.001, maxval=50)
offset = input.int(0, "Offset", minval = -500, maxval = 500)
// Calculate Bollinger Bands using 15 minute data
src = close
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev
// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h
// Enter long position when 15 minute chart Bollinger Band is touched and 1hr band is touched (or previous 1 hr candle)
longCondition = ta.crossover(close, lower) and (ta.crossover(low, lower1h) or ta.crossover(low[1], lower1h))
if longCondition
strategy.entry("BB Long", strategy.long)
entryPrice = close
// Enter short position when 15 minute chart Bollinger Band is touched and 1hr band is touched (or previous 1 hr candle)
shortCondition = ta.crossunder(close, upper) and (ta.crossover(high, upper1h) or ta.crossover(high[1], upper1h))
if shortCondition
strategy.entry("BB Short", strategy.short)
entryPrice = close
// Calculate profit target
profitTarget = 1 + 0.1 // 10% profit target
// Exit long position when profit target is reached
exitLongCondition = close > entryPrice * profitTarget
if exitLongCondition
strategy.close("BB Long")
// Exit short position when profit target is reached
exitShortCondition = close < entryPrice / profitTarget
if exitShortCondition
strategy.close("BB Short")
// Plot Bollinger Bands
plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)
plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)
Here is the full code of the indicator (Which plots the bands correctly)
// Function to plot Bollinger Bands on a 1 hour timeframe
// Define function and set input parameters
//#version=5
indicator(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true, timeframe="", timeframe_gaps=true)
//study("Bollinger Bands - 1 Hour", overlay=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.5, minval=0.001, maxval=50, title="StdDev")
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h
// Plot Bollinger Bands
plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)
plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)
How can I use multi timeframe correctly in a strategy? The tutorials I found all use the indicator function, but I really need to backtest this!
Thanks!
I tried removing timeframe and timeframe_gaps from the strategy function, but the result is that the 1 hour Bollinger bands are incorrect.
Try this:
Change
src1h = request.security(syminfo.tickerid, '60', close)
To
src1h = request.security(syminfo.tickerid, '60', close, lookahead=barmerge.lookahead_on, gaps=barmerge.gaps_on)
I haven't tested this but have experienced something vaguely similar before, let us know how it goes.
I have this pine script I'm using on tradingview to plot a line that is the average of the highs and lows of a given time period.
I'd like to plot the standard deviation on the chart of this value:
highLowAvg =(sum(maxValue,length_avg) + sum(minValue,length_avg)) / (length_avg*2)
Is there an easy way to do this? Below is the whole script.
//Fill Arrays with latest LOWS/HIGHS
//Get highest HIGH and lowest LOW values from the arrays.
//Do Average between X highest highs and X lowest lows and plot line (Length can be modified)
//If price LOW is above line, Up Trending (Green) (Source can be modified)
//If price HIGH is below line, Down Trending (Red) (Source can be modified)
//If neither above, slow down in trend / reversal might occur (White)
study("Low - High Simple Tracker",overlay=true)
////==================Inputs
length = input(8)
length_avg = input(8)
up_Trend_Condition = input(high)
down_Trend_Condition = input(low)
stdev = stdev(highLowAvg, length)
////==================Array setup and calculations
lowArray = array.new_float(0)
highArray = array.new_float(0)
//Fill Lows to an array
for i = 0 to length-1
array.push(highArray,high[i])
//Fill Highs to an array
for i = 0 to length-1
array.push(lowArray,low[i])
//Get the highest value from the high array
maxValue= array.max(highArray)
//Get the lowest value from the low array
minValue= array.min(lowArray)
//Average between highest and lowest (length can be modifed)
highLowAvg =(sum(maxValue,length_avg) + sum(minValue,length_avg)) / (length_avg*2)
////////==================Plotting
colorHL = down_Trend_Condition > highLowAvg ? color.green : up_Trend_Condition < highLowAvg ?
color.red : color.white
plot(highLowAvg, color =colorHL, style = plot.style_line, linewidth = 2)
why if i use it code - rotation is working
p_partnew.Position = Vector3.new (i,p_coord_y, p_coord_z)
p_partnew.CFrame = p_partnew.CFrame*CFrame.Angles(p_angles_x,p_angles_y, p_angles_z)
if i use it code - rotation is NOT working
p_partnew.CFrame = CFrame.new (i,p_coord_y, p_coord_z)
p_partnew.CFrame = p_partnew.CFrame*CFrame.Angles(p_angles_x,p_angles_y, p_angles_z)
In the first example, only the position of the part is being modified and then the rotation is applied. The second example sets the whole CFrame to the position which will override the original rotation of the object, and then applies the rotation.
Simply put, #1 adds p_angles to the rotation, while #2 sets the rotation to p_angles.
To understand what's going on, take a look at Understanding CFrames.
A CFrame is a 4x3 matrix with components corresponding to the Part's Position and Orientation. When you get or set a Part's Position property, it is just reading and writing to that specific section of the CFrame's values.
Let's look at some example CFrames :
Example
CFrame Components
A Part located at (0, 0, 0) with no rotationPart.CFrame = CFrame.new(0,0,0)
0 0 0 10 0 0 1 0 0 0 1
A Part located at (1, 2, 3) with no rotationPart.CFrame = CFrame.new(1,2,3)
1 2 3 10 0 0 1 0 0 0 1
A Part located at (0, 0, 0) with (90, 0, 0) rotationPart.CFrame = CFrame.new(0,0,0) * CFrame.Angles(math.rad(90), 0, 0)
0 0 0 10 0 0 A-1 0 1 A
A Part located at (0, 0, 0) with (0, 90, 0) rotationPart.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0, math.rad(90), 0)
0 0 0 A0 1 0 10 -1 0 A
A Part located at (0, 0, 0) with (0, 0, 90) rotationPart.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0, 0, math.rad(90))
0 0 0 A-1 0 1 A0 0 0 1
A Part located at (1, 2, 3) with (90, 90, 90) rotationPart.CFrame = CFrame.new(1,2,3) * CFrame.Angles(math.rad(90), math.rad(90), math.rad(90))
1 2 3 10 A A B-1 0 1 B
Terms
Values
A
-4.3711388286738e-08
B
1.9106854651647e-15
In your first code sample, you are setting the Position first. This preserves the original CFrame, and updates just the values for Position.
-- imagine that p_partnew.CFrame looks like this :
-- ? ? ? ?
-- ? ? ? ?
-- ? ? ? ?
-- set just the position values in the CFrame, keep everything else
p_partnew.Position = Vector3.new(i, p_coord_y, p_coord_z)
-- p_partnew.CFrame now looks like this :
-- i p_coord_y p_coord_z ?
-- ? ? ? ?
-- ? ? ? ?
-- apply a transformation of angles
p_partnew.CFrame = p_partnew.CFrame * CFrame.Angles(p_angles_x, p_angles_y, p_angles_z)
In the second code sample, you are setting the entire CFrame first with just the position values. This wipes out all the other data that existed in that CFrame before.
-- set the entire CFrame
p_partnew.CFrame = CFrame.new(i, p_coord_y, p_coord_z)
-- p_partnew.CFrame now looks like this :
-- i p_coord_y p_coord_z 1
-- 0 0 0 1
-- 0 0 0 1
-- apply a transformation of angles
p_partnew.CFrame = p_partnew.CFrame * CFrame.Angles(p_angles_x, p_angles_y, p_angles_z)
So if the first example works with rotation, but the second doesn't, then the answer is that the original rotation information is getting lost when you set the CFrame. You could try saving that information first, then applying it to the new position, and then applying your changes (assuming that your changes are small increments). That would look something like this :
-- store the previous orientation
local o = p_partnew.Orientation
-- create a set of changes based on new angles
local angles = CFrame.Angles(math.rad(o.X) + p_angles_x, math.rad(o.Y) + p_angles_y, math.rad(o.Z) + p_angles_z)
-- set the new CFrame
p_partnew.CFrame = CFrame.new(i, p_coord_y, p_coord_z):ToWorldSpace(angles)
# Kepler's Laws.py
# plots the orbit of a planet in an eccentric orbit to illustrate
# the sweeping out of equal areas in equal times, with sun at focus
# The eccentricity of the orbit is random and determined by the initial velocity
# program uses normalised units (G =1)
# program by Peter Borcherds, University of Birmingham, England
from vpython import *
from random import random
from IPython import display
import pandas as pd
def MonthStep(time, offset=20, whole=1): # mark the end of each "month"
global ccolor # have to make it global, since label uses it before it is updated
if whole:
Ltext = str(int(time * 2 + dt)) # end of 'month', printing twice time gives about 12 'months' in 'year'
else:
Ltext = duration + str(time * 2) + ' "months"\n Initial speed: ' + str(round(speed, 3))
ccolor = color.white
label(pos=planet.pos, text=Ltext, color=ccolor,
xoffset=offset * planet.pos.x, yoffset=offset * planet.pos.y)
ccolor = (0.5 * (1 + random()), random(), random()) # randomise colour of radial vector
return ccolor
scene = display(title="Kepler's law of equal areas", width=1000, height=1000, range=3.2)
duration = 'Period: '
sun = sphere(color=color.yellow, radius=0.1) # motion of sun is ignored (or centre of mass coordinates)
scale = 1.0
poss = vector(0, scale, 0)
planet = sphere(pos=poss, color=color.cyan, radius=0.02)
while 1:
velocity = -vector(0.7 + 0.5 * random(), 0, 0) # gives a satisfactory range of eccentricities
##velocity = -vector(0.984,0,0) # gives period of 12.0 "months"
speed = mag(velocity)
steps = 20
dt = 0.5 / float(steps)
step = 0
time = 0
ccolor = color.white
oldpos = vector(planet.pos)
ccolor = MonthStep(time)
curve(pos=[sun.pos, planet.pos], color=ccolor)
while not (oldpos.x > 0 and planet.pos.x < 0):
rate(steps * 2) # keep rate down so that development of orbit can be followed
time += dt
oldpos = vector(planet.pos) # construction vector(planet.pos) makes oldpos a varible in its own right
# oldpos = planet.pos makes "oldposs" point to "planet.pos"
# oldposs = planet.pos[:] does not work, because vector does not permit slicing
denom = mag(planet.pos) ** 3
velocity -= planet.pos * dt / denom # inverse square law; force points toward sun
planet.pos += velocity * dt
# plot orbit
curve(pos=[oldpos, planet.pos], color=color.red)
step += 1
if step == steps:
step = 0
ccolor = MonthStep(time)
curve(pos=[sun.pos, planet.pos], color=color.white)
else:
# plot radius vector
curve(pos=[sun.pos, planet.pos], color=ccolor)
if scene.kb.keys:
print
"key pressed"
duration = 'Duration: '
break
MonthStep(time, 50, 0)
label(pos=(2.5, -2.5, 0), text='Click for another orbit')
scene.mouse.getclick()
for obj in scene.objects:
if obj is sun or obj is planet: continue
obj.visible = 0 # clear the screen to do it again
I copied Kepler's Laws code in google and compiled it on pycharm.
But there is an error that
scene = display(title="Kepler's law of equal areas", width=1000, height=1000, range=3.2)
TypeError: 'module' object is not callable
I found some information on google that "pandas" library can improve this error so I tried it but I can't improve this error.
What should I do?
Replace "display" with "canvas", which is the correct name of this entity.
I have a circle of radius 10 m. I want to count the number of vehicles entering the circle it (the distance from the center car <= 10m)
I'm right . I can use the toolbar "Minitor" to count the number of vehicles currently in liquidation xe.nhung "minitor" much larger than the actual number of vehicles that pass through the circle. I attached the "minitor" by "total-cars".
how to properly count the number of vehicles?
ask cars
[
if distancexy 0 0 < 10
[
set total-cars (total-cars + 1)
]
]
I am not very sure about your question, but maybe this code could help you:
set total-cars count cars with [distancexy 0 0 <= 10]
You can use the following code in the monitor control directly:
count cars with [distancexy 0 0 <= 10]
import cv2
import time
bgsMOG = cv2.createBackgroundSubtractorMOG2(detectShadows=False)
kernal=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
cap = cv2.VideoCapture(0)
counter =0
time.sleep(2)
if cap:
while True:
ret, frame = cap.read()
if ret:
#fgmask = bgsMOG.apply(frame, None, 0.01)
blur = cv2.GaussianBlur(frame, (5, 5), 0)
fgmask = bgsMOG.apply(blur)
morhpho = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernal)
#line for detection
cv2.line(frame,(20,270),(320,270),(175,175,0),5)
_,contours, hierarchy = cv2.findContours(morhpho,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ax1=20 #coordinate of line where vehicle will be count if intersect
ay1=270
ax2=320
ay2=270
try: hierarchy = hierarchy[0]
except: hierarchy = []
#for contour, hier in zip(contours, hierarchy):
for (i, contour) in enumerate(contours):
(x,y,w,h) = cv2.boundingRect(contour)
if w > 20 and h > 25:
rec=cv2.rectangle(frame, (x,y), (x+w,y+h), (180, 0, 0), 1)
x1=w/2 #to find centroid
y1=h/2
cx=x+x1
cy=y+y1
centroid=(cx,cy)
M = cv2.moments(contour)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the contour and center of the shape on the image
cv2.circle(frame, (cX, cY), 2, (255, 255, 255), -1)
cv2.circle(frame,(int(cx),int(cy)),1,(0,255,0),-1)
dy=cY-270 #my first code to increase counter
print("centroid",cX,":",cY)
if dy==0:
if (cX<=320)and(cX>=20):
counter=counter+1
print("1st ct",counter)
print len(contour)
#FileName = "D:/data/" + str(y) + ".jpg"
#cv2.imshow("cropped",rec)
#cv2.imwrite(FileName,rec)
if cy==270:
if centroid > (27, 268) and centroid < (325, 285):
if (cX <= 320) and (cX >= 20):
counter =counter+1
print "counter=", counter
if cY > 10 and cY < 250:
cv2.putText(frame, str(counter),(10,150),cv2.FONT_HERSHEY_SIMPLEX,2, (255, 0, 0), 1, True)
#cv2.resizeWindow('Output',320,180)
cv2.imshow('Output', frame)
cv2.imshow('mor', morhpho)
cv2.imshow('blur', blur)
#cv2.imshow('FGMASK', morhpho)
key = cv2.waitKey(1)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()