Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Number of trades might not be correct #1047

Answered by peacerider
huytradingz asked this question in Q&A
Discussion options

Hi everyone. I am coding an EMA Crossover Strategy with RSI trigger. The strategy create a buy signals when both EMA12 crossover EMA21 & RSI > 50 are true, and a sell signals vice versa. I am crosschecking the strategy performance to see if there is any difference when I add the RSI trigger comparing to when I am not (EMA12 vs EMA21 with RSI trigger & EMA12 vs EMA21 without RSI trigger).

But I noticed that the number of trades when I add RSI trigger is higher comparing to when I am not and this is not quite right. Let say the EMA12 cut EMA21 3 times and among those position the RSI is above 50 2 times. That means the strategy should buy two times, and this number of trades should not go above 3 which is again the number of times EMA12 crossover EMA21, but it resulted differently. I am not sure if this is a bug or not, and would like to ask if you guys could help me with this. Below is how I code the buy/sell trigger. Let me know if you guys would like to look at more stuff. Thank you!!

`
if (crossover(self.ema1, self.ema2)) and (self.RSI > 50):
self.buy(size = order_size)

elif(crossover(self.ema2, self.ema1)) and (self.RSI < 50):
self.sell(size = order_size_short)

`

You must be logged in to vote

Hi,

I checked by my simple code.
But, the trade number is decrease when I add the RSI trigger.
sorry. I don't know why your situation is occured.

Example

from backtesting import Backtest, Strategy
from backtesting.lib import crossover
import talib as ta
from backtesting.test import SMA, GOOG
def get_rsi(close, n3):
 return ta.RSI(close, timeperiod=n3)
class SmaCross(Strategy):
 n1 = 12
 n2 = 21
 n3 = 14
 def init(self):
 close = self.data.Close
 self.sma1 = self.I(SMA, close, self.n1)
 self.sma2 = self.I(SMA, close, self.n2)
 self.RSI = self.I(get_rsi, close, self.n3)
 def next(self):
 # if crossover(self.sma1, self.sma2) and sel...

Replies: 1 comment

Comment options

Hi,

I checked by my simple code.
But, the trade number is decrease when I add the RSI trigger.
sorry. I don't know why your situation is occured.

Example

from backtesting import Backtest, Strategy
from backtesting.lib import crossover
import talib as ta
from backtesting.test import SMA, GOOG
def get_rsi(close, n3):
 return ta.RSI(close, timeperiod=n3)
class SmaCross(Strategy):
 n1 = 12
 n2 = 21
 n3 = 14
 def init(self):
 close = self.data.Close
 self.sma1 = self.I(SMA, close, self.n1)
 self.sma2 = self.I(SMA, close, self.n2)
 self.RSI = self.I(get_rsi, close, self.n3)
 def next(self):
 # if crossover(self.sma1, self.sma2) and self.RSI < 50:
 if crossover(self.sma1, self.sma2):
 self.buy()
 # elif crossover(self.sma2, self.sma1) and self.RSI > 50:
 elif crossover(self.sma2, self.sma1):
 self.sell()
bt = Backtest(GOOG, SmaCross,
 cash=10000, commission=.002,
 exclusive_orders=True)
output = bt.run()
bt.plot()
print(output)
You must be logged in to vote
0 replies
Answer selected by huytradingz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /