How to control buy & sell from the portfolio signal

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
fbertram
Posts: 128
Joined: 16 Oct 2014
Has thanked: 37 times
Been thanked: 51 times

How to control buy & sell from the portfolio signal

Postby fbertram » 14 Oct 2016

The portfolio signal is executed after all the trading strategies have been executed. If you want to emit trades based on portfolio decisions, this leads to a one bar delay - which in turn might force you out of daily bars and into 5 minutes or so.

The code I am sharing below overcomes this limitation. This how it works:
* the trading signal always emits buy and sell orders
* the portfolio signal enables and disables these orders and adjusts the entry size as required

In order for this to work, the following is required:
* the trading signal must set [SameExitFromOneEntryOnce = false]
* in portfolio trader, set 'allow up to xxx entry orders in the same direction' to a ridiculously high value

The code I am providing keeps the money invested per strategy close to $10k. You will sure have better ideas what to do here.

Hope this helps,
best regards, Felix

Trading signal:

Code: Select all

[SameExitFromOneEntryOnce = false]

buy 1 share next bar at market;
sell 1 share total next bar at market;

pmm_set_my_named_num("STRATEGY_POS", {marketposition *} currentcontracts);
pmm_set_my_named_num("STRATEGY_PRICE", close);


Portfolio signal:

Code: Select all

once ClearPrintLog;

var: idx(0);
for idx = 0 to pmms_strategies_count - 1 begin
   var: pos(0);
   pos = pmms_get_strategy_named_num(idx, "STRATEGY_POS");
   var: price(0);
   price = pmms_get_strategy_named_num(idx, "STRATEGY_PRICE");
   var: net(0);
   net = pos * price;
   var: delta(0);
   delta = IntPortion((10000 - net) / price);

   print(ELDateToString(date),
      " ", pmms_strategy_symbol(idx),
      ", pos = ", pos,
      ", price = ", price,
      ", net  = ", net,
      ", delta = ", delta);

   if delta > 0 then begin
      pmms_strategy_deny_exit_from_long(idx);
      pmms_strategy_allow_long_entries(idx);
      pmms_strategy_set_entry_contracts(idx, delta);
   end else if delta < 0 then begin
      pmms_strategy_deny_long_entries(idx);
      pmms_strategy_allow_exit_from_long(idx);
      pmms_strategy_set_entry_contracts(idx, -delta);
   end else begin
      pmms_strategy_deny_long_entries(idx);
      pmms_strategy_deny_exit_from_long(idx);
   end;
end;

TraderWalrus
Posts: 48
Joined: 13 Sep 2016
Has thanked: 26 times
Been thanked: 5 times

Re: How to control buy & sell from the portfolio signal

Postby TraderWalrus » 08 Aug 2017

My (long) strategy exits with a limit, and has a stoploss as well.

So if I understand correctly, using pmms_strategy_deny_exit_from_long will cancel not only the "sell 1 share total next bar at market" but also those orders, in the case size should be increased (delta > 0).

How would you handle that?

fbertram
Posts: 128
Joined: 16 Oct 2014
Has thanked: 37 times
Been thanked: 51 times

Re: How to control buy & sell from the portfolio signal

Postby fbertram » 09 Aug 2017

I haven't tried that, and I moved on to MultiCharts .NET where these things can be handled in a slightly more convenient way. Having said that, I would try to add the limit and the stop loss right where the buy 1/ sell 1 orders are created. I am reasonably sure the limit order is going to work, the stop loss might be controlled by the 'deny exit' - which is not what you would want.

fbertram
Posts: 128
Joined: 16 Oct 2014
Has thanked: 37 times
Been thanked: 51 times

Re: How to control buy & sell from the portfolio signal

Postby fbertram » 09 Aug 2017

just in case going to .NET is an option at all, here is how this could look:
* in your strategy signal:
- create a function (1) in your strategy returning indicator values, probably performance, to the portfolio signal
- create a function (2) in your strategy to emit orders, with a parameter taking dollar amounts
* in your portfolio signal
- call (1) to collect all your data from the individual strategies
- do your magic to determine how much money to allocate to each strategy
- call (2) to actually emit the orders

There is a sample here: viewtopic.php?t=50286

The upside of using .NET is that can tailor things much better to your needs. The downside is, that you will need to code a little more, and can no longer use any EasyLanguage code. For my personal application, .NET was the right choice, but your mileage may vary.

Cheers, Felix

TraderWalrus
Posts: 48
Joined: 13 Sep 2016
Has thanked: 26 times
Been thanked: 5 times

Re: How to control buy & sell from the portfolio signal

Postby TraderWalrus » 09 Aug 2017

fbertram wrote:The upside of using .NET is that can tailor things much better to your needs. The downside is, that you will need to code a little more, and can no longer use any EasyLanguage code. For my personal application, .NET was the right choice, but your mileage may vary.


Yes, apparently the code simplicity of EasyLanguage does not always save time, since complex workarounds are sometimes needed to overcome limitations, if they're possible at all.

I have several strategies running live 24/5 on MC for way over a year now though, and it will not be trivial to move to .NET.


Return to “User Contributed Studies and Indicator Library”