/******************************************************************* * Moving average crossover strategy. ********************************************************************/ /******************************************************************* * Please read algotrader-util.afl file to understand all functions * provided by AutoTrader. But DO NOT modify the file, if you are * not familiar with AFL coding. * * NOTE: It is recommended to start with a blank chart. ******************************************************************** * Following variables are declared for strategy parameters in * algotrader-util.afl. ******************************************************************** * * AT_SYMBOL : Order symbol * AT_QUANTITY : Order quantity (lot size) * AT_EXCHANGE : Order exchange * AT_INSTRUMENT : Order instrument * AT_PRODUCT_TYPE : Order product type * AT_ORDER_TYPE : Order order type * AT_EXPIRY : Order expiry (expiry date (Example: 28-DEC-2017), pass NA if not applicable) * AT_OPTION_TYPE : Order option type * AT_STRIKE_PRICE : Order strike price * AT_SQUARE_OFF_FLAG: Intraday Auto Square-off (on|off) * AT_SQUARE_OFF_TIME: Intraday Square-off Time * ********************************************************************/ /******************************************************************* * How to see strategy logs in AMIBroker? * 1. Enable log window from amibroker menu (Window -> Log) * 2. You will see log window at the bottom. * 3. Go to "Trace" tab (all strategy logs are printed here). ********************************************************************/ /******************************************************************** * SECTION 2 - BEGIN * This section contains your strategy afl code. ********************************************************************/ // Moving Average SHORT_MA_PERIOD = Param("Short term moving average period", 9, 1, 200, 2); LONG_MA_PERIOD = Param("Long term moving average period", 20, 1, 200, 2); _SECTION_BEGIN("AutoTrader Demo 1 - Moving Average Crossover"); /* Exponential Moving averages */ EMA1 = EMA( C, SHORT_MA_PERIOD); EMA2 = EMA( C, LONG_MA_PERIOD); /* Buy and Sell Condition */ Buy = Cross(EMA1,EMA2); Sell = Cross(EMA2,EMA1); /* Remove excess signals */ Buy = ExRem(Buy,Sell); Sell = ExRem(Sell,Buy); /* Prices when Buy, Sell, Short, Cover condition is true */ //buyPrice = ValueWhen(Buy,C); //sellPrice = ValueWhen(Sell,C); /* We use live prices here, as ValueWhen function gives negative price sometimes */ buyPrice = LastValue(C); sellPrice = LastValue(C); /* Plot Buy and Sell Signal Arrows */ shape = Buy * shapeUpArrow + Sell * shapeDownArrow; PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) ); GraphXSpace = 5; /* Plot EMA lines and Candles */ Plot(EMA1,"EMA", colorRed); Plot(EMA2,"EMA2", colorGreen); Plot(C, "Close", colorRed, styleCandle); /******************************************************************** * SECTION 2 - END ********************************************************************/ /******************************************************************** * SECTION 3 - BEGIN * This section contains your code for placing orders. ********************************************************************/ if ( LastValue(Buy) == True ) { placeOrderUsingParams("BUY", AT_ORDER_TYPE, AT_QUANTITY, buyPrice, defaultTriggerPrice(), 1); } if ( LastValue(Sell) == True ) { placeOrderUsingParams("SELL", AT_ORDER_TYPE, AT_QUANTITY, sellPrice, defaultTriggerPrice(), 1); } /******************************************************************** * SECTION 3 - END ********************************************************************/ _SECTION_END();