Jump to content
KyleK29

DAS - Dynamically Calculate Shares on %Risk or $Risk - Hot Key + Configuration, Updated 12/13, v1.46

Recommended Posts

Updated: 11/19/2018 @ 8:20am (PST)

Finally out of the alpha stage and releasing this to the community, I've been using it with success. Because I had to do some musical chairs with memory I made a configuration utility as the script itself is very ugly.

This is more of a BETA release for this, so if anyone wants to try this out in SIM and let me know if you have any issues with the configuration sheet or the hotkeys themselves. It's based on the work started by @fjmocke here: https://forums.bearbulltraders.com/topic/469-das-calculate-shares-based-on-account-risk/ . 

What it is:

It's a hotkey command script that can be used to dynamically alter the share total based on:

  • Available Buying Power (capital)
  • Stop Location (Risk)
  • % Account Risk OR Fixed Dollar Amount

The script includes purchase power protection and won't send an order that you can not afford, it does this by calculating two factors:

A - Shares You Can Afford
B - Shares at Risk Parameter (e.g. $25,000 account equity, 1% risk = $250 risk, $250 * a stop distance of .10 = 2500 shares)
min{A,B} = 0.5(A + B - | A - B | )

But, why male models?

I just told you. /Zoolander reference

You'd use this to calculate your share total based on what you're willing to risk. So instead of blindly throwing 500 shares at every setup, you can dynamically alter risked amount based on the per-trade setup. I use it on my StreamDeck (will also release the icon packs soon) with modifiers of 100%, 75%, 50%, and 25%. 100% is the A-Plus setups I see, those I have HIGH confidence in. Alternatively, if a stock has a large spread or is low-float, I may only use the 25% modifier key for those.

Instructions for Configuration:

  1. Go to this link:
    BETA: v1.46 https://drive.google.com/file/d/1p1J5sFOXjEE1B9HOkaxtfc3cXSO2fnrV
    ^^ Requires DAS version 5.2.0.34 or above (current BETA branch as of 11/19/2018) for the physical stop portion to work. If you don't use the physical stop, you don't have to worry about it.
    v1.2 https://drive.google.com/open?id=1v45q-BJ86oaD3cXi0DAPeLraE2AojQ80
    NOTE: Version marked as BETA should be thoroughly tested in SIM to make sure it's doing what you expect it to do.
  2. Choose: Either "Download" [Excel Users] or "Add to My Drive" [Sheets Users]
  3. On "Setup & Instructions" configure your settings.
    1. Account Leverage (default for DAS is 4), this is the margin your broker gives you. Some off-shores give 6. It needs to match what is configured in DAS for proper calculations.
    2. Max Account Risk %. This is the maximum percent of equity you're willing to risk on every trade (default is 1%). You can always risk lower (more on that later).
    3. % of Total Buying Power. If you don't want to calculate based on the total buying power of 100%, you can set this to a lower percentage (example: 100,000 buying power with 60% here equals $60,000 maximum position size)
    4. Route. LIMIT, MARKET, SMRTL. Default is LIMIT.
    5. Order Bid/Ask Offset. This is the offset you use when you send the price for order, e.g. "Ask + 0.05" (meaning fill me up to 5 cents above ask)
    6. Time in Force. Default: Day+
    7. Default Shares. This is the amount of shares you want to set as the DEFAULT SHARES for all trades (e.g. when you click a Symbol and it loads,  this is the share total). You can see why this is here in the technical breakdown section below.
    8. Minimum Stop Buffer. This is an offset to the stop distance. If you set this to 0.05, it'll add 5 cents to the stop distance calculation (so if your stop distance is 0.05, it'll be calculated on 0.10).
  4. Switch to the "Hotkeys" tab.
  5. Choose your preferred style. % Risk of Equity (Dynamic) or Fixed Price (e.g. $150 risk).
    1. %Equity Risk: Use the drop down to select what you want the value to be % equity. NOTE: This is a modifier AFTER your account risk maximum %. So if you have 1% account risk, and set this to 50%, your effective account risk is 0.005 --> 0.5%.
    2. $ Fixed: Use the drop down to select what you want the value to be for dollar risk.
    3. Select "long" or "short" to flip the script's direction.
    4. Click the cell that contains the start of the command (E column) and Ctrl + C (copy). Paste it into DAS. It should look like a sample command below.

Instructions for Usage:

  1. First, you must have "Double Click to Trade" turned on in Chart, Right-Click --> Configure --> Settings --> Double-click to trade.
  2. Double click the chart where you want to set a mental stop (it does not place a stop order, you can always put one in after).
  3. Hit your configured hotkey.

 

