Programming partial exits using MultiCharts

Questions about MultiCharts and user contributed studies.
User avatar
furytrader
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 154 times
Been thanked: 215 times

Programming partial exits using MultiCharts

Postby furytrader » 05 Oct 2010

I am coming across a strange situation when I try to get MultiCharts to partially exit a position. Here is some very simple code I have written:

Code: Select all

   If Date = 1101005 then Begin
   
      If Time = 0835 then
         Buy 3 contracts next bar at the market;
         
          If Time = 0900 then
                 Buy 2 contracts next bar at the market;
      
      If Time = 0930 then
                 sell 1 contracts next bar at the market;
                
          If Time = 1000 then 
                 Sell 4 contracts next bar at the market;
   End;


As you can see, if the day is today's date, the system will buy and sell based upon what time it is. It first buys 3 contracts, then buys 2 contracts, then sells 1 contract and then sells 4 contracts.

However, when I actually run this code, it does the buys correctly but then when it comes to the first sell (at 9:30 am) it sells 2 contracts and then it sells 3 contracts at 10:00 am. I've attached a chart showing this.

Right now, the system is configured to allow 10 entry orders in the same direction as the currently held position regardless of the entry that generated the order. I'm not sure how this impacts the execution of the strategy but that's how it is currently configured.

If anyone has any idea on how to correctly have these buys and sells executed by MultiCharts, it would be greatly appreciated.
Attachments
Sample MultiCharts.GIF
Sample MultiCharts.GIF (68.81 KiB) Viewed 319 times

User avatar
furytrader
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 154 times
Been thanked: 215 times

Re: Programming partial exits using MultiCharts

Postby furytrader » 05 Oct 2010

What's strange as well is that if you change all of the contract sizes (for the buys and sells) to 1 contract, the program gets out of all of the contracts at 9:30 am (selling two contracts even though the program clearly states that we only want to sell one).

How do I change this behavior so that it does what I want it to do?

gregorio123456
Posts: 117
Joined: 08 Nov 2005
Been thanked: 3 times

Re: Programming partial exits using MultiCharts

Postby gregorio123456 » 06 Oct 2010

use this (from entry ("xxxxx"))
viewtopic.php?f=1&t=7803

.... use in your script the keyword (x contracts)w from entry ("XXXX")..... go to help of multicharts

Jo

User avatar
furytrader
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 154 times
Been thanked: 215 times

Re: Programming partial exits using MultiCharts

Postby furytrader » 06 Oct 2010

Thanks for the tip. However, doesn't that mean that you must "hard code" names for each of your trading signals into the code of your strategy?

In other words, let's imagine that you have a strategy that could concievably pyramid into a position with 30 different buy signals and then, as counter-signals are generated, needs to scale out of the postion, one trade at at time.

Wouldn't you then have to write specific entry and exit lines for each of these 30 potential entries in order to make sure that the exits are handled correctly?

I thought there might be a way to generate trading signal names (i.e., "Buy('Entry1')) on the fly but the buy and sell commands do not accept variables for signal names, only string constants (string literals).

I wish there was an elegant way to solve this problem without having to write repetitive lines of code such as:

Buy("Entry1") This Bar At Close;
Buy("Entry2") This Bar At Close;
Buy("Entry3") This Bar At Close;

... etc.

Any ideas or suggestions would be greatly appreciated.

gregorio123456
Posts: 117
Joined: 08 Nov 2005
Been thanked: 3 times

Re: Programming partial exits using MultiCharts

Postby gregorio123456 » 06 Oct 2010

you only need to write 30 signal(separate script signal......for exempla ...signal break ,signal oversell ,signal gap,signal crosover mov.averagen,signal volatility,etc) w entry and out(target or close position) and stoploss for each 30 pontential entries......and the system is configured to allow 30 entry orders in the same direction w diferent signal

jo

User avatar
furytrader
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 154 times
Been thanked: 215 times

Re: Programming partial exits using MultiCharts

Postby furytrader » 11 Oct 2010

It turns out that the solution to this is to use the "TOTAL" keyword. From the MultiCharts documentation:

"Used in strategy exit statements, following a numerical expression and the words Shares or Contracts, to indicate that only the number of contracts or shares specified by the numerical expression is to be sold or covered in total, regardless of the number of open entries. The contracts or shares will be sold or covered in the same order they were bought or shorted: First In, First Out. If the word Total is not used, the number of contracts or shares specified by the numerical expression will be sold or covered for each one of the open entries."


Return to “MultiCharts”