open/close position with multiple condition

Created at 24 Dec 2017, 18:32
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
ER

erk86

Joined 19.05.2017

open/close position with multiple condition
24 Dec 2017, 18:32


Hi,

Can you please guide me coding in cAlgo with this case,I'm using sample RSI cbot.

i want open/close position when RSI line cross 30 or 70,

protected override void OnTick()
        {
            if (rsi.Result.LastValue > 30 || rsi.Result.LastValue > 70)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue < 70 || rsi.Result.LastValue < 30)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

and the problem is in backtest, when RSI line cross down 70 and open sell position but when it crossing up 70 again before reach level 30, its not close sell position or open buy position.

Did i miss something?

i'm sorry for my bad english,

Thanks in advance


@erk86
Replies

PanagiotisCharalampous
27 Dec 2017, 15:26

Hi ctid258463,

The problem is with your conditions. Your conditions are not checking if the indicator has crossed a value or not but if the last value is above or below another value. You should reconsider your conditions and check HasCrossedAbove and HasCrossedBelow functions which you should use for your conditions.

Best Regards,

Panagiotis


@PanagiotisCharalampous