Simple Coding Help - No Promise.

mastermind007

Well-Known Member
Thanks, Happy,
It is tricky to code so that trade is executed as soon as a price is touched instead of trade execution at CLOSE, OPEN,HI,LOW ?
do you auto execute your trades? If yes, all you have to do is send market (OR LTP limit) orders at the key time intervals and your trades would happen

If you are doing it manually, arrow shown on ami is of no relevance because you may not get the same price ...
 
hi,
I have a simple strategy and needs some one who can do the programming so i can backtest the same.
Strategy is based on dual timeframe.
In 30mins TF, if the close price is above 200MA, i will buy the stock, once the price cross 200MA in 5mins
Exit for the Buy is when the prices closes below 200MA on 5min TF

I need to backtest this stratergy kindly help me out .
 

ethan hunt

Well-Known Member
hi,
I have a simple strategy and needs some one who can do the programming so i can backtest the same.
Strategy is based on dual timeframe.
In 30mins TF, if the close price is above 200MA, i will buy the stock, once the price cross 200MA in 5mins
Exit for the Buy is when the prices closes below 200MA on 5min TF

I need to backtest this stratergy kindly help me out .
_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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy = Close > MA( Close , 200 );

Sell = Close < MA( Close , 200 );

Short = 0;

Cover = 0;
_SECTION_END ();

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();


settings in automatic analysis > trades > buy price = open > sell price = open > buy delay =1 > sell delay =1

you may want to add short & cover conditions also
 
Last edited:

Nehal_s143

Well-Known Member
Hi All,
Could you please someone help me to create the below AFL? Thanks in advance

The AFL should identify the last pivot high and pivot low. If last pivot high is above 15 SMA, it should create buy signal while pivot high is taking out. If last pivot low is below 15 SMA, it should create sell signal while pivot low is taking out. Thanks again for the help
Hi

I am just learning AFL coding little bit, may be this will be helpful

_SECTION_BEGIN("Pivot High Pivot Low Orignal");

SetBarsRequired(350, -0);
// ------------- EazyTradePlus V1.1
SetChartOptions(0,chartShowDates|chartWrapTitle);
AboveUBB =0;
SellSignal=0;
BuySignal=0;
//--------------------------------EazyTradePlus V1.1 Main AFL
SetChartBkColor(ParamColor("Outer panel color ",colorLightYellow)); // color of outer border
SetChartBkGradientFill( ParamColor("Inner panel color upper half", colorBlack),
ParamColor("Inner panel color lower half", colorDarkOliveGreen)); // color of inner panel
//user parameters
parmPlotScoreCard = ParamToggle("Plot KPScoreCard", "No|Yes", 1);
parmPlotA900AutoStop = ParamToggle("Plot A900/AutoStop", "No|Yes", 0);
parmA900Color = ParamColor("A900 Color", colorWhite);
parmA900Style = ParamStyle("A900 Style", styleLine, maskAll);
parmAutoStopColor = ParamColor("AutoStop Color", colorYellow);
parmAutoStopStyle = ParamStyle("AutoStop Style", styleLine, maskAll);
parmPPTextColor = ParamColor("PP Text color", colorBlack);
parmPPTrndColorUp = ParamColor("PP Trend Up color", ColorRGB(167,224,243) );
parmPPTrndColorDn = ParamColor("PP Trend Dwn color", ColorRGB(255,192,203) );
parmPPTextOffSet = Param("PP OffSet", 0.60, 0.40, 1.5, 0.1);
parmTickMultipler = Param("M/W tick allowance", 1, 0, 10, 1);
parmA900AutoStopX = ParamToggle("Plot A900/AutoStop Cross", "No|Yes");
parmA900AutoStopColorX = ParamColor("A900/AutoStop Cross Color", colorBlack);
ParmSCThreshold = Param("ScoreCard Threshold", 3, 1, 9, 1);
parmVoice = ParamToggle("Voice 123 Setups", "No|Yes", 0);
parmAlert = ParamToggle("Alert 123 Setups", "No|Yes", 0);
parmPivotPop = ParamToggle("PivotPop", "No|Yes", 1);
parmBarCancel = Param("Bar Cancel", 7, 1, 20, 1);
parmWaterLevelColor = ParamColor("WalterLevel Color", ColorRGB(127,255,212));
parmWaterLevelStyle = ParamStyle("WaterLevel Style", styleLine, maskAll);
parmBBPeriod = Param("Bollinger Band Period", 10, 2, 30, 1);
parmBBSD = Param("bollinger Band SD", 0.8, 0.2, 3.0);
ParmPlotPPIndicators = ParamToggle("Plot Pivot Pop indicators", "No|Yes", 0);
parmBBColor = ParamColor("BBands Color", colorBlack);
parmBBStyle = ParamStyle("BBands Style", styleLine, maskAll);

ParmDebug = ParamToggle("Debug", "No|Yes", 0);

// constants
_N(PaneName = Name() + Interval(2)+ _SECTION_NAME());
_N(NewBarName = "NewBar" + PaneName);