Sample Scripts:

LONG:

DefShare=BP*0.98; Share=DefShare*0.25* Price * 0.01; Price = Ask - Price + 0.02;SShare = Share / Price; Share = DefShare - SShare; DefShare = DefShare + SShare; SShare = Share; SShare = DefShare - SShare; Share = 0.5 * SShare; TogSShare; ROUTE =LIMIT; Price = Ask +  0.05; TIF=DAY+; BUY=Send; DefShare = 500;

SHORT:

DefShare=BP*0.98; Share=DefShare*0.25* Price * 0.01; Price = Price - Bid + 0.02;SShare = Share / Price; Share = DefShare - SShare; DefShare = DefShare + SShare; SShare = Share; SShare = DefShare - SShare; Share = 0.5 * SShare; TogSShare; ROUTE =LIMIT; Price = Bid - 0.05; TIF=DAY+; SELL=Send; DefShare = 500;

 

Technical Breakdown:

DAS has basic scripting. Montage commands have access to very few read/write variables, basic operations, and only operators of addition, subtraction, division, and multiplication.  To do this calculation we need additional operators (min function, and absolute function) and more memory for storage of variables. 

This command gets around these limitations by using user-writeable areas of memory in the program. Since DAS is written in the C++ language (from what I can tell), it's strict on what can be done in these existing memory locations. The hotkey uses the following items (plus the usual Price -- FLOAT):

(Assumptions on Datatypes)
DefShare -- INT (Used as a temporary variable for storage)
SShare -- Unsigned INT (Behaves like an Unsigned INT in certain situations. Used as a temporary variable for storage)
Share -- INT (Used as a temporary variable for storage)

With the 3 INT variables, objects are moved around in memory so that we can calculate and compare with our variable limitation (be much easier if we could assign our own). To facilitate the ABS() function, we use a trick --> When a negative value is placed into an Unsigned INT it loses it's sign (thus, it becomes a POSITIVE value in memory).

A more detailed technical breakdown (step by step) is located in the Configuration spreadsheet up above.

Future Enhancements:

  • If need be, I can make a step-by-step video of this entire process.
  • I have a version that uses an AutoHotKey macro to drop a line at the stop location, I can upload that as well if people want it.
    • ^^ Update, I discontinued this as it was too cumbersome. You had to have two sets of hotkeys for each command. I may someday revisit it if I can build out a configuration tool for it.

TLDR:

It does the math for you so you can risk a known amount (% or $) based on your per-trade risk position (stop distance). And yes, I'm a bit of a tech nerd.

Also, longest post .. ever. Would not read again, 0/5 stars.

---

KNOWN ISSUES:

  • %Account Risk gets smaller and smaller when subsequent open positions
    • Reason: No Equity variable, we reverse calculate equity using Buying Power. On subsequent positions, the % (e.g. 1%) calculation will be based on the available buying power and NOT the account equity.
    • Workaround: Precalculate the %risk and use it for the $risk versions. So 1% of $25,000 equity equals $250.
  • SSR rejection on LONG position when scaling out; rejection message (e.g. "Short marketable limit order disable due to SSR!") if using the automatic STOP trigger.
    • Reason: DAS calculates that the position will drop below the open stop order position and reject as this can cause the position to "flip" if it was triggered.
    • Workaround: Have a hotkey to clear the open orders (CXL ALLSYMB), clear it, scale the position (e.g. 25%). Either replace the stop or switch to a mental stop. Alternatively, you can add "CXL ALLSYMB;" to the front of the scale-out hotkeys. You just have to be cognizant to replace the stop order.
  • Equated position size if very small (e.g. 4 or 5 shares when expected is hundreds).
    • Reason: Wrong side was used for the order. E.g. a long hotkey is used when trying to go short. -or- Stop Distance was calculated to be a negative value (clicked too close to current price).
    • Workaround: Be cognizant of the hotkeys used and the stop distance clicked. Clicking too close (a really tight stop) can be very dangerous if you do it inadvertently.
  • TriggerOrder for automatic STOP placement not being sent (no stop order placed).
    • Reason: Montage is not set to a style that doesn't allow TriggerOrder input. Styles not compatible are: Default [DAS's, if you changed it], Basic, OCO, Option, Full
    • Fix: Use a style that is compatible, they are: Stop Order, Detail, Trigger  -- I recommended using the "Stop Order" montage style. To change this, right click the montage area around where you'd enter a price and select Style --> Your Choice.

---

UPDATES:

10/17/2018

- Added v.1.1 link, you'd need to use the new version to change anything.
- General cleanup of the script. Added instructions for the IB issue (discussed in this thread)
- NEW FEATURE: Added a new section to the Hotkeys sheet, it will now create a set up for Dynamic Scale-In hotkey commands.  You'd use these by setting a scale value (say you want an additional 50% of your current position size). The hotkey will calculate the maximum share you can afford (how much you can afford at the moment) and the scale value, choosing to take the least amount. So if your current position is 1500 shares (@ $50.00) and you want to scale in at 50% your current position, it'd check if you can afford an additional 750 shares, if you can't, it'll buy the maximum you can afford. For this example, you can't afford it (if Buying Power is 100k), so it'd buy roughly $25k worth (500 shares).
- CLEANUP: Cleaned up the $Dollar Risk version and removed unnecessary steps. Don't really need to replace yours if they exist, but worth noting.

10/30/2018

- Added @Michael P's suggested fixes for Excel. Configuration tool should now work in both Sheets and Excel.
- NOTICE: This was a configuration tool change, no changes were made to the hotkey scripts, so no need to change any existing hotkeys.

11/19/2018
- Shortened some of the commands so we don't hit any hotkey character limit, makes them less readable, but shorter. Couldn't get them low enough to fit the montage buttons though (although removing the portions for the buying power rejection protection would likely do it).
- Added a section for SELL/COVER buttons for people who just need to create those. E.g. "Sell 25% position" or "Sell 33% position".
- Added @Robert H's stop suggestion. New fields on the setup page for enabling physical stops. If enabled, it'll place a MARKET or LIMIT (settings included) trigger order to go into the market once the initial order is fulfilled, these are placed at the location you double-clicked on the chart.

11/20/2018
- Added a stop-order setting to set an additional buffer for the stop price (for those that want to include or exclude the double-clicked price).
- Added conditional formatting to subdue the stop settings that aren't required if you disable sending a physical stop into the market.

12/10/2018
- Added a known issues section to this post and the spreadsheet (for when a new version goes up).

12/12/2018
- Updated known issues section to include the "Montage Style" issue for TriggerOrders.

12/13/2018
- Updated to new version 1.46. Fixed a bug in the Trigger Order script which could cause it to not be interpreted by DAS's command parser on certain user settings.
- Added "modifier" extra hotkeys. See instructions next to these on how to use them.
- - - Set Stop to Breakeven - Long or Short - Stop Limit or Stop Market (cancels any pending orders for SYMB)
- - - Set Stop to Breakeven - Bidirectional - Stop Market (cancels any pending orders for SYMB)
- - - Stop - Update Price - Long or Short - Stop Limit or Stop Market (cancels pending orders, double click chart where you want stop before firing hotkey)
- - - Stop - Update Price - Bidirectional - Stop Market (cancels pending orders, double click chart where you want stop before firing hotkey)
- - - Stop - Update Position - Long or Short - Stop Limit or Stop Market - Replace (requires you double-click the original stop in the Orders window)
- - - Stop - Update Position - Bidirectional - Stop Market Orders Only - Replace (requires you double-click the original stop in the Orders window).
 

Edited by KyleK29
Updated Version to 1.46
  • Like 3
  • Thanks 6

Share this post


Link to post
Share on other sites

Nice!! Thanks for putting in the time and work to take the script one step further 👍 

Share this post


Link to post
Share on other sites

Holy smokes. You are super smart sir.

I'm brand new and struggling to understand the setup of my initial Hotkeys. This provided version of yours is incredible. Thank you.

Now, if I can only figure out how to program this darn Genovation 24 keypad...but I digress.

seamus

Share this post


Link to post
Share on other sites

Great work @KyleK29 thank you!...

On 9/28/2018 at 1:37 PM, KyleK29 said:

I have a version that uses an AutoHotKey macro to drop a line at the stop location, I can upload that as well if people want it.

I´d like to take a look at this version...

Share this post


Link to post
Share on other sites
On 9/29/2018 at 11:07 AM, Seamus said:

Holy smokes. You are super smart sir.

I'm brand new and struggling to understand the setup of my initial Hotkeys. This provided version of yours is incredible. Thank you.

Now, if I can only figure out how to program this darn Genovation 24 keypad...but I digress.

seamus

Ha, I wouldn't go that far. 

I don't have experience with the Genovation, but a quick glance ( https://store.genovation.com/programmables/cp24-usbhid.html ) tells me it's writing the hotkey command to the hardware memory (so you don't need the driver once you config it). Have you installed the config software for it? 

On 9/30/2018 at 10:29 AM, Abiel said:

Great work @KyleK29 thank you!...

I´d like to take a look at this version...

Give me a little while to test it, I've made a few modifications. Right now it's literally just doing this type of operation (in AHK script language):

IF Active Window = Das Trader Pro
Send RAYLINE Command
Left-Mouse Click
Move Mouse 4px to the right
Left-Mouse Click
Double-Click Left Mouse
Send ORDER Hotkey Command

I use the RayLine because A) I never use it (so I can assign a RED color to it) and B) it allows you to get the longest line without moving them mouse a lot. The difference in order procedure is that you have to HOVER over where you want to place the stop as it does the double-click for you.

 

Share this post


Link to post
Share on other sites

Kyle. 

Appreciate the reply and quick dive on your part into Genovation. I believe that yes, installs are complete. I understand the hotkey creation in DAS without any real issue, so I believe I’ve the handle on basic practice. I’m stuck on what I’d call mapping or quite possibly, I don’t fully understand the Genovation (external keypad) concept. 

A conventional “buy now at +.05” assigned to F1 makes sense. The Geno keypad doesn’t have an F1 and for the matter, it doesn’t appear any key is fixed to the system. I also cannot locate a cell or placement in the setup Geno software where I would establish say, the top left key is recognized by the system as F1 and thus, maps the command when said Geno button is pressed. I’m pretty certain I am making the situation quite complicated.

The trading theory currently studied is rather intuitive in my brain, programming...not so much. 

Thanks again man for the reply. 

Share this post


Link to post
Share on other sites

This is awesome - can't wait to try it out! I've been doing this calculation manually via a scripted webpage I created. Genius use of variables as well as utilizing the double click to set the Stop distance! 

Share this post


Link to post
Share on other sites

This is brilliant. I am going to define my hotkeys using your script template and try it in my simulator (this is my 2nd week in the simulator). 

I think there is one caveat of the script which I describe below. Please correct me if I am wrong.

When calculating the number of shares one could afford with current account buying power:(e.g. DefSahre = BP*0.98), your script would be using the "stop loss" price instead of actual bid/ask price. This difference would lead to not having enough buying power when going for a long position with current account buying power. I guess you must have realized that and hence set 98% of total buying power as default. I could not think of an easy way to deal with this issue as we do need the stop loss price in the calculation. But I think this could be listed as "know issues" of the script to warn potential users. 

(Additional note: DefSahre = BP*0.98: This was confusing to me at first: how could buying power ($) multiple 0.98 is equal to share number?. After I checked the Dastrader manual and realized that it was actually calculating BP*0.98/Price. This was really Dastrader's fault. I share this here in case other users had the same confusion.)

Share this post


Link to post
Share on other sites
Posted (edited)

Thanks again for this amazing script.  

 

I download this sheet as .xlsx on my computer, it needs to ENABLE EDITING to change the value. But after I enable editing, the spreadsheet stop working.  

 

Is there anyway to fix this ? 

 

Thank you 

reference isn't valid.png

reference isn't valid 2.png

Edited by Jason W

Share this post


Link to post
Share on other sites
1 hour ago, Jason W said:

Thanks again for this amazing script.  

 

I download this sheet as .xlsx on my computer, it needs to ENABLE EDITING to change the value. But after I enable editing, the spreadsheet stop working.  

 

Is there anyway to fix this ? 

 

Thank you 

reference isn't valid.png

reference isn't valid 2.png

I had the same issue. It seemed to me that the calculations only work in the google spreadsheet. 

Share this post


Link to post
Share on other sites

Yo IB requires 25% for margin so it 4:1 on long, but 30% for margin on short, so that's 3.33333:1 on short ?

So if we gonna do a short script with 1% risk  , is this the correct script ?

DefShare=BP*0.98; Share=DefShare*0.333333333333333* Price * 0.01; Price = Price - Bid + 0.02;SShare = Share / Price; Share = DefShare - SShare; DefShare = DefShare + SShare; SShare = Share; SShare = DefShare - SShare; Share = 0.5 * SShare; TogSShare; ROUTE =LIMIT; Price = Bid - 0.05; TIF=DAY+; SELL=Send; DefShare = 500;

                                     

or is it Share=DefShare*0.3* Price * 0.01 ?

Thanks again man !!

Share this post


Link to post
Share on other sites
On 10/3/2018 at 5:41 PM, Seamus said:

Kyle. 

Appreciate the reply and quick dive on your part into Genovation. I believe that yes, installs are complete. I understand the hotkey creation in DAS without any real issue, so I believe I’ve the handle on basic practice. I’m stuck on what I’d call mapping or quite possibly, I don’t fully understand the Genovation (external keypad) concept. 

A conventional “buy now at +.05” assigned to F1 makes sense. The Geno keypad doesn’t have an F1 and for the matter, it doesn’t appear any key is fixed to the system. I also cannot locate a cell or placement in the setup Geno software where I would establish say, the top left key is recognized by the system as F1 and thus, maps the command when said Geno button is pressed. I’m pretty certain I am making the situation quite complicated.

The trading theory currently studied is rather intuitive in my brain, programming...not so much. 

Thanks again man for the reply. 

Yeah, I'm not physically familiar with the device, from what I've read about it, it will flash a key-combination to the button (I believe each button can have an A and B mode). That is stored on the devices EEPROM (hardware memory) and then it sends that stored hot-key to the PC via the USB as that key. This way it can be used without driver software (moved to another PC). 

See the attached screenshot -- From the looks of it, you run the "Genovation MacroMasterCPxx (CP24)" software and then:
1) Select the key you want to program (#1 on the screen)
2) Select the level you want (since each key can have two operations stored), #2 in the image.
3) Select the key combination, in this example I selected SHIFT + CTRL + A (see #3 and #4)
4) Save the file. I'd imagine you have to flash this file some how, but I don't have the hardware.
5) The rest is done in DAS.

I wouldn't use the F13-F24 keys as DAS doesn't appear to support them. 

InkedGenovation_MacroMasterCPxx_(CP24)_MacroMasterCPxx_2018-10-10_18-14-59_LI.jpg

Share this post


Link to post
Share on other sites
On 10/6/2018 at 5:07 PM, jimguo22bbt said:

This is brilliant. I am going to define my hotkeys using your script template and try it in my simulator (this is my 2nd week in the simulator). 

I think there is one caveat of the script which I describe below. Please correct me if I am wrong.

When calculating the number of shares one could afford with current account buying power:(e.g. DefSahre = BP*0.98), your script would be using the "stop loss" price instead of actual bid/ask price. This difference would lead to not having enough buying power when going for a long position with current account buying power. I guess you must have realized that and hence set 98% of total buying power as default. I could not think of an easy way to deal with this issue as we do need the stop loss price in the calculation. But I think this could be listed as "know issues" of the script to warn potential users. 

(Additional note: DefSahre = BP*0.98: This was confusing to me at first: how could buying power ($) multiple 0.98 is equal to share number?. After I checked the Dastrader manual and realized that it was actually calculating BP*0.98/Price. This was really Dastrader's fault. I share this here in case other users had the same confusion.)

 

Yes, that's the reason for the 0.98%. I'm sure if you set a really large stop price, you could effectively run out of buying power. And yes, that DAS calculation confused me at first because I didn't realize the BP function was converting to shares.

 

On 10/7/2018 at 7:57 PM, Jason W said:

Thanks again for this amazing script.  

 

I download this sheet as .xlsx on my computer, it needs to ENABLE EDITING to change the value. But after I enable editing, the spreadsheet stop working.  

 

Is there anyway to fix this ? 

 

Thank you 

I haven't tested it in Excel. It's probably messing up on SHEETS Validation functions as they may not be compatible with that of Excel's. 

 

22 minutes ago, Jason W said:

Yo IB requires 25% for margin so it 4:1 on long, but 30% for margin on short, so that's 3.33333:1 on short ?

So if we gonna do a short script with 1% risk  , is this the correct script ?

DefShare=BP*0.98; Share=DefShare*0.333333333333333* Price * 0.01; Price = Price - Bid + 0.02;SShare = Share / Price; Share = DefShare - SShare; DefShare = DefShare + SShare; SShare = Share; SShare = DefShare - SShare; Share = 0.5 * SShare; TogSShare; ROUTE =LIMIT; Price = Bid - 0.05; TIF=DAY+; SELL=Send; DefShare = 500;

                                     

or is it Share=DefShare*0.3* Price * 0.01 ?

Thanks again man !!

Are these numbers set in DAS as well? With that in mind, if DAS does know the your buying power for long is different than short, then it should work. You can use either 0.33333333333 (<--- sheets defaults to 15 places) or 0.33 (33%) or 0.3 (30%). It won't make too big of a difference.

I haven't thought of having mixed values for long / short. What you could do is either test it in SIM or use one of the test stock symbols, I believe TEST is one of them (it's a dummy symbol for testing things). Let me know what you find out and if it calculates correctly. 

Share this post


Link to post
Share on other sites

I'm not even sure if DAS knows the difference. Because DAS does not separate long BP  and short BP 

If DAS does not know the difference, then the value should still be 0.25 right ?

If DAS knows the difference, then 0.3 is fine. 

There is no way to test this in the SIM, SIM has the same buying and shorting power I think. 

So I'm gonna have to take one for the team .....   

Thanks again Kyle. 

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..