Australian (ASX) Stock Market Forum

Amibroker coding

I am struggling to see to what you want from the system.

If I understand what you want correctly I would run a backtest for each First Day of Week setting and put it in spreadsheet as per the below snip, then compare which day works best.

Then run exploration for the system with the preferred day selected each week.

You could probably do the below in a custom backtest format but I am struggling to understand why when the below can be done in 5 minutes. Saying that I know the CBT is pretty powerful and would like to dabble more with it.

If this is what you want you would probably need to know the setoption command for the First Day of Week setting, then step it through....so is this the desired outcome ? if so I will have a look around as I do not know the answer but always keen to learn.


upload_2019-11-28_11-14-43.png
 
@qldfrog I have been thinking about this and I think that we have 1 option for you to try.

It may not be the complete answer for you but will help us dig a little deeper into this and maybe one of the experts might chime in :cool:

Ok so the code below is pretty simple.

1. Strategy MA cross over direct from AmiBroker files
2. Sets time frame to weekly so we can use the weekly close array
3. Restores time frame to normal daily mode as we are using EOD data
4. Buy / Sell lines use the weekly close data but decompressed on the day that we run the backtest (in daily mode)
5. Some basic code just for the looks
6. Plot - Weekly MA plotted on Daily chart for visual checking of cross over


Code:
PositionSize = 10000; // Fixed value for each position in BackTesting
Quantity = PositionSize / BuyPrice; // Quantity of shares to purchase

/* Time Frame Change:--------------------------------------------------------------*/
// Here we set the time frame to weekly and use the weekly close price for the moving average calculation
// You would put you code in here to get weekly array data

TimeFrameSet(inWeekly);
maW = MA( Close, 20 );     // simple moving average using weekly close
CW = C;                    // Close in weekly
TimeFrameRestore(inDaily);

Buy = Cross(Close, TimeFrameExpand (maW, inWeekly, expandLast)); // Buys in Daily backtest mode but using MA from weekly Time Frame to detect cross
Sell = Cross(TimeFrameExpand (maW, inWeekly, expandLast), Close); // Sells in Daily backtest mode but using MA from weekly Time Frame to detect cross

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

/* Exploration: --------------------------------------------------------------*/

Filter = Buy OR Sell AND StrLen( Name () ) == 3;
SetSortColumns( 2 );
AddColumn( IIf(Buy, 'B', IIf( Sell, 'S', 'N') ), "Signal", formatchar, IIf( Buy, colorGreen, IIf( Sell, colorRed, colorBlue)) );
AddColumn( IIf(Buy, PositionSize, Null), "Position Size", 1);
AddColumn( IIf(Buy, Quantity, Null), "Qty", 1);

/* Charting: --------------------------------------------------------------*/

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open= %g High= %g Low= %g Close= %g (%.1f%%) Volume= " + WriteVal( V/1, 1.0 ) +
"\n"+"{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorPaleTurquoise ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( TimeFrameExpand (maW, inWeekly, expandLast), "maW", colorAqua, styleLine ); // Plots weekly MA on daily chart

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

Some backtest results for comparison.
1. Normal MA system unmodified run in 2 time frame settings
2. Modified Weekly MA system run in 2 time frame settings

You will notice the green shaded areas match up which in my mind proves the weekly data is fine ( not corrupted ) in the compress / expand parts of the program

3. you can look at a chart say westpac and the backtest would have given you an earlier exit which I think you are looking for.

upload_2019-12-3_15-28-35.png

upload_2019-12-3_15-34-55.png

Have a look / play and see what you think. Maybe 1 step forward ??
 
@qldfrog I have been thinking about this and I think that we have 1 option for you to try.

It may not be the complete answer for you but will help us dig a little deeper into this and maybe one of the experts might chime in :cool:

Ok so the code below is pretty simple.

1. Strategy MA cross over direct from AmiBroker files
2. Sets time frame to weekly so we can use the weekly close array
3. Restores time frame to normal daily mode as we are using EOD data
4. Buy / Sell lines use the weekly close data but decompressed on the day that we run the backtest (in daily mode)
5. Some basic code just for the looks
6. Plot - Weekly MA plotted on Daily chart for visual checking of cross over


Code:
PositionSize = 10000; // Fixed value for each position in BackTesting
Quantity = PositionSize / BuyPrice; // Quantity of shares to purchase

/* Time Frame Change:--------------------------------------------------------------*/
// Here we set the time frame to weekly and use the weekly close price for the moving average calculation
// You would put you code in here to get weekly array data

TimeFrameSet(inWeekly);
maW = MA( Close, 20 );     // simple moving average using weekly close
CW = C;                    // Close in weekly
TimeFrameRestore(inDaily);

Buy = Cross(Close, TimeFrameExpand (maW, inWeekly, expandLast)); // Buys in Daily backtest mode but using MA from weekly Time Frame to detect cross
Sell = Cross(TimeFrameExpand (maW, inWeekly, expandLast), Close); // Sells in Daily backtest mode but using MA from weekly Time Frame to detect cross

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

/* Exploration: --------------------------------------------------------------*/

Filter = Buy OR Sell AND StrLen( Name () ) == 3;
SetSortColumns( 2 );
AddColumn( IIf(Buy, 'B', IIf( Sell, 'S', 'N') ), "Signal", formatchar, IIf( Buy, colorGreen, IIf( Sell, colorRed, colorBlue)) );
AddColumn( IIf(Buy, PositionSize, Null), "Position Size", 1);
AddColumn( IIf(Buy, Quantity, Null), "Qty", 1);

/* Charting: --------------------------------------------------------------*/

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open= %g High= %g Low= %g Close= %g (%.1f%%) Volume= " + WriteVal( V/1, 1.0 ) +
"\n"+"{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorPaleTurquoise ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( TimeFrameExpand (maW, inWeekly, expandLast), "maW", colorAqua, styleLine ); // Plots weekly MA on daily chart

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

Some backtest results for comparison.
1. Normal MA system unmodified run in 2 time frame settings
2. Modified Weekly MA system run in 2 time frame settings

You will notice the green shaded areas match up which in my mind proves the weekly data is fine ( not corrupted ) in the compress / expand parts of the program

3. you can look at a chart say westpac and the backtest would have given you an earlier exit which I think you are looking for.

View attachment 98867

View attachment 98868

Have a look / play and see what you think. Maybe 1 step forward ??
Much thanks, sounds very promising.i think it Is what I am trying to achieve.
Doing a short consulting job today and busy yesterday
Hopefully can work on this tomorrow
Much appreciated
 
actually still keen to use the weekly but shifted, but the same principle should work I can compress uncompress with the shift..i think..will try to work more on this,I believe that compress uncompress is the key indeed
you lead me in the right direction
 
Top