-1

I am very new to pine script and trying to generate an EMA mean reversion algo. Conditions are as follows TimeFrame: 10mins, Time 9:15 a.m to 10:15.a.m otherwise no entry

  • Candle low should be far away and above from the user input EMA (example 7EMA).

  • If there are multiple candles far away from that EMA than consider that candle which has farthest low.(Ambiguity: havenot decided how much far way the low should be but it should not be very near to the EMA)

  • If the low of that farthest candle is broken take short enrty,if time is > 10.15 no entry to be taken.

  • SL should be the HH(Highest high) of the candles which are away from the EMA.

    As I am new to Pine Script

  • I have only been able to plot EMA.

  • Also found out the candles whose close and low are away from EMA.

  • Yet to decide the exit condition

Code:

//@version=5
strategy("7ema", overlay=true)
ema_input=input.int(7,'EMA Length')
ema_=ta.ema(close,ema_input)

plot(ema_)
barcolor((close > ema_) and (low > ema_ or high < ema_) ? color.black : na)

Candles detached from EMA

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Please ask one question per post and show us what you have tried and where you have problems. I understand that you are new to `pinescript` but there is absolutely no attempt to code any of those requirements in your code. I would like to kindly remind you that this not a code writing service. – vitruvius Jun 28 '22 at 19:17
  • I already added what I have tried @vitruvius also attached the image of the result I am getting. Now out of these black candles I am trying to find out which candle has the farthest low from the EMA. – sachin.pandey Jun 29 '22 at 02:47
  • @vitruvius let me post that code which I tried but was giving wrong{ resultstotalbars=(close > ema_) and (low > ema_ or high < ema_) plot(totalbars)} – sachin.pandey Jun 29 '22 at 02:50
  • @vitruvius other line of code which I tried plot(ema_) newLow=low+(ema_*0.015) barcolor((newLow > ema_) and (newLow > ema_ or newLow < ema_) ? color.black : na) – sachin.pandey Jun 29 '22 at 02:52
  • @vitruvius I do agree that I should have posted the wrong codes I tried But what I have not understood about the comment 'Please ask one question' if I would be trying something then shouldn't I mention all the conditions – sachin.pandey Jun 29 '22 at 02:55

1 Answers1

2

You will have to first find when is the first time a candle is having low above ema. Then you have to check each successive candle above ema, if its low is above the previous low. If the low is below the previous low then you have to generate signal. I have done a basic part of that without time checking etc. It will plot all such reversal like in this image

Plot

//@version=5
strategy("7ema", overlay=true)

ema_input=input.int(7,'EMA Length')
ema_=ta.ema(close,ema_input)

plot(ema_)

var pLowAboveEMA=0.0
var isClosingAboveEMA=false
var pHighBelowEMA=0.0
var isClosingBelowEMA=false
var buySignal=false
var sellSignal=false
var signalgeneratedafterbreach=false


if low>ema_
    if isClosingAboveEMA
        if low>pLowAboveEMA
            pLowAboveEMA:=low
        else if not signalgeneratedafterbreach
            sellSignal:=true
            signalgeneratedafterbreach:=true
    else
        isClosingAboveEMA:=true
        pLowAboveEMA:=low
        isClosingBelowEMA:=false
        signalgeneratedafterbreach:=false
if high<ema_
    if isClosingBelowEMA
        if high<pHighBelowEMA
            pHighBelowEMA:=high
        else if not signalgeneratedafterbreach
            buySignal:=true
            signalgeneratedafterbreach:=true
    else
        isClosingBelowEMA:=true
        pHighBelowEMA:=high
        isClosingAboveEMA:=false
        signalgeneratedafterbreach:=false
barcolor((close > ema_) and (low > ema_ or high < ema_) ? color.black : na)
plotshape(buySignal, "Long", shape.triangleup, location.belowbar, color = color.blue, size = size.small)
plotshape(sellSignal, "Short", shape.triangledown, location.abovebar, color = color.red, size = size.small)
buySignal:=false
sellSignal:=false
Rohit Agarwal
  • 1,258
  • 1
  • 3
  • 9
  • I was trying to edit code a bit also was trying to write it down in AFL. I tried to remove the buy condition and also when the low break it was considering the candle which is above EMA but what if the candle breaks the low but touches the EMA. You code helped a lot but I was able to do it in AFL. Posting it in another comment. – sachin.pandey Jul 07 '22 at 20:27
  • far = 1; shortCondition = Ref(L,0)> EMA(C,5)*far AND TimeNum()>091500 AND TimeNum()< = 101500; SL_price = ValueWhen(ShortCondi ShortP= ValueWhen(ShortCondition,HHV(H,4); Short = BarSince(ShortCondition)<=4 AND Ref(L,0)< ShortP AND TimeNum()<=100000; Short = ExRem(Short, BarSince(Short)>3); ShortPrice = ShortP; pos=50; SetPositionSize = (Pos,spsPercentOfEquity); – sachin.pandey Jul 07 '22 at 20:28
  • SL= abs(ShortP- SL_price); n= Optimize("Profit Multiplier",2,1,5,0.25); Cover = TimeNum()>=151500; ApplyStop(0,2,SL,1); ApplyStop(1,2,n*SL,1); SetChartOptions(0, chartShowArrows | chartShowDates ); _N(Title = StrFormat("{{Name}}-{{INTERVAL}} {{DATE}} open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}, O,H,L,C, Selectedvalue(ROC(C,1)))); Plot(C, "Close", ParamColor("Color", colorDefault), styleNoTitle| ParamStyle("Style")|GetPriceStyle()); Plot(EMA(C,5)),"ema",colorBlack); PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,H, Offset = -45); – sachin.pandey Jul 07 '22 at 20:29
  • Now I have tradingview which supports Pinescript and Ambibroker is expensive.What if want to put a Stop Loss at the HH of the candle which is away from EMA, In AFl it was bit easier but free version doesnot support backtesting – sachin.pandey Jul 07 '22 at 20:33