Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsEmaBullishCross Or IsEmaBearishCross Always false #53

Closed
RickPons opened this issue Jul 7, 2018 · 16 comments
Closed

IsEmaBullishCross Or IsEmaBearishCross Always false #53

RickPons opened this issue Jul 7, 2018 · 16 comments

Comments

@RickPons
Copy link

RickPons commented Jul 7, 2018

Hello I am trying to figure out how to know if the Ema is Bullish or Bearish.

I did a backtesting using IsEmaBullishCross and IsEmaBearishCross and they are working fine

` var buyRule = Rule.Create(ic => ic.IsEmaBullishCross(21, 55));
var sellRule = Rule.Create(ic => ic.IsEmaBearishCross(21, 55));

        var runner = new Trady.Analysis.Backtest.Builder()
            .Add(candles)
            .Buy(buyRule)
            .Sell(sellRule)`

The problem is when I am trying to get the buy signal and sell signal from the exchange always is False.

I am using the IndexedCandle like this:

`
var ic = new IndexedCandle(candles, candles.Count - 1);
var buyRule =(ic => ic.IsEmaBullishCross(21, 55));
var sellRule = (ic => ic.IsEmaBearishCross(21, 55));

`

Maybe I am doing something wrong.

I would appreciate some help.

I did my own testing and I found something very interesting
I implemented my own indicator called TemaIndicator
I followed the examples and I did thisd

//public static bool IsTemaBullishCross(this IIndexedOhlcv ic, int periodCount1, int periodCount2)
// => ic.Get(periodCount1, periodCount2).ComputeNeighbour(ic.Index)
// .IsTrue((prev, current, _) => prev.Tick.IsNegative() && current.Tick.IsPositive());

    //public static bool IsTemaBearishCross(this IIndexedOhlcv ic, int periodCount1, int periodCount2)
    //    => ic.Get<TemaIndicatorOcillator>(periodCount1, periodCount2).ComputeNeighbour(ic.Index)
    //         .IsTrue((prev, current, _) => prev.Tick.IsPositive() && current.Tick.IsNegative());

Same as many extensions when I use them with back testing is working fine but with real data never always they return false like macd crossing, emabearishcross, emabullishcross etc..

I modified this extension like this:

public static bool IsTemaBullishCross(this IIndexedOhlcv ic, int periodCount1, int periodCount2)
    {

// original code
var test = ic.Get(periodCount1, periodCount2).ComputeNeighbour(ic.Index)
.IsTrue((prev, current, _) => prev.Tick.IsNegative() && current.Tick.IsPositive());

// traditional way
var tema1 = ic.Get(periodCount1).Compute()[ic.Index];
var tema2 = ic.Get(periodCount2).Compute()[ic.Index];
if (tema1.Tick.Value > tema2.Tick.Value)
{
return true;
}
else
{
return false;
}

    }

and finally the cross was fired ....

there is a bug with the extensions.

I would like to help to fix the problem but for now is the only way I found to fix them.

Thank you!

@karlwancl
Copy link
Owner

@RikardoPons Sorry for late response. I have double checked and tested the code on IsEmaBullishCross, and it should be correct for the result.

You have mentioned that you have changed the logic from the original one to your traditional one, and works. But they are different in concept, my code is determining if an "intersection point" exists when ema10 cross ema30 upwards for IsEmaBullishCross, similar for IsEmaBearishCross. But your code is determining if ema10 is larger than ema30 which includes the case of "intersect", so you may find a larger set of result when using this logic.

I've also included a test case "TestBullishCrossAlwaysFalseAsync" and the verification excel for the case for the reference. Thanks.

@RickPons
Copy link
Author

Thank you for your answer.

I think I found a possible issue but it is not from your code. I am getting data from som Exchanges like Bittrex and Binance but I realized there are missing candles in the request sometimes. I guess this is affecting the interection when I use IsEmaBullishCross for example.

@karlwancl
Copy link
Owner

okay. May I close the issue as it's a fixed issue?

@karlwancl
Copy link
Owner

The issue should have fixed, but no response from @RikardoPons . Close.

@RickPons
Copy link
Author

I am sorry for the late reply. I could not fix the problem. I mean when I do backtesting the method is working fine but... when I do a request to get the candles the result is always false. I am struggling with this issue now..

@karlwancl karlwancl reopened this Sep 11, 2018
@karlwancl
Copy link
Owner

Do you mean you want to get all the candles that should fulfill a condition or not?

@RickPons
Copy link
Author

I am getting the candles from the exchange trying to find a buy signal or sell signal.. If I download these candles and I do a backtesting everything is fine I can get the buy and sell signals only when I do backtesting.. If I get the candles from the exchange and I apply the algo using this method Always is false.

@karlwancl
Copy link
Owner

karlwancl commented Sep 11, 2018

May I know how you are trying to apply the algo to determine the condition in code level? It may be misuse of the method. Could you paste the code to let me test it?

@RickPons
Copy link
Author

For example I want to apply this algo just it is working when I use backtesting.

`
var ic = new IndexedCandle(candles, candles.Count - 1);

var buyRule = (ic.IsEmaBullishCross(21, 55) && ((ic.Ema(8) > ic.Ema(13)) && ic.Ema(21)> ic.Ema(55)));
var sellRule = (ic.IsEmaBearishCross(21, 55) && ((ic.Ema(8) < ic.Ema(13)) && ic.Ema(21) < ic.Ema(55)));`

@karlwancl
Copy link
Owner

karlwancl commented Sep 11, 2018

Is the Ema method declared by you? I have to confirm this first.

@RickPons
Copy link
Author

Yes I did my own extension. It's forking fine. You can try just using IsEmaBullishCross and IsEmaBearishCross and I got the same result for example trying to get a buy signal and sell signal using Emas strategy 12,26 whatever always raturn false. But did some testing and your code looks fine my theory is the candles I'm requesting each period of time must be synchronized with the exchange and get the candles few seconds after the exchange update the their feed. I will try to do that. Just I'm thinking why just in backtesting your methods are working fine and they don't work when I am requesting the candles from the exchange

@karlwancl
Copy link
Owner

karlwancl commented Sep 11, 2018

Okay. I have to check it later as I am now busy with another project, I will get back to you later.

karlwancl pushed a commit that referenced this issue Sep 12, 2018
@karlwancl
Copy link
Owner

@RikardoPons I've just checked the logic, the index you use in transaction differs the actual index by 1. The buy/sell procedure should be
Hit buy/sell rule (current timeframe) -> Buy/Sell stock (current timeframe + 1)
So, if you need to verify the condition you should use new IndexedCandle(candles, index - 1) to check rather than new IndexedCandle(candles, index)

@RickPons
Copy link
Author

If I understand my code should be like this
var ic = new IndexedCandle(candles, candles.Count - 2);

I will try it!

Thank you!

@karlwancl
Copy link
Owner

@RikardoPons Once you've tested ok, it would be good to help to close this issue. Thanks.

@RickPons
Copy link
Author

@lppkarl thank you Now is working properly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants