NexusFi: Find Your Edge


Home Menu

 





improving responsiveness on automated strategy


Discussion in TradeStation

Updated
    1. trending_up 1,700 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 5 posts
    2. attach_file 1 attachments




 
Search this Thread

improving responsiveness on automated strategy

  #1 (permalink)
maggtrading
quintana roo, méxico
 
Posts: 84 since Mar 2013
Thanks Given: 222
Thanks Received: 40

hello everyone,



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 }

inputs: Price( Close ), Length( 9 ), ConfirmBars( 1 ) ;
variables: Counter( 0 ) ;

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. ** }




movavg cross lx:

inputs: Price( Close ), Length( 9 ) ;
variables: Avg( 0 ) ;

Avg = AverageFC( Price, Length ) ;

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,

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
Better Renko Gaps
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
What broker to use for trading palladium futures
Commodities
Cheap historycal L1 data for stocks
Stocks and ETFs
 
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

maggtrading,

can you post the short entries and exits and the settings you used to come up with the posted chart?

Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
maggtrading
quintana roo, méxico
 
Posts: 84 since Mar 2013
Thanks Given: 222
Thanks Received: 40


ABCTG,



danke.



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


Sell Short PSize Shares next bar at open;



Avgs = AverageFC( Price, Lengths ) ;

If MP = -1 and Price > Avgs then


Buy To Cover all shares next bar at open ;



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.

Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

maggtrading,

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.

 
Code
inputs: Price( Close ), Lengthl( 9 ), Lengths( 18 ), ConfirmBarsl( 1 ), ConfirmBarss( 1 ) ;
variables: Counterl( 0 ), Counters( 0 ), PSize ( 1 ), Avgl( 0 ),  Avgs( 0 ), MP ( 0 );

MP = MarketPosition;

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 ;


if Price < AverageFC( Price, Lengths ) then
	Counters = Counters + 1
else
	Counters = 0 ;

if CurrentBar > ConfirmBarss and Counters = ConfirmBarss then
	Sell Short PSize Shares next bar at open;

Avgs = AverageFC( Price, Lengths ) ;

If MP = -1 and Price > Avgs then
	Buy To Cover all shares next bar at open ;
Regards,
ABCTG


maggtrading View Post
ABCTG,



danke.



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


Sell Short PSize Shares next bar at open;



Avgs = AverageFC( Price, Lengths ) ;

If MP = -1 and Price > Avgs then


Buy To Cover all shares next bar at open ;



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.


Follow me on Twitter Reply With Quote
  #5 (permalink)
maggtrading
quintana roo, méxico
 
Posts: 84 since Mar 2013
Thanks Given: 222
Thanks Received: 40

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:

leline = ( AverageFC( Price, Lengthl ) + alteration );
lxline = ( AverageFC( Price, Lengthl ) + alteration );

MP = marketposition;

if Price > leline then
Counterl = Counterl + 1
else
Counterl = 0 ;

if CurrentBar > ConfirmBarsl and Counterl = ConfirmBarsl then


Buy PSize Shares next bar at open ;


If MP = 1 and Price crosses under lxline then


Sell all shares next bar at open ;


short trades follow the same structure reversed.

Reply With Quote
Thanked by:
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

maggtrading,

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.

You can do it like this for example:
 
Code
if Price < ( Avgl + alteration ) then
Plot1(+1)
else
Plot1(0);
Regards,
ABCTG


maggtrading View Post
put some work into this and seemingly solved it. i share my conclusions in case it could be of help to someone.


Follow me on Twitter Reply With Quote




Last Updated on April 17, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts