Hi, I'm trying to create a script to initiate a long position when the fast MA crosses two other slower MAs. The problem I have when using the "crossabove" or "crossbelow" is that trades are initiated when the crossover occurs over 1 bar. Likewise for the exit. By default however, it will exit on close. It will not initiate or exit a position if the crossover occurs over a few bars.
The attached chart show a long position at approx 9:37 when the green lines crosses above the red and orange lines. But at approximately 11.57, when the green line crosses below the other 2 lines, it should exit. Instead it exits on close. Would anybody be able to assist?
From what I can see, the problem is that your logic is looking for both crossovers to happen on the same bar. What you need to do is use one crossover as a trigger (using the actual crossover keyword) and another as a filter (by just checking that the value is higher or lower).
if you try to trade crossovers you will blow your account if it was a good method of trading everyone would do it trust me just look in another direction to trade it wont help you ...sharky
Guys, your comments are noted and appreciated. Thanks. The initial question i posed allows me to 1) learn how to write the code for similar stuff which I have very little idea how to at the moment and 2) see how the strategy works in asia.
Hope you understand.
The following user says Thank You to kaywai for this post:
Does that mean one should refrain from automated trading with crossovers? May be it
varies from instrument to instrument as I know someone who has been trading
different types of crossovers with very good end results.
kaywai: crossover has problems like that. I ended up coding it manually like:
if MAfast[1] < MAslow[1] and MAfast[0] > MAslow[0] then that's an upcross.
In terms of crossovers in general: if you are trading generally trending markets they work well. In high-volume heavily institutional markets this often doesn't work out since the chops eat up the winners. But there are many markets out there which do trend nicely.
Thing is: those that do trend nicely you can probably get the same results with simple turtle-trading style, i.e. buy higher highs and sell lower lows. Throw up a Donchian or two and see if this is true or not.
Another classic approach - again with markets which tend to trend - and which uses MA's:
Let's say you have a slow MA for the LT trend, like a Weekly 20 EMA = 100 Daily (5*), or if you are intraday it's the same principle: something quite slow for LT trend (I find 63-ish is often good = 3* 21).
Then any time that weekly/LT is long, say, and the ST Stochastic (say 21) crosses up from underneath the 20 level, you got yourself a good-looking pullback. Very simple, but quite effective. In this case the trick is not so much in the system as
a) picking strong trending markets (no mean trick, although I suspect many Asian instruments do trend strongly since you guys are in boom times like the US in the late 1800's - late 1920's and your boom is MUCH bigger than ours since you are starting with over 1.4 billion people and the US had less than 100 million).
b) money management in terms of loss and trailing stops, multiple scaleouts etc.
Good luck!
The following user says Thank You to cclsys for this post:
Just for fun, I picked a trending market: Crude.
Since it seems you are trading in Asia I threw up:
1) LT Ichimoku Kinkyo Ho clouds, which use medians from 27 to 81. If price is above clouds = bull mkt. Simple.
2) ST Stochastics from NT basic indicators.
3) For fun: an oscillator derived from the Ichimoku fast and slow lines also in above chart (red and green) which give added medium term info; the indy is designed to help with trend trading but is slower than the stochs and the buy areas tend to be in crosses above zero versus from down below, i.e. above zero is bullish and below zero is bearish.
4) Ichimoku Kinkyo Ho Lines (Donchian Medians 9 and 27). Developed in the 1960's I believe (before computers). Still solid. The clouds are VERY Japanese and VERY helpful (drawn 81 bars before current price action, i.e. they predict probable future price. If price is above this prediction, it's bullish.)
As you can see, in a strong trending market, buying pullbacks works pretty well. Probably your crossing MA's are doing the same thing on some level.
They have to. Why: if market is not strongly trending, MA cross strategies get chewed to bits.
If you are making money in trending markets you must be getting on board (somehow) and staying along for the ride long enough to more than make out for the losing entries. So the cheapest way to get in is on pullbacks. The most momentum-rich way is on breakouts (that work). MA crosses tend to do one or the other depending on how they are configured, but no matter the indicator if you are buying into a strongly trending bull market, it doesn't really matter the entry technique: the strongly trending market will take care of it for you!
The real issue is: what happens when it's not strongly trending? Are you ceasing to trade that instrument? If so, no worries. But if you are trading it all the time no matter what, it's going to be a very sophisticated MA technique that will come away with net gains over time.
my 2 cents about MAs crossovers.
Agree with Sharky trading based solely on crossovers seems not quite good way, but...
big difference how you use that crossovers.
For example, if you wait for crossover and then enter as per that crossover, that one way
if you wait for crossover and then enter on the pull back from "longer" MA in your crossover that's another way.
Not brilliant idea, because apart from that better to have some meaningful levels supports that pullback, but it helps me to see some picture.
+ pullbacks from "longer" MA works only when price reach that first time, at least second time on third it usually not pullsback from that "longer" MA.
I've noted also that simply crossover of 10 SMA and 10 EMA shows quite interesting levels intraday, but not deepen yet in to that enough.
+ also quite important seems what MAs used for crossovers.
P.s. if any I will be glad to discuss further that issue
Nice equity curve ......your exits must be really good ....
You know what you know but you do not know what you do not know.
You do not see things how they are, you see things how you are.
In life you do what you want but you do not want what you want.
The equity curve went sharply negative early, on top of that, there are no commissions factored in. This would support Sharky's comments that you'll blow out the account before you would ever be profitable long term.
Based on my experience crossovers can be profitable but you need
1) strong filters ...e.g. always trade in the 100-200 daily MA slope or only trade if the intra-day gap is > a certain amount
2) non-related Crossover exits techniques
3) Strong cross : The bigger the length the MA is crossed by the price ,the bigger the chances of a profitable trade, unfortunately the length of these MA are not compatible with day-trading time frames (in and out same days )
4) forecasting choppy days as these pull 100 un-profitable trades..
What I do not understand is why would you want to automate such strategies if you can easily replicate these by discretionary trading .... Human Eye still the best for low-frequency trading
...
You know what you know but you do not know what you do not know.
You do not see things how they are, you see things how you are.
In life you do what you want but you do not want what you want.
The following 2 users say Thank You to gabga100 for this post:
I agree, I don't automate this. Also since the backtested results wait for the bar to close, I get much better fills in real time, I also use s/r levels to help with exits, but the tested strategy is just an end of day exit, and has out performed all the exit strategies I have attempted to automate.
nice. it looks like you're getting about one trade every 2 days on a 10-minute chart, and you're in the market about 15% of the time (presuming you're not trading after-hours). is that about right?
It looks good .... but optimizations are really far from reality .... it can be fitted ....
Is this a Walk-forward optimization or a simple optimization ?
With many parameters and a good Genetic optimizer , everybody can pull-out results like that.... even for 6 years .....
You know what you know but you do not know what you do not know.
You do not see things how they are, you see things how you are.
In life you do what you want but you do not want what you want.
Just simple optimization, ran the walk forward and still looks good (I think), not sure how to post the results though, since they are not summarized, so attaching as xls. Only has 2 parameters and stop loss.