Jump to content
fjmocke

DAS - Calculate Shares based on account risk %

Recommended Posts

Hi everyone,

I've seen most people calculating shares to trade as a quick estimate.  I like to trade a fix risk percentage for risk management and my brain unfortunately struggles to do the calculation in one second!  Below is script for buy and sell that you can configure a hot key with to automate the calculations for you.

What is important is that you enable 'Double click to trade' in your chart settings (Configure).  Double clicking on any area in the chart will save the Price, which is your stop loss price.  The script will then calculate the amount of shares required, and open the trade for you in one go.

The 125 value in the script is the $ amount you want to risk.  Change this as your account grows.

Hope it helps, and let me know if there is a bug in the scripts.

Buy

Price=Ask-Price + 0.01;Share=125 / Price;ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+;BUY=Send

Sell

Price=Price-Bid + 0.01;Share=125 / Price;ROUTE=SMRTL;Price=Bid-0.10;TIF=DAY+;SELL=Send

Edited by fjmocke
  • Like 4

Share this post


Link to post
Share on other sites

Does this script allow you to buy $125 worth of a stock (i.,e 5 shares x$25), or does it allow you to risk $125 max based on a stop price?

Share this post


Link to post
Share on other sites
8 minutes ago, Robert H said:

Does this script allow you to buy $125 worth of a stock (i.,e 5 shares x$25), or does it allow you to risk $125 max based on a stop price?

Hi Robert,

Second option

The script calculates the amount of shares required to risk the given amount, based on the selected stop loss (double clicking on the chart).  I have tested it and seems to work fine.

What would be nice is to automatically draw a line of where the stop loss was selected.  However that is beyond DAS Trader unfortunately.  

Edited by fjmocke

Share this post


Link to post
Share on other sites

That's amazing!

I wonder if you can expand on the script and use the same stop price to send a STOP order? So basically, you double-click to set the stop price and hit the hotkey which sends an entry order along with a stop order.

Share this post


Link to post
Share on other sites
1 hour ago, Robert H said:

That's amazing!

I wonder if you can expand on the script and use the same stop price to send a STOP order? So basically, you double-click to set the stop price and hit the hotkey which sends an entry order along with a stop order. 

That should definitely be possible if there was an interim variable available to save the stop price.  The problem is I'm using the 'Price' variable which is overwritten when the buy/sell order is executed.  My communications with DAS suggested there is no such variable.  This won't be a problem if a stop can be placed before the buy/sell order, however I don't think that will be possible.

UPDATE 

The stop price could potentially be 'back-calculated', as the shares are available:

x = 125 / Share

Stop Loss = Ask - x or Bid + x (depending on long/short)

Scripting is DAS is very primitive, so will have to test if this works

Edited by fjmocke

Share this post


Link to post
Share on other sites

I figured out how to use this script and it works great. Now, I just have a question. I have zero knowledge when it comes to scripting or coding so I was just wondering what the highlighted part does. Thank you!

Capture.PNG

Edited by Jones

Share this post


Link to post
Share on other sites

Hi all,

 

I've started using this hotkey and it's working well. One question I have is: what does the red highlighted part do? Why add another price=ask+0.10 when we already have a price=ask-price+0.02.

 

Price=Ask-Price + 0.02;Share=100 / Price;ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+;BUY=Send

 

I'd be very grateful for any help 🙂

 

Share this post


Link to post
Share on other sites

I’ve been using this script a bit as well and I like it apart from when it doesn’t let me have a 10 cent stoploss because it’s trying to buy more shares than I have BP for 😆 (depending on the stock price of course)

To answer the question about the +0.1, I’ve noticed that when you double click on the chart (say at 50.05) the price in the montage is set at 50.04, thus adding the 0.1 gives you the original price for your stoploss.

Share this post


Link to post
Share on other sites

Thanks DC - I appreciate your help. Sadly, I'm still not quite sure on the differences between:

Price=Ask-Price + 0.02;

and

Price=Ask+0.10

 

Can you shed any light on it please?

 

Share this post


