Marketposition for Data2

Questions about MultiCharts and user contributed studies.
quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Marketposition for Data2

Postby quod_erat » 14 Dec 2020

I must be misunderstanding something about the different marketposition functions. Let's say that I'm long AAPL, and I want to automatically hedge against market risk by going short SPY. Because MC can't send buy/sell orders on data2, my understanding was that the setup would look something like this:

Strategy 1:
Data1: AAPL

Code: Select all

code to go long AAPL
Strategy 2:
Data1: SPY
--- Data2: AAPL

Code: Select all

if marketposition_at_broker of Data2 > 0 then sell short next bar at market;
But it appears as if Strategy 2 can't read my AAPL marketposition from the broker, but I'm not certain why.

User avatar
Smoky
Posts: 507
Joined: 03 Dec 2010
Location: Thailand
Has thanked: 97 times
Been thanked: 115 times

Re: Marketposition for Data2

Postby Smoky » 15 Dec 2020

I think stategy only run with data1, you have to open a second charts and use global variables to shares datas between 2 stategies...

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Marketposition for Data2

Postby TJ » 15 Dec 2020

Go to the Wiki
8 PowerLanguage Keyword Reference
Look under Plotting‎ (14 P)

I_getplotvalue
I_setplotvalue

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Marketposition for Data2

Postby quod_erat » 15 Dec 2020

Go to the Wiki
8 PowerLanguage Keyword Reference
Look under Plotting‎ (14 P)

I_getplotvalue
I_setplotvalue
I thought those only worked with indicators, right? I'm trying to communicate between two signal-based strategies. Let's say I wanted to automatically hedge a new position. On the most basic level, it would be something like:

Strat 1 [this is basically just a manual buy]
data1 AAPL

Code: Select all

if currentime_s > 000008 then buy next bar at market;
Strat 2
data1 SPY
--- data2 AAPL

Code: Select all

if marketposition_at_broker of data2 > 1 then sell short next bar at market;
My issue seems to be that I can't get marketposition from broker. I applied a test script to a chart for which I have a position with IB:

Code: Select all

print(MarketPosition_at_Broker of data1);
... but the printout shows 0, where (I believe) it should print the # of shares.
Last edited by quod_erat on 15 Dec 2020, edited 2 times in total.

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Marketposition for Data2

Postby quod_erat » 15 Dec 2020

Here's one way I found around it:

Strat 1
AAPL Data1

Code: Select all

[IntrabarOrderGeneration = True] if currenttime_s > 080000 and marketposition = 0 then buy next bar at market; pmm_set_global_named_num("mpx", 1);
Strat 2
QQQ Data1
--- AAPL Data2

Code: Select all

if pmm_get_global_named_num("mpx") > 0 then sell short num_share shares next bar at market;
This works, in Portfolio Trader. I'm still not sure why marketposition_at_broker can't initiate the same Strat 2 hedge, but if it works here it works. Would i_getplotvalue accomplish the same on charts/outside Portfolio Trader?

Mydesign
Posts: 177
Joined: 15 Feb 2017
Has thanked: 32 times
Been thanked: 39 times

Re: Marketposition for Data2

Postby Mydesign » 15 Dec 2020

Hi,

You should bear in mind that i_setplotvalue / i_getplotvalue do not work intrabar, only at the close of a bar.

Also it does not work between 2 signals. Nevertheless, here is a trick: you can use a function as a "middleman" to transfer infos from one signal to another signal, and thus possibly having them to "talk to each others" within a chart ( i_setplotvalue in Signal1, i_Getplotvalue in Function1, and Signal2 calling Function1).


Return to “MultiCharts”