Long short enable disable strategy

This strategy teaches how to create flags (boolean fields) to tell your strategy open long or/and short trades. In the settings, you will see only two fields that define which type of trades to create. In case there is only one trade selected the strategy will close the previous position that should be in opposite direction.

// @version=5
strategy('Long short enable disable strategy', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=25, commission_type=strategy.commission.percent, commission_value=0.04)

// define two boolean fields to determine which entries to create
isLongEnabled = input(title="Enable long", defval=true, tooltip="Enable long trades")
isShortEnabled = input(title="Enable short", defval=true, tooltip="Enable short trades")


// *******************************************************************************************
// Strategy logic simulation
// *******************************************************************************************
// this logic is only to simulate long and short positions
// just to generate strategy signals as an example
rsiEmulationData = ta.rsi(close, 7)
rsiEmulationCrossover = ta.crossover(rsiEmulationData, 70) // go long condition
rsiEmulationCrossunder = ta.crossunder(rsiEmulationData, 30) // go short condition


// entry loogic based on the rsi calculations
if isLongEnabled and rsiEmulationCrossover
    // go long if trades are enabled and the condition is met
    // the condition - rsi is crossed over the 70 line (see above)
    strategy.entry("Long", strategy.long)
if isShortEnabled and rsiEmulationCrossunder
    // go short if trades are enabled and the condition is met
    // the condition - rsi is crossed below the 30 line (see above)
    strategy.entry("Short", strategy.short)

// exit positions if opposite signal is generated
// but the opposite entry is disabled
if not isLongEnabled and rsiEmulationCrossover
    // if the short is opened but the signal is long
    // we have to close the short position as the long entries are disabled
    strategy.close("Short")
if not isShortEnabled and rsiEmulationCrossunder
    // if the long is opened but the signal is short
    // we have to close the long position as the short entries are disabled
    strategy.close("Long")
Made on
Tilda