Simple Coding Help - No Promise.

pratapvb

Well-Known Member
sir this solution is not working... if used on 4 or more digit value stock/script p&f plots only for 2 rows and that to only few boxs

please provide other solution
for 500 you are using 1....1 / 500 is 0.002

so should be 0.002 and not 0.02
 
Need help from Senors.
What lines should be added for backtesting the following condition.
stop loss for buy is SHORT level(yyy) and stoploss for sell is BUY level(xxx).
BT is buy target and ST is sell target.

C1 = SelectedValue( TimeFrameGetPrice( "C", inDaily, -1 ));
O1 = SelectedValue( TimeFrameGetPrice( "O", inDaily, -1 ));
H1=SelectedValue( TimeFrameGetPrice( "H",inDaily, -1 ));
L1=SelectedValue( TimeFrameGetPrice( "L", inDaily, -1 ));

xxx=C1+((H1-L1)*0.25);
yyy=C1-((H1-L1)*0.25);
BT=xxx+((H1-L1)*0.25);
ST=yyy-((H1-L1)*0.25);
Buy=Cross(C,xxx);
Sell=Cross(C,BT);

Short=Cross(yyy,C);
Cover=Cross(ST,C);
 

pratapvb

Well-Known Member
what is the problem? can you summarize?
Hi,

We needed to make a afl on these grounds..

See hourly candle lets say at 10:00
See 30min candle at 10:00
See 15min candle at 10:00
See 5min candle at 10:00
if all the four candles are bullish, then we place a buy order at the close price at 1000 with sl at the low of 5min candle at 10 and profit target as 2 times risk. i.e 2*(c-l) of the 5 min candle at 10.

This is the basic setup and gives good success ratio.. if its 50 also then also this is a winning strategy.

There are many thing which can be looked into such as the definition of a bull candle(we here take it as c>o), the logic of trailing stops, the scaling logic etc.

All buy/short signals should be generated after the closing of 1 hour candle, hence at 10 or 11 or 12 etc. sell and cover are based on sl and tp.
Thanks in advance.
 
This should do the job if xxx and yyy are your SL's then the code is modified. You may also use Low/High instead of Close of candle for the SL part if you like.

Code:
C1 = SelectedValue( TimeFrameGetPrice( "C", inDaily, -1 ));
O1 = SelectedValue( TimeFrameGetPrice( "O", inDaily, -1 ));
H1=SelectedValue( TimeFrameGetPrice( "H",inDaily, -1 ));
L1=SelectedValue( TimeFrameGetPrice( "L", inDaily, -1 ));

xxx=C1+((H1-L1)*0.25);
yyy=C1-((H1-L1)*0.25);
BT=xxx+((H1-L1)*0.25);
ST=yyy-((H1-L1)*0.25);
Buy=Cross(C,xxx);
Sell=Cross(C,BT) OR Cross(C, yyy);

Short=Cross(yyy,C);
Cover=Cross(ST,C) OR Cross(xxx, C);
Dear abhig10 , thanks for your response...but the backtest is not giving the proper results. If the target hits it must show the exit price, but its not showing.

Ticker Trade Date Price Ex. date Ex. Price % chg Profit % Profit Shares Position value Cum. Profit # bars Profit/bar MAE MFE Scale In/Out

NIFTY Open Long 28/02/2014 09:15:00 6251.5 28/02/2014 15:30:00 6269.2 0.28% 17.70 0.28% 1 6251.50 -278.75 27 0.66 -0.37% 0.49% 0/0
After crossing 6261.47 it should have exited.
 

Attachments

My best guess is it is taking Close of candle as the exit price as that is usually set to default in the settings window. You can define your Buy/Sell/Short/Cover prices.

Code:
BuyPrice = valuewhen(Buy, close);
Shortprice = valuewhen(short, close);
// In the cover and sell you will have to place 2 conditions. 1 for SL and 2nd for target
Coverprice = valuewhen(Cross(ST,C), ST); // if target hit
Coverprice = valuewhen(Cross(xxx, C), xxx); // if SL hit
// CoverPrice = valuewhen(Cross(ST,C) OR Cross(xxx, C), iif(Cross(ST,C), ST, xxx)); // Single line code

SellPrice = valuewhen(Cross(C,BT), BT); // if target
SellPrice = valuewhen(Cross(C, yyy), yyy); // if SL hit
Still some error mate....please look at the code and try backtest please.

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("Op %g, Hi %g, Lo %g, Cl %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorWhite ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("TEST");
function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}

C1 = SelectedValue( TimeFrameGetPrice( "C", inDaily, -1 ));
O1 = SelectedValue( TimeFrameGetPrice( "O", inDaily, -1 ));
H1=SelectedValue( TimeFrameGetPrice( "H",inDaily, -1 ));
L1=SelectedValue( TimeFrameGetPrice( "L", inDaily, -1 ));

