I have this Pine strategy to open Long position when MA cross, and close position only if close price is higher than open order and also I put condition to close only if profit is more than 2% (((price/close)-1)*(-1)*100)>2
Strategy works fine, orders are closed only in profit but issue is that many trades I am missing because in general due to my strategy each new Long starts in higher price then previos trade, So when price drops, no other Long is opened. I think this is job for pyramiding to open more orders, but how to do it ? Lets say if price drops 5% from last opened Long price, then new Long order should be opened, when new buy signal is coming (crossover(outShort,outLong)
// Strategy functions
var price = 0.0
if (crossover(outShort,outLong)) and afterStartDate and strategy.position_size == 0 and close < 59000
price := close
strategy.entry(id="Long", long=strategy.long)
if (crossunder(outShort,outLong)) and afterStartDate and ( price < close ) and (((price/close)-1)*(-1)*100)>2
strategy.close(id="Long")
Here is my full simple code. By default strategy start in 1st May
//@version=4
strategy(title="My_MA_strategy", shorttitle="MyMA Strategy_0.04", format=format.price, initial_capital=1000, overlay=true)
startDate = input(title="Start Date", type=input.integer,
defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
defval=5, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
defval=2021, minval=1800, maxval=2100)
afterStartDate = (time >= timestamp(syminfo.timezone,
startYear, startMonth, startDate, 0, 0))
// Inputs
i_oversold = input(30, title="Oversold")
i_overbought = input(70, title="Overbought")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
lenShort = input(10, minval=1, title="LengthShort")
srcShort = input(close, title="SourceShort")
offsetShort = input(title="OffsetShort", type=input.integer, defval=0, minval=-500, maxval=500)
outShort = sma(srcShort, lenShort)
plot(outShort, color=color.green, title="MA_Short", offset=offsetShort)
lenLong = input(19, minval=1, title="LengthLong")
srcLong = input(close, title="SourceLong")
offsetLong = input(title="OffsetLong", type=input.integer, defval=0, minval=-500, maxval=500)
outLong = sma(srcLong, lenLong)
plot(outLong, color=color.red, title="MA_Long", offset=offsetLong)
// Strategy functions
var price = 0.0
if (crossover(outShort,outLong)) and afterStartDate and strategy.position_size == 0
price := close
strategy.entry(id="Long", long=strategy.long)
if (crossunder(outShort,outLong)) and afterStartDate and ( price < close )
strategy.close(id="Long")