Please help me to get these 4 lines of code amended to get the desired results.

Tags: #<Tag:0x00007f8045e6d080> #<Tag:0x00007f8045e6cf18>


I tested the ATR based trade exit and I feel they are not better than the sell and cover codes.

Regarding applystop , even I tried many times and could not find it working properly----may be we are not coding it properly.

This EMA CROSSOVER is a very basic strategy. Could you please advise any advanced and/or at least a decent profit making intraday strategy?


any advise please......


i can't help here i will leave that answer to people smarter than me.

Howard Bandy has some guidelines see also his post here: Safe-f parameters: a check of the resuts (post of June 24 2017).

I am actually surprised that he says portfolio trading is dead. I thought this was the easiest way of trading for small but steady returns. I have to read that discussion again though.


I am studying Bandy's post.
Will the below code look into future in live trading?

Ref(C,1) > C


can't be used in trading. What that expression says: todays Close is less that tomorrows Close.

when in doubt just use playback for things like this. Just make a signal like:

tt = Ref( C, 1 ) > C;
//tt = C < Ref( C, 1 );
PlotShapes( IIf( tt, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );

and do a playback


Portfolio trading has, in the past, been based on several assumptions.
One. The distributions being analyzed are stationary -- that the indicators and metrics computed from historical data will continue to have similar distributions in the future. Modern portfolio theory assumes that future returns and variances will be similar to those computed using historical data.
Two. That the best metric for risk is standard deviation of price.
Three. That backtest results are sufficient to forecast future results without need for following the scientific method of out-of-sample validation.
Four. The issues selected for the portfolio can be chosen from a very large universe of tradable issues without concern for the size of the search space that implies.

My suggestion is that a portfolio be formed as follows:
One. Pick a single issue to trade. The issue must have enough variation in price to offer reasonable opportunity for profit, but not so much that being on the wrong side of the trade causes excessive drawdown. Two. Given that issue, develop a trading system that trades it long / flat. After finishing that, you can try short / flat, but it will have different parameters and it will be more difficult to validate. The characteristics of rising prices are different than those of falling prices. Tops are more difficult to identify than bottoms.
Three. Use state signals. Buy and Sell are impulse signals that are True for a single bar. For example, Buy = cross(a,b), where a and b are some indicators. These cross on one bar, then not again for several bars. State signals are signals that give the signal for the next measurement period and remain the same until the conditions change. I describe them as beLong / beFlat / beShort. Once a beLong signal is given to enter a trade, the signal continues to be beLong until the exit when the signal changes to beFlat. AmiBroker fully supports state signals. The results of a series of beLong signals is identical to the single Buy/Sell period. But, importantly, using state signals gives the opportunity to manage the trade every day.
Four. By all means, search for the best fit of the model to the data in backtesting. You must optimize. Optimization is the "fitting" process of fitting a model to a set of data.
Five. You must have confidence that the future resembles the past. That whatever indicators and metrics you use are stationary. Compute them over historical data and study how they change over time. If necessary, and it probably will be, use transformations to make them stationary.
Six. Best is measured by the score given to the system (model plus data) using some metric. AmiBroker supports custom metrics. Use them. The trades that are found in a backtest are in a time sequence. Try to avoid using metrics that depend on the order of the trades (such as drawdown). Given a set of trades, we can hope that the trades signaled during live trading in the future have the same distibution as those found in development, but we cannot assume they will be in the same order. Final equity of a set of trades is independent of order. Drawdown will change as the trades are reordered.
Seven. The equity curve that catches your attention during testing is always optimistic.
Eight. Follow the scientific method. Data mine and fit the model, then validate using data not seen by the model. In-sample results have no value in estimating future performance. The "best estimate" of future performance is the set created by the walk forward process.
Nine. Study the two metrics I have developed, safe-f and CAR25. They are described in several of my articles and books, and in the videos I have posted to YouTube. Together they allow comparisons of sets of trades, normalized for risk. Given two or more alternative uses of funds, we can determine the maximum safe position size for each (safe-f). At that position size, we can estimate future compound annual rate of return (CAR25).
Ten. The portfolio I recommend is a portfolio of systems. Work through the steps above to develop several trading systems. Each trades a single issue long / flat or short / flat. Each day, compute safe-f and CAR25 for each of the systems. Take the position indicated by the system that has the highest CAR25 for the next day's trade.

Best regards, Howard


I tried to understand Bandy's post fully yesterday. It seems that the concept is over and above the capacity of my tiny brain. still I am in the process to digest it in order to make use of it.

However, can the below code be used in live trading as I think it will be neither repaintig, future looking nor vanishing.

O > Ref(C,-1)


thanks Howard. I am still a little confused. With a single issue to trade you mean that the development of a trading system or method should best concentrate on a single issue. So, if one chooses to trade the Nasdaq 100 it would be advised not to use 1 method for all stocks but look at them individually. Do I understand that correctly? If so, then would you favor concentrating on trading for instance only gold futures over trading a whole market of stocks?

I have to read your post again about where you talk about AI. Apart from the fact that a lot of code is already written for Python with respect to data mining etc., could you maybe elaborate a bit (in layman's terms :slight_smile: ) about what you call "traditional trading system development platforms" like Amibroker. From that post on June 24, 2017 I understand that the traditional trading software might not be suited for developing trading methods that rely on data mining or AI in general. What are the most important reasons for that?

thank you


Whether a trader is planning to use a traditional trading system development platform such as AmiBroker, or a machine learning development platform such as one using Python, my recommendation is the same. Work with a single issue -- a single ticker. If you are hoping to trade the NASDAQ 100, use either the futures contract or QQQ. If you are hoping to trade an individual stock that is a member of the NASDAQ 100, pick one and work with it exclusively. After developing that one system, pick another stock and develop a separate system for it. And so on. Evaluate each system every day. Rotate among them, trading the system that has the highest risk-normalized profit potential as of that day.

Making all 100 of the stocks that make up the NASDAQ 100 available for testing is an optimization over a 100 element symbol space. (Be aware of membership bias -- the 100 constituents this year are not the same as they were last year. Using this year's list to test historical data will give overly optimistic results.) Just as testing the lookback periods for ten values each for two moving averages is an optimization over a 100 element space. Doing both is an optimization over a 10,000 element space.

The models we build using AmiBroker are single decision trees. They divide the data by using a sequence of rules that form upsidedown trees. One rule might be, if c > ma(c,10) then use the right branch, otherwise use the left branch. Eventually the branches lead down to "leaves" that are labeled Buy or Sell. Decision trees are very good at fitting the model to the data. So good, that there is a serious risk of overfitting. A model that has perhaps 1000 (four years) points of data and a large number of rules results in a very good fit. The only way to tell whether the fit is to a profitable and persistent signal is to test the model out-of-sample.

This is not in any way a criticism of AmiBroker. It is a caution that applies to all trading systems, no matter how they are developed.

My mantra ------------
My preference is for systems that have these characteristics, each of which trades a single issue in a single direction: accurately -- 65% or so is minimum.
...hold a short period -- no longer than one or two days. frequently -- once a week, more or less.
...avoid serious losing trades.

It is much more than a preference -- it is a requirement. When the risk associated with systems that do not have these characteristics is evaluated, and position size is set so that the risk to the trading account remains within my risk tolerance, they do not have enough profit potential to make trading them worthwhile.

Comparing traditional development platforms with machine learning -----------

Traditional platforms come from chart analysis and periodic Buy / Sell signals that mark the beginning and end of trades. Early on, the markets had large inefficiencies, signals were easy to identify, trends held for long periods, it was safe to hold long periods. The platform is built around handling and analysis of time series with functions that can easily refer to earlier data. The primary model (set of equations and rules) is single decision tree. The analysis came from and still is "compute an indicator, then see what happens later."

Machine learning works with independent data points, including a target for each data point. Time references and time dependencies must be computed as separate data elements and explicitly included so that each data point can be analyzed independently. One of the techniques for model fitting and validation is sorting the data points into random sets in a process called cross-validation. The focus moves from the indicator to the target. It is "identify a profitable trade, then see what came earlier." Combining the four-rule mantra I described above with the focus on the target, the trading system as we came to know it disappears.

Best regards, Howard


thanks Howard. That is interesting what you say about focusing on a single ticker, or better treating each ticker separately. This is what I have become to do more and more however, I found it easier to build a system that uses the same rules for all members of a market. Or at least give somewhat of a smooth return curve.

But in the case of stocks indeed you need to be aware of membership/survival bias. When shorting stocks you need to be aware that you'll be paying dividends etc. Building a good system on a single ticker is however very hard, at least that is my experience.

With respect to using traditional platforms for data mining / AI I asked because I have started a bit on that topic but did not do enough on it that I can make a good judgement. I rather stick with what I know at first and maybe tweak it somewhat in order that we still can do the same calculations. One thing is certain, we need the data to do the research and the data is nicely available in Amibroker.

thanks again for your remarks


Have you been able to use Python in Amibroker like for example repeated sending part of the Amibroker data to Python, predict next day close with a ML algorithm and import signal back to Amibroker and have the results in Amibroker?



The data is independent. Indicators can be computed in either AmiBroker or Python. It is possible to exchange that data using disc files for intermediary storage.

Think through what data you have at what time relative to the market hours and to your signal generation and order placement. Decide what you want done, where the data that is needed comes from, which program will be performing which tasks. Draw a flow chart that has a time scale axis. See if the process becomes clearer.