thank you very much @mradtke this has been super helpful.
Regards,
Dave
When I add sell signals it strangely doesn't pick up all the sell signals. It will if I run a date range, but won't for a specific date (i.e. in backtester using from-to dates using the same day). For example there should be sell signals on 02/05/2018 using the simple MACD crossover, but they aren't output to the file?
for( bar = 0; bar < BarCount; bar++ )
{
// Cnt = 0;
// open file in "share-aware" append mode
fh = fopen( filepath, "a", True );
if (fh)
{
for( sig = bo.getfirstsignal( bar ); fh AND sig; sig = bo.getnextsignal( bar ) )
{
if (sig.IsExit())
{
fputs( NumToStr(dt[bar],formatDateTime) + " " + "STC " + sig.Symbol + " " + "MOC" + "\n", fh );
}
}
@mradtke it appears that I have to select the day before and the current date for the sell signals to work properly. For example if I want May 2nd 2018... I have to select May 1st to May 2nd.
@DaveDM, AmiBroker only sends signals to the CBT if it believes you need them. Built-in efficiencies like these are part of what make AB so fast, but sometimes you need more signals than AB thinks are "necessary". To have better control over what's sent, see the SetBacktestMode()
command.
Thanks @mradtke
I tried a few options and same result. I'm curious why the following code isn't spitting out the sell signals to file when selecting the same from and to date range (i.e. 1 specific date), any ideas? If you select a buy date of the day before in the from and a day after in the to date fields, the output in the file is different than the exploration. If you select the same date in the to and from fields in analysis, there is no output to the file, just empty. thanks, Dave
maxpos = Param( "Max Number of Positions", 1500, 1, 1000, 1 ); // maximum number of open positions
SetOption( "InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 100 / maxpos, spsPercentOfEquity );
// SetBacktestMode( backtestRegularRaw ); // I tried this too
SetBacktestMode( backtestRegularRawMulti );
SetOption( "usecustombacktestproc", True );
buysignal = Cross( MACD(), Signal() );
Buy = buysignal ;
sellsignal = Cross( Signal(), MACD() );
Sell = sellsignal ;
BuyPrice = Close;
SellPrice = Close;
Filter = sell ;
AddSummaryRows( 16 ) ;
AddColumn( sellsignal, "MACD Status", format = 1, colorDefault, IIf( sell, bkcolor = colorred, bkcolor = colordefault ) ) ;
// SetSortColumns( -3 );
SetCustomBacktestProc( "" );
// CBT mid-level
if( Status( "action" ) == actionPortfolio )
{
dt = DateTime();
bo = GetBacktesterObject();
bo.PreProcess();
// Delete old file
filepath = "C:\\Users\\Dave\\Alera\\TestAccount1\\1001\\CBT Frame Test - SELLS.sig";
fdelete( filepath );
for( bar = 0; bar < BarCount; bar++ )
{
// Cnt = 0;
// open file in "share-aware" append mode
fh = fopen( filepath, "a", True );
if( fh )
{
for( sig = bo.getfirstsignal( bar ); fh AND sig; sig = bo.GetNextSignal( bar ) )
{
// if( sig.IsExit() AND sig.Reason == 1 AND Cnt < maxpos )
if( sig.IsExit() )
{
// write to file
fputs( NumToStr( Cnt + 1, format = 8 ) + "." + NumToStr( dt[bar], formatDateTime ) + " " + "STC " + sig.symbol + " " + "DAY" + " " + "MOC" + "\n", fh );
// Cnt++;
}
} // end For all signals
// close file handle
fclose( fh );
}
else
{
_TRACE( "Failed to open the file" );
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}
Do you have a trade delay set? How does the output in the file compare to the back test results?
No trade delays set, it just assumes buy on close of signal bar.
When scanning for one day/specific date using the same date in the from and to date fields I get ZERO results. When using multiple days between the from and to fields in analysis window, I get only a few of the signals. The results vary substantially between exploration and back testing.
@mradtke a thought occurred to me. When looking for buy signals the backtester method makes sense because it will rank and "buy" whatever signals it gets on any given start date. But when looking for sell signals on a start date Amibroker can't necessarily match up any sell signals to buy signals (for one specific given date). Is this assumption accurate? If so, it might make more sense for me to generate my buy signals using the backtester and file output and then use the explorer and file output for generating all sell signals since I don't require any ranking for sell signals. Thoughts?
What do you mean by "match up"? I thought you wanted everything sorted by date, and within each date the Buy signals should be sorted by rank (PositionScore).
It is probably the case that AB is filtering out Sell signals that don't have a corresponding Buy within the date range, and perhaps that's what your "match up" comment is referring to. If you're only interested in writing the signals for the most recent bar to your text file, then I would run the back test over a sufficiently long period to get all applicable Buys and Sells, and then put logic in the CBT to only write the text file on the last bar.
Yes, this is correct. I have the buy signals working properly now (thank you) so that the file output is ranking/sorting properly and I can choose the number of signals that I want to output.
The last issue I have is with the sell signals, which is what I meant by "match up". Your idea of "run the back test over a sufficiently long period to get all applicable Buys and Sells, and then put logic in the CBT to only write the text file on the last bar." is a good one, and I may go that route in order to be able to combine everything into one script. The other option I was thinking was just to output ALL sell signals to a separate file (or append to existing buy file) simply using the exploration and outputting all sell signals. In terms of implementation with the software I'll be using to take the signals into production, it will ignore sell signals when no positions actually exist, but those exit signals will be needed for other traders that might already be in positions depending on when they started trading the same system. Thanks again. DaveDM
You're welcome. If you ever need consulting help, please check out my web site at quantforhire.com.
I definitely will. I found your site and enjoyed watching a presentation that you gave to the MTA on Backtesting. I understand that you also do Amibroker training?
Regards,
DaveDM
That's correct. If you send me email at quantforhire@gmail.com, I can send you my standard agenda. We can also customize the agenda to fit your current needs.