Jump to content

Detailed Walk-through of a KyleK29 hotkey script (and maybe a bug?)

Recommended Posts

This is an example of using KyleK29's hotkeys to execute a long position with maximum 1% equity risk. Kyle has examples in his spreadsheet, but there's some additional details I came across while studying the hotkeys so thought I'd share them with the BBT community. This is part DAS Trader hotkey tutorial and part KyleK29 hotkey tutorial.
Description of KyleK29's Hotkeys
If you're not familiar with @KyleK29's hotkeys, the purpose of the hotkey is to limit the number of shares purchased so that you don't lose more than a predefined amount of your capital should the stock price move against you. The process of using the hotkey is to first double-click on the chart to define the Stop Loss price, then activate the hotkey using your keyboard. The hotkey will then automatically calculate the number of shares you can trade so that your losses are limited in case you get stopped out.
For example, assume you have $10,000 equity ($40,000 total buying power on 4:1 margin) and don't want to risk more than 1% equity ($100) should you get stopped out. When viewing the chart for a particular stock you decide an appropriate Stop Loss would be $0.50 lower than current price, so that's where you double-click. Let's assume the stock price is $100/share, so you COULD afford 400 shares. But if you bought 400 shares, and you got stopped out, that would be a $200 loss (400 * 0.50 = $200). That's more than the 1% ($100) you wanted to limit your losses to. So the purpose of the hotkey is automatically decrease the number of shares to $100/0.50 = 200 shares. Now if we had instead placed our stop loss at .10 below the price, then it'd take 1000 shares to reach our max loss of $100.  This is more than we can afford so the hotkey would set the numbers of shares to max afforded 400 shares.
This means this hotkey needs to calculate two different share amounts: 1) The maximum number of shares we can afford with our buying power. 2) The maximum number of shares we can buy and not lose more than 1% of our equity, assuming the price moves all the way to the Stop Loss.
If DAS Trader hotkey engine had a min() function, then we could easily find the smaller of the two shares sizes and execute our trade. Unfortunately DAS Trader does not have a min() function; however, Kyle cleverly found a workaround for this.
The following formula is how to find the minimum of two values, as implemented in Kyle's hotkeys:
So Kyle's hotkeys calculate Max Shares Affordable and Max Shares to Fit Risk, finds the min of the two, and purchases that number of shares. In addition to the clever formula, Kyle's hotkeys also utilize clever usage of the montage fields to store/read values during the calculations. In another post on these forums, Kyle describes the montage fields that were available to him:
SSHARE --> Display Box (next to share), this is an unsigned INT, read/write
SHARE --> Share Box, this is an INT, read/write
PRICE --> Price Box, this is a FLOAT, read/write
DEFSHARE --> Default Shares (not displayed, but accessible), this is an INT, read/write
TRAILPRICE --> Trailing Price, FLOAT, but only available if STOP is selected and stop type = Trailing, read/write
STOPPRICE --> Trigger Price, FLOAT, only available if STOP is selected and "trigger price" box is writeable, read/write
CAPSHARE --> String, write only
PREF --> String, write only.
Not much to work with, and some consolations had to be made (e.g. in one case Kyle uses an INT field to store a float which loses precision, but, hey, you work with what you have). But all-in-all it's no small miracle what Kyle pulled off given what DAS Trader makes available to hotkey authors.
Example Hotkey Walk-through
Now I'll go into a detailed breakdown of one of the hotkeys generated by v2.1 of Kyle's hotkey spreadsheet. There's also a suggestion included below for an alternate command as I think the current command may have a bug. The settings for this example are:
The stock we're trading is AMD at Last Price of 44.47 with Bid=44.46 and Ask=44.47. And we kick-off our trade by double-clicking on the AMD chart at 44.35 (Stop Loss), followed by hitting the appropriate key combination on our keyboard to execute the hotkey script.
We then get the hotkey from the E15 cell on the "Hot Keys" tab of Kyle's spreadsheet.
The full hotkey script is: 
DefShare=BP*0.49;Share=DefShare*0.25*Price*0.01;Price=Ask-Price+0.01;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=400;
Below is a detailed walk-through of each command in the script.
"Max Shares Affordable"
Returns Number of shares. Kind of strange since the units on the right-hand side of the equation are dollars isn't it? Welcome to the world of DAS Trader hotkey scripting. This is because behind the scenes this command is really "DefShares=BP*.49/Price". So you end up with a unitless value equal to the number of shares afforded. "Price" is last price in Time & Sales. See explanation of this in DAS Trader hotkey setup window:
In this example, DefShare = $20,000 * .49 / $44.47 = 220 shares. This is our "Max Shares Affordable".
Note it'd be preferable to use Ask instead of Last Price, since Ask is where we buy. However, you'd need to set Price=Ask first, which would then wipe out the Stop Loss price that got defined when we double-clicked on the chart. So we have to calculate Max Shares Affordable using Last Price instead of Ask, which I'm sure you'll agree is acceptable.
Share=DefShare *0.25 *Price *0.01;
Alternate formula:
Share = BP *.25 *Price *.01;
Returns $ Risk.
Again, the units are confusing and it's difficult to see if you're actually calculating shares or a dollar amount. Once we "show all of our work", we'll see it's a dollar amount, even though the left-hand side of the equation says "Share". DAS Trader hotkeys don't have things like user-defined variables, so we're limited to just a few fields in the montage to read/write values from. These fields serve as our variables and for "$ Risk" we're going to store it in Share. (Since Share is an INT only field, our dollar risk will be rounded. e.g. $24.50 -> "24" in Share field)
Share=DefShare*0.25*Price*0.01 = (BP*.49/Price) *.25 *Price *.01 = BP * .49 * .25 * .01 = $24.50 
However, a 1% risk of a $5k account should actually be $50. This is also what the spreadsheet shows:
Alternate formula:
Share = BP * .25 * Price * .01; = $50 (1% of $5,000 equity account)
I use the alternate formula in my hotkey script so going forward in this example, "$ Risk" / "Share" is $50 and not $24.50 (which would show as "24" in montage since Share is a INT field, not float).
Price on right hand side is the Price we define by double-clicking in the chart window to define the Stop Loss price.
Price on the left hand side is therefore the Stop Distance, the distance between where we buy (Ask) and where we want to stop out (where we double-clicked).
.01 is the "minimum stop buffer" we defined in settings so that's added on there as well.
Let's assume I double-clicked in the chart at 44.35 to define the Stop Distance;
Price = Ask-Price+0.01 = 44.47 - 44.35 + .01 = 0.13
SShare is the number of shares "shown" to the exchange, but we're using it to store "max shares to fit risk". So we take our $ Risk ($50) and divide that by "Price" aka "Stop Distance":
$50 / 0.13 = 384 shares
So to summarize what we've done so far:
  • Calculated Max Shares Afforded (220) using Last Price in Time & Sales and our buying power of $20,000*.49 and stored this is DefShare
  • Calculated max equity risk ($50) and stored this in Share
  • Calculated Stop Distance (0.13) and stored this in Price
  • Calculated Max Shares to Fit Risk (384) and stored this is SShare
So for this example we are limited by Buying Power (220 shares) rather than $ Risk (384 shares). So we expect the hotkey to trade 220 shares.
Now we get into finding the minimum of "Shares Afforded" and "Shares to Fit Risk":
Calculate  A - B  of min{a,b} = 0.5( A + B - | A - B | ) and store it in "Share". We don't need Share any more to store $ Risk.
A = Max Shares Afforded (DefShare)
B = Max Shares to Fit Risk (SShare)
A - B = 220 - 384 = -164 (shows in montage window as "164-")
Calculate A + B of min{a,b} = 0.5( A + B - | A - B | ) and store in DefShare.
220 + 384 = 604
This is our way of doing Absolute Value of A - B since SShare only holds positive integers.
Now we're calculating everything in parentheses: min{a,b} = 0.5( A + B - | A - B | )
Then we multiply by 0.5 and store in Share:
You might think that the last two lines could be combined as "Share=0.5*DefShare - 0.5*SShare", but that does not work for some reason and I get a value of "49446" in the Share box. So better to stick with what Kyle has done.
This just deletes the number of shares in the "Show Share" box. We don't intend to show a different number of shares to the exchange than we're actually trading. We just needed that field for our arithmetic, and now we're done with it.
Price= Ask+0.05;
The normal stuff.
*** A note regarding ROUTE for CMEG users (like myself). *** When you join CMEG, you get an email saying you need to use SMRTL route to get the discounted BBT commission and ECN fee structure. CMEG also tells you this appears as "LIMIT, MARKET, and STOP" in DAS Trader platform. You may be tempted to use SMRTL or SMRTM in your hotkey, but that is not what you should do. I even had a chat with DAS Trader support and they also said I should use SMRTL/SMRTM in my hotkeys. However, those are not in the montage Route drop-down menu. In other words, if for some reason you had "MERQL" selected in the Route drop-down, then ran the hotkey with "Route=SMRTL" in the hotkey script, the Route field is not going to change to "SMRTL" since it's not available in the drop-down. The route will stay as "MERQL" and that's the route DAS Trader will use for the trade. So use "ROUTE=LIMIT" (or MARKET or STOP). In the Trade Log window you'll see the intended "SMAT" route as the one being used.
Change DefShare back to our default amount. This doesn't impact the trade and is more of a housekeeping command.
After the hotkey is executed, the montage looks like this:
And now we are long 220 shares of AMD. And if the price moves to 44.35, we can (manually) close our position and not lose more than $50.
It should also be noted that Kyle's spreadsheet can add Stop Loss orders too, but I haven't looked at those in detail yet.
  • Like 1

