[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: 'Simple!' dynamic applystop help please



PureBytes Links

Trading Reference Links

Hi,

Thank you for your code.

With something so apparently simple I was really hoping to it done 
without looping code.

Regards
Alan

Stephane Carrasset wrote:

>Hello,
>I was often dispointed with applystops, so i have written my own loop
>to manage differents exits.
>of course the code is a "little" long, but if you take some time to 
>try it you must find it usefull, below is an example to get two 
>differents profit, so you can change it to get two differents stoploss
>
>Stephane
>
>Title= "Rem afl";
>Setup=Cross( MFI(3),0);
>
>/*****************************/
>BuyCond= Ref(Setup,-1);
>Entryprice=O;
>Exitprice=Low;// or Close
>Stoploss=LLV(L,3)-0.75*ATR(10) ;
>Stoptrail=LLV(L,3)-1.75*ATR(10);
>Pf=7;
>Stopprofit=(1+(Pf/100))*Entryprice;
> 
>
>//Initialize
>Buy=Sell=0;
>SellPr=Null;
>TrailPr=Null;
>TargetPr=Null;
>StopLossPr=Null;
>lastbuyprice=0;
>stoplossprice=0;
>profitprice=0;
>trailingprice=0;
>oldtrailingprice=0;
>trailingstop=0;
>Count=0;
>
>Empty=IsNull(stoploss) OR IsNull(stoptrail) OR IsNull(stopprofit);
>
>for( i = 0; i < BarCount; i++ )
>{
>	if(buycond[i] AND NOT(Empty[i])) 
>	{
>		Buy[i]=1;
>		Sell[i]=0;
>
>	}
>/*Remove Buy signals if trade was already initiated*/
>	if(lastbuyprice>0 )
>	{
>		Buy[i]=0;
>		Count++;
>	}
>
>/*get the buyprice AND the stoplossprice AND the profitprice*/
>	if (lastbuyprice == 0 )
>	{
>		if( Buy[ i ] == 1)
>		{
>			trailingstop = 0;
>			trailingprice  = 0;
>			oldtrailingprice = 0;
>			lastbuyprice= entryprice[i];
>			stoplossprice=stoploss[i];
>			profitprice=stopprofit[i];
>		}
>	}
>
>//	CONDITIONS EXIT
>
>	if(lastbuyprice>0)
>	{
>//StopLoss price
>		StopLossPr[i]=stoplossprice;
>//stopTrail price
>		trailingprice = stoptrail[i] ;
>		if (trailingprice < oldtrailingprice)
>		trailingprice = oldtrailingprice;
>		else
>		oldtrailingprice = trailingprice;
>		TrailPr[i]=trailingprice; // pour connaitre l'array 
>du trailing price init;
>//Target Price array
>		if(Count<5)
>		TargetPr[i]=profitprice;
>		else
>		TargetPr[i]=profitprice + profitprice-trailingprice;
>
>		if (Exitprice[i] <= Max
>(stoplossprice,trailingprice ))//stoploss
>		{
>						if(Exitprice[i]==Low
>[i])
>						{
>							if(Open[i]
><=Max(stoplossprice,trailingprice )) 
>								SellPr
>[i]=Open[i];
>							else
>								SellPr
>[i]=Max(stoplossprice,trailingprice );
>						}
>						else
>						{
>							if(exitprice
>[i]==Close[i])
>								SellPr
>[i]=Close[i];
>						}
>
>						Sell[i]=1;
>						lastbuyprice=0;
>						stoplossprice=0;
>						profitprice=0;
>						trailingprice  = 0;
>						oldtrailingprice = 0 ;
>						trailingstop=0;
>						Count=0;
>		}
>		else
>		{
>			if( High[i] > TargetPr[i] ) 
>//	trailingstop  stoploss
>			{
>				if(Open[i]> TargetPr[i] ) 
>					SellPr[i]=Open[i];
>				else
>					SellPr[i]= TargetPr[i] ;
>					
>				
>					Sell[i]     = 1;
>					lastbuyprice  = 0;
>					stoplossprice  = 0;
>					profitprice   = 0;
>					trailingprice  = 0;
>					oldtrailingprice = 0;
>					trailingstop=0;
>					Count=0;
>			}
>		}
>	}
>
>	if(Sell[i]==0)
>	SellPr[i]=Close[i]; 		
>	nCpt[i]=Count;	
>}
>
>
>Plot(Close,"Close",IIf( Buy, colorGreen, IIf(Sell , 
>colorRed ,1 )),64);
>PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-20); 
>PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-20);  
>
>Plot(TrailPr,"",colorGreen,1);
>Plot(StopLossPr,"",colorRed,1);
>Plot(TargetPr,"",colorBlue,1);
>Plot(ValueWhen(Sell,SellPr),"SellPr",colorYellow,1);
>//Plot(ncpt,"",colorBlue,1);
>  
>  
>
>>Thank you for pointing out the flaw. I changed the code from day() 
>>    
>>
>to
>  
>
>>datenum() which gives a unique day for the trade.
>>
>>
>>StopLvl = 0.0030;
>> 
>>Buydate = ValueWhen(Buy,Datenum()); // Determine buy date
>> 
>>StopLvlWid = 0.0010;
>> 
>>StopLvl = IIf( Datenum()!=Buydate, StopLvl+StopLvlWid, StopLvl);
>> 
>>ApplyStop(stopTypeLoss, stopModePoint, StopLvl, True, True);
>>
>>
>>With an addcolumn, I can see StopLvl changes from 0.0030 to 0.0040
>>when the date changes but the trade is still stopped out at 0.0030 
>>    
>>
>on
>  
>
>>the following day.
>>
>>Still puzzled.
>>
>>Regards
>>Alan
>>
>>
>>--- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <amibroker@xxxx>
>>wrote:
>>    
>>
>>>Hello,
>>>
>>>Your formula logic is flawed.
>>>
>>>Day() represents DAY NUMBER (1-31) not date,
>>>so
>>>ValueWhen(Buy,Day())
>>>will be true EVERY MONTH when day is the same as day of the entry
>>>(previous month(s))
>>>
>>>
>>>Best regards,
>>>Tomasz Janeczko
>>>amibroker.com
>>>----- Original Message ----- 
>>>From: "firehorse888uk" <firehorse888uk@xxxx>
>>>To: <amibroker@xxxxxxxxxxxxxxx>
>>>Sent: Friday, May 27, 2005 7:50 PM
>>>Subject: [amibroker] 'Simple!' dynamic applystop help please
>>>
>>>
>>>      
>>>
>>>>Hi,
>>>>
>>>>I'm backtesting forex.
>>>>
>>>>On the day of the trade, I want the stop loss to be 30pts.
>>>>On subsequent days, I want the stop loss to be 40pts.
>>>>Edited relevant section of code follows:
>>>>
>>>>StopLvl = 0.0030;
>>>>
>>>>Buydate = ValueWhen(Buy,Day()); // Determine buy day
>>>>
>>>>StopLvlWid = 0.0010;
>>>>
>>>>StopLvl = IIf( Day()!=Buydate, StopLvl+StopLvlWid, StopLvl);
>>>>
>>>>ApplyStop(stopTypeLoss, stopModePoint, StopLvl, True, True);
>>>>
>>>>
>>>>Can anyone see why the above code doesn't work.
>>>>I've tracked StopLvl and it changes correctly for the following
>>>>        
>>>>
>>day.
>>    
>>
>>>>I've set dynamic level to true so I don't understand why the 
>>>>        
>>>>
>trade
>  
>
>>>>stops out at 30pts rather than 40pts on subsequent days.
>>>>
>>>>Thanks
>>>>Alan
>>>>        
>>>>
>
>
>
>
>
>Please note that this group is for discussion between users only.
>
>To get support from AmiBroker please send an e-mail directly to 
>SUPPORT {at} amibroker.com
>
>For other support material please check also:
>http://www.amibroker.com/support.html
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>
>  
>



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Has someone you know been affected by illness or disease?
Network for Good is THE place to support health awareness efforts!
http://us.click.yahoo.com/Rcy2bD/UOnJAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/