BullCharts Forum Homepage
Forum Home Forum Home > BullCharts > BullScript
  New Posts New Posts RSS Feed: Stop loss issue using highest
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Stop loss issue using highest

 Post Reply Post Reply
Author
Message / View First Unread Post
krickaby View Drop Down
Newbie
Newbie
Avatar

Joined: 08 Nov 2009
Posts: 2
Post Options Post Options   Quote krickaby Quote  Post ReplyReply Direct Link To This Post Topic: Stop loss issue using highest
    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);

Back to Top
rcameron View Drop Down
Moderator Group
Moderator Group


Joined: 04 Aug 2008
Posts: 21
Post Options Post Options   Quote rcameron Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post 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 :)


Edited by maximo - 09 Nov 2009 at 3:28am
Back to Top
krickaby View Drop Down
Newbie
Newbie
Avatar

Joined: 08 Nov 2009
Posts: 2
Post Options Post Options   Quote krickaby Quote  Post ReplyReply Direct Link To This Post 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);

Back to Top
maximo View Drop Down
BullCharts Guru
BullCharts Guru
Avatar

Joined: 02 Sep 2006
Location: Australia
Posts: 232
Post Options Post Options   Quote maximo Quote  Post ReplyReply Direct Link To This Post Posted: 14 Nov 2009 at 4:10pm

Brilliant work Kim :)

 

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.69
Copyright ©2001-2010 Web Wiz