Okay I have modified my code using the XAO as the filter code.
Code:MaxPositions = 4; SetOption("MaxOpenPositions", MaxPositions ); SetOption("WorstRankHeld", MaxPositions + 2 ); SetPositionSize( 100 / MaxPositions, spsPercentOfEquity ); // trade on next day open SetTradeDelays( 1, 1, 1, 1 ); BuyPrice = Open; SetBacktestMode( backtestRotational ); // offsetting by large positive number // makes sure that our score is always positive and we don't enter short trades SetForeign("XAO"); Filter = Cross( EMA(C,180), EMA(C,50)); RestorePriceArrays(); PositionScore = Iif( Filter, 10000 - ROC( C, 252 ), 0 );
The results are not as I have expected.
It has made a correct entry point [when condition TRUE on Filter = Cross( EMA(C,180), EMA(C,50));]
but closes the position unexpectedly [no negative crossover FALSE on Filter = Cross( EMA(C,180), EMA(C,50));] a few days later,
then makes a new entry a month or so later - at a time when no positive cross [TRUE] has occurred and then closes again without a negative crossover.
Hopefully what I have explained is somewhat clear.
MaxPositions = 4;
SetOption("MaxOpenPositions", MaxPositions );
SetOption("WorstRankHeld", MaxPositions + 2 );
SetPositionSize( 100 / MaxPositions, spsPercentOfEquity );
// trade on next day open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = Open;
SetBacktestMode( backtestRotational );
SetForeign( "XAO" );
Filter = EMA( C, 50 ) > EMA( C, 180 );
RestorePriceArrays();
// offsetting by large positive number
// makes sure that our score is always positive and we don't enter short trades
PositionScore = Iif( Filter, 10000 - ROC( C, 252 ), 0 );
StaticVarSet( Name() + "Score", 10000 - ROC( C, 252 ) );
SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.backtest( 1 );
// closed trades
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{ // two additional columns
trade.addcustomMetric( "Score@entry", Lookup( StaticVarget( trade.symbol + "Score" ), trade.EntryDateTime ) );
trade.addcustomMetric( "Score@exit", Lookup( StaticVarget( trade.symbol + "Score" ), trade.ExitDateTime ) );
}
// open trades
for ( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() )
{ // two additional columns
trade.addcustomMetric( "Score@entry", Lookup( StaticVarget( trade.symbol + "Score" ), trade.EntryDateTime ) );
trade.addcustomMetric( "Score@exit", Lookup( StaticVarget( trade.symbol + "Score" ), trade.ExitDateTime ) );
}
bo.listTrades();
}
Your Filter is wrong
Thanks for the heads up Captain Black. I am always on the look out for Amibroker tips. Some good articles there.
Regards,
Tradezy
Do you have any suggestions as to where I can learn the real basics of this language? Something like "Looping for Dummies" would be great! I guess it is like learning the alphabet before being able to spell.
Hi Tradezy --
There are examples in my "Introduction to AmiBroker" book. You can download a free copy from the book's website:
http://www.introductiontoamibroker.com/
Best regards,
Howard
Your Filter is wrong
Code:MaxPositions = 4; SetOption("MaxOpenPositions", MaxPositions ); SetOption("WorstRankHeld", MaxPositions + 2 ); SetPositionSize( 100 / MaxPositions, spsPercentOfEquity ); // trade on next day open SetTradeDelays( 1, 1, 1, 1 ); BuyPrice = Open; SetBacktestMode( backtestRotational ); SetForeign( "XAO" ); Filter = EMA( C, 50 ) > EMA( C, 180 ); RestorePriceArrays(); // offsetting by large positive number // makes sure that our score is always positive and we don't enter short trades PositionScore = Iif( Filter, 10000 - ROC( C, 252 ), 0 ); StaticVarSet( Name() + "Score", 10000 - ROC( C, 252 ) ); SetCustomBacktestProc(""); if ( Status( "action" ) == actionPortfolio ) { bo = GetBacktesterObject(); bo.backtest( 1 ); // closed trades for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) { // two additional columns trade.addcustomMetric( "Score@entry", Lookup( StaticVarget( trade.symbol + "Score" ), trade.EntryDateTime ) ); trade.addcustomMetric( "Score@exit", Lookup( StaticVarget( trade.symbol + "Score" ), trade.ExitDateTime ) ); } // open trades for ( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() ) { // two additional columns trade.addcustomMetric( "Score@entry", Lookup( StaticVarget( trade.symbol + "Score" ), trade.EntryDateTime ) ); trade.addcustomMetric( "Score@exit", Lookup( StaticVarget( trade.symbol + "Score" ), trade.ExitDateTime ) ); } bo.listTrades(); }
// This is the original filter I have been using
SetForeign( "XAO" );
Filter = EMA( C, 50 ) > EMA( C, 180 );
RestorePriceArrays();
//This is the additional filter I have introduced to try and filter out low volume stocks
VolA = Volume*C;
MAVol = MA(VolA,15);
Filter = MAVol >100000;
RestorePriceArrays();
I am trying to add a volume filter into the above code by adding the following code:
Code:// This is the original filter I have been using SetForeign( "XAO" ); Filter = EMA( C, 50 ) > EMA( C, 180 ); RestorePriceArrays(); //This is the additional filter I have introduced to try and filter out low volume stocks VolA = Volume*C; MAVol = MA(VolA,15); Filter = MAVol >100000; RestorePriceArrays();
The result of adding the volume code as above seems to eliminate the EMA cross filter..
If someone could assist me in adding a volume filter that will be very much appreciated.
// This is the original filter I have been using
SetForeign( "XAO" );
Filter = EMA( C, 50 ) > EMA( C, 180 );
RestorePriceArrays();
//This is the additional filter I have introduced to try and filter out low volume stocks
VolA = Volume * C;
MAVol = MA( VolA, 15 );
Filter = Filter AND MAVol > 100000;
// This is the original filter I have been using
SetForeign( "XAO" );
Filter1 = EMA( C, 50 ) > EMA( C, 180 );
RestorePriceArrays();
//This is the additional filter I have introduced to try and filter out low volume stocks
VolA = Volume * C;
MAVol = MA( VolA, 15 );
Filter = Filter1 AND MAVol > 100000;
Hi Allan --
I think of Explore as a development tool, Scan as a trading tool.
Hi All,
I want to backtest Long and Short strats independently (on the same symbol), but want to use the "Long and Short" option in the backtester. IOW, instead of testing Long-only and Short-only, I want the backtester to combine each as if they were run separately. I hope this is clear.
TIA,
RutheezKey
p.s. I have read tons of documentation, but they only seem to address when using different symbols.
long = 0; // long backtest true/false
period = 20; // number of averaging periods
m = MA( Close, period ); // simple moving average
if ( long )
{
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
SetFormulaName( Name() + "_Long Backtest" ); // displayed in the backtest result explorer.
}
else
{
Short = Sell;
Cover = Buy;
SetFormulaName( Name() + "_Short Backtest" ); // displayed in the backtest result explorer.
}
period = 20; // number of averaging periods
m = MA( Close, period ); // simple moving average
if ( StrFind( Name(), "_1" ) && groupID() == 253 )
{
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
SetFormulaName( Name() + "_Long Backtest" ); // displayed in the backtest result explorer.
}
if( StrFind( Name(), "_2" ) && groupID() == 253 )
{
Short = Sell;
Cover = Buy;
SetFormulaName( Name() + "_Short Backtest" ); // displayed in the backtest result explorer.
}
or create one or two clones of a symbol (like ~AAPL_1 and ~AAPL_2 ) via addtocomposite
and then you could use individual backtester (not portfolio BT) to run over group 253:
if ( Status( "action" ) == actionScan )
{
atcmode = atcFlagDefaults;
for ( i = 1; i < 3; i++ )//create two clones
{
indexname = "~" + Name() + "_" + i;
AddToComposite( Open, indexname, "O", atcmode );
AddToComposite( High, indexname, "H", atcmode );
AddToComposite( Low, indexname, "L", atcmode );
AddToComposite( Close, indexname, "C", atcmode );
AddToComposite( Volume, indexname, "V", atcmode );
AddToComposite( OI, indexname, "I", atcmode );
}
Buy = 0;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?