Different condition buy and sell signal
up vote
1
down vote
favorite
I'm looking to implement a trading strategy working with two moving averages.
A slow (60 day SMA) and a fast (10 day SMA). In general, whenever the price is above the slow MA I want to be long, with the exception that if the price is more than 5% above the slow MA, and moves below the fast MA I will sell, and buy again when/if the price crosses above the fast ma.
The position and signals for the slow MA is trivial:
df["slowma"] = df["AdjClose"].rolling(60).mean()
df['positions'] = 0.0
df['positions'] = np.where(df["AdjClose"] > df['slowma'], 1.0, 0.0)
df['signal'] = df['positions'].diff()
The fast MA crossing, conditional of the price being more than 5% above, is a bit trickier, since I'm only interested in generating a buy signal if I previously had a sell signal.
df["fastma"] = df["AdjClose"].rolling(10).mean()
df['positions_fast'] = 0.0
df['positions_fast'] = np.where(df["AdjClose"] > df['fastma'], 1.0, 0.0)
Adding a "logical and", and checking whether the price is above the 5% more than the slow MA doesn't help me, since this check should only be done when the fast MA signal happens. Any help that could point me in the right direction would be greatly appreciated.
Edit:
Here is some testdata and an image that hopefully somewhat describes what I'm trying to achieve. Please do not pay attention to the fact that the slow and fast MA are not averages at all. I typed it in manually.

The idea is that we only get a sell signal the second time the price crosses under the fast ma.
For this data I changed the 5% threshold to 50%.

Here is data in python format:
close = [1,5,8,12,19,24,26,25,20,25,30,35,40,35,30,25,40,45,50,55,50,40,30,20]
slow_ma = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
fast_ma = [5, 6, 8, 9, 12, 15, 19, 24, 22, 24, 27, 31, 35, 39, 34, 30, 33, 37, 41, 46, 50, 50, 46, 40]
pandas quantitative-finance financial
add a comment |
up vote
1
down vote
favorite
I'm looking to implement a trading strategy working with two moving averages.
A slow (60 day SMA) and a fast (10 day SMA). In general, whenever the price is above the slow MA I want to be long, with the exception that if the price is more than 5% above the slow MA, and moves below the fast MA I will sell, and buy again when/if the price crosses above the fast ma.
The position and signals for the slow MA is trivial:
df["slowma"] = df["AdjClose"].rolling(60).mean()
df['positions'] = 0.0
df['positions'] = np.where(df["AdjClose"] > df['slowma'], 1.0, 0.0)
df['signal'] = df['positions'].diff()
The fast MA crossing, conditional of the price being more than 5% above, is a bit trickier, since I'm only interested in generating a buy signal if I previously had a sell signal.
df["fastma"] = df["AdjClose"].rolling(10).mean()
df['positions_fast'] = 0.0
df['positions_fast'] = np.where(df["AdjClose"] > df['fastma'], 1.0, 0.0)
Adding a "logical and", and checking whether the price is above the 5% more than the slow MA doesn't help me, since this check should only be done when the fast MA signal happens. Any help that could point me in the right direction would be greatly appreciated.
Edit:
Here is some testdata and an image that hopefully somewhat describes what I'm trying to achieve. Please do not pay attention to the fact that the slow and fast MA are not averages at all. I typed it in manually.

The idea is that we only get a sell signal the second time the price crosses under the fast ma.
For this data I changed the 5% threshold to 50%.

Here is data in python format:
close = [1,5,8,12,19,24,26,25,20,25,30,35,40,35,30,25,40,45,50,55,50,40,30,20]
slow_ma = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
fast_ma = [5, 6, 8, 9, 12, 15, 19, 24, 22, 24, 27, 31, 35, 39, 34, 30, 33, 37, 41, 46, 50, 50, 46, 40]
pandas quantitative-finance financial
4
Can you add some sample data and expected outputs to this question?
– Scott Boston
Nov 20 at 13:48
Hi @ScottBoston. Sure made up some data - and a chart.
– darkdark
Nov 20 at 18:57
Your example is not reproducible. Have a look at mcve. Then even the explanation is not that clear: at the beginning you only talk about slowMA and then you want to use fast MA. Could you please review your question? Then you can post a pic adding markers when you want to sell/buy.
– user32185
Nov 21 at 11:41
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm looking to implement a trading strategy working with two moving averages.
A slow (60 day SMA) and a fast (10 day SMA). In general, whenever the price is above the slow MA I want to be long, with the exception that if the price is more than 5% above the slow MA, and moves below the fast MA I will sell, and buy again when/if the price crosses above the fast ma.
The position and signals for the slow MA is trivial:
df["slowma"] = df["AdjClose"].rolling(60).mean()
df['positions'] = 0.0
df['positions'] = np.where(df["AdjClose"] > df['slowma'], 1.0, 0.0)
df['signal'] = df['positions'].diff()
The fast MA crossing, conditional of the price being more than 5% above, is a bit trickier, since I'm only interested in generating a buy signal if I previously had a sell signal.
df["fastma"] = df["AdjClose"].rolling(10).mean()
df['positions_fast'] = 0.0
df['positions_fast'] = np.where(df["AdjClose"] > df['fastma'], 1.0, 0.0)
Adding a "logical and", and checking whether the price is above the 5% more than the slow MA doesn't help me, since this check should only be done when the fast MA signal happens. Any help that could point me in the right direction would be greatly appreciated.
Edit:
Here is some testdata and an image that hopefully somewhat describes what I'm trying to achieve. Please do not pay attention to the fact that the slow and fast MA are not averages at all. I typed it in manually.

