Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
i have posted a couple of questions before and have had great response as well as helpful coding advice. i have an automated strategy i have traded live and in the simulator with rather promising results, but i would still like to improve it further. the issue is that being a moving average cross strategy, my code misses a lot of entries it should make, and it also holds positions way past price levels where it should close / reverse them (let's say it holds a short position after price has clearly crossed over the average where it should reverse into a long position).
i adapted the moving average cross that comes with tradestation. the original entries and exits for a long trade look like this:
movavg cross le:
{ Buys if Price crosses over Avg and then stays above Avg for one or more bars }
if Price > AverageFC( Price, Length ) then
Counter = Counter + 1
else
Counter = 0 ;
if CurrentBar > ConfirmBars and Counter = ConfirmBars then
{ CB > ConfirmBars check used to avoid spurious cross confirmation
at CB = ConfirmBars }
Buy ( "MACrossLE" ) next bar at market ;
{ ** Copyright (c) 2001 - 2010 TradeStation Technologies, Inc. All rights reserved. **
** TradeStation reserves the right to modify or overwrite this strategy component
with each release. ** }
if CurrentBar > 1 and Price crosses under Avg then
{ CB > 1 check used to avoid spurious cross confirmation at CB = 1 }
Sell ( "MACrossLX" ) next bar at market ;
{ ** Copyright (c) 2001 - 2010 TradeStation Technologies, Inc. All rights reserved. **
** TradeStation reserves the right to modify or overwrite this strategy component
with each release. ** }
mine are practically the same, i just use one moving average length for long and another for short trades:
if Price > AverageFC( Price, Lengthl ) then
Counterl = Counterl + 1
else
Counterl = 0 ;
if CurrentBar > ConfirmBarsl and Counterl = ConfirmBarsl then
Buy PSize Shares next bar at open ;
Avgl = AverageFC( Price, Lengthl ) ;
If MP = 1 and Price < ( Avgl + BarSizelx ) then
Sell all shares next bar at open ;
but, as i mentioned at first, if i optimize the strategy leaving some data out of sample it looks good, but when the strategy is then extended to the out of sample period it just misses a lot of entries and reversals. i include a picture in daily bars to illustrate how the strategy should go short / reverse / exit a long position when price breaks clearly below the lowest simple moving average and go long / reverse / exit a short position when price moves clearly above the highest sma but doesn't. in this amzn trade, the strategy should have dropped the short position as soon as price is above the highest average, yet it stupidly holds it until the second time price crosses over the sma. is there a way to improve the responsiveness of the strategy? either using some other entry / exit code or maybe using several redundant sets of entries / exits (using both - Price crosses under Avg - and - Price < Avg -) ?
oks, thanks a lot, any help will be enormously appreciated,
Can you help answer these questions from other members on NexusFi?
this is the code for the short trades. entries do require a couple additional conditions, but exits are coded just like this, which makes it very frustrating when the strategy is not even close to following this logic:
if Price < AverageFC( Price, Lengths ) then
Counters = Counters + 1
else
Counters = 0 ;
if CurrentBar > ConfirmBarss and Counters = ConfirmBarss then
i have coded the same concept into an indicator as illustrated in the image, and the crosses can be easily seen graphically but an automated strategy won't act on them.
you are welcome. Putting your code pieces together into one signal I don't see any issues there.
It closes open trades when the price is above/below the respective average and the entries occur when price
is above/below the average for enough bars.
put some work into this and seemingly solved it. i share my conclusions in case it could be of help to someone.
my code initially looked like this:
if Price + alteration > AverageFC( Price, Lengthl ) then
Counterl = Counterl + 1
else
Counterl = 0 ;
if CurrentBar > ConfirmBarsl and Counterl = ConfirmBarsl then
Buy PSize Shares next bar at open ;
Avgl = AverageFC( Price, Lengthl ) ;
If MP = 1 and Price < ( Avgl + alteration ) then
Sell all shares next bar at open ;
i coded the strategy again, going step by step, and i have found that it was better to define a line for long entry, another for long exit, short entry and short exit. seems like execution suffers massively if the strategy calls for additional operations to be performed simultaneously to the comparison between price and simple moving average.
so, it ended like this and the glitches seem solved:
good job. I had to block BarSizelx in the code I did as I had no idea what exactly its purpose was and therefore blocked it. That's why the code worked without an issue.
As you can't plot out of strategies it sometimes helps with the debugging to plot the entry conditions below the chart with an indicator. In your case this could be the counters or the exit condition.