Need help

#1
Hello , I have been trying code this strategy since last 11days but I am not getting it right as the system is not show proper entry , so I really need your help by correcting this code (right entry) .
Buy = Buy only if the 1st candle of current green supertrend goes above prev green or +ve supertrend.
( Plz see attached pics )

Code for entry
Buy = supertrend<C AND C>Ref(supertrend,-1) ;
Sell = supertrend>C ;

Thank you….
Copy & paste for better reference
_SECTION_BEGIN("My first trading Strategy");
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates); //Enable X-Axis (Date/Time Axis)
Plot(Close,"Candles",colorDefault,styleCandle); //Plot Candles
_SECTION_END();

// SUPERTRENT FUNCTION //
function FunctionST (Period, Multiplier)
{
ATR_Val=ATR(Period);
UpperBand=LowerBand=final_UpperBand=final_LowerBand=SuperTrend=0;


// CALCULATE SUPERTRENT //
for( i = Period; i < BarCount; i++ )
{
UpperBand=((High + Low)/2) + Multiplier*ATR_Val;
LowerBand=((High + Low)/2) - Multiplier*ATR_Val;
final_UpperBand = IIf( ((UpperBand<final_UpperBand[i-1]) OR (Close[i-1]>final_UpperBand[i-1])), (UpperBand), final_UpperBand[i-1]);
final_LowerBand = Iif( ((LowerBand>final_LowerBand[i-1]) OR (Close[i-1]<final_LowerBand[i-1])), (LowerBand), final_LowerBand[i-1]);

SuperTrend = IIf(((SuperTrend[i-1]==final_UpperBand[i-1]) AND (Close<=final_UpperBand)),final_UpperBand,
IIf(((SuperTrend[i-1]==final_UpperBand[i-1]) AND (Close>=final_UpperBand)),final_LowerBand,
IIf(((SuperTrend[i-1]==final_LowerBand[i-1]) AND (Close>=final_LowerBand)),final_LowerBand,
IIf(((SuperTrend[i-1]==final_LowerBand[i-1]) AND (Close<=final_LowerBand)),final_UpperBand,0))));

}
Plot( SuperTrend, "SuperTrend", (IIf( SuperTrend>Close, ParamColor("Resistance", colorRed ), ParamColor( "Support", colorGreen ))), ParamStyle("Style") | styleThick | styleLine );
Return SuperTrend;
}


Periods_set = Param("Periods", 1, 1, 50 );
Multiplier_set = Param("Multiplier ", 1, 1, 10 );
Multiplier = Multiplier_set;
Period = Periods_set ;

SuperTrend = FunctionST(Period,Multiplier);

Sell = supertrend>C ;
Buy = supertrend<C AND C>Ref(supertrend,-1) ;

Buy = ExRem(Buy,Sell); //remove excessive buy signals
Sell = ExRem(Sell,Buy); //remove excessive sell signals

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
Screenshot_20220318-123959_2.png
 

Attachments

chintan786

Well-Known Member
#2
Check if this is what u are looking for:
Code:
////////////////////////////////////////////////////////////////////////////
// Supertrend Indicator
//
// By Chintan
//
/////////////////////////////////////////////////////////////////////////////

function SuperTrend(lenATR, width)
{
    nATR = ATR(lenATR);
    pAvg = (H+L) / 2;

    upperBand = pAvg + width * nATR;
    lowerBand = pAvg - width * nATR;
    isUpTrend = True;
    dn = DateNum();

    for (i=lenATR; i<BarCount; ++i)
    {
        if (C[i] > upperBand[i-1])
            isUpTrend[i] = True;
        else if (C[i] < lowerBand[i-1])
            isUpTrend[i] = False;
        else
            isUpTrend[i] = isUpTrend[i-1];

        if (isUpTrend[i])
            lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]);
        else
            upperBand[i] = Min(upperBand[i], upperBand[i-1]);
    }

    super = IIf(isUpTrend, lowerBand, upperBand); 
    return super;
}

lengthATR = Param("ATR Length", 10, 1, 100, 1);
widthBands = Param("Band Width", 3, 1, 20, 0.1);
st = Supertrend(lengthATR,widthBands);

Buy = st<C AND C>Ref(st,-1) ;
Sell = st>C ;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Cover = Buy;
Short= Sell;

Plot(st, "Supertrend("+lengthATR+","+widthBands+")", ParamColor( "Color", colorCycle ), ParamStyle("Style") );

Filter = True;
AddColumn(O,"Open");
AddColumn(H,"High");
AddColumn(L,"Low");
AddColumn(C,"Close");
AddColumn((H+L)/2,"Avg");
AddColumn(ATR(lengthATR), "ATR("+lengthATR+")");
AddColumn(st, "Supertrend("+lengthATR+","+widthBands+")");

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);  
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);  
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);  
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);  
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);  
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 

_SECTION_BEGIN("Price");
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() ); 
_SECTION_END();
 
#3
hello chintan , thanks for your reply
The code which you have made is actually showing buy and sell signals whenever there is a change in supertrend , but actually i want the signals to appear only when the 1st candle of currrent +ve supertrend has broken prev high of +ve supertrend
plz go through the pics again
thank you :)
sorry for late reply:confused:
 

Similar threads