Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Scale-In Positions Explained – Amibroker AFL Code

1 min read

If in case you want to add to your existing open positions to your systematic trading then assign sigScaleIn to the BUY/SHORT variable if you want to scale-in (increase the size of) LONG/SHORT position.

sigScaleIn is a special value assign to BUY/SHORT value to add to the existing positions. Here is a prototype AFL code that performs automated scale-in when the buy signal continues and also 39-EMA hook reversal happens.

What is 39-EMA hook reversal?

Is is a price zone where the previous candle breaks down below the 39-EMA followed by the candle which closes above 39EMA

buycontinue = Flip(Buy,Sell);

//Scalein Conditions
pyramid = buycontinue AND Ref(C,-1) < EMA(Close,39) AND Close > EMA(Close,39);

Position Sizing

Position Sizing needs to be done accordingly. If Buy = True then regular positionsize should happen For Example Purchasing 2,00,000 worth of shares and during scalein 1,00,000 worth of shares will be added to the existing position.

pyramidposition = IIf(Buy == sigScaleIn , 10000, 20000);

SetPositionSize( pyramidposition, spsValue);

Here is the complete Amibroker AFL Code

_SECTION_BEGIN("Scalein Positions at Hooked 39-EMA Reversals");

//Candle and Axis Plot

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

//Buy and Sell without pyramiding

Buy = Cross(EMA(Close,20), EMA(Close,50));
Sell = Cross(EMA(Close,50), EMA(Close,20));

buycontinue = Flip(Buy,Sell);

//Scalein Conditions
pyramid = buycontinue AND Ref(C,-1) < EMA(Close,39) AND Close > EMA(Close,39);


Plot(EMA(Close,20),"EMA20",colorRed,styleLine);
Plot(EMA(Close,50),"EMA50",colorblue,styleLine);
Plot(EMA(Close,39),"EMA39",coloryellow,styleLine);


/* 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);


PlotShapes(IIf(pyramid, shapeCircle, shapeNone),colorGreen, 0, L, Offset=-40);


SetTradeDelays(0,0,0,0);

//Modified Buy Rules with ScaleIn at 39-EMA Hook Reversals

Buy = Buy + pyramid*sigScaleIn;


BuyPrice = Close;
SellPrice = Close;


pyramidposition = IIf(Buy == sigScaleIn , 10000, 20000);

SetPositionSize( pyramidposition, spsValue);


_SECTION_END();

References

Pyramiding (scaling in/out) and multiple currencies in the portfolio backtester

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Line Trading – OpenAlgo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...
Rajandran R
55 sec read

Mini Certification Course on Algorithmic Trading

Welcome to the Mini Certification on Algorithmic Trading using Amibroker and OpenAlgo! This comprehensive 4-part series is designed to equip you with the knowledge...
Rajandran R
1 min read

Pivot Reversal Strategy – Tradingview Pinescript to Amibroker AFL…

Pivot Reversal Strategy is a popular in-built strategy in Tradingview Platform. Here I had attempted to convert the pinescript v5 to Amibroker AFL Code....
Rajandran R
3 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More