Overview
This Pine Script strategy is designed for trading Bitcoin (BTC) by placing pending orders (BuyStop and SellStop) based on local price extremes. The script also implements a trailing stop mechanism to protect profits once a position becomes sufficiently profitable.
________________________________________
Inputs and Parameter Setup
1. Trading Profile:
o The strategy is set up specifically for BTC trading.
o The systemType input is set to 1, which means the strategy will calculate trade parameters using the BTC-specific inputs.
2. Common Trading Inputs:
o Risk Parameters: Although RiskPercent is defined, its actual use (e.g., for position sizing) isn’t implemented in this version.
o Trading Hours Filter:
SHInput and EHInput let you restrict trading to a specific hour range. If these are set (non-zero), orders will only be placed during the allowed hours.
3. BTC-Specific Inputs:
o Take Profit (TP) and Stop Loss (SL) Percentages:
TPasPctBTC and SLasPctBTC are used to determine the TP and SL levels as a percentage of the current price.
o Trailing Stop Parameters:
TSLasPctofTPBTC and TSLTgrasPctofTPBTC determine when and by how much a trailing stop is applied, again as percentages of the TP.
4. Other Parameters:
o BarsN is used to define the window (number of bars) over which the local high and low are calculated.
o OrderDistPoints acts as a buffer to prevent the entry orders from being triggered too early.
________________________________________
Trade Parameter Calculation
• Price Reference:
o The strategy uses the current closing price as the reference for calculations.
• Calculation of TP and SL Levels:
o If the systemType is set to BTC (value 1), then:
Take Profit Points (Tppoints) are calculated by multiplying the current price by TPasPctBTC.
Stop Loss Points (Slpoints) are calculated similarly using SLasPctBTC.
A buffer (OrderDistPoints) is set to half of the take profit points.
Trailing Stop Levels:
TslPoints is calculated as a fraction of the TP (using TSLTgrasPctofTPBTC).
TslTriggerPoints is similarly determined, which sets the profit level at which the trailing stop will start to activate.
________________________________________
Time Filtering
• Session Control:
o The current hour is compared against SHInput (start hour) and EHInput (end hour).
o If the current time falls outside the allowed window, the script will not place any new orders.
________________________________________
Entry Orders
• Local Price Extremes:
o The strategy calculates a local high and local low using a window of BarsN * 2 + 1 bars.
• Placing Stop Orders:
o BuyStop Order:
A long entry is triggered if the current price is less than the local high minus the order distance buffer.
The BuyStop order is set to trigger at the level of the local high.
o SellStop Order:
A short entry is triggered if the current price is greater than the local low plus the order distance buffer.
The SellStop order is set to trigger at the level of the local low.
Note: Orders are only placed if there is no current open position and if the session conditions are met.
________________________________________
Trailing Stop Logic
Once a position is open, the strategy monitors profit levels to protect gains:
• For Long Positions:
o The script calculates the profit as the difference between the current price and the average entry price.
o If this profit exceeds the TslTriggerPoints threshold, a trailing stop is applied by placing an exit order.
o The stop price is set at a distance below the current price, while a limit (profit target) is also defined.
• For Short Positions:
o The profit is calculated as the difference between the average entry price and the current price.
o A similar trailing stop exit is applied if the profit exceeds the trigger threshold.
________________________________________
Summary
In essence, this strategy works by:
• Defining entry levels based on recent local highs and lows.
• Placing pending stop orders to enter the market when those levels are breached.
• Filtering orders by time, ensuring trades are only taken during specified hours.
• Implementing a trailing stop mechanism to secure profits once the trade moves favorably.
This approach is designed to automate BTC trading based on price action and dynamic risk management, although further enhancements (like dynamic position sizing based on RiskPercent) could be added for a more complete risk management system.