PureBytes Links
Trading Reference Links
|
Tomasz,
I have been exploring more features since registering in the last
two weeks and really like the studies. I find however that I
continue to be confused by the ApplyStop function. After getting
unexpected results in a AFL program that I was testing, I decided to
go back to the exact example that you used in describing the
ApplyStop function. I have excerpted it from a previous post:
> buy = cross( macd(), signal() );
> sell = 0;
> ApplyStop( 0, 1, 2, 1 );
> ApplyStop( 1, 1, 2, 1 );
I have noticed that if the condition to satisfy either ApplyStop
call above happens on the same day as a buy signal, then exit sale
does not happen until the next day, and then at the wrong price,
regardless of whether you set the buy delay to 0 or 1, set the
buyprice to be at the open, etc.
I have found that if you want to include the possibility of having
the exit happen on the same day, you can program something more
elaborate to generate an additional sell signal only on the day of
the buy signal and if the exit criteria specified by the ApplyStop
calls are met, such as the following:
buy = cross( macd(), signal() );
maxstoplosspct=2;
limitorderpct=2; /* profit target percent */
limitorder=limitorderpct/100;
maxstoploss=maxstoplosspct/100;
highcondition=high>(1+limitorder)*buyprice;
sell=( highcondition or low<(1-maxstoploss)*buyprice) and buy;
ApplyStop( 0, 1, maxstoplosspct, 1 );
ApplyStop( 1, 1, limitorderpct, 1 );
However, the above will not sell at the exit price by the ApplyStop
if the exit sell signal happens on the same day, but rather at the
price indicated by the sellprice array as specified in
the "Settings" dialog box (which, of course, is what you would
expect).
How do I get the functionality of ApplyStop for exits that happen on
the same day as the buy?
|