Wednesday, October 2, 2013

Moving Average Crossover Buy and Sell Signal Amibroker AFL

Moving Average Crossovers are a popular style of trading in Intraday trading.
The below Amibroker AFL for Moving Average Crossover is a 5 SMA crossing the
9 SMA. The Buy or Sell Arrows are shown on the closing candle at the crossover point.
Entries made on basis of Moving Average Crossover may turn out to be loss making
in sideways markets. So Trend should always be taken into account when such
crossover trades are being initiated.

When the Low Moving Average crosses the Higher Moving Average [Above or Below]
the trade is initiated in the direction of the crossover. That is if 5 SMA crosses 9 SMA
from above then a sell trade is to be initiated and when 5 SMA crosses the 9 SMA from below
then Long trade has to be initiated.

For reference an image has been posted below the post.
===========================Copy The Code Below============================

_SECTION_BEGIN("SMA5X9");

MA5 = MA(C, 5);
MA9 = MA(C, 9);

Buy = Cross(MA5, MA9);
Sell = Cross(MA9, MA5);

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

trigger = WriteIf(Buy, "BUY", WriteIf(Sell, "SELL", " "));
fg = IIf(Buy, colorDarkGreen, IIf(Sell, colorRed, colorDefault));
bg = IIf(Buy, colorPaleGreen, IIf(Sell, colorPink, colorDefault));


_N(Title = StrFormat("{{NAME}} {{DATE}} {{INTERVAL}} : O=%1.2f, H=%1.2f, L=%1.2f, C=%1.2f, Vol=%1.0f\n{{VALUES}}", O, H, L, C, V));

if(Status("action") == actionIndicator)
    {
        PlotOHLC(O, H, L, C, "", colorGrey40, styleCandle);
        Plot(MA5, "MA(Close, 5)", colorSeaGreen, styleLine);
        Plot(MA9, "MA(Close, 9)", colorRed, styleLine);
        PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-20);
        PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-10);
        PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15);
        PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
        PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=10);
        PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-15);

    }

if(Status("action") == actionExplore)
    {
        SetOption("NodefaultColumns", True);

        AddTextColumn(Name(), "Symbol", 77, colorDefault, colorDefault, 120);
        AddColumn(DateTime(), "Date", formatDateTime, colorDefault, colorDefault, 96);
        AddTextColumn(trigger, "Signal", 77, fg, bg);
        AddColumn(C, "C. M. P.", 1.2, fg, colorDefault, 96);
    }



_SECTION_END();

=====================================================================
                                                     CLICK TO ENLARGE

2 comments:

  1. अच्छा ब्लॉग है.helping info. मै मॅन्युअली अल्गो करता हु. ये सब बङा मुश्किल लगता है.

    ReplyDelete
    Replies
    1. You should try using Amibroker Bridge for Algo trades.

      Delete

Required US Government Disclaimer & CTFC Rule 4.41

Futures trading contains substantial risk and is not suitable for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only consider risk capital that should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. CTFC RULE 4.41 – HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN LIMITATIONS. UNLIKE AN ACTUAL PERFORMANCE RECORD, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES HAVE NOT BEEN EXECUTED, THE RESULTS MAY HAVE UNDER-OR-OVER COMPENSATED FOR THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS SUCH AS LIQUIDITY. SIMULATED TRADING PROGRAMS IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFIT OR LOSSES SIMILAR TO THOSE SHOWN. All trades, patterns, charts, systems, etc., discussed in this website or advertisement are for illustrative purposes only and not construed as specific advisory recommendations. All ideas and materials presented herein are for information and educational purposes only. No system or trading methodology has ever been developed that can guarantee profits or prevent losses. The testimonials and examples used herein are exceptional results which do not apply to average people and are not intended to represent or guarantee that anyone will achieve the same or similar results. Trades placed on the reliance of Trend Methods systems are taken at your own risk for your own account. This is not an offer to buy or sell futures interests.