Help on afl..partial booking

#11
sorry rajivir i am not so adept in afl coding that i could even understant the code u have posted..

@Debarghya ..i read some help files etc and could get till this levle ... still doesnt run :-(...can you help

a=0;
b=5;
IIf(Buy==1 AND a==0 AND Close<Ref(Low,-BarsSince(Buy)),a=1,a=0);
IIf(Buy==1 AND a==0 AND Close-BuyPrice>(Close-(Ref(Low,-BarsSince(Buy)))+b),a=2,a=0);
IIf(Buy==1 AND a==2 AND Cross(EMA(Close,Y),Close),a=3,a=0);
IIf(a==2 OR a==3,SetPositionSize(50,3),SetPositionSize(100,3));
IIf(a>0,Sell=Close,a=0);
 
#12
Okay if i understand properly you want to have 2 ways of scaling out ?

It is simple to do

let's say
doScaleOut1 = .............. ;
doScaleOut2 = .............. ;

are the two conditions which when true you want to execute (i am sure by now you are adept in writing the internal conditions for doScaleOut1 and doScaleOut2).

now, since both of them have different positionsizing, following line will do that

Buy = IIF(doScaleOut1 OR doScaleOut2, sigScaleOut, Buy);

scalingOutPositionSize = IIF(doScaleOut1, 50, 100); // in this i have conveniently not included position sizing for Buy condition, which i am sure you will be able to chang it to if needed

SetPositionSize(IIF(Buy == sigScaleOut, scalingOutPositionSize, False), spsPercentOfPosition);
//again in this one, I have not included position sizing for Buy, please add that if needed




Also i have given the solution to your other problem in that thread
http://www.traderji.com/amibroker/6...us-open-position-same-security-amibroker.html
 
#13
sorry, just now realised that position sizing of doScaleOut2 is either 50 or 100 depending upon it came before doScaleOut1,

in that case, instead of the value of scalingOutPositionSize as given in previous post, use the one given below

scalingOutPositionSize = IIF(BarsSince(doScaleOut1) > BarsSince(doScaleOut2), 50, 100);
 
#14
thank a ton mate.. i will get my head around this and get back.. i have not used doscaleout ....in the other thread i just looked at your code and tried to make sense of it.. but what i arrived at was not enough.. i ll study what u have suggested and get back with what i have..

thanks again..

regards
hadit
 
#15
aditya after reading both your posts i tried to combine both the posts .. i have arrived at some set of codes however they are not working ...for some reason my i am unable to send you a pm it is blocked or something.. i have written to the admin but till then here is what i have..

//i have taken exmaple of this simple ema cross over signal system.
//lets Say Buy is at EMA Cross over AND the Sell is at the condidionts mentioned below...its just that this is NOT generating any trades at all
//i am trying to generate multiple sell signals for one buy signal.. ie multiple ways in which the position can be exited..and in multiple portions
//what i am trying here is to exit 50% at points enough to make my position breakeven ..even if after first exit the rest hits stoploss
//second i want the rest 50% to run free till the trend is over.. in this case i am judging the trend by ema cross over..
//third thing if the price goes below stop loss before even achieving the first target then book loss on whole position
//can you please correct this and point the mistakes so that i know where i was wrong...i should say where all i was wrong ;-)


SetPositionSize(100,4);//to analyse the system on trading just 2 lots..
Buy= Cross(Close,EMA(Close,34));
Sell=0;

lastBuyPrice = ValueWhen(Buy,BuyPrice,1);
brokeragePoints=5;
stopLossInPoints = abs(LastBuyPrice - Ref(Low,-BarsSince(Buy))); //as i want the stop loss to be the low of the bar i bought at.
FirstExitPrice = LastBuyPrice + stopLossInPoints + brokeragePoints;//earning sl + brokerage will make rest 50% position free

doScaleOut1 = Buy==1 AND Cross(Close,FirstExitPrice);//first 50% to be sold at this price
doscaleout2=Buy==1 AND Cross(EMA(Close,34),Close); // second 50% to be sold at this price..this is to allow the trade to capture the whole move...in this case let assume the second exit is at ema,price cross over

Sell = IIf( doScaleOut1 OR doscaleout2, sigScaleOut, Sell);
PosSizeScaleOut = 50;
SetPositionSize(IIf( Sell == sigScaleOut , PosSizeScaleOut, 100 ), spsPercentOfPosition); //i have replaces flase with 100 thinking that the position should be exited 100% in event of stoploss hit

ApplyStop(stopTypeLoss, stopModePoint, stopLossInPoints); //this is in case the stop loss hits..

thanks for the help and guidance ..
 
#16
Hardit

Well i dont' know what the problem with PM is (it is enabled !) (buy anyways, I have activated subscription of posted threads (for the time being till PM is fixed).

see, you got one thing wrong,
To scaleOut you dont' use Sell, you use Buy, i.e Buy = sigScaleOut means to exit (partially), (yes it is counter intuitive, but may be for some reason Tomasz thinks this way!!)

so in the code change it to
Buy = IIf( doScaleOut1 OR doscaleout2, sigScaleOut, Buy);
and let me know if that solves the problem ! Also if you tell me which market/instrument you are using this (and in which TF), so that I may run the same and see if there is any problem.
 
#17
okay..well i thought u had used flip(buy,sell) in the earlier post.. which makes the trade do an opposite scale out.. huh well that a good thing to know..
here u would notice i have removed intrade=flip(buy,sell) hope that wont affect me using buy with doscale right away..

i am using this for nifty futures and kinda trying to optimize it on 15,30 and 60 minute to see which tf give the best return.. right now i have kept the entry very simple coz i first want to get this right and then i probably can build upon later as my knowlege of afl grows..

i will definately run this and get back by eod (in office :-(..)

Thanks a lot mate for all the efffort an patience u taking in guiding me ... :)
 
#18
well, no flip doesn't reverse the signals ! (though you might think that by the nomenclature !! (actually here flip is more like flip-flop !! (have you seen 2 way switches in house , its this)

intrade = flip(buy,sell); // this line will make the intrade to 1 when first buy occurs and it will remain 1 for all the bars until sell has occured (if you see the e.g of exrem you will understand it is doing the opposite (thus this is used to know if we are currently in the trade (hence the name InTrade)

not using intrade in this e.g will not affect the code (I am hoping !!) but soon your programs will scale into larger size and complications where speed and simplicity will be higher priority (you don't want to process doScaleOut if you are not in trade, you get it right ?)

I am more than glad to help ! Good thing is you are trying to learn and do lot of stuff yourself (exploring) (which is good in long run), unlike few others who just want spoon feed code !!!

anyways, have fun, explore more and remember the AB User guide (in .pdf) is available in amibroker's site and it is a very good tool. May I also suggest 'Introduction to Amibroker' by howard bandy and also two of his other books, (both of which uses AB as platform source and examples)
 
#19
Okay i got it now.. yes i understand that as things progress and i get better on it.. i would aspire to do more complex stuff so simplicity will become important...

Thanks a lot for the books suggestion it would be great to get reading material specific to AFL..and i have been refering the help files all this while ....which is where i have learned what ever i know so far.. will read the books you suggested so that i get more insight into how it works..

Regards
Hardit
 
#20
Hi Aditya..

i was trying different permutation combinations and follwing is what i got..

when i change the sell to buy for doscale out along with changing the position size to false from 100 in the iif statement.. it works but gives only 3 signals for a period of 5 years ..so i dont think it is working properly

if i change 100 to false and keep sell in doscaleout statement it works and is giving me 800+ signal on 15 min .. but still i dont think the code is working properly as i checked a couple of trades and all of them are not exiting properly.. some are waiting for cross over even when sl is hit .. some are getting exited where actually a buy signal should be generated..

i still doing trial and error..meanwhile if in case anything srrikes you as possible errors ... please let me know ..

regards
hardit
 

Similar threads