The idea is that we only get a sell signal the second time the price crosses under the fast ma.
For this data I changed the 5% threshold to 50%.

Here is data in python format:
close = [1,5,8,12,19,24,26,25,20,25,30,35,40,35,30,25,40,45,50,55,50,40,30,20]
slow_ma = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
fast_ma = [5, 6, 8, 9, 12, 15, 19, 24, 22, 24, 27, 31, 35, 39, 34, 30, 33, 37, 41, 46, 50, 50, 46, 40]
pandas quantitative-finance financial
I'm looking to implement a trading strategy working with two moving averages.
A slow (60 day SMA) and a fast (10 day SMA). In general, whenever the price is above the slow MA I want to be long, with the exception that if the price is more than 5% above the slow MA, and moves below the fast MA I will sell, and buy again when/if the price crosses above the fast ma.
The position and signals for the slow MA is trivial:
df["slowma"] = df["AdjClose"].rolling(60).mean()
df['positions'] = 0.0
df['positions'] = np.where(df["AdjClose"] > df['slowma'], 1.0, 0.0)
df['signal'] = df['positions'].diff()
The fast MA crossing, conditional of the price being more than 5% above, is a bit trickier, since I'm only interested in generating a buy signal if I previously had a sell signal.
df["fastma"] = df["AdjClose"].rolling(10).mean()
df['positions_fast'] = 0.0
df['positions_fast'] = np.where(df["AdjClose"] > df['fastma'], 1.0, 0.0)
Adding a "logical and", and checking whether the price is above the 5% more than the slow MA doesn't help me, since this check should only be done when the fast MA signal happens. Any help that could point me in the right direction would be greatly appreciated.
Edit:
Here is some testdata and an image that hopefully somewhat describes what I'm trying to achieve. Please do not pay attention to the fact that the slow and fast MA are not averages at all. I typed it in manually.

The idea is that we only get a sell signal the second time the price crosses under the fast ma.
For this data I changed the 5% threshold to 50%.

Here is data in python format:
close = [1,5,8,12,19,24,26,25,20,25,30,35,40,35,30,25,40,45,50,55,50,40,30,20]
slow_ma = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
fast_ma = [5, 6, 8, 9, 12, 15, 19, 24, 22, 24, 27, 31, 35, 39, 34, 30, 33, 37, 41, 46, 50, 50, 46, 40]
pandas quantitative-finance financial
pandas quantitative-finance financial
edited Nov 21 at 18:54
user32185
1,1911823
1,1911823
asked Nov 20 at 13:42
darkdark
53210
53210
4
Can you add some sample data and expected outputs to this question?
– Scott Boston
Nov 20 at 13:48
Hi @ScottBoston. Sure made up some data - and a chart.
– darkdark
Nov 20 at 18:57
Your example is not reproducible. Have a look at mcve. Then even the explanation is not that clear: at the beginning you only talk about slowMA and then you want to use fast MA. Could you please review your question? Then you can post a pic adding markers when you want to sell/buy.
– user32185
Nov 21 at 11:41
add a comment |
4
Can you add some sample data and expected outputs to this question?
– Scott Boston
Nov 20 at 13:48
Hi @ScottBoston. Sure made up some data - and a chart.
– darkdark
Nov 20 at 18:57
Your example is not reproducible. Have a look at mcve. Then even the explanation is not that clear: at the beginning you only talk about slowMA and then you want to use fast MA. Could you please review your question? Then you can post a pic adding markers when you want to sell/buy.
– user32185
Nov 21 at 11:41
4
4
Can you add some sample data and expected outputs to this question?
– Scott Boston
Nov 20 at 13:48
Can you add some sample data and expected outputs to this question?
– Scott Boston
Nov 20 at 13:48
Hi @ScottBoston. Sure made up some data - and a chart.
– darkdark
Nov 20 at 18:57
Hi @ScottBoston. Sure made up some data - and a chart.
– darkdark
Nov 20 at 18:57
Your example is not reproducible. Have a look at mcve. Then even the explanation is not that clear: at the beginning you only talk about slowMA and then you want to use fast MA. Could you please review your question? Then you can post a pic adding markers when you want to sell/buy.
– user32185
Nov 21 at 11:41
Your example is not reproducible. Have a look at mcve. Then even the explanation is not that clear: at the beginning you only talk about slowMA and then you want to use fast MA. Could you please review your question? Then you can post a pic adding markers when you want to sell/buy.
– user32185
Nov 21 at 11:41
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394364%2fdifferent-condition-buy-and-sell-signal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394364%2fdifferent-condition-buy-and-sell-signal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Can you add some sample data and expected outputs to this question?
– Scott Boston
Nov 20 at 13:48
Hi @ScottBoston. Sure made up some data - and a chart.
– darkdark
Nov 20 at 18:57
Your example is not reproducible. Have a look at mcve. Then even the explanation is not that clear: at the beginning you only talk about slowMA and then you want to use fast MA. Could you please review your question? Then you can post a pic adding markers when you want to sell/buy.
– user32185
Nov 21 at 11:41