Your interactive trading laboratory!
 • 
6 users online

Topic for Model "Connors TPS"

Just published the Connors TPS Model to the public. It's the first time, I have used the MarketClose OrderType but it looks like during back-testing, a buy order will only be submitted on the open on next day. Furthermore, it would be interesting to see the model when scaling-in using the 10%-20%-30%-40% approach. Any other improvements are appreciated...

Attachment

Cancel

Responses

That's correct, the backtester always places orders for the following bar as it goes bar by bar processing the data. In order to get orders to place on the close of the same bar requires a little work. I'm writing a dev blog article about this now, but basically you need to "peek into the future" 1 bar, look at the future bar's data, and make your buy/sell decisions based on that and then issue a MarketClose order. Here's a minimal example based on the 4x2 system from James Altucher. The first version uses Market orders to open the next bar at market open when there are 4 consecutive down days, and sells the next bar at market open when there are 2 consecutive up days.


using QuantaculaBacktest;
using System;
using QuantaculaCore;
using QuantaculaIndicators;
using QuantaculaChart;
using System.Drawing;
using System.Collections.Generic;

namespace Quantacula
{
    public class MyModel : UserModelBase
    {
        //create indicators and other objects here, this is executed prior to the main trading loop
        public override void Initialize(BarHistory bars)
        {
		cd = new ConsecDown(bars.Close, 1);
		cu = new ConsecUp(bars.Close, 1);
		PlotIndicator(cd);
		PlotIndicator(cu);
        }

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
            if (!HasOpenPosition(bars, PositionType.Long))
            {
                if (cd[idx] == 4)
		PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
            }
            else
            {
                if (cu[idx] == 2)
		PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
            }
        }

	//declare private variables below
	ConsecDown cd;
	ConsecUp cu;
    }
}

Here is the corresponding system using MarketClose orders. Note I cannot run the system on the last bar of data, since it needs to peek into the future 1 bar in order to place an order at market closed based on same-bar data. These hurdles in coding a system like this hint at the difficulties in trading something like this in the real world. Here's the Execute method, the other code remained the same.

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
		//we can't examine a future bar if it's the last bar
	        if (idx == bars.Count - 1)
			return;
	        
		if (!HasOpenPosition(bars, PositionType.Long))
            	{
                	if (cd[idx + 1] == 4)
				PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose);
            	}
            	else
            	{
                	if (cu[idx + 1] == 2)
				PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);
            	}
        }

Incidentally, running this on the QPremium Nasdaq 100 for the past 10 years resulted in a 104% profit for the Market version, and a 183% profit for the MarketClose version. Maybe there's some value here :)

That's correct, the backtester always places orders for the following bar as it goes bar by bar processing the data. In order to get orders to place on the close of the same bar requires a little work. I'm writing a dev blog article about this now, but basically you need to "peek into the future" 1 bar, look at the future bar's data, and make your buy/sell decisions based on that and then issue a MarketClose order. Here's a minimal example based on the 4x2 system from James Altucher. The first version uses Market orders to open the next bar at market open when there are 4 consecutive down days, and sells the next bar at market open when there are 2 consecutive up days. [CODE] using QuantaculaBacktest; using System; using QuantaculaCore; using QuantaculaIndicators; using QuantaculaChart; using System.Drawing; using System.Collections.Generic; namespace Quantacula { public class MyModel : UserModelBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { cd = new ConsecDown(bars.Close, 1); cu = new ConsecUp(bars.Close, 1); PlotIndicator(cd); PlotIndicator(cu); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { if (cd[idx] == 4) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { if (cu[idx] == 2) PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } //declare private variables below ConsecDown cd; ConsecUp cu; } } [/CODE] Here is the corresponding system using MarketClose orders. Note I cannot run the system on the last bar of data, since it needs to peek into the future 1 bar in order to place an order at market closed based on same-bar data. These hurdles in coding a system like this hint at the difficulties in trading something like this in the real world. Here's the Execute method, the other code remained the same. [CODE] //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { //we can't examine a future bar if it's the last bar if (idx == bars.Count - 1) return; if (!HasOpenPosition(bars, PositionType.Long)) { if (cd[idx + 1] == 4) PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose); } else { if (cu[idx + 1] == 2) PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } [/CODE] Incidentally, running this on the QPremium Nasdaq 100 for the past 10 years resulted in a 104% profit for the Market version, and a 183% profit for the MarketClose version. Maybe there's some value here :)

Just published a Dev Blog article addressing considerations around MarketClose.

Just published a [Dev Blog article](https://www.quantacula.com/Help/OpenHelp/devblog/167) addressing considerations around **MarketClose**.

Thanks for the detailed blog post.

What about introducing a bar status that let's us check if we're on "historical" or "real time" data? That would make a clear distinction in code. Let's assume i would like to send out messages (E-Mail, WhatsApp, Twitter etc.) in code. In "historical mode" completely useless but when in "real time mode", i may need to be informed when my system makes any new transactions.

Thanks for the detailed blog post. What about introducing a bar status that let's us check if we're on "historical" or "real time" data? That would make a clear distinction in code. Let's assume i would like to send out messages (E-Mail, WhatsApp, Twitter etc.) in code. In "historical mode" completely useless but when in "real time mode", i may need to be informed when my system makes any new transactions.

Hi Merlin, hmmm … a system will issue signals only when there are transactions generated on the last bar of data. So I'm not sure how the concept of historical versus real-time could integrate.

The problem with MarketClose systems is that we're kind of cheating, looking ahead at the next day of data and seeing if the conditions are met. For example, if we want to sell our position after two consecutive up closes, we typically have to wait until the market close, examine the closing price, and see if we have that second consecutive close.

What we need is a way to get the partial daily bar data a few minutes before market close (and hope it remains more or less consistent until close) and then act on that "pre-market close", issuing a MarketClose transaction if the closing price of the partial daily bar is up.

What I was proposing in my article was a component that could somewhat automate that process, allowing daily MarketClose system to be more easily tradable.

Hi Merlin, hmmm … a system will issue signals only when there are transactions generated on the last bar of data. So I'm not sure how the concept of historical versus real-time could integrate. The problem with MarketClose systems is that we're kind of cheating, looking ahead at the next day of data and seeing if the conditions are met. For example, if we want to sell our position after two consecutive up closes, we typically have to wait until the market close, examine the closing price, and see if we have that second consecutive close. What we need is a way to get the partial daily bar data a few minutes before market close (and hope it remains more or less consistent until close) and then act on that "pre-market close", issuing a **MarketClose** transaction if the closing price of the partial daily bar is up. What I was proposing in my article was a component that could somewhat automate that process, allowing daily **MarketClose** system to be more easily tradable.
Forum Tips

Please sign in if you want to participate in our forum.

Our forum uses Markdown syntax to format posts.

To embed code snippets, enclose them in [CODE][/CODE] tags.