Simple Coding Help - No Promise.

amitrandive

Well-Known Member
Abhi,

Thanks for your efforts, when i tried it, it plots yeterday's range and today's open and range. I do not see any buy/sell arrows and also there is no results in backtest.

i have tried in all different time frames, still entry/exit arrows are not getting plotted and buy/sell price doesn't show up in backtest

Cubt

I would like to point out that getting an exact open=high or open=low does not give many signals.

We should define a certain percentage of change which is acceptable in this theory.For example Refer the attached daily chart of Nifty .

All the days marked with an Oval have given fantastic opportunities for long and short , but the Open,High and Low differ slightly by a percentage.

I have not marked all the values , but highlighted then with ovals .

 
Last edited:
Dear Amibroker Friends..

There is a standart trend following kpl code below.. This is a good way to follow the trend.. I tried to make some changes on this code but i failed.
Normally , Close price triggers the tsl function and sometimes price can trigger and return back immediately which creates fake signals..
In order to prevent fake signals , I tried to trigger tsl function with EMA5. I modified Close with EMA5 in Buy function but it didnt work..
I need to make backtesting with the model that i described.
Anybody can help ?

//AFL by Kamalesh Langote. Email:kpl@...
no=Param( "Swing", 2, 1, 255 );
tsl_col=ParamColor( "Color", colorCycle );

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);
Buy=Cross(C,tsl) ;
Sell= Cross(tsl,C) ;

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase);
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shape,IIf(Buy,tsl_col,tsl_col),0,IIf(Buy,Low,High));
SetPositionSize(300,spsShares);
ApplyStop(0,1,10,1);
 
Hi I am compelled to seek experts' opinion once again. No, sorry, not expert but any newbie afl coder can solve this but not me. You would die laughing if I state my problem.

Objective:

To buy at 100 and to sell at 1% profit. Let me call it "Target". So Target=0.01, OK????

I have written like this,,,,

sell= C>=Buyprice * Target;

The result? The sell takes place at its own whim. Sometimes it gives me 10% profit also and sometimes loss.

I may not have handled the variable properly but what's the correct code?

Thanks in advance!!
 
I had taken this piece of code from some site and was trying to find out the name from many days. Thanks for that.

What you want to do is use EMA(Close, 5) instead of Close to trigger Buy and Sell?

Code:
Thank you for your help abhig,
But this is not what i want..
KPL trigger level must be like a horizontal line which will be triggered with EMA5.
for instance,
Buy signal is triggered by crossing tsl level by EMA5..
if the prices will continue to raise. No problem.. Just follow the trend..
BUT
after creating buy signal with EMA5 , if the prices come back ,
it must create a sell signal by crossing tsl level by EMA5.
By this way ; you can change your trend way with a small loss..
There is no stoploss on this system..
i m sure this way will make profit higher..
but it must be backtested..
 
Simple maths. Buyprice * 0.01 = 1% of buy price. You should have done Buyprice* 1.01 = 1% profit of your buyprice.

Now Sell is what we call a boolean variable in programming. It takes either true or false or lets say 1 or 0. Anything that is not 0 is 1. So Buyprice * target is always true unless either of them is 0.

Explain what you are trying to do so that we can help you according to that.
Thanks abhi for pointing out! Yes it was a typo and in the code I had actually written 1.01 (or similar).

I have been trying for some time to write a tsl/sl formula but could not control the variables, though I believe the logic is OK. Here it goes,,,,,

Sell= (Short) OR (NOT Buy AND IIf ((H>=BuyPrice*tslthreshold + (HighestSince (Buy,H,1)-BuyPrice*tslthreshold)*tslfactor),1,0)) OR L<=BuyPrice*slfactor OR exit ;;
Cover= (Buy) OR (NOT Short AND ( IIf ((L<= ShortPrice*(2-tslthreshold)-(ShortPrice*(2-tslthreshold)-LowestSince (Short,L,1))*tslfactor),1,0))) OR(H>=(2-slfactor)*ShortPrice) OR exit;

