[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: EnterTrade and ExitTrade question



PureBytes Links

Trading Reference Links


Tomasz (and all),

The new formula is greatly simplified based on your feedback - 
thanks. I am still having a problem in that the ExitTrade is not 
actually exiting the trades. When I run the code below for any time 
period I end up with 20 positions opened the first day and closed 
the last day. I even went as far as setting a direct exit trade with 
no conditions to see if the position exited - and it didn't. Any 
idea what I am dong wrong? TIA. 

Tom

---------------------------------------------
SetOption("UseCustomBacktestProc", True );

if( Status("action") == actionPortfolio )
{
	// retrieve the interface to portfolio backtester
	bo = GetBacktesterObject();
	bo.PreProcess();

	for( bar = 0; bar < BarCount; bar++ )
	{
		for( i = 0, sig = bo.GetFirstSignal( bar ); i++; sig 
= bo.GetNextSignal( bar ))
		{
			// If we do not have position in top N 
signals, enter position
			if (bo.FindOpenPos(sig.symbol) == Null AND i 
< numStocks) 
				bo.EnterTrade(bar, sig.Symbol, True, 
O[bar+1], -10);
			// If we have position that is not in top N 
signals, exit position
			if (bo.FindOpenPos(sig.symbol) == sig AND i 
>= numStocks) 
				bo.ExitTrade(bar,sig.Symbol,O
[bar+1]);
		}

	bo.ProcessTradeSignals( bar );
	}
bo.PostProcess();
}

numStocks = 20;
SetOption("MaxOpenPositions", numStocks );
PositionSize = -100/numstocks;
PositionScore= 100-RSI();
Buy = 1;
Sell = Short = Cover = 0;

--- In amibroker@xxxxxxxxxxxxxxx, "tkoinaustin" <tkoinaustin@xxxx> 
wrote:
> 
> Wow, I couldn't have screwed that up much worse if I tried! Thanks 
> Tomasz, I will take another look at it tonight.
> 
> Tom
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" 
<amibroker@xxxx> 
> wrote:
> > Hello,
> > 
> > There are few things you are doing wrong.
> > First of all, to check if you are in the trade (i.e. have 
openend 
> position)
> > you can not use single boolean flag, because
> > custom backtest operates on portfolio and can have multiple 
> positions open.
> > Instead you should use 
> > 
> > FindOpenPos method of backtester object:
> > http://www.amibroker.com/guide/a_custombacktest.html
> > 
> > There are other mistakes too (like using array instead of number 
in 
> Exit / Entry trade call)
> > 
> > You are getting error message because you are referring to 
> uninitialized
> > object (you can calling ExitTrade when bINTrade is false and are 
> attempting to
> > call openpos.Symbol).
> > 
> > Best regards,
> > Tomasz Janeczko
> > amibroker.com
> > ----- Original Message ----- 
> > From: "tkoinaustin" <tkoinaustin@xxxx>
> > To: <amibroker@xxxxxxxxxxxxxxx>
> > Sent: Sunday, March 06, 2005 2:59 PM
> > Subject: [amibroker] EnterTrade and ExitTrade question
> > 
> > 
> > > 
> > > 
> > > I am having trouble using the new backtester. My code (shown 
> below) 
> > > will error out on the "if( bInTrade == False) bo.ExitTrade
> > > (bar,openpos.Symbol,Ref(O,1));" and "if( bInTrade == False)
> > > bo.EnterTrade(bar, trd.Symbol, True, C, -10);" lines. The 
error I
> > > get is either error 18 "COM object variable is not initialized 
or
> > > has invalid type" or error 19 "COM method/function 'ExitTrade' 
> call
> > > failed".
> > > 
> > > I suspect I am not initializing something properly, but I would
> > > appreciate a pair of eyes to look at this and see if something 
> jumps
> > > out. I am using 4.70 RC 1. Thanks for any feedback.
> > > 
> > > Tom
> > > 
> > > ---------------------------------------------------------
> > > SetOption("UseCustomBacktestProc", True );
> > > 
> > > if( Status("action") == actionPortfolio )
> > > {
> > > // retrieve the interface to portfolio backtester
> > > bo = GetBacktesterObject();
> > > bo.PreProcess();
> > > for( bar = 0; bar < BarCount; bar++ )
> > > {
> > > // If not in trade, then get top positions in
> > > for( openpos = bo.GetFirstOpenPos(); openpos; openpos =
> > > bo.GetNextOpenPos() )
> > > {
> > > bInTrade = False;
> > > for( cnt = 0,sig = bo.GetFirstSignal( bar ); Cnt++; cnt <
> > > numStocks , sig = bo.GetNextSignal( bar ))
> > > {
> > > if( sig.Symbol == openpos.Symbol) bInTrade = True;
> > > }
> > > if( bInTrade == False) bo.ExitTrade(bar,openpos.Symbol,Ref
> > > (O,1));
> > > }
> > > 
> > > for( sig = bo.GetFirstSignal( bar ); sig; sig = 
bo.GetNextSignal
> > > ( bar ))
> > > {
> > > bInTrade = False;
> > > // If in trade and not top 5 signals, exit trade
> > > {
> > > for( trd = bo.GetFirstTrade(),Cnt = 0; Cnt++; trd =
> > > bo.GetNextTrade(), Cnt < numStocks )
> > > {
> > > if( trd.Symbol == sig.Symbol ) bInTrade = True;
> > > }
> > > if( bInTrade == False) bo.EnterTrade(bar, trd.Symbol, True,
> > > C, -10);
> > > }
> > > }
> > > 
> > > bo.ProcessTradeSignals( bar );
> > > }
> > > bo.PostProcess();
> > > }
> > > 
> > > numStocks = 5;
> > > SetOption("MaxOpenPositions", numStocks );
> > > PositionSize = -numstocks;
> > > PositionScore= 100-RSI();
> > > Buy = 1;
> > > Sell = Short = Cover = 0;
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > Check AmiBroker web page at:
> > > http://www.amibroker.com/
> > > 
> > > Check group FAQ at: 
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
> > > Yahoo! Groups Links
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > >





------------------------ Yahoo! Groups Sponsor --------------------~--> 
In low income neighborhoods, 84% do not own computers.
At Network for Good, help bridge the Digital Divide!
http://us.click.yahoo.com/EpW3eD/3MnJAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/