Simple Coding Help - No Promise.

Well I backtested it after changing a couple of this and it was highly disappointing, may be coz I changed too many things. Are you carrying over positions or closing them everyday? If closing, at what time?

You should be able to backtest this AFL now. I've already done the required changes.
Purely Intraday. Either the target hits or stoploss...not any fixed time.

Did the backtesting,still not giving proper results.
 

pratapvb

Well-Known Member
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.
yes that was the original problem. but what is the problem with the code already written?
 
yes that was the original problem. but what is the problem with the code already written?
RoundLotSize = 1;
MarginDeposit = 350;
TickSize = 0;
PointValue = 25;
SetPositionSize(4,spsShares);
SetBarsRequired(5*12);

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

expand = expandLast;

x5 = Close - Open;

TimeFrameSet(in15Minute);
x15 = Close - Open;
x15e = TimeFrameExpand(x15, in15Minute, expand);
TimeFrameRestore();

TimeFrameSet(in15Minute*2);
x30 = Close - Open;
x30e = TimeFrameExpand(x30, in15Minute*2, expand);
TimeFrameRestore();

TimeFrameSet(inHourly);
x60 = Close - Open;
x60e = TimeFrameExpand(x60, inHourly, expand);
TimeFrameRestore();


_SECTION_BEGIN("Trading signals");
tsl = Param("bars", 2, 1, 10, 1);
tsl = Optimize("bars", tsl, 1, 10, 1);

Buy = x5>0 AND x15e>0 AND x30e>0 AND x60e>0;
LongSL = ValueWhen(Buy, LLV(Low, tsl));
Sell = Cross(LongSL, Close);

Short = x5<0 AND x15e<0 AND x30e<0 AND x60e<0;
ShortSL= ValueWhen(Short, HHV(High, tsl));
Cover = Cross(Close, ShortSL);

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

BuyPrice = ValueWhen(Buy, Close);
SellPrice = ValueWhen(Sell, Close);
ShortPrice = ValueWhen(Short, Close);
CoverPrice = ValueWhen(Cover, Close);

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low, -28);
PlotShapes(Short*shapeDownArrow, colorRed, 0, High, -28);
PlotShapes(Cover*shapeHollowUpArrow, colorGreen, 0, Low, -45);
PlotShapes(Sell*shapeHollowDownArrow, colorRed, 0, High, -45);

dist = 1.5*ATR(10);
for (i=0; i<BarCount; i++) {
if (Cover) {
PlotText( "\nCover short: " + CoverPrice, i+1.5, L[ i ]-dist-3, colorLime);
PlotText( "\n\nProfit: " + (ShortPrice-CoverPrice), i+1.5, L[ i ]-dist-3, colorLime);
} else if (Sell) {
PlotText( "\nSell bought: " + SellPrice, i+1.5, H[ i ]+dist+5, colorOrange);
PlotText( "\n\nProfit: " + (SellPrice-BuyPrice), i+1.5, H[ i ]+dist+5, colorOrange);
}
if(Buy) {
PlotText( "Buy: " + BuyPrice, i+1.5, L[ i ]-dist-3, colorLime);
sig = "Buy";
} else if( Short) {
PlotText( "Short: " + ShortPrice, i+1.5, H[ i ]+dist+5, colorOrange);
sig = "Short";
}
}
_SECTION_END();


This was the try by abhi.. We were facing problems in the exits.. The entries were also taken in running candle whereas entries should only be taken after the close of the hourly candle but on 5 min tf.

Thanks for ur concern.
 

pratapvb

Well-Known Member
This was the try by abhi.. We were facing problems in the exits.. The entries were also taken in running candle whereas entries should only be taken after the close of the hourly candle but on 5 min tf.

Thanks for ur concern.
ok will think about it....just to let you know unless I can think of a possible solution to all the issues I don't even start coding.....I need to have a 80-90% complete mental code before I start coding.....so lets see
 

pratapvb

Well-Known Member
ok will think about it....just to let you know unless I can think of a possible solution to all the issues I don't even start coding.....I need to have a 80-90% complete mental code before I start coding.....so lets see
identified the trigger bar....now it is just a question of putting a target line / level and SL line / level.....I will get back to it later as I have to go out

note:
1. the lower panes show 15min 30min and 60min block charts around the 5min chart....the blue or pink body is shown for bullish / bearish
2. my ami is set so that in TFs 15, 30 and 60 bar completes at 9.30. I have done this as the 1st 5min absorbs the volatility of overnight. so my arrows are at 9.30 or 10.30 or 11.30.....



code as of now

Code:
tf1s = 5 * in1Minute ;
tf2s = 15 * in1Minute ;
tf3s = 30 * in1Minute ;
tf4s = 60 * in1Minute ;
expandmode = expandLast ;

TimeFrameSet(tf4s) ;
tf4oe = TimeFrameExpand(O, tf4s, expandmode) ;
tf4he = TimeFrameExpand(H, tf4s, expandmode) ;
tf4le = TimeFrameExpand(L, tf4s, expandmode) ;
tf4ce = TimeFrameExpand(C, tf4s, expandmode) ;
TimeFrameRestore() ;