Tslthrehold=the minimum profit level where the trailing starts to work
tslfactor=percent of erosion of maximum profit (factor, >0 and <1)

PROBLEMS FACED

The exit levels sometimes work and sometimes don't. I want the tsl activated at price x tslthreshold and to exit when the max profit beyond activation falls by a factor of tslfactor.

EXAMPLE

Let tslthreshold=1.005 and tslfactor=0.8

Buy at 100, so activation level=100*1.005=100.50
price reaches 102, tsl activated at 100.5. now price falls to 101. tsl triggered=exit @ 100.5 + (102-100.5) x 0.8 =101.70

SPECIAL REMARKS

I want my trade to exit by TSL/SL only, as per risk and profit appetite and not by any indicators.


Though I think it is an unnecessary complication, I added the IIf function after getting frustrated with the other attempts.

Thanks again!
 
You wanted instead of close the trades be triggered using EMA of close with period 5. Now this is what I did. Check line 5. Now the original code you provided is made for taking only long positions. To be able to take short positions you need to add short and cover.

On second thought, do you want your tsl be made using EMA5 instead of close?
You are right..
tsl must be made by using ema5 instead of close..
and code can be used for long and short positions and there is no stoploss.

in the original code tsl triggered by Close..
but in my system tsl must be triggered by EMA5..
 
I think this is what I just did. You can add short and cover if you like.
I have also added short and cover but it doesnt make backtesting..?

How can i backtest your code?

//AFL by Kamalesh Langote. Email:kpl@...
no=Param( "Swing", 2, 1, 255 );
tsl_col=ParamColor( "Color", colorCycle );

EMA5 = EMA(Close, 5);

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);
Buy=Cross(EMA5,tsl);
Sell= Cross(tsl,EMA5);
Short = Sell;
Cover = Buy;

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase);
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shape,IIf(Buy,tsl_col,tsl_col),0,IIf(Buy,Low,High));
SetPositionSize(300,spsShares);
ApplyStop(0,1,10,1);
 
Hi abhig, but that is the 2 lines of code I am trying to write on tsl/sl and that is all there is to it. Kindly tell me what is the error in the code.

Buy, Shorts are simple crossovers of EMA 10 and 20, just for a trial.
 
Last edited:
Abishekji, I hv answered your queries in bold fonts. Pls have a look at it.

I changed your code some more. But still couldn't test it as no signals are coming on my charts. I did get some values while exploring but backtests were empty for me as well. Not sure what is causing the problem.

Yes, that is the main issue. And that is the obvious target of amending the afl. Not sure what is causing the problem.

// This code might give you good results but able to get them in real time might not be possible.

This code scans in real time, gives correct values.

//Also do you want your order to fill only when the price reaches high low of previous candle or just anytime?

Yes, I want to get filled when price crosses prv bar high or low, when all other conds r satisfied. That is mentioned as cond8 & cond 15 in the afl.

// This will filter better.
Buy = BarsSince(Buy)<BarsSince(Sell) AND Buy;
Sell = BarsSince(Buy)>=BarsSince(Sell) AND Sell;
Short = BarsSince(Short)<BarsSince(Cover) AND Short;
Cover = BarsSince(Cover)>=BarsSince(Short) AND Cover;

This code gives excess sell signal and not giving original cover signal.
I think if it can be sorted out, we can get backtest results properly. Hope exit creates the problem.

This chart shows the excess signals.


This system is the modification of the system mentioned in this link. http://www.traderji.com/position-trading/69080-reversal-bar-cci-day-position-trading.html

Thanks for rangarajanji who brought it to my notice.

Thanks to you abishekji. You are doing a great help here for fellow traders w/o any expectations. Hats off to you. Thank you very much.

Regards,
 
Last edited:

Similar threads