0
$\begingroup$

I want to be able to plot Donchian-channels. It's probably easier to use numpy but, am not that fluent with numpy yet so i thought i could make a function that takes two arrays as input(Highs and lows), make a little size-limited-dynamical-array over x days and then within that array find out the max and min value. And at the end output two arrays to plot the data with.

The code that i came up with is


zl=[2,3,4,5,5,4,3,2,3,4,3,4,5,6,7,8,7,6,5,4,5,6]

zh=[3,4,5,6,6,5,4,3,4,5,4,5,6,7,8,9,8,7,6,5,6,7]

def donchianChannels(highValue, lowValue, window):

highOutput = []
lowOutput = []
lsth = [None]*window
lstl = [None]*window
for i in range(0, len(highValue)):
    maxValue=0
    lsth.append(highValue[i])
    lsth = lsth[-window:]

    for j in range(0, len(lsth)):
        if lsth[j] > maxValue:
            maxValue = lsth[j]

    highOutput.append(maxValue)

for i in range(0, len(lowValue)):
    minValue=100000
    lstl.append(lowValue[i])
    lstl = lstl[-window:]

    for j in range(0, len(lstl)):
        if lstl[j] < minValue:
            minValue = lstl[j]

    lowOutput.append(lowValue)

return highOutput, lowOutput

maxvalue, minvalue = donchianChannels(zh, zl, 3) print maxvalue, minvalue


and the output i get is


[3, 4, 5, 6, 6, 6, 6, 5, 4, 5, 5, 5, 6, 7, 8, 9, 9, 9, 8, 7, 6, 7] [[2, 3, 4, 5, 5, 4, 3, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6], [2, 3, 4, 5, 5, 4, 3, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6], [2, 3, 4, 5, 5, 4, 3, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6], [2, 3, 4, 5, 5, 4, 3, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6], [2, 3, 4, 5, 5, 4, 3, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6], [2, 3, 4, 5, 5, 4, 3, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 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, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6]]

the algorithm for the maximum array works fine, but the minimum one does not work at all. First of all it makes 22 arrays(the lenght of the input array) within one array. Second none of the minimum arrays plots the minimum values. They should be


[2,2,2,3,4,4,3,2,2,2,3,3,3,4,5,6,7,6,5,4,4,4,5]

Due to my calculations i can not see the problem it would be great if i could get some help please :)

$\endgroup$
  • $\begingroup$ In the loop for j in range(0, len(lstl)).... you are appending lowValue to lowOutput array. You need to change that to be minValue. $\endgroup$ – meh Aug 20 '15 at 14:15
  • $\begingroup$ Omg Thanks! i knew it was something simple like a variable name in the wrong place... lesson learned to not create more non lookalike variablenames :) Thanks again! $\endgroup$ – Delorian1986 Aug 20 '15 at 14:23
1
$\begingroup$
highOutput = []
lowOutput = []
lsth = [None]*window
lstl = [None]*window
for i in range(0, len(highValue)):
    maxValue=0
    lsth.append(highValue[i])
    lsth = lsth[-window:]

    for j in range(0, len(lsth)):
        if lsth[j] > maxValue:
            maxValue = lsth[j]

    highOutput.append(maxValue)

for i in range(0, len(lowValue)):
    minValue=100000
    lstl.append(lowValue[i])
    lstl = lstl[-window:]

    for j in range(0, len(lstl)):
        if lstl[j] < minValue:
            minValue = lstl[j]

    lowOutput.append(minValue)    #Change this

return highOutput, lowOutput
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.