I have been running a backtest which has a filter for EMA. I have had assistance with this backtest about a month ago.
The code currently is:
Code:MaxPositions = 4; SetOption("MaxOpenPositions", MaxPositions ); SetOption("WorstRankHeld", MaxPositions + 2 ); SetPositionSize( 100 / MaxPositions, spsPercentOfEquity ); // trade on next day open BuyPrice = Open; SetTradeDelays( 1, 1, 1, 1 ); SetBacktestMode( backtestRotational ); SetForeign( "XAO" ); FastMALength = Optimize("FMA",62,30,70,2); SlowMALength = Optimize("SMA",182,180,200,2); Filter1 = EMA( C, FastMALength ) > EMA( C, SlowMALength ); 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 > 350000; // 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(); }
The issue I am having is the EMA 62 is below EMA 182 on "XAO" and has been throughout December. The backtest has however entered into trades for 1 or two days, firstly entering 3 trades on 1 December [closing 2 December] and then entering another trade on 8 December before closing the next day.
Very strange - I note that both sets of trades have occurred on the Monday... possibly a coincidence, but....
Does anyone have an idea where this is messing up...
In the Automatic Analysis settings window under the "general" tab is the option to "pad and align all data to reference symbol". Tick this box and enter XAO as the reference symbol.
Hello,
Auto-Trading --- you either love it or hate it. Well, I want to go there. I have not been able to find anyone that offers the needed software for connecting AB to IB. Might any of the posters here know where to direct my attention?
FWIW, my existing code produces all of the orders and position sizes, etc.....just need the next step.
TIA,
RutheezKey
Anyone know how to do the following:
If i have price pane and two indicator panes on a sheet, how can I draw a vertical line through the three panes (that stays there)? i.e. from top of sheet to bottom of sheet.
Use static variables and mouse and/or keyboard functions and gfx or plot. If code is finished it will draw 3 lines in three panes at the same time and same position if you click on price chart, for example.
Thanks for the reply trash.
I am not quite sophisticated enough with Amibroker to fully understand your answer but will see what I can learn.
Do you know if it is possible to modify a drawing tool. For example would it be possible to modify the vertical line tool to change it from just acting on the pane to instead act on the sheet?
Yes, you can store the studyID to staticvarset using study() function. then you call that study in other chart pane via staticvarget. In addition in that other pane(s) of same sheet you would have to add additional plot code to plot that receiving study (vertical line study being save to static variable)) from master pane (i.e. price chart pane).
It just sounds complicated but it isn't.
BTW the selector line is shown on all chart panes already if you click on a bar. Why do you wanna have an additional vertical line?
SetOption ( "maxopenpositions", mp = 20 );
index = "XAO"; // replace with your index symbol
SetForeign ( index );
{
TimeFrameSet ( inWeekly );
{
Average = MA ( C, 10 );
buysignal = average > Ref ( average, - 1 );
buysignal1 = average > Ref ( average, - 1 ); // Buy Condition #1
uptrend = close > average;
RestorePriceArrays();
periodHigh = HHV ( Close, 20 );
WeeklyClose = close;
chg = ROC ( Close, 20 ) ;
buysignal2 = Close > Ref ( periodhigh, -1 ); // Buy Condition #2
buysignal3 = chg > 30 ; // Buy Condition #3
buysignal4= uptrend;
endofweek = 1;
}
TimeFrameRestore ();
buysignal = TimeFrameExpand ( buysignal , inWeekly, expandpoint );
uptrend = TimeFrameExpand ( uptrend , inWeekly );
endofweek = TimeFrameExpand ( endofweek , inWeekly, expandpoint ) ;
firstdayofweek = Ref ( endofweek, -1 );
}
Buy= buysignal1 AND buysignal2 AND buysignal3;
//Trailing Stop Information
Buy = Sell = Short = Cover = 0;
position = ExitTrade = EnterTrade = 0;
stopArray = Null ;
TrailingDistance = IIf ( uptrend , 40 , 10 ) / 100 * Close;
//*************************************
pricecolor = colorDefault;
dow = DayOfWeek();
dt = DateTime();
dtsignal = "";
for ( i = 0 ; i < BarCount ; i ++ )
{
if ( ExitTrade )
{
if ( firstdayofweek [i] ) // exit on monday
{
Sell [i] = 1;
SellPrice [i] = Open[i] ;
position = 0;
ExitTrade = 0 ;
}
}
if ( position )
{
if ( endofweek [i] )
{
if ( WeeklyClose [i ] < stop )
{
ExitTrade = 1;
pricecolor[i] = colorred;
dtsignal = DateTimeToStr ( dt[i] );
}
}
if ( ExitTrade == 0 )
{
stop = max ( stop , Close [i] - TrailingDistance [i] ) ; // adjust stop
}
}
if ( EnterTrade )
{
if ( firstdayofweek [i] )
{
Buy [i] = 1;
BuyPrice[i] = Open[i];
stop = buyprice [i] * ( 1 - 49 / 100 ) ; // initial stop
position = 1;
EnterTrade = 0;
}
}
if ( !position )
{
if ( buysignal [i] )
{
//printf ( "\n" + dow[i] );
EnterTrade = 1;
pricecolor[i] = colorBrightGreen;
dtsignal = DateTimeToStr ( dt[i] );
}
}
if ( position )
StopArray [i] = stop ;
}
//*****************************************
Plot ( C, "", colorDefault , stylebar );
shape = Buy *shapeUpArrow;
//Plot ( stopArray, "", colorRed , stylestaircase );
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );
//PlotShapes (Buy,colorGreen, 0 , Low);
//PlotShapes ( sell shapedownArrow, colorred, 0 , high );
ribbonColor = IIf( uptrend , colorGreen, colorRed );
Plot( 2, "", ribbonColor , styleOwnScale | styleArea | styleNoLabel, -0.5, 100 );
GraphXSpace = 10 ;
SetChartOptions( 0 , chartShowDates | chartShowArrows );
Hello,
Does anyone know how to access the properties of a study through AFL? I would like to draw a bunch of trend lines and then get their start date & price and end date & price exported to a .txt file.
This thread demonstrates how to access a hand-drawn trend line but there doesn't seem to be any way of directly getting the start and end date & price.
Hello and welcome to Aussie Stock Forums!
To gain full access you must register. Registration is free and takes only a few seconds to complete.
Already a member? Log in here.