OPEN-SOURCE SCRIPT

MA Crossover with Demand/Supply Zones + Stop Loss/Take Profit

Stop Loss and Take Profit Inputs:

Added stopLossPerc and takeProfitPerc as inputs to allow the user to define the stop loss and take profit levels as a percentage of the entry price.

Stop Loss and Take Profit Calculation:

For long positions, the stop loss is calculated as strategy.position_avg_price * (1 - stopLossPerc), and the take profit is calculated as strategy.position_avg_price * (1 + takeProfitPerc).

For short positions, the stop loss is calculated as strategy.position_avg_price * (1 + stopLossPerc), and the take profit is calculated as strategy.position_avg_price * (1 - takeProfitPerc).

Exit Strategy:

Added strategy.exit to define the stop loss and take profit levels for each trade. The from_entry parameter ensures that the exit is tied to the specific entry order.

Flexibility:

The stop loss and take profit levels are dynamic and adjust based on the entry price of the trade.

How It Works:
When a buy signal is generated (MA crossover near a demand zone), the strategy enters a long position and sets a stop loss and take profit level based on the input percentages.

When a sell signal is generated (MA crossunder near a supply zone), the strategy enters a short position and sets a stop loss and take profit level based on the input percentages.

The trade will exit automatically if either the stop loss or take profit level is hit.

Example:
If the entry price for a long position is $100, and the stop loss is set to 1% while the take profit is set to 2%:

Stop loss level =
100

(
1

0.01
)
=
100∗(1−0.01)=99

Take profit level =
100

(
1
+
0.02
)
=
100∗(1+0.02)=102

Notes:
You can adjust the stopLossPerc and takeProfitPerc inputs to suit your risk management preferences.

Always backtest the strategy to ensure the stop loss and take profit levels are appropriate for your trading instrument and timeframe.

免責聲明