Link to post
Share on other sites

I’m not really up on scripts but the way I’m interpreting this particular one is (Price=Ask-Price+0.01) translates to doing a quick sum to work out your stoploss size from where you double clicked on the chart compared to what the Ask price is at that particular time.

Say the (ASK) is 50.00 and you double click on the chart at 40.80 (PRICE) because you want a 20 cent stoploss. It will take the 50.00, minus the 48.80 and leave you with your .20 stoploss size (Ask-price). The +0.01 part is because for some reason when you double click the price on the chart, DAS decides to take away 1cent before it puts that price into the montage (so if that wasn’t in the script your stoploss would be 19 cents, not 20 cents). I think it would read easier if it were (ask-price=stoploss size). 

So now the script has determined your stoploss size (Price=Ask-price)  it will continue on to working out you share size from that initial sum, and then finally on to price=ask+0.10 part which works the same as our normal scripts, buy those shares at 50.00 but if that price is no longer available, pay anything up to 50.10 but no higher. 

I hope this helps, sorry about the wall of text (well it is like a wall on my phone anyways lol)

 

Share this post


Link to post
Share on other sites

DC,

Thanks man, I think I get it now, so correct me if I'm wrong.

For example: Going long

Stop Loss Price: 50 (when you double click, montage will show 49.99)

When you press your hotkey with this script: Price=Ask-Price + 0.01;Share=125 / Price;ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+;BUY=Send

(ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+;BUY=Send) This is your normal marketable limit order going long getting in at a price no more than 0.10 cents from the asking price.

So assuming you get in at exactly 50.20, your order will be sent with a calculated share size like so.

Script: Price=Ask-Price + 0.01;Share=125 / Price;ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+;BUY=Send

Result: 0.20=50.20-(49.99+0.01);625=125 / 0.20;ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+;BUY=Send

So I guess the (+ 0.01) is just to fix the price that's on your montage when you double click on your chart?

Edited by Jones

Share this post


Link to post
Share on other sites

Thanks for a really useful way to keep position sizing logical, even in the heat of the moment.  It's a shame that when you've sorted out a trade with a really tight stop it tends to get thrown out for having insufficient buying power.  Is DAS capable of handling the logic of ordering the lesser of two numbers, and is there a member of BBT able and willing to supply the code?

Share this post


Link to post
Share on other sites

This script is really great! I saw it in this weeks Bearbull Buzz and traded with it today. I didn't have the problem with insufficient buyong power but I could see that happening. It WOULD be nice if you could set a cap somehow. The best way I found to use it is to draw a line at the stop loss I want as the pattern is setting up & the double-click and hit the hot key when I'm ready. 

One thing I found a little confusing is that it didn't trade on the double-click, but on the hot key press. 

It's also worth noting that this script is using the $0.10 window instead of the $0.05 that is in Andrews script for the buttons if you're using those. 

 

Share this post


Link to post
Share on other sites
On 8/30/2018 at 7:16 PM, fullmoonguru said:

This script is really great! I saw it in this weeks Bearbull Buzz and traded with it today. I didn't have the problem with insufficient buyong power but I could see that happening. It WOULD be nice if you could set a cap somehow. The best way I found to use it is to draw a line at the stop loss I want as the pattern is setting up & the double-click and hit the hot key when I'm ready. 

One thing I found a little confusing is that it didn't trade on the double-click, but on the hot key press. 

It's also worth noting that this script is using the $0.10 window instead of the $0.05 that is in Andrews script for the buttons if you're using those. 

 

On 8/30/2018 at 4:11 PM, Peter S said:

Thanks for a really useful way to keep position sizing logical, even in the heat of the moment.  It's a shame that when you've sorted out a trade with a really tight stop it tends to get thrown out for having insufficient buying power.  Is DAS capable of handling the logic of ordering the lesser of two numbers, and is there a member of BBT able and willing to supply the code?

On 8/29/2018 at 2:15 AM, DC said:

I’ve been using this script a bit as well and I like it apart from when it doesn’t let me have a 10 cent stoploss because it’s trying to buy more shares than I have BP for 😆 (depending on the stock price of course)

