Convert TOS Thinkscript 'strategy' to AFL

How would I convert the strategy below to AFL?

input price = close;
input length = 20;
input averageType = AverageType.HULL;

plot Avg = MovingAverage(averageType, price, length);
Avg.AssignValueColor(if Avg > Avg[1] then Color.GREEN else Color.RED);

def crossAbove = Avg crosses above Avg[1];
def crossBelow = Avg crosses below Avg[1];

#===These AUTO settings take the place of BUY_TO_OPEN or CLOSE and are intended to produce an order to enter the trade in the opposite direction after exiting the current position.

AddOrder(OrderType.BUY_AUTO, crossAbove, tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "LE");
AddOrder(OrderType.SELL_AUTO, crossBelow, tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "SE");

Something along these lines...

/// @link http://forum.amibroker.com/t/convert-tos-thinkscript-strategy-to-afl/3820/2
price = Close;
length = 20;

_avg = HMA(price, length);// HULL mov. average

crossAbove = Cross(_avg , Ref(_avg, -1));// TOS: def crossAbove = Avg crosses above Avg[1];
crossBelow = Cross(Ref(_avg, -1), _avg );// TOS: def crossBelow = Avg crosses below Avg[1];

Buy = crossAbove;
Sell = crossBelow;

// Plot HMA
Plot( _avg, "HMA", IIf( _avg > Ref(_avg ,- 1), colorGreen, colorRed), styleLine );
// plot Buy/Sell shapes
PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ), layer = 0, y = _avg, -12 );
// Plot price
Plot( C, "Price", colorDefault, GetPriceStyle() );

// etc.
// Order code depends on broker being used

14

1 Like

Nicely done!

Does one buy or selll order allow for the prior exit of the current position and re-entry in the opposite direction?