Is it possible to attach a stop loss to a moving average ?
Mmmm maybe you need to define more what is in your mind?
A Stop Loss is attached an Open Trade and therefore attached to Price of that Open Trade, which – by chance – could be the same as the value of a Moving Average … if your entry was, for example …
If Close = Average[20](Close) Then
Buy at Market
Endif
But to achieve an entry exact same as the value of MA[20] would be unusual / luck. Chances would be best if running on a 1 second (or less) Chart Timeframe.
You can use the SET STOP PRICE instruction which put the stoploss order on a price value.
At order ignition:
MA = average[20]
if buyCondition then
buy at market
set stop price MA
endif
during the lifetime of the order: (stoploss will follow strictly the MA value)
if longonmarket then
set stop price MA
endif