Not getting API profit, etc info.  [SOLVED]

Questions about MultiCharts and user contributed studies.
bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Not getting API profit, etc info.

Postby bowlesj3 » 31 Jan 2020

Hi, Maybe a request for change is in the works or maybe there is a solution to these issues.

I am using MC64 release 11 with Interactive Brokers. I am using the MC scanner with 3 to 15 stocks in the scanner list each day for trading. I am currently testing MC auto-trading commands with the IB paper trading account. Earlier this morning in the power language I was able to get the info from the IB api socket for OpenPositionProfit and CurrentContracts and display it on the screen as well as send it to fileappend output. Now I can no longer get it (comes back as zero even though the chart trader displays this info correctly). I am wondering if I was able to get it because I submitted an auto-trading order for the symbol which got rejected and maybe that somehow woke up the auto trading connection so it knew what symbol I was working with. I guess I have to try tomorrow when the market opens to see if this theory is correct. I tried it now after the market closed and it does not solve it. I did turn the auto trading back on using the "SA" button in the top right of the chart thinking it would get the info but it did not.

The other issue is every time I move to another symbol in the scanner the upper left side of the chart button currently set to "SA" for synchronized auto-trading gets turned off. It does not turn back on automatically for the next symbol on the scanner list that appears in the charts. I am guessing there is a solution to this because the chart trader does show the info for that new symbol without my constantly having to click the button on the chart scanner to reconnect to the API socket info. Maybe putting the OpenPositionProfit and CurrentContracts keywords in an indicator rather than a signal is the solution? Nope! Just tried it and the indicator will not compile with these keywords. I am hoping to feed the info regarding the trade to my MS-Access program database via PL code and GVs.


That is it for now. It seems that to work around (the Investment Industry Regulatory Organization of Canada recently forcing TWS to block API buys and sells of Canadian stocks) I will be loading all 60+ symbols into IB's TWS and sending out keystrokes to highlight the correct one. This is exactly the same technique I am using with my MS-access program to select the correct symbol in the MC scanner. Why would I do that? Because MS-Access is used to greatly increase my efficiency during the trading day helping me to know what stocks to check when when to check them. The scanner programming can not do what I can do in MS-Access. The scanner list feeds certain info to MS-Access to help with all this but MC can not provide all the info I need. So I make the decision from MS-Access and it lines up the scanner charts. As I said it also now has to line up TWS to the correct symbol so I can make the trade from TWS. Access will of course get me to verify this TWS selection.

Thanks,
John

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Not getting API profit, etc info.

Postby bowlesj3 » 03 Feb 2020

Hi, I am part way through switching away from using the chart trader back to using the IB TWS to submit orders. However I have noticed that the chart trader still is able to display the current profit and loss of an order for a symbol that was placed with the TWS software. I double checked this by stepping in and out of the symbol with the scanner watch list. Unfortunately I went through every strategy command and none of them can pick up the open order price from the broker. I would like to display a line on all chart showing the price I got. Is there something I am missing that will allow me to do this.

I just realized I can use the chart trader open position marker and just hide the chart trader itself. So that part is solved. I would like to get a stop loss line displaying however. There seems to be no way to get this info. So not fully solved.

Thanks,
John

User avatar
Anna MultiCharts
Posts: 560
Joined: 14 Jul 2017
Has thanked: 42 times
Been thanked: 140 times

Re: Not getting API profit, etc info.

Postby Anna MultiCharts » 13 Feb 2020

Hello, bowlesj3!

Let me address both of your posts.

1. If you change the symbol on the chart this will disable your autotrading:
https://www.multicharts.com/trading-sof ... to_Trading
Any change made to the strategy settings and data series also switch autotrading off.
This is by design. If you need to trade several symbols at the same time, then you need to have to keep several charts open with autotrading on. Or use Portfolio Trader for autotrading.
2. Autotrading doesn’t imply active manual intervention by the user. The only actions that are meant to be done by the user are turning the strategy on and off.
3. OpenPositionProfit and CurrentContracts work the same way in backtesting and in live trading. Please refer to the keyword descriptions in the Help of PowerLanguage Editor.
4. OpenPositionProfit and CurrentContracts cannot be used in indicators. In indicators you can use i_CurrentContracts and i_OpenEquity.
5. To request the info about the position on the broker use these keywords: AvgEntryPrice_at_Broker, MarketPosition_at_Broker, MarketPosition_at_Broker_for_The_Strategy.
6. You can view pending orders and the open position in the chart trading panel, from which you can also trade manually. Go Format -> Chart trading -> Common settings and enable the desired options.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Not getting API profit, etc info.

Postby bowlesj3 » 13 Feb 2020

Thanks Anna,

I just did a quick test of the commands you mentioned in an indicator (as indicated below). As you said the only two that would compile have the "i_" at the front so I commented the others out. I opened a position with TWS and the "i_" values show zero. So I applied a dummy signal to the chart and turn on auto trading (button top left of the chart). I still get zero values in the "i_" values.

FileAppend(LogPath,
" InPosition=" +
" i_CurrentContracts=" +
NumToStr(i_CurrentContracts,0) +
" i_OpenEquity=" +
NumToStr(i_OpenEquity,0) +
{
" AvgEntryPrice_at_Broker=" +
NumToStr(AvgEntryPrice_at_Broker,0) +
" MarketPosition_at_Broker=" +
NumToStr(MarketPosition_at_Broker,0) +
" MarketPosition_at_Broker_for_The_Strategy=" +
NumToStr(MarketPosition_at_Broker_for_The_Strategy,0) +
}
NewLine);

What I have done is programmed a set of work arounds over the past 2 weeks. They should be ready for live trading tomorrow. I just have to manually enter the price I got, the entry time and if I want to apply a stop I need to pick up the value MS-Access gives me and apply that manually to Traders Work Station to create the stop order. (my stops are based upon average true range).

Also I have contacted Interactive Brokers to see if they are going to put in a protective mechanism that allows auto trading of Canadian Stocks. They are currently working with the regulatory body to see if they can figure out a resolution. I have a ticket with them that they will respond to when they figure something out. I am also gathering info regarding trading US stocks. I may do that. In that case I would prefer to have a currency chart in MC so I can make those trades when both are likely to be going up.

John

User avatar
Anna MultiCharts
Posts: 560
Joined: 14 Jul 2017
Has thanked: 42 times
Been thanked: 140 times

Re: Not getting API profit, etc info.  [SOLVED]

Postby Anna MultiCharts » 14 Feb 2020

bowlesj3,

CurrentContracts and OpenEquity return the values calculated by the strategy on the given chart, they don’t return the actual values from the broker. You can check how these words work on historical data.
Still in certain situations these keywords can reflect the broker position, e.g when you are trading in SA mode and your strategy and broker positions match.
You should also keep in mind that MultiCharts allows for trading one and the same instrument from multiple charts, so the market position will be individual on each chart, but the broker will have cumulative position.
MarketPosition_at_Broker returns the broker position (the one that can be seen in Order and Position Tracker -> Strategy Positions -> Broker Position column), and MarketPosition_at_Broker_for_The_Strategy returns the position of the given strategy (the one that can be seen in Order and Position Tracker -> Strategy Positions -> Strategy Position column).
For more info please refer to the Help of PowerLanguage Editor.


Return to “MultiCharts”