bug in backtesting

Created at 21 Jul 2016, 20:33
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!
GO

goldspanner

Joined 12.07.2016

bug in backtesting
21 Jul 2016, 20:33


I've found what I can only assume is a backtesting bug (not tried this live trading):

In the code snippet below, you can see that my ExecuteMarketOrder methods are called inside if statement blocks. The issue I'm having is that during backtesting, the bot will only ever execute 1 order. This doesn't make any sense, because the Print() that I've put in each of those blocks are printing to the log evidence of a lot of trades. In short, those if blocks are being executed as they should be, but for some reason ExecuteMarketOrder will only every be executed once during backtesting.

 

The code:

protected override void OnBar()
        {
            if (long_position != null)
            {
                ModifyPosition(long_position, long_ATR_stop(long_position.StopLoss), null);
            }

            if (long_position == null && short_position == null && long_crossover() == true && long_bias() == true && above_avg_vol() == true && ADX_long_ok() == true)
            {
                // open_long_position();

                long volume = (long)(Math.Floor(Account.Balance) * Account.Leverage) / trade_size;

                if (volume > max_trade_size)
                    volume = max_trade_size;

                long_position = ExecuteMarketOrder(TradeType.Buy, Symbol, volume, label, stop_distance, null).Position;
                Print("Long Position Opened");
            }

            if (short_position != null)
            {
                ModifyPosition(short_position, short_ATR_stop(short_position.StopLoss), null);
            }

            if (short_position == null && long_position == null && short_crossover() == true && short_bias() == true && above_avg_vol() == true && ADX_short_ok() == true)
            {
                // open_short_position();

                long volume = (long)(Math.Floor(Account.Balance) * Account.Leverage) / trade_size;

                if (volume > max_trade_size)
                    volume = max_trade_size;

                short_position = ExecuteMarketOrder(TradeType.Sell, Symbol, volume, label, stop_distance, null).Position;
                Print("Short Position Opened");
            }
        }

 

Unless ExecuteMarketOrder has some conditional operators that I'm not aware of, I can only assume that this is a bug. Why else would all of a block of code apart from one line be executed???


@goldspanner
Replies

... Deleted by UFO ...

goldspanner
21 Jul 2016, 22:34

RE:

lucian said:

Maybe you need:

 long volume =  Symbol.NormalizeVolume((Math.Floor(Account.Balance) * Account.Leverage) / trade_size);

Thanks! Yeah, I hadn't considered that at all


@goldspanner