0
votes

Im trying modificate my simple script which buy position when price crossover 200 MA and sell when price is crossunder 200 EMA.

I wanna add pyramiding: 5 positions max - each position is 20% of the total capital next position when price is 2% higher than previous long Sell all positions when price crossunde 200 EMA

@version=3
strategy("EMA200", shorttitle="EMA200", overlay=true, initial_capital=10000, pyramiding=1)

EMA=input(200, minval=1)
s=ema(close,EMA) // sma values stored in s1 and s2 variables
plot(s)
buy= close>s
sell= close<s
addToLong = (strategy.position_size > 0) and strategy.position_avg_price +
               strategy.position_avg_price*0.02 //close > high[1]
ordersize=floor(strategy.equity/close) // To dynamically calculate the order size as the account equity increases or decreases.
strategy.entry("long",strategy.long,ordersize,when=buy) // Buys when buy condition met
strategy.order(id="Additional Long", when = addToLong, long=true)
strategy.close("long", when = sell ) // Closes position when sell condition me 
1
when price crossover 200 MA do you mean 200 EMA? - Michel_T.

1 Answers

0
votes
//@version=3
strategy("EMA200", shorttitle="EMA200", overlay=true, initial_capital=10000, pyramiding=5, default_qty_type=strategy.percent_of_equity, default_qty_value=20)

EMA=input(200, minval=1)
s=ema(close,EMA) // sma values stored in s1 and s2 variables
plot(s)
sell= crossunder(close, s)
buy= (strategy.position_size > 0) ? (strategy.position_avg_price*1.02 < close) : crossover(close, s)

strategy.entry("long",strategy.long,when=buy) // Buys when buy condition met
strategy.close("long", when = sell ) // Closes position when sell condition me

I suppose that is similar to what you want.