TimeFrameSet(tf3s) ;
tf3oe = TimeFrameExpand(O, tf3s, expandmode) ;
tf3he = TimeFrameExpand(H, tf3s, expandmode) ;
tf3le = TimeFrameExpand(L, tf3s, expandmode) ;
tf3ce = TimeFrameExpand(C, tf3s, expandmode) ;
TimeFrameRestore() ;

TimeFrameSet(tf2s) ;
tf2oe = TimeFrameExpand(O, tf2s, expandmode) ;
tf2he = TimeFrameExpand(H, tf2s, expandmode) ;
tf2le = TimeFrameExpand(L, tf2s, expandmode) ;
tf2ce = TimeFrameExpand(C, tf2s, expandmode) ;
TimeFrameRestore() ;

TimeFrameSet(tf1s) ;
tf1oe = TimeFrameExpand(O, tf1s, expandmode) ;
tf1he = TimeFrameExpand(H, tf1s, expandmode) ;
tf1le = TimeFrameExpand(L, tf1s, expandmode) ;
tf1ce = TimeFrameExpand(C, tf1s, expandmode) ;
TimeFrameRestore() ;

compbar4 = tf4he != Ref(tf4he, -1) AND tf4le != Ref(tf4le, -1) ;
compbar3 = tf3he != Ref(tf3he, -1) AND tf3le != Ref(tf3le, -1) ;
compbar2 = tf2he != Ref(tf2he, -1) AND tf2le != Ref(tf2le, -1) ;
compbar1 = tf1he != Ref(tf1he, -1) AND tf1le != Ref(tf1le, -1) ;

compbar = compbar4 AND compbar3 AND compbar2 AND compbar1 ;

//Plot(2, "", IIf(compbar , colorBlue, IIf(compbar4 , colorRed, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;

Oc4 = ValueWhen(compbar, tf4ce - tf4oe) ;
Oc3 = ValueWhen(compbar, tf3ce - tf3oe) ;
Oc2 = ValueWhen(compbar, tf2ce - tf2oe) ;
Oc1 = ValueWhen(compbar, tf1ce - tf1oe) ;

Buytrig = Oc4 > 0 AND Oc3 > 0 AND Oc2 > 0 AND Oc1 > 0 ;
Shorttrig = Oc4 < 0 AND Oc3 < 0 AND Oc2 < 0 AND Oc1 < 0 ;

Buy = Buytrig AND Compbar ;
Short = Shorttrig AND Compbar ;

BuyPrice = ValueWhen(Buy, C) ;
ShortPrice = ValueWhen(Short, C) ;

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

//Plot(4, "", IIf(Buy, colorBlue, IIf(Short, colorRed, colorDarkGrey)), styleArea|styleOwnScale, 0, 100) ;
 

pratapvb

Well-Known Member
Can you confirm that in above code triggers are as per what you wanted?
 
Last edited:
pratap sir i need some help. i want signals on 5 min timeframe from reference of 15 min timeframe. example if there is buy signal on 15 min timeframe then after that time, there should be only buy signals on 5 min timeframe . and vice versa for sell. i tried all but didnt get succesful.i tried time frame set expand all, but signals are repaintaing. below is sample code on which can u tell me how this can be done.



_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();

x = EMA(Close,5);
y = EMA(Close,13);

Buy=Cross(x,y);
Sell=Cross(y,x);

GraphXSpace = 5;
dist = 3.5*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy ) PlotText( "Buy\n" + C[ i ], i, L[ i ]-dist, colorGreen );
if( Sell ) PlotText( "sell\n" + C[ i ], i, L[ i ]+dist, colorRed);
}



Plot(EMA(Close,5),"",colorBrightGreen,styleLine);
Plot(EMA(Close,13),"",colorRed,styleLine);

Cover = Buy;
Short = Sell;

_SECTION_END();
 

pratapvb

Well-Known Member
pratap sir i need some help. i want signals on 5 min timeframe from reference of 15 min timeframe. example if there is buy signal on 15 min timeframe then after that time, there should be only buy signals on 5 min timeframe . and vice versa for sell. i tried all but didnt get succesful.i tried time frame set expand all, but signals are repaintaing. below is sample code on which can u tell me how this can be done.



_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();

x = EMA(Close,5);
y = EMA(Close,13);

Buy=Cross(x,y);
Sell=Cross(y,x);

GraphXSpace = 5;
dist = 3.5*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy ) PlotText( "Buy\n" + C[ i ], i, L[ i ]-dist, colorGreen );
if( Sell ) PlotText( "sell\n" + C[ i ], i, L[ i ]+dist, colorRed);
}



Plot(EMA(Close,5),"",colorBrightGreen,styleLine);
Plot(EMA(Close,13),"",colorRed,styleLine);

Cover = Buy;
Short = Sell;

_SECTION_END();


Means you want the signal to come on 5min after the 15bar is complete at which crossover happens?
 
Means you want the signal to come on 5min after the 15bar is complete at which crossover happens?
thank u for reply pratap sir. i want signals on 5 min chart but signals should be on reference of 15 min signal. if 15 min signal is buy on 15 min candle close. there should be only buy signals on 5 min chart no sell signals. basically i want to filter 5 min signals, i want to trade on 5 min chart but looking at 15 min chart trend..

yes i want signal to come on 5 min after 15bar signal is complete at which crossover happens.
 
Last edited:

Similar threads