Jump to content
Medved Trader Forums

Can Medved automate my trading style and save me from the dreaded TWS?


alv123

Recommended Posts

Hi guys,

I can no longer cope with the lack of good risk management tools on TWS and i'm hoping Medved trader will save the day!

This is how I trade:

1) Decide what % of my account i'm willing to risk on the trade

2) Decide price of 1st scale in

3) Decide price of 2nd scale in
 
4) Decide price of my Stop

5) Calculate the correct position size for the trade and place it
 
6) Scale out half of my total position
 
7) Update my stop to reflect the new total position size

On TWS this is painfully slow /  manual / and takes a lot of concentration.

All I really need is a slightly more sophisticated version of the long position tool on tradingview. If that tool could accommodate scaling in, and if you could change % account risk without having to open up the menu, it would be perfect. It even auto populates the order form for you.

Do you think it would be possible for me to automate these steps using your chart trader, or perhaps with the new hot keys you are working on?

I believe you do have a position size tool on the chart, but it does not accommodate scaling in.

Thank you for all of your efforts to make interactive brokers actually usable!

position size tool auto populate order.png

Link to comment
Share on other sites

Go to the Settings / Application / API - and click on the Documentation link there next to WS API.

Or just go here: https://docs.google.com/document/d/15IvYQEYzSeftTSEdVhsptAguM0fGm9mTJFSDDAK7zWE/edit#heading=h.vjt2wnr366ew

It is a simple Websocket/JSON API and we can provide enough C# code in the API Tester to get you started.

 

Using this API you can fully automate anything at all since it allows you to subscribe to/get market data, log into your trading account, get all the info from your trading account, place/monitor trades etc. All streaming.

 

Link to comment
Share on other sites

Sorry to be a pain.

I've read through the API documentation and I feel like I would be able to make something useful by combining some of the code examples together.

However, despite lots of googling I am not sure what I am supposed to do with the Test api files you sent above.

I believe i am on the right track by downloading 'visual studio community'. 

I then opened up the API tester.snl file, which I believe is the project file.

But from that point I am lost on the next steps.

I assume you have lots of better things to do than walking me through this. If you think i am way out of my depth then please say so lol.

I think if I can just get to the stage where I can type out commands and see the responses, I will be able to get the hang of it, but this initial setup part is tripping me up.

Edited by alv123
"visual basic" changed to "visual studio"
Link to comment
Share on other sites

My programming experience is making simple indicators on tradingview and a few apple script programs for keyboard shortcuts.

I think I have now successfully loaded the files into visual studio.

I believe that my next step is editing some of the code to include the 127.0.0.1 websocket described in the API document.

Where would I enter this 127.0.01 number?

I think I also need to enter the websocket port number from the medved application into the code. Where would I enter that number?

If anyone could point me in the right direction, I would greatly appreciate it.

visual studio.png

websocket.png

ws enabled.png

Link to comment
Share on other sites

Hey @alv123,

The code provided by the MT Team is non-compiled C# which needs compiled into a Windows executable through an interpreter like Visual Studio (you have this already).

Next step would be to add using MT.Util; at the top of ByteBufferQueue.cs and WSTester.cs (inadvertently missing as far as I can tell). Next, you press the "Play" button at the top which will compile and launch the WSTester application which can be used to send API commands to MT and observe the output. If you receive errors during compilation, we would need to look at those and would typically be a missing class that can be downloaded from NuGet (i.e., WebSocket4Net), but by default all required ones should be there.

Beyond the above, you would then want to look at adding business logic to the WSTester for your needs or using it as a baseline and developing your own application. For example, I created a sample function that outputs a message based on the balanceUpdate and alertTriggerUpdate messages.

        private void BusinessLogic(String data)
        {
            var json = JsonConvert.DeserializeObject<dynamic>(data);

            if (json.cmd.ToString() == "balancesUpdate")
            {
                if (json.result["account_value/change"] <= 1000.00)
                {
                    UpdateLog($"Oh no! Still poor, keep trading... {json.result["account_value/change"]}");
                }
                else
                {
                    UpdateLog($"Wowzers! Done for the day... {json.result["account_value/change"]}");
                }
            }

            if (json.cmd.ToString() == "alertTriggerUpdate")
            {
                if (json.result["alertDesc"] == "Volume")
                {
                    UpdateLog($"Wish me luck, getting long {json.result["sym"]}");
                    String order = OrderBuilder(json.result["sym"])
                    SendSocket(order);
                }
                else
                {
                    UpdateLog($"Hmm, waiting for next alert to execute a trade.");
                }
            }
        }

Link to comment
Share on other sites

SUCCESSFUL BUILD :D:D

Thank you so much Donald! I would have been sat here for the next 6 months trying to figure that out.

I ran into a couple of other errors a long the way, which I will document here for in-case other coding newbs need it in the future.

After I made the MT.Util corrections, I still had a warning about 'WebSocket4net' could not be found, as you mentioned above Donald.

So I went to trusty google and downloaded the package from: https://www.nuget.org/packages/Websocket.Client/

I then copy and pasted that package into my API test folder and that seemed to resolve it.

Finally, I still had an error asking me to remove 'the mark of the web' from WS Tester.resx

That was solved by finding the file in windows, clicking on properties, and clicking on 'unblock' at the bottom of the general section.
 

list of errors (2).png

from the web (2).png

Link to comment
Share on other sites

So now its a case of me playing around with the sample code in here I guess, to see if I can get something working:
 https://docs.google.com/document/d/15IvYQEYzSeftTSEdVhsptAguM0fGm9mTJFSDDAK7zWE/edit#

Are there any risks I should be aware of before I start typing code in willy nilly?

It is only connected to my IB paper trading account so I should be fine to do whatever, right? :unsure:

Edited by alv123
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...