Need help with robots logic for opening and closing position.

Created at 18 Nov 2018, 00:12
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!
WI

willd7781

Joined 18.09.2018

Need help with robots logic for opening and closing position.
18 Nov 2018, 00:12


I’ve programmed a robot that looks at RSI and ADX to decide whether to enter the market. the robots core logic states that if the RSI indicates the market is oversold and if the ADX is below a certain level the position then the robot buys into the market. The opposite applies for overbought markets 

the code I used for this logic is bellow

protected override void OnTick()
        {
            if (rsi.Result.LastValue <= Min & dms.ADX.LastValue < ADX)
            {
                Open(TradeType.Buy);
                Close(TradeType.Sell);
            }
            if (rsi.Result.LastValue >= Max & dms.ADX.LastValue < ADX)
            {
                Open(TradeType.Sell);
                Close(TradeType.Buy);
            }
        }

The problem with this is that the position should only open when the position is over brought or oversold and ADX is low. The position should however be allowed to close even when the ADX is high if the RSI has reversed from overbrought to oversold and vice versa. I tired separating out the IF statements, but the close logic interferes with each other and it REALLY DOESNT WORK.

protected override void OnTick()
        {
            if (rsi.Result.LastValue <= Min & dms.ADX.LastValue < ADX)
            {
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue <= Max)
            {
                Close(TradeType.Sell);
            }
            if (rsi.Result.LastValue >= Max & dms.ADX.LastValue < ADX)
            {
                Open(TradeType.Sell);
            }
            else if (rsi.Result.LastValue >= Min)
            {
                Close(TradeType.Buy);
            }
        }

Any sollutions to this problem would be much appricated. 

 

Thank you 

 

William


@willd7781
Replies

PanagiotisCharalampous
19 Nov 2018, 12:20

Hi William,

Try removing the else statements.

Best Regards,

Panagiotis


@PanagiotisCharalampous