Jump to content
KyleK29

DAS - Dynamically Calculate Shares on %Risk or $Risk - Hot Key + Configuration, Updated 9/10/19, v2.1

Recommended Posts

3 hours ago, Ishyster said:

Hello everyone. I appreciate any help if possible. After I get in a position and my stop is already in place I use these scripts to help scale me out.

This really doesn't belong in this thread... but below works perfectly fine for me... The only change I made to your script was changing LIMIT to SMRTL for IB

CXL ALLSYMB;Route=SMRTL;Share=Pos*.25;Price=Bid-0.05;TIF=DAY+;SELL=Send;ROUTE=STOP;StopType=Market;StopPrice=StopPrice;Share=Pos-share;TIF=DAY+;SELL=SEND

If you're not required to change your route with whatever broker you are using, I suspect it's simply a syntax issue. Re-copy and paste what I have in the text box above.

Edited by Justin

Share this post


Link to post
Share on other sites

Hi guys... Really delighted with finding your comunity. I came across Kyle's scripts with equilized risk and since then trying to implement them in my trading strategies. However they don't work for me all the time at the moment. More often than not I get the "Can't get valide price" error message. It happens randomly so it's hard for me to find the solution. I tried fresh installs and also older versions. I tried to edit the scripts in different variations. Nothing works. I mostly trade low priced OTC's and sometimes subpenny stocks. Yes, more often it happens when I trade those subpennies but sometimes it happens even with 0.50$ stocks. I was thinking maybe to include "Round" in the script, but I trade stocks with diverse number of decimals and it means that for each category of price I would have to make separate hotkeys with according "Round". Maybe there is something I am missing. Please have a look at the script I am using : CXL ALLSYMB;StopPrice=Price-0;DefShare=BP*0.925;Price=Ask-Price+0.00;SShare=50/Price;Share=DefShare-SShare;DefShare=DefShare+SShare;SShare=Share;Sshare=DefShare-SShare;Share=0.5*SShare;TogSShare;ROUTE=SMRTL;Price= Bid+0.00;TIF=DAY+;BUY=Send;DefShare=200;TriggerOrder=RT:STOP STOPTYPE:MARKET PX:StopPrice-0.00 ACT:SELL STOPPRICE:StopPrice QTY:Pos TIF:DAY+; 

I always trade using $ amount of risk. Can I delete the BP part from the script so it doesn't complicate it or it would be a stupid idea? 

It is a brilliant script when it's doing the job though...

Any help will be much appreciated...

Share this post


Link to post
Share on other sites

 

On 2/28/2021 at 10:30 PM, peterB said:

you need to keep the BP there otherwise it would not work in cases which would require a bigger than you BP position size

the invalid price could be that you are trading a stock which has more than 2 decimal values but it is just a guess and you need to find out which work and which do not work but if a price is 0.274 and you want to set a stop at 0.270 it might be the case of rounding for 2 decimals. in that case you would need the Round3 command to implement into the script

Understood about BP...

Regarding the error message: I tried messing around with all types of "Round" and it doesn't work. But even if it worked, in your example would be perfect for prices with 3 decimals. But if I would use the same script for 0.0022 Bid and 0.0023 Ask, I would be screwed. It would be great if there was a function of "Round" to round always the last decimal ( NOT "Round2", "Round3", "Round4", etc.), so I don't have to make a lot of hotkeys differentiated by decimals. I was wondering maybe there is an easier solution to that or maybe there is a bug I should sort. Anyway... Thank You for your contribution Peter... Much appreciated...

Share this post


Link to post
Share on other sites

Hi, I've been using the hotkeys for some time, but lately the "Stop - Update Position ..." hotkeys don't want to work for me....I get the error message in DAS "Replace:Order is not Open!"....

I'm sure I'm using the right hot key (Long-Replace code for a long trade).  My procedure is like this for say a long trade:  

-Add shares to an existing position  (position has a corresponding stop-limit order generated by a hot key)   

-Click on the existing stop limit order

-Hit the hotkey.

Nothing happens (# shares in stop order is not updated) and I get the mentioned error....

....What else do I need to check?, thanks!

 

Miguel

Edited by MIGS

Share this post


Link to post
Share on other sites

Thanks for these hotkeys Kyle, they are awesome!  Something I have realized is that when a symbol is restricted by IB to not allow using margin (like shown in the screen shot where it says long margin rate is 100% - that means you cannot use margin), then DAS trader calculates my buying power as only my cash balance because for that symbol, that's all it truly is, I don't get margin.  Then when I use the dynamic hot key to risk a % of my balance, I end up only getting 1/4th of the amount of shares I would expect.  I spent the better portion of today digesting the code in the dynamic hotkeys to try to figure out what's happening and I finally get it!  (It was quite an achievement LOL!)  I just don't know how to fix my problem here.  Actually, I think it probably cannot be fixed, but before I give up, I wanted to pose the question to the community and see if there is something I am missing or if somebody might be able to come up with a very creative solution which I have not thought of yet.

So the script compares the total amount of shares you can afford based on buying power to the total shares you can buy based on your selected stop price and how much you want to risk losing if the trade goes against you, then sends a buy order to market for the lesser of these two amount of shares (genius script Kyle!  Took me a whole day's effort just to understand it LOL!).  The problem I am having is when DAS sees my buying power as $30K instead of $120K because of the limitation as shown in the screen shot attached (IB does not allow margin, only cash on certain symbols on certain days), then the first part of the equation (shares based on BP) calculates fine, but the second part (amount based on desired risk level) gets incorrectly limited to 1/4th of what it should be and the shares that get sent is this lower amount.  This is because of the part of the equation where it says, "Share=DefShare*0.25*Price*0.01" (assuming I want to risk 1% of my portfolio balance), comes up with a much lower number than it should since DefShare was just set to "BP*.97" in the previous line of code and now it's being divided in a quarter again and in this instance BP was only $30K (normally it is $120K) which was equal to my cash balance, not my cash balance times 4.  In essence, in this particular scenario, the script should not multiply DefShare*0.25, that is where it goes wrong and this is why I end up only buying 1/4 of the amount of shares I should have gotten.  But in all other normal cases, i.e. when margin is allowed by IB, it has to multiply DefShare*0.25 or you would be getting 4x the amount you want, which would be really bad.

I do not think there is a way to fix this automatically.  I think the only options are to either have two sets of hotkeys, one where it doesn't cut DefShare in 1/4 and another where it does.  Then I would just have to be sure and use the right set of hotkeys depending on if margin is allowed or not.  Or, the other alternative would be to not use the % of risk hotkey at all, but rather go to the fixed dollar risk hotkey and just update the amounts to manually calculate the % of my portfolio balance as my account balance changes.

Is there any other way around this that I might be missing?  Is there a way to continue using the % of risk hotkey and somehow have it not cut my shares in 1/4 size when the symbol is limited to cash only but I still have enough buying power?

To be clear, I am not talking about a situation where I do not have enough buying power.  Let me put some numbers to this to illustrate:

I want to risk 1% of my portfolio balance when buying a stock that is selling for $20 with a stop loss of $19.80, with all the cushions and what not in place, that would get me 1,372 shares which would cost $27,437, so I have enough cash to buy it.  But the hotkey only gets me 1/4 of 1,372 shares, or 343 because of the equation "Share=DefShare*0.25*Price*0.01" which in this instance = $72.  $72 is then used as the risk based loss (instead of 4x that amount which would be $288) and at a loss of $0.21/share I can only buy 343 shares in order to lose $72.  It then compares the calculated 343 shares to the max shares I can afford, which would be 1,455 and sends the lesser of the two, 343 shares when what I really wanted to buy was 1,372 shares which would have risked $288 (0.21 X 1,372 = 288).

 

image.png.512aa68b006d14015f8b857215172bbc.png

  • Like 1

Share this post


Link to post
Share on other sites
On 3/2/2021 at 4:30 AM, FlyOrSwim said:

 

Understood about BP...

Regarding the error message: I tried messing around with all types of "Round" and it doesn't work. But even if it worked, in your example would be perfect for prices with 3 decimals. But if I would use the same script for 0.0022 Bid and 0.0023 Ask, I would be screwed. It would be great if there was a function of "Round" to round always the last decimal ( NOT "Round2", "Round3", "Round4", etc.), so I don't have to make a lot of hotkeys differentiated by decimals. I was wondering maybe there is an easier solution to that or maybe there is a bug I should sort. Anyway... Thank You for your contribution Peter... Much appreciated...

You might want to suggest that Round option to DAS at [email protected] .. to be honest, I don't know anyone in the community that trades stocks that low of a price, and I know DAS gets picky with decimals, so you're likely on the right track there.  What might be a good idea is to ask around the community to see if anyone else trades that type of stock and uses these hotkeys, compare notes.

Sorry I'm not as helpful on this, it's one of those rare edge use cases that isn't easy to diagnose.

 

19 hours ago, MIGS said:

Hi, I've been using the hotkeys for some time, but lately the "Stop - Update Position ..." hotkeys don't want to work for me....I get the error message in DAS "Replace:Order is not Open!"....

I'm sure I'm using the right hot key (Long-Replace code for a long trade).  My procedure is like this for say a long trade:  

-Add shares to an existing position  (position has a corresponding stop-limit order generated by a hot key)   

-Click on the existing stop limit order

-Hit the hotkey.

Nothing happens (# shares in stop order is not updated) and I get the mentioned error....

....What else do I need to check?, thanks!

 

Miguel

What version of DAS are you using? Just to clarify, you're double clicking the QTY value on the on-chart order flag? There was a version of DAS that broke that functionality recently.

As a work around, you should be able to load the order into the montage for editing by either double-clicking the order in the Open Orders window -or- in that same window, right-clicking it and select edit order (iirc). See if those work to load it into the montage.

 

19 hours ago, jeremyjohnolson said:

....

image.png.512aa68b006d14015f8b857215172bbc.png

 

As a note, DAS calculates "margin" as your entire buying power, so 100% margin is the available equity * your leverage (a $25k acct would be $100k buying power, or 100% of the margin).

25% margin is your buying power  * 0.25 (so $25k equity * 4:1 leverage = $100k, * 0.25 margin rate = $25k available BP on that stock).

This is an issue with using Equity based hotkeys and the current market climate. It's something DAS didn't account for and is relatively new to being this widespread. It's not something I can fix and I have sent them suggestions to address it, but I don't know if they'll implement them.

The way DAS works is it gets your leverage and the equity from the broker as a global value. BP is calculated off this global value.

I know in the latest 5.6.4.2 they did modify the BP function to pull it from the stocks value, the problem is, they don't know which direction so they use the lesser of the two. Thus, if you go long on a stock with a 100% (long margin) and 25% (short margin), it'll always use the 25% calculation.

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
33 minutes ago, KyleK29 said:

The way DAS works is it gets your leverage and the equity from the broker as a global value. BP is calculated off this global value.

And to further make things worse... (at least from those on IB) The .dat file DAS gets from IB listing every stock's margin rate is wrong in many cases. DAS will show 100% margin rate when in actuality it's 300%.

Last week I finally managed to connect someone at DAS support and someone at IB support to communicate to each other about the problem. And now "This issue remains under review." 🤷‍♂️

Edited by Justin

Share this post


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

...I know in the latest 5.6.4.2 they did modify the BP function to pull it from the stocks value, the problem is, they don't know which direction so they use the lesser of the two. Thus, if you go long on a stock with a 100% (long margin) and 25% (short margin), it'll always use the 25% calculation.

Thanks Kyle, I think I am just going to switch to using fixed dollar risk, at least for now, and update the dollar amounts as my portfolio balance changes in order to get the same result as using % of equity.

Also, it's a little counter intuitive, but I was told in chat last Monday by Bryan W that when it says "LongMargRate: 100%" that actually means you have to have 100% of cash for the position.  At first I thought that it was the short side that was limiting me where it says "ShortMargRate: 25%" but it is actually the other way around.  Under normal circumstances both LongMargRate and ShortMargRate should say 25% if your leverage is 4:1.

I think what they are getting at in that alert screen is giving you the % of your total position which you must have in cash.  So 100% margin rate means you must own 100% of the position in cash, whereas 25% margin rate means that you only need to have the cash for 25% of the position, or, in other words, 4:1 margin.  I confirmed that Bryan W is correct about this.  Here is an example of what I see on ticker $AAPL today which allows me to use my full 4:1 margin (I know $AAPL is not limited because I traded it today, plus, well, it's Apple 🙂 )

 

image.png.c3b855ec492ba9fc813e5bc820d997eb.png

 

Edited by jeremyjohnolson

Share this post


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

And to further make things worse... (at least from those on IB) The .dat file DAS gets from IB listing every stock's margin rate is wrong in many cases. DAS will show 100% margin rate when in actuality it's 300%.

Last week I finally managed to connect someone at DAS support and someone at IB support to communicate to each other about the problem. And now "This issue remains under review." 🤷‍♂️

 

42 minutes ago, jeremyjohnolson said:

Thanks Kyle, I think I am just going to switch to using fixed dollar risk, at least for now, and update the dollar amounts as my portfolio balance changes in order to get the same result as using % of equity.

Also, it's a little counter intuitive, but I was told in chat last Monday by Bryan W that when it says "LongMargRate: 100%" that actually means you have to have 100% of cash for the position.  At first I thought that it was the short side that was limiting me where it says "ShortMargRate: 25%" but it is actually the other way around.  Under normal circumstances both LongMargRate and ShortMargRate should say 25% if your leverage is 4:1.

I think what they are getting at in that alert screen is giving you the % of your total position which you must have in cash.  So 100% margin rate means you must own 100% of the position in cash, whereas 25% margin rate means that you only need to have the cash for 25% of the position, or, in other words, 4:1 margin.  I confirmed that Bryan W is correct about this.  Here is an example of what I see on ticker $AAPL today which allows me to use my full 4:1 margin (I know $AAPL is not limited because I traded it today, plus, well, it's Apple 🙂 )

 

image.png.c3b855ec492ba9fc813e5bc820d997eb.png

 

 

I think the problem is there's a disconnect between the MargRate you see in the symbol status popup and how the BP functions calculate.

It could be true that on the broker side, 25%/25% is the normal. But if you have the montage calculate what the value will be for the hotkey, you'll notice it's not like that. 

25% MargRate will return 25% of the accounts available Buying Power. 100% will return 100% of the Buying Power (includes the leverage given).

You can test this by setting a Hotbutton to this:

Price=1.00; SShare=BP; 

(price=1.00 sets the function to return dollars)

The Display box will show what the BP function would return in number of dollars. If I pull up a symbol with 25% in my SIM account, it returns 25000. If I pull up a symbol with 100%, it pulls up 100000.

 

A few versions back a bug existed that always returned 1/4 of your BP .. so this isn't exactly surprising that it maybe broke elsewhere.

 

Now that doesn't mean you may not get the order through on the broker side. I'm just saying what is happening within the script that is throwing things off. A stock that shows 25% may very well let you buy worth the full account + leverage if you don't use the BP function. 

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
12 minutes ago, KyleK29 said:

 

 

I think the problem is there's a disconnect between the MargRate you see in the symbol status popup and how the BP functions calculate.

It could be true that on the broker side, 25%/25% is the normal. But if you have the montage calculate what the value will be for the hotkey, you'll notice it's not like that. 

25% MargRate will return 25% of the accounts available Buying Power. 100% will return 100% of the Buying Power (includes the leverage given).

You can test this by setting a Hotbutton to this:

Price=1.00; SShare=BP; 

The Display box will show what the BP function would return in number of dollars.

Are you sure that is right?  It's not how DAS is behaving for me.  I loaded that hotkey you gave me, Price=1.00; SShare=BP, and this is what I get.  Here are two examples:

First is $DIS where the DAS shows 25%/25% and I am getting $123,675 BP:

image.thumb.png.e28e5b2c7ace269b71aefeb5caadeda4.png

 

Second is $AMC where the DAS shows 100%/25% and I am getting $30,918 BP:

 

image.thumb.png.468381c3afd8a5e58520f0c527c3bb13.png

 

 

Edited by jeremyjohnolson

Share this post


Link to post
Share on other sites
4 minutes ago, jeremyjohnolson said:

Are you sure that is right?  It's not how DAS is behaving for me.  I loaded that hotkey you gave me, Price=1.00; SShare=BP, and this is what I get.  Here are two examples:

First is $DIS where the DAS shows 25%/25% and I am getting $123,675 BP:

image.thumb.png.e28e5b2c7ace269b71aefeb5caadeda4.png

 

Second is $AMC where the DAS shows 100%/25% and I am getting $30,918 BP:

 

image.thumb.png.468381c3afd8a5e58520f0c527c3bb13.png

 

 

I'm not at my desk, but what version of DAS are you using?

I'd have to check my version, but I remember last time I checked this is how it was. It's why I have a TestBP hot button now, it seems to break every other release.

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
1 hour ago, jeremyjohnolson said:

5.6.4.2  It says I have the latest version.

Yes, that's the latest production version. I think the last time I checked these values (and noticed they were inverted) was in 5.6.4.1 ... that maybe the reason that 5.6.4.2 replaced 5.6.4.1 so quickly (it was like a few days).   In my 5.6.4.2, it does calculate as it shows for you.

 

Doesn't solve the actual issue of the hotkeys not having access to a BPLong and BPShort value (which I requested). Hopefully they add those.

Edited by KyleK29
  • Like 1

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

Share this post


Link to post
Share on other sites

pful on this, it's one of those rare edge use cases that isn't easy to diagnose.

 

On 3/9/2021 at 5:34 PM, MIGS said:

Hi, I've been using the hotkeys for some time, but lately the "Stop - Update Position ..." hotkeys don't want to work for me....I get the error message in DAS "Replace:Order is not Open!"....

I'm sure I'm using the right hot key (Long-Replace code for a long trade).  My procedure is like this for say a long trade:  

-Add shares to an existing position  (position has a corresponding stop-limit order generated by a hot key)   

-Click on the existing stop limit order

-Hit the hotkey.

Nothing happens (# shares in stop order is not updated) and I get the mentioned error....

....What else do I need to check?, thanks!

 

Miguel

What version of DAS are you using?

>>5.5.2.1

Just to clarify, you're double clicking the QTY value on the on-chart order flag? There was a version of DAS that broke that functionality recently.

>>THAT WAS THE PROBLEM THANK YOU...I was not double clicking the QTY  value...  I guess I forgot the instructions after some time of being away from trading.

Miguel

Edited by MIGS

Share this post


Link to post
Share on other sites

I'm having an issue where I can't exit a trade unless I remove the stop that's sent to the market. How do I fix this?

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.