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 ..
//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 ..
hi hardit, on giving a glance at your code, I found several errors (i stopped counting once my fingers weren't sufficient).
1. first I told you already, it will always be Buy = sigScaleOut and never ever Sell.
2. variable names are case sensitive, please check in the code, i find few instances of that
3. when you say doScaleOut1 = Buy ==1 and ..... , now this is ill-logical, because buy will be 1 only on the bar when there was original buy signal, and the buy signal will not remain 1 , so above line is actually trying to scale out in the same bar ??? that is why I had introduced InTrade variable, use InTrade variable instead of Buy inside that line
4. same for doScaleOut2
5. best way to debug would be to print the values of the variable using writeval() and see them bar by bar , because I suspect your calculation of stopLossInPoints and FirstExitPrice doesn't make logical sense, especially the part Ref(Low,-BarsSince(Buy)) , you know that this will give you the low of the bar in which buy took place and it will be constant till next buy ??
if this is what you wanted then you should rather use ValueWhen(Buy, Low,1);
do one thing
just save the following code in a fresh afl and plot it as indicator
Buy= Cross(Close,EMA(Close,34));
Sell=0;
lastBuyPrice = ValueWhen(Buy,BuyPrice,1);
xx = Ref(Low,-BarsSince(Buy));
stopLossInPoints = abs(LastBuyPrice - xx);
Plot(C, "c", colorBlack, styleCandle);
Plot(Buy, "buy", colorBlue, styleLine | styleOwnScale);
Plot(lastBuyPrice , "lastBuyPrice ", colorRed, styleLine);
Plot(xx, "xx", colorGreen, styleLine);
and observe the lines, add other criteria and observe the behaviour and make sure that the logic which you are trying to incorporate is correct.
all the best and god speed