Help with Exiting Positions at IB with PowerLanguage

Questions about MultiCharts and user contributed studies.
konrad7
Posts: 3
Joined: 02 Aug 2010

Help with Exiting Positions at IB with PowerLanguage

Postby konrad7 » 04 Dec 2011

I'm looking for a way to exit all existing long (or short) positions using PowerLanguage, and I'd really appreciate any ideas or guidance any of you can give me. Here's my situation:

I'm working on a short-term trend following strategy that I've set up with synchronous auto-trading connected to an IB advisor account. I use advisor profiles to allocate trades to multiple accounts automatically. I use the Strategy Properties dialog to set the number of contracts to trade. Everything works fine using the standard order commands:
Entry: Buy next bar at market
Exit: Sell all contracts next bar at market
Entry: Sellshort next bar at market
Exit: Buytocover all contracts next bar at market

The problem I'm having is that if I start with an initial position that's different from what's on the chart it doesn't get handled properly. For example, say I have the number of contracts to trade set to 7 in strategy properties, but my initial position is 3 long contracts, and the strategy is long. When I start up the auto-trading, I tell the chart about my initial position. What I want to happen is when the strategy goes flat, the 3 long contracts get closed out. What actually happens is an order gets sent to sell 7 contracts (even though the existing position is only 3 contracts). The net result is that I'm short 4 contracts instead of being flat.

I thought of using the MarketPosition_at_Broker function to get the number of contracts, but I don't think that would work because the buy (or sell) order would get allocated by the advisor profile. What I really need is a command to exit all existing long (or short) positions, regardless of which account they are in. In IB TWS, I just right-click on a security and choose "Close Position", without using an advisor profile. Is there any way to do this with PowerLanguage? I've looked around and can't seem to find anything.

Thanks so much for your help!

User avatar
Roman MultiCharts
Posts: 50
Joined: 28 Nov 2011
Has thanked: 21 times
Been thanked: 66 times

Re: Help with Exiting Positions at IB with PowerLanguage

Postby Roman MultiCharts » 06 Dec 2011

Hello konrad7,

Unfortunatelty, in PowerLanguage there's no such a command for flattering all positions, regardless of which account they are.

You may want to submit a feature request to the Project Management of our web site so other users can vote for it: https://www.multicharts.com/pm/


Return to “MultiCharts”