Stoploss modification not working

Created at 23 Aug 2020, 04:22
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!
PU

puppiebullet1

Joined 11.07.2020

Stoploss modification not working
23 Aug 2020, 04:22


Below is my code and I want to modify my stoploss when the ask price moves in favour of my trade by a certain number of pips but for some reason the trade gets executed alright but the stoploss doesn't get modified. Any help please!

protected override void OnTick()
        {
            double SMA = sma.Result.LastValue;
            double buySMA = SMA + 10 * Symbol.PipSize;
            double buySMA2 = SMA + 10.2 * Symbol.PipSize;
            double sellSMA = SMA - 10 * Symbol.PipSize;
            double sellSMA2 = SMA - 9.8 * Symbol.PipSize;            
            if (Symbol.Ask > buySMA && Symbol.Ask < buySMA2)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
                newBuytop();
            }
            else if (Symbol.Ask > sellSMA && Symbol.Ask < sellSMA2)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
                newSellStop();
            }
        }
        private void newBuytop()
        {
            foreach (var position in Positions)
            {
                var stTrail = position.EntryPrice + 50 * Symbol.PipSize;
                var sstTrail = position.EntryPrice + 50.2 * Symbol.PipSize;
                var ndTrail = position.EntryPrice + 100 * Symbol.PipSize;
                var nndTrail = position.EntryPrice + 100.2 * Symbol.PipSize;
                var rdTrail = position.EntryPrice + 200 * Symbol.PipSize;
                var rrdTrail = position.EntryPrice + 200.2 * Symbol.PipSize;
                var pr = Symbol.Ask;
                var entry = position.EntryPrice;
                if (pr > stTrail && pr < sstTrail)
                {
                    ModifyPosition(position, entry, position.TakeProfit);
                }
                if (pr > ndTrail && pr < nndTrail)
                {
                    entry += 50 * Symbol.PipSize;
                    ModifyPosition(position, entry, position.TakeProfit);
                }
                if (pr > rdTrail && pr < rrdTrail)
                {
                    entry += 100 * Symbol.PipSize;
                    ModifyPosition(position, entry, position.TakeProfit);
                }
            }
        }
        private void newSellStop()
        {
            foreach (var position in Positions)
            {
                var stTrail = position.EntryPrice - 50 * Symbol.PipSize;
                var sstTrail = position.EntryPrice - 50.2 * Symbol.PipSize;
                var ndTrail = position.EntryPrice - 100 * Symbol.PipSize;
                var nndTrail = position.EntryPrice - 100.2 * Symbol.PipSize;
                var rdTrail = position.EntryPrice - 200 * Symbol.PipSize;
                var rrdTrail = position.EntryPrice - 200.2 * Symbol.PipSize;
                var pr = Symbol.Ask;
                var entry = position.EntryPrice;
                if (pr < stTrail && pr > sstTrail)
                {
                    ModifyPosition(position, entry, position.TakeProfit);
                }
                if (pr < ndTrail && pr > nndTrail)
                {
                    entry += 50 * Symbol.PipSize;
                    ModifyPosition(position, entry, position.TakeProfit);
                }
                if (pr < rdTrail && pr > rrdTrail)
                {
                    entry += 100 * Symbol.PipSize;
                    ModifyPosition(position, entry, position.TakeProfit);
                }
            }
        } 


@puppiebullet1
Replies

firemyst
26 Aug 2020, 14:28

Have you run this code in Visual Studio or put in "Print" statements to see if the actual calls to ModifiyPosition are called?

You have quite a bit of "IF" statements, so how do you know any of those are true to execute the modifyposition method?


@firemyst