To answer the question about the +0.1, I’ve noticed that when you double click on the chart (say at 50.05) the price in the montage is set at 50.04, thus adding the 0.1 gives you the original price for your stoploss.

Re: Over Buying Power

Not fully tested so USE ONLY IN SIM for the time being, but this might work. Note that we have to do some tomfoolery since DAS doesn't allow actual math on a lot of things, so the roundabout way of doing this is to get around the limitations. I'll explain what's going on down below.

This should load in the shares based on an equation of 1% of your account value. In DAS Simulator, Buying Power is 4 * account balance, so by default that's 25,000 * 4 = $100,000. If you wanted to risk 1% of account value as Andrew says (as the maximum, you may chose .5%), that'd be $250 dollars risked. 

 

Buy:

SShare = BP * 0.25  * 0.01 * Price; Price = Ask-Price + 0.01; Share = SShare / Price;  TogSShare; ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+; BUY=Send;

SShare = BP * [reversing the leverage given, so 4:1 is a decimal of 0.25 = 25% of buying power]  * [decimal of your risk percentage, 0.01 = 1%] * Price; Price = Ask-Price + [this is an offset in the initial equation]; Share = SShare / Price; TogSShare; ROUTE=SMRTL;Price=Ask+ [this is the fill-zone you use to make sure you get filled, Andrew uses 0.05 I believe] ;TIF=DAY+; BUY=Send

Sell:

SShare = BP * 0.25  * 0.01 * Price; Price = Price-Bid + 0.01; Share = SShare / Price;  TogSShare; ROUTE=SMRTL;Price=Bid-0.10;TIF=DAY+; SELL=Send;

 

So what's happening (I'll use the Buy example, but applicable to both):

1) SShare = BP * 0.25  * 0.01 * Price; ---> This is setting a "Share Display" box to equal the BP * 0.25 * 0.01 * Price. It's worth noting that using the BP function (buying power) in DAS is actually divided by symbol price (so BP * 0.25 = $100,000 * 0.25 = $25,000 / PRICE). The 0.01 in this equation is the decimal for 1%. If you wanted to risk 2% of your account value, it'd be 0.02.

BP = 100,000 / PRICE --> If Stock Price = $10, this would be 10,000 --> 10,000 * 0.25 = 2,500 --> 2,500 * 0.01 [1% risk] = 25 --> 25 * PRICE [$10 for example] = $250 --> $250 is the amount we're willing to risk on this trade.

Side Notes: A) If you're wanting to do 1% of BUYING POWER, I'd recommend setting the "decimal of account equity" (0.25 up above) to 0.90, as this gives you a math cushion to not get rejected for lack of buying power.
B) If you want to do a fixed dollar amount, you may need to play around with the ratios a tad. Setting SShare to a fixed price will result in the same issue of not having enough buying power in certain scenarios.

2)  Price = Ask-Price + 0.01; --> Untouched from the original. It's essentially the Level 1 ASK Price minus the chart-clicked PRICE + 0.01 (offset value). For this example, let's assume we wanted the stock price on ASK was $10.00 and we clicked the chart at $9.95 --> Stop Distance = $0.05

3)  Share = SShare / Price; --> Instead of the assigning the default 125 / Price (for $125 / Price), we use our stored value of $250 so it becomes $250 / Price. For the $10 example, this would be 250 / stop distance of $0.05 = 5000 shares

4)  TogSShare; --> This toggles the "Share Display" box that we used to store our temporary value to off, releasing the variable.

5)  ROUTE=SMRTL;Price=Ask+0.10;TIF=DAY+; BUY=Send; --> Unchanged from the original.

 

 

Some Gotchas:

We have to pass from float to int values in memory a few times [DAS wants INT for the display box], this will "lop off" anything after the decimal. e.g. 5.85 becomes 5. So it might not be mathematically exact, but close enough for our purposes.

 

Edited by KyleK29
Clean Up
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using this site, you agree to our Terms of Use, Privacy Policy and use of We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..