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

//@version=5
strategy('Ema Scalp', overlay=true)

// Moving Averages to get some example trades generated

eg1 = ta.ema(close, 100)

plot(eg1,color=color.aqua)

long = close>eg1
short = close<eg1

var countbuy=0

if long
    countbuy:=countbuy+1
if strategy.position_size<0 
    countbuy:=0


var countsell=0

if short
    countsell:=countsell+1
if strategy.position_size>0 
    countsell:=0

////////////////////////////////
// stratgey started

if long and strategy.opentrades==0 and countbuy==1  // open first long when close>ema 
    strategy.entry("enter long", strategy.long)
if short and strategy.opentrades==0 and countsell==1 // open first short when close<ema
    strategy.entry("enter short", strategy.short)

// checking last trade is long or short
is_pos_closed = (strategy.position_size[1] != 0 and strategy.position_size == 0) or ((strategy.position_size[1] * strategy.position_size) < 0)
is_long = strategy.position_size > 0
is_short = strategy.position_size < 0

ptrade_islong=is_pos_closed and is_long[1]
ptrade_isshort=is_pos_closed and is_short[1]

// checking is last trade is profit
var lastTradeWasprofit = false
if (strategy.wintrades[0] > strategy.wintrades[1])   
    // last trade was a profit
    lastTradeWasprofit := true
if  (strategy.losstrades[0] > strategy.losstrades[1])  
    // unsuccessful trade, reset
    lastTradeWasprofit := false

// if last trade is a long trade and loss and close>ema open 2nd trade long side
new_counnb=0
new_couns=0
if   lastTradeWasprofit and ptrade_islong
    new_counnb:=new_counnb+1

    
if   lastTradeWasprofit and ptrade_isshort
    new_couns:=new_couns+1

if   lastTradeWasprofit and ptrade_islong  
    strategy.entry("El2",strategy.long,comment ="long")
if   lastTradeWasprofit and ptrade_isshort
    strategy.entry("Es2",strategy.short,comment ="short")


//exit in percentage 
// User Options to Change Inputs (%)
stopPer = input.float(0.8,step=0.1, title='Stop Loss %') / 100
takePer = input.float(1.0,step=0.1, title='Take Profit %') / 100

// Determine where you've entered and in what direction
longStop  =strategy.position_avg_price * (1 - stopPer)
shortStop =strategy.position_avg_price * (1 + stopPer)
shortTake =strategy.position_avg_price * (1 - takePer)
longTake  =strategy.position_avg_price * (1 + takePer)

if strategy.position_size > 0
    strategy.exit(id='Close Long',from_entry = "enter long", stop=longStop, limit=longTake)
if strategy.position_size < 0
    strategy.exit(id='Close Short',from_entry = "enter short", stop=shortStop, limit=shortTake)

if strategy.position_size > 0
    strategy.exit(id='Close Long2',from_entry = "El2", stop=longStop, limit=longTake)
if strategy.position_size < 0
    strategy.exit(id='Close Short2',from_entry = "Es2", stop=shortStop, limit=shortTake)

//PLOT FIXED SLTP LINE
plot(strategy.position_size > 0 ? longStop : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Long Fixed SL')
plot(strategy.position_size < 0 ? shortStop : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Short Fixed SL')
plot(strategy.position_size > 0 ? longTake : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='Long Take Profit')
plot(strategy.position_size < 0 ? shortTake : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='Short Take Profit')


