0

I have written my first pine script, the situation is:

  • when the RSI is below 30 --> buy
  • when the RSI is above 70 --> sell

The Problem is, that I correctly get into my buy position, but there is no sell position.

Code:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © debonairToucan74454

//@version=5
strategy("70/30", overlay = true, initial_capital = 4000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity)

longCondition = ta.crossunder(ta.rsi(close, 14), 30)
sellCondition = ta.crossover(ta.rsi(close, 14), 70)

timePeriod = time >= timestamp(syminfo.timezone, 2022, 11, 1, 0, 0)

if (longCondition and strategy.position_size == 0 and timePeriod)
    strategy.entry("Entry", strategy.long)

if(sellCondition and strategy.position_size == 1 and timePeriod)
    strategy.close("Exit")

1 Answers1

0

First - strategy.position_size is a series float and not series bool. It is the actual size of the position.

Second - strategy.close() first parameter is the entry id (not exit id).

You should try:

if(sellCondition and strategy.position_size > 0 and timePeriod)
    strategy.close("Entry")
mr_statler
  • 1,913
  • 2
  • 3
  • 14