'Pause strategy execution when profit curve crosses under it ma

I have a strategy in pine-script. When the equity curve crosses under the sma(equity, 10), I want to pause trading (no more alerts/orders sent). However, I still need to keep track of what the equity curve would have looked like, so that can see when there is crossover again.

I am uncertain on how to implement this 'meta'-strategy and would appreciate any advice. Maybe there are tricks to implement this easily. The concept is called equity curve filtering.

Update To clarify: During my strategy creation in pine script I want to do dynamic position sizing, depending on the equity curve compared to its moving average.

This means I need to keep track of the imaginary behaviour of the strategy if it were to run as 100% orders.

I have tried to approximate the behaviour like this. Although I am not certain I did it right.

Update:

ordersize=(strategy.equity/close)
float lastTradeProfit = na

% if a strategy change is detected: 
if strategy.position_size != strategy.position_size[1]
    % save the change in profit, but correct for the fact that only half of the capital was used
    lastTradeProfit := strategy.netprofit - strategy.netprofit[1]
    if (lastTradeProfit[2] < sma(lastTradeProfit, 21)[1])
        lastTradeProfit := lastTradeProfit * 2

% set the current order position
if (lastTradeProfit[1] < sma(lastTradeProfit, 20)[1])
    ordersize := 0.5 * ordersize

I feel like this is close, but it would work better if I can access the % change in order.



Solution 1:[1]

Give me your code, I can do that. Else you can write

YourMa = close<ma(X, Y)
If (Your Condition) and YourMa
      (Your Strategy)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 m4n0