//functions
function NewBarP()
{
PrevDT = StaticVarGet( NewBarName);
DT = LastValue(DateTime());
StaticVarSet( NewBarName,DT);
return DT != PrevDT;
}
function MRoundP(Number, Multiple )
{
if(Multiple == 0 )
{

xMultiple = 0.01; }
else
{
xMultiple = Multiple;
}
Divided = Number / xMultiple;
intDivided = int(Divided);
intDivided = intDivided + round(Divided - intDivided);
return intDivided * xMultiple;
}

//miscellaneous setups
ObjAB = CreateObject("Broker.Application");
ticker = objAB.Stocks(Name() );
if(ticker.TickSize == 0)
{
TickValue = 0.01; //set TickValue to a penney
}
else
{
TickValue = ticker.TickSize; // use Tick Size for this symbol
}
NewBarSignal = NewBarP();
// KP Indicators
KPA900 = E_TSKPA900(Close);
KPAutoStop = E_TSKPAUTOSTOP(High,Low,Close);
Ctmpl = E_TSKPCOLORTMPL(Open,High,Low,Close,Volume); //ScoreCard
KPScoreCard = 0;
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd0 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd1 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd2 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd3 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd4 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd5 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd6 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd7 > 0, 1, -1);
KPScoreCard = KPScoreCard + IIf(tskp_colortmplcnd8 > 0, 1, -1);

if(parmDebug == 1)
{
printf("a900: %0.6f% \nAutoStop: %0.6f%\nScoreCard: %0.0f%\n", KPA900, KPAutoStop, KPScoreCard);
}
if(parmPlotScoreCard == 1)
{
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g% (%0.4f%) {{VALUES}}", O, H, L, C, SelectedValue( C - Ref(C, -1)) ));
if( ParamToggle("Tooltip shows", "All Values|Only Prices" ) )
{
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.2f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 )));
}
Color = IIf(KPScoreCard >= parmSCThreshold, colorBlue, IIf(KPScoreCard <= -parmSCThreshold, colorRed, colorYellow) );
Plot( C, "Close", Color , styleNoTitle | ParamStyle("OHLC Style",styleBar | styleThick) | GetPriceStyle() );
}
//user want A900/AutoStop plotted
if(parmPlotA900AutoStop == 1)
{
Plot(KPA900, "A900", parmA900Color, parmA900Style);
Plot(KPAutoStop, "AutoStop", parmAutoStopColor, parmAutoStopStyle);
}
// find A900/AutoStop cross over/under with ScoreCard confirmation.
XOUp = (KPA900 > KPAutoStop) AND (KPScoreCard >= parmSCThreshold); // New Pivot Low
XODn = (KPA900 < KPAutoStop) AND (KPScoreCard <= -parmSCThreshold); // New Pivot High
if(parmDebug == 1)
{
printf(WriteIf(XOUp, "before= XOUp: True", "before= XOUp: False") + WriteIf(XODn, " XODn: True\n", " XODn: False\n") );
}
//remove duplicate signals
XOUp = ExRem(XOUp, XODn);
XODn = ExRem(XODn, XOUp);

if(parmDebug == 1)
{
printf(WriteIf(XOUp, "after= XOUp: True", "after= XOUp: False") + WriteIf(XODn, " XODn: True\n", " XODn: False\n") );
}
//find the current Pivot Points - PL and PH
//remember XOUp = 1 means a PL and XODn =1 means a PH
PLBars = IIf(XOUp, LowestSinceBars(XODn, L ,1), 0); //find the bar that produced the Lowest Low
PHBars = IIf(XODn, HighestSinceBars(XOUp, H, 1),0); //find the bar that produced the Highest High
//PLPrice = IIf(XOUp, Ref(L, -PLBars), 0);
//PHPrice = IIf(XODn, Ref(H, -PHBars),0);
PLPrice = Ref(L, -PLBars);
PHPrice = Ref(H, -PHBars);

