Day Trading Strategy - Closing trades at end of day OR Apply Stop

I am working on a "day trading" system.

I want to run my algorithm for 1 day only, then close out all trades at the end of the day. The only exception is that I want to close out poor performing trades that same day by setting a stop loss as some percent (e.g., 3%) - whatever.

I can't seem to be able to figure this out. Below is the skeleton of the code I am having difficulty with...

SetOption("AllowSameBarExit",true);
SetOption( "ActivateStopsImmediately", 1 );
SetTradeDelays( 0, 0, 0, 0 );
ExitAtStop = 1;

[ **** CODE **** ]

ApplyStop( stopTypeNBar, stopModeBars, 1);
ApplyStop(stopTypeLoss, stopModePercent, 3 );

It seems to "make sense" to me, but clearly I am missing something. Any help / suggestions would be appreciated.

@pmfiorini this might help
http://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/

And in a similar way

http://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/

1 Like

Thanks for the info - I believe that works

But, how do you make sure that if I want to put a STP order for my day trade, that it gets executed on that same day. I've used this code below.

              ApplyStop(stopTypeLoss,  stopModePercent, 3 );  

But this does not seem to work...Any ideas? Does this make sense?

@pmfiorini I am not really the right person to answer this as I never use ApplyStop. But I can read the User Guide, so perhaps this

image

While your have set

SetOption( "ActivateStopsImmediately", 1 );

Try reviewing the guide and experiment with different settings.
https://www.amibroker.com/guide/afl/applystop.html

I saw that...

Here's what I did - I believe I followed the directions...

It still does not work

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "ActivateStopsImmediately", false );

[ CODE ]

ApplyStop( stopTypeNBar, stopModeBars, 0 );
ApplyStop(stopTypeLoss, stopModePercent, 3, 1 );

@pmfiorini Perhaps others who use ApplyStop can possibly help if you show them your code, and properly use code tags as is the rule on the forum.

I removed this line

ApplyStop( stopTypeNBar, stopModeBars, 0 );

And ran a system that shows the ApplyStop works fine.
image

Good luck, I am off.

1 Like

Try this

ApplyStop( stopTypeNBar, stopModeBars, 0, ExitAtStop = 0 );

You need to set exitatstop = 0 if you want to have stoploss go before number of bars

1 Like

Yes!!! This works. Thanks so much. I've struggled with this for a while...

Here is the code, in general, for anyone interested in case they have the same issue...

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "ActivateStopsImmediately", True );

[ ***** CODE ***** ]

ApplyStop( stopTypeLoss, stopModePercent, StopLossPct, 1 );
ApplyStop( stopTypeNBar, stopModeBars, 0, ExitAtStop = 0 );