Reputation: 23
I am trying to translate the following code from ThinkScript (TD Ameritrade code) to Python, and am having trouble getting it to output the right shape.
ThinkScript:
This code in TDA outputs a line bound by "lengtha
", -10, 10. Using "fold
", it iterates between the previous 10 open prices and compares them to the current close price. Depending on the iterative output the loop adds or subtracts 1 from "s
", where "s
" is zero initially by default.
For example if ca > GetValue(oa, i)
is true
for the last 10 oa
values, the output is 10.
I would like to translate this into python, where oa
and ca
are numpy arrays. And, where the output is another numpy array of equivalent detention as oa
and ca
input lengtha = 10;
def oa = open();
def ca = close();
def data = fold i = 0 to lengtha
with s
do s + (if ca > GetValue(oa, i)
then 1
else if ca < GetValue(oa, i)
then - 1
else 0);
What I have tried:
This is one of many version of this I have tried. This particular one does appear to go through the math in the way that I expect (possibly). However, it outputs an array of arrays, where I would like to simply return 1 value for each value in ca
.
oa = np.array(open)
ca = np.array(close)
window_size = 10
running_count = 0
result = []
for i in range(window_size):
oa_window = oa[i:i+window_size]
ca_window = ca
window_result = np.where(ca_window > oa_window, running_count+1, running_count-1)
running_count += np.sum(np.where(ca_window > oa_window, 1, -1))
running_count = min(max(running_count, -10), 10)
result.append(window_result)
result = np.array(result)
print(result)
Output:
X
(1D) array, rather than this 10 by X
by 10.
(where X
is the size of ca
and oa
)[[[ -1 1 -1 ... 1 1 -1]
[ 1 1 -1 ... 1 1 1]
[ -1 1 -1 ... 1 1 1]
...
[ -1 -1 -1 ... -1 -1 -1]
[ -1 -1 -1 ... -1 -1 -1]
[ -1 -1 -1 ... -1 -1 -1]]
[[ -9 -11 -11 ... -9 -11 -11]
[ -9 -11 -9 ... -9 -9 -9]
[ -9 -11 -9 ... -9 -9 -11]
...
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]]
[[-11 -11 -9 ... -11 -11 -9]
[-11 -9 -9 ... -9 -9 -9]
[-11 -9 -9 ... -9 -11 -9]
...
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]]
...
[[ -9 -9 -11 ... -9 -9 -9]
[ -9 -9 -9 ... -9 -9 -9]
[ -9 -9 -9 ... -9 -9 -9]
...
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]]
[[ -9 -11 -11 ... -9 -9 -9]
[ -9 -9 -9 ... -9 -9 -9]
[ -9 -9 -11 ... -9 -9 -9]
...
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]]
[[-11 -11 -9 ... -9 -9 -9]
[ -9 -9 -9 ... -9 -9 -9]
[ -9 -11 -9 ... -9 -9 -9]
...
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]
[-11 -11 -11 ... -11 -11 -11]]]
Desired Output: (random examples)
[ 1
-8
3
9
10
0
. . .
-10
-9 ]
Possible Solution
Spent some more time on this and this formula looks like it may be doing what I want:
This doesn't quite get to the output I'm looking for though. Seems like it gets stuck at -10 for a long time.
for j in range(i-10, i):
sum += np.where(ca > oa[j], 1, np.where(ca < oa[j], -1, 0))
Upvotes: 1
Views: 176
Reputation: 23
Figured it out I think. the addition of np.roll()
brought it home.
for j in range(0, 10):
sum += np.where(ca > np.roll(oa,j), 1, np.where(ca < np.roll(oa,j), -1, 0))
Upvotes: 0