//keep track of the previous Pivot Points
PrevPLBars = Ref(BarsSince(XOUp), -1) +1;
PrevPHBars = Ref(BarsSince(XODn), -1) +1;
PrevPLPrice = Ref(PLPrice, -prevPLBars);
PrevPHPrice = Ref(PHPrice, -PrevPHBars );
PivotsCloseEnough = TickValue * parmTickMultipler;
PLDifference = MroundP(PLPrice - PRevPLPrice, ticker.TickSize);
PHDifference = MroundP(PHPrice - PrevPHPrice, ticker.TickSize);
PPTrend = IIf(XOUp AND (PLDifference > PivotsCloseEnough) AND PrevPHPrice > PrevPLPrice AND PRevPHPrice > PLPrice, 1,
IIf(XOUp AND (PLDifference < - PivotsCloseEnough) AND PRevPHPrice > PrevPLPrice AND PrevPHPrice > PLPrice, -1,
IIf(XODn AND (PHDifference > PivotsCloseEnough) AND PrevPLPrice < PrevPHprice AND PrevPLPrice < PHPrice, 1,
IIf(XODn AND (PHDifference < -PivotsCloseEnough) AND PrevPLPrice < PrevPHPrice AND PrevPLPrice < PHPrice, -1,
IIf(XOUp AND (abs(PLDifference) <= PivotsCloseEnough) AND PrevPHPrice > PrevPLPrice AND PRevPHPrice > PLPrice, 2,
IIf(XODn AND (abs(PHDifference) <= PivotsCloseEnough) AND PrevPLPrice < PrevPHPrice AND PrevPLPrice < PHPrice, -2, 0)))) ));
if(ParmDebug)
{
printf("Current PH Bar: %g% /Price: %g%\n", PHBars, PHPrice);
printf("Current PL Bar: %g% /Price: %g%\n", PLBars, PLPrice);
printf("Previous PH Bar: %g% /Price: %g%\n", PrevPHBars, PrevPHPrice);
printf("Previous PL Bar: %g% /Price: %g%\n", PrevPLBars, PrevPLPrice) ;
printf("PP Trend: %g%\n", PPTrend);
printf("PHPrice - PrevPHPrice: %g%\nPLPrice - PrevPLPrice: %g%\nPivotsCloseEnough: %g%", PHDifference, PLDifference, PivotsCloseEnough);
}
//PLot pivots as text
dist = parmPPTextOffSet * ATR(10);
//for( i = 0; i < BarCount -1; i++)
for( i = 0; i < BarCount ; i++)
{
if(XOUp[i ] == 1 AND abs(PPTrend) != 2) //cross up -plot the Pivot Low
{
PlotText("PL", i - PLBars, PLPrice - dist , parmPPTextColor, IIf(PPTrend == 1, parmPPTrndColorUp, IIf(PPTrend == -1, parmPPTrndColorDn, colorYellow) ));
}
if(XODn[i ] == 1 AND abs(PPTrend) != 2) //cross down - plot the pivot high
{
PlotText("PH", i - PHBars, PHPrice + dist, parmPPTextColor, IIf(PPTrend == 1, parmPPTrndColorUp, IIf(PPTrend == -1, parmPPTrndColorDn, colorYellow) ));
}
if(XOUp[i ] == 1 AND (PPTrend) == 2) // the Pivot Low is a W Bottom
{
PlotText("PW", i - PLBars, PLPrice - dist , parmPPTextColor, colorYellow) ;
}
if(XODn[i ] == 1 AND PPTrend == -2) //cross down - pivot high is a M Top
{
PlotText("PM", i - PHBars, PHPrice + dist, parmPPTextColor, colorYellow) ;
}
PPL = (XOUp[i ] == 1 AND abs(PPTrend) != 2);
PPH = (XODn[i ] == 1 AND abs(PPTrend) != 2);

} //end For

_SECTION_END();

PMA = MA(C,15);

Plot(PMA, "", colorLime);

Buy=PPH>PMA AND C>PPH;
Sell=PPL<PMA AND C<PPL;
Short = PPL;
Cover=PPH;

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

PlotShapes(Buy*shapeUpArrow,colorBlue,0,L,-35);
PlotShapes(Short*shapeDownArrow,colorRed,0,H,-35);


I dont know why arrows are not coming, where did I went wrong, may be seniors may help to correct the mistake
 

Nehal_s143

Well-Known Member
Hi

I am using Hurst Band instead of Bollinger Bands
Hurst Band is repainting on chart, I want to fix the bands, once formed should not re-paint

Kindly help


CY1 = Param("Short Cycle Length?" ,10, 1 ,1000 ,1 )/2;
CY2 = Param("Medium Cycle Length?" ,80, 1 ,1000 ,1 )/2;
M1 = Param("Short Cycle Multiplyer?" ,1, 0.01 ,10 ,1 );
M2 = Param("Medium Cycle Multiplyer?" ,3, 0.01 ,10 ,1 );
T1 = Ref(MA(Close ,CY1 ),CY1/2)+ M1*ATR(CY1 );
B1 = Ref(MA( Close ,CY1 ),CY1/2)- M1*ATR(CY1 );
T2 = Ref(MA(Close ,CY2 ),CY2/2)+ M2*ATR(CY2 );
B2 = Ref(MA( Close ,CY2 ),CY2/2)- M2*ATR(CY2 );

//Plot(T1, "", colorLime);
//Plot(B1, "", colorLime);
Plot(T2, "", colorLime);
Plot(B2, "", colorLime);
 
_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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy = Close > MA( Close , 200 );

Sell = Close < MA( Close , 200 );

Short = 0;

Cover = 0;
_SECTION_END ();

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();


settings in automatic analysis > trades > buy price = open > sell price = open > buy delay =1 > sell delay =1

you may want to add short & cover conditions also
Thank you ethan,
but the code dosen't have the dual-time frame coding also i need to backtest the dualtimeframe strategy.
Here, if 30TF close is abv 200MA, the we buy on the 5mins chart if prices in 5min TF also goves abv 200MA

Hope you got my point.
 

Similar threads