xxx=C1+((H1-L1)*0.25);
yyy=C1-((H1-L1)*0.25);
BT=xxx+((H1-L1)*0.25);
ST=yyy-((H1-L1)*0.25);
BT1=xxx+((H1-L1)*0.5);
ST1=yyy-((H1-L1)*0.5);
BT2=xxx+((H1-L1)*0.75);
ST2=yyy-((H1-L1)*0.75);

Buy=Cross(C,xxx);Sell=Cross(C,BT) OR Cross(C, yyy);

Short=Cross(yyy,C);Cover=Cross(ST,C) OR Cross(xxx, C);

Plot (yyy,"SELL",32,4);
Plot (xxx,"BUY",27,4);
Plot (BT,"BUY TARGET",40,styleDashed|styleNoLabel);
Plot (ST,"SELL TARGET",40,styleDashed|styleNoLabel);
Plot (BT1,"BUY TARGET",40,styleDashed|styleNoLabel);
Plot (ST1,"SELL TARGET",40,styleDashed|styleNoLabel);
Plot (BT2,"BUY TARGET",40,styleDashed|styleNoLabel);
Plot (ST2,"SELL TARGET",40,styleDashed|styleNoLabel);

BuyPrice = ValueWhen(Buy, Close);
ShortPrice = ValueWhen(Short, Close);
// In the cover and sell you will have to place 2 conditions. 1 for SL and 2nd for target
CoverPrice = ValueWhen(Cross(ST,C), ST); // if target hit
CoverPrice = ValueWhen(Cross(xxx, C), xxx); // if SL hit
// CoverPrice = valuewhen(Cross(ST,C) OR Cross(xxx, C), iif(Cross(ST,C), ST, xxx)); // Single line code

SellPrice = ValueWhen(Cross(C,BT), BT); // if target
SellPrice = ValueWhen(Cross(C, yyy), yyy); // if SL hitPlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,Low,-15);
Filter=Buy OR Cover OR Sell OR Short;
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,Low,-15);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,High,-15);
AddColumn (xxx, "BUY LEVEL", 1.1,colorWhite,colorBlue );
AddColumn (BT, "B TAR1", 1.1, colorWhite,bkcolor =IIf(H>BT, colorGreen,colorDefault));
AddColumn (BT1, "B TAR2", 1.1, colorWhite,bkcolor =IIf(H>BT1, colorGreen,colorDefault));
AddColumn (BT2, "B TAR3", 1.1, colorWhite,bkcolor =IIf(H>BT2, colorGreen,colorDefault));
AddColumn (yyy, "SELL LEVEL", 1.1, colorWhite,colorBlue );
AddColumn (ST, "S TAR1", 1.1, colorWhite,bkcolor =IIf(L<ST, colorRed,colorDefault));
AddColumn (ST1, "S TAR2", 1.1, colorWhite,bkcolor =IIf(L<ST1, colorRed,colorDefault));
AddColumn (ST2, "S TAR3", 1.1, colorWhite,bkcolor =IIf(L<ST2, colorRed,colorDefault));
Title ="\n"+EncodeColor(colorYellow)+" BUY "+WriteVal(xxx,1.2)+EncodeColor(colorRed)+" SELL "+WriteVal(yyy,1.2);

_SECTION_BEGIN("TITLE");

Title = EncodeColor(colorWhite) +"Op-"+WriteVal(Open,1.2)+" "+"Hi-"+WriteVal(High,1.2)+" "+"Lo-"+WriteVal(Low,1.2)+" "+" "+"Cl-"+WriteVal(Close,1.2) +
"\n"+EncodeColor(colorGreen)+" BUY "+WriteVal(xxx,1.2)+EncodeColor(colorYellow)+" T1 "+WriteVal(BT,1.2)+" T2 "+WriteVal(BT1,1.2)+" T3 "+WriteVal(BT2,1.2)+
"\n"+EncodeColor(colorRed)+" SELL "+WriteVal(yyy,1.2)
+EncodeColor(colorYellow)+" T1 "+WriteVal(ST,1.2)+" T2 "+WriteVal(ST1,1.2)+" T3 "+WriteVal(ST2,1.2);
_SECTION_END();
 
HI abhig10,

I think this is the line that's causing the glitch. Technically a Buy/Cover should be Cross(OHLC, crosspoint) and Short/Sell be like Cross(crosspoint, OHLC). But before I can check it I need to know on what TF you are checking it.
I want to test this in 15 min time frame.

And I was correct. Here it goes. Looks good. Did you try to trade with it in RT? What were the results?
Iam trading with it on 15 min time frame with candle close. The values will not change intraday,they are constant. Of course the results are good....you can check them manually on your charts. I want to backtest atleast for 6 months data.Till now i can't do it :confused: :fatigue:

Every day we can get trade. Suitable for any kind of instrument/market.

Check the levels and targets on weekly and monthly charts also...it will be interesting.
 

Similar threads