Print Page | Close Window

Stop loss issue using highest

Printed From: BullCharts Forum
Category: BullCharts
Forum Name: BullScript
Forum Discription: Technical discussion related specifically to the BullScript programming language.
URL: http://www.bullcharts.com.au/forum/forum_posts.asp?TID=593
Printed Date: 24 Apr 2025 at 12:50am
Software Version: Web Wiz Forums 9.69 - http://www.webwizforums.com


Topic: Stop loss issue using highest
Posted By: krickaby
Subject: Stop loss issue using highest
Date Posted: 08 Nov 2009 at 12:34pm
Hope someone can help with a problem with a indicator I am creating.
 
I am trying to create a stop loss that calculates the highest closing price since purchasing a stock.  The stop loss works provided that the stock has been going up, however if the purchase price is less than a high x times ago before purchase date it calculates the stop based on that high. 
 
The issue is that I calculated the number of bars between purchase date and today, I then do a highest.  But as the highest starts from the day of purchase it then ends up going back x number of bars before purchase.  I tried to make the bar value adjust based on the NOW which should be the bar being processed but Bullcharts returns and error which is "Error905: Object reference not set to an instance of an object.  If I replace the BARS variable in the highest function with a number then error goes away but indicator returns wrong values.
 
To see this exact problem use WOW on a weekly and set the number to replace BARS as 32, the indicator has the defaults set for the rest of the values which should work.
 
Any help on this would be aprreciated.
 
Thanks

[Target=Price; Author=Rickaby; Period=Weekly; Category=Stop Loss; Description="Used to have trailing stop loss based on the number of shares purchased and how much money to risk. Calculates stop loss based on highest weekly close since purchased. Need to select 'This indicator is relevant to this security' only in the advanced tab when creating." ]

Dataarray:=input("Enter Type. 1=o, 2=h, 3=l, 4=c", 4, 1, 4);

Value2:=If(Dataarray=1,O, If(Dataarray=2,H, If(Dataarray=3,L, C)));

Dollar:= input("Enter MAX dollar loss", 500, 1, 10000);

Noshares:=input("Enter number shares", 291, 1, 500000);

Purchasedate:=inputdate("Purchase Date",date(2009,03,24));

Bars:=if(now>Purchasedate, datediff("W", Purchasedate, now) ,0);

Highvalue:=if(now>Purchasedate,Highest(Value2,Bars),undefined);

Stoploss:=Highvalue-(Dollar/Noshares);

[color=red; linestyle=dotted; width=2]

if (now>Purchasedate,Stoploss,undefined);

[color=blue; linestyle=Bar; width=1]

if(OnOrSkipped(Purchasedate),h,undefined);




Replies:
Posted By: rcameron
Date Posted: 08 Nov 2009 at 4:21pm
I can't help with a solution to your issue after having looked for a similar problem for some time. However, a small tip may help you on the coding, but you may already know it. You can use the expression function to select H, L, O, C, V rather than the Dataarray  variable and If statement logic you've used. Eg expr:= expression("Select parameter",C) and substitute expr for Value2 in the formula. Cheers Rod


Posted By: maximo
Date Posted: 09 Nov 2009 at 12:46am

Here's a way of auto offsetting from your purchase date:

Barnumber ; returns the number of the current bar.

LastValue(Barnumber) ;returns the number of the last bar in the chart.

You can subtract the barnumber of the exact date of purchase using the above

function of the last bar in the chart and then render any value or indicator :)


Posted By: krickaby
Date Posted: 12 Nov 2009 at 10:35pm
Thanks for your help with this, the barnumber and lastvalue were the trick that worked.
Here is the final indicator that is working.
 

[Target=Price; Author=Rickaby, Kim; Period=Weekly; Category=Stop Loss; Description="Used to have trailing stop loss based on the number of shares purchased and how much money to risk. Calculates stop loss based on highest weekly close since purchased. Need to select 'This indicator is relevant to this security' only in the advanced tab when creating." ]

Pricetype:=expression("Enter Tpye. O,H,L,C",c);

Dollar:= input("Enter MAX dollar loss", 500, 1, 10000);

Noshares:=input("Enter number shares", 1, 1, 500000);

Purchasedate:=inputdate("Purchase Date");

Bars:=lastvalue(barnumber)- datediff("W", Purchasedate, systemdate)-1;

Startofweek:=if(dayofweek(Purchasedate)=1,Purchasedate, dateadd("d",-dayofweek(Purchasedate)+1, Purchasedate));

Highvalue:=if(barnumber>=Bars, highest(Pricetype, barnumber-Bars), hist(c,Bars));

Stoploss:=Highvalue-(Dollar/Noshares);

[color=red; linestyle=dotted; width=2; Name=Stoploss]

if (now>=dateadd("w",-1, Purchasedate), Stoploss, undefined);

[color=blue; linestyle=Bar; width=1; Name=Purchasedate]

if(onorskipped(Startofweek), L, undefined);



Posted By: maximo
Date Posted: 14 Nov 2009 at 4:10pm

Brilliant work Kim :)

 




Print Page | Close Window

Bulletin Board Software by Web Wiz Forums® version 9.69 - http://www.webwizforums.com
Copyright ©2001-2010 Web Wiz - http://www.webwiz.co.uk