Share this post

Link to post
Share on other sites

actually CMEG said to me that "Your BearBull Traders commission structure is as follows: 0.004 per share for less than 100 shares and 0.50 per share for more than 100 shares using the LAMP route only" which is also not listed so i use LIMIT route as SMRTL never worked for me

Share this post

Link to post
Share on other sites
On 4/5/2020 at 3:27 PM, JasonH said:


Hi Jason,

Great visual write-up. Regarding the bug .. I think this is more of a labeling issue than a bug in the formula. The % of Buying Power is to do a slight offset to the calculated maximum affordable shares for buying power (default is 0.97). The intention of that value was to act as a buffer for an 'Insufficient Buying Power' error people would get without it, it's why by default it's 0.97. Reversing the leverage to get equity occurs off of this value. I can see where a user might expect different behavior in certain situations.

Looking at the settings in the quoted screenshots. If you have $5,000 equity with 4:1 margin (Total BP of $20,000) and you only wish to use 49% of the total Buying Power (maximum), then your effective Buying Power is $9,800, when we recalculate for Margin that becomes an equity of ~$2,450. With a max-account-risk of 1% of this refactored equity, it becomes a max $Risk of $24.50. This matches up with the share size given when you account for rounding.

To get the 1% risk of $5000, you'd want to set this value to 100% (or more likely the 0.97 default). 


I'll adjust the instructions for it on the next version so users better understand it's intention.

Edited by KyleK29

Profile / Project Pages (Dynamic Hotkeys, StreamDeck Files, and other contributions are located here)

Share this post

Link to post
Share on other sites

Kyle, the problem is that when I entered $5,000 for equity, and went to the "Hot Keys" tab and looked up 1% equity risk, the spreadsheet shows $50 for "Factored Dollar Risk" (which is good, because that's 1% of $5,000) but your hotkey actually calculates and uses $24.50. i.e. the stop loss trade will stop you out after a loss of $24.50 instead of $50.


Edited by JasonH

Share this post

Link to post
Share on other sites

@JasonH....very good...actually excellent write up. This will help ppl understand some of wht these hokeys actually do.

Share this post

Link to post
Share on other sites

Great information Jason and Kyle.

I Have a trouble combining bracket with Kyle's defined risk. Can you help me. The reason i want to do that is have better share size based on defined stop. 

Her is my original Long hot key based on $100 risk.

ROUTE=SMRTL; Share=100; Price=Ask+0.03;TIF=DAY+;BUY=Send;TriggerOrder=RT:STOP STOPTYPE:RANGE LowPrice:AvgCost2-.99 HighPrice:AvgCost2+2.99 ACT:SELL QTY:POS TIF:DAY+

I want to add Kyle's flexibility entering from a chart (by clicking at where i want to put stop) and automated share calculation. (Info: 25.000 equity, 100.000 buying power, max risk per trade 100, reward 300)

and the same for short if it different other wise i will change myself. 


Thanks in advance!!! 


Share this post

Link to post
Share on other sites


I'm currently trading on DAS SIM and predominantly use Kyle's dynamic hot key scripts to execute trades, however yesterday I was having trouble selling my position on MRNA and was getting an error message every time. This wasn't happening with other stocks so I'm wondering if I'm missing something or have not configured it correctly.

My sell scripts:

Sell 25%:

ROUTE=SMRTL;Price=Bid-0.05 ;Share=Pos*0.25;TIF=DAY+;SELL=Send 

Sell 100%


Thanks in advance!


Share this post

Link to post
Share on other sites

stonks88, Not certain this is the problem, but it will cause another problem down the road so you might as well change it now. Check the routes in your Montage window. I’m guessing “SMRTL” doesn’t actually appear in the drop down.

I use CMEG and set route=limit for my hotkeys. This shows up as LAMP route in my CMEG account reports. And the BBT commission rate is applied. 

Share this post

Link to post
Share on other sites
5 hours ago, Justin said:

Hi Jason,

You're correct it doesn't. I'll update to LIMIT and see how that goes.



Wait... Another Justin?!


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

  • Create New...

Important Information

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.