Stop loss issue using highest |
Post Reply |
Author | |
krickaby
Newbie Joined: 08 Nov 2009 Posts: 2 |
Post Options
Quote Reply
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); |
|
rcameron
Moderator Group Joined: 04 Aug 2008 Posts: 21 |
Post Options Quote Reply 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
|
|
maximo
BullCharts Guru Joined: 02 Sep 2006 Location: Australia Posts: 232 |
Post Options Quote Reply 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 |
|
krickaby
Newbie Joined: 08 Nov 2009 Posts: 2 |
Post Options Quote Reply 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); |
|
maximo
BullCharts Guru Joined: 02 Sep 2006 Location: Australia Posts: 232 |
Post Options Quote Reply Posted: 14 Nov 2009 at 4:10pm |
Brilliant work Kim :)
|
|
Post Reply |
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |