Scaling Out Strategy with IOrderMarket instead of IOrderPriced

Questions about MultiCharts .NET and user contributed studies.
AsoStrife
Posts: 1
Joined: 24 Mar 2020

Scaling Out Strategy with IOrderMarket instead of IOrderPriced

Postby AsoStrife » 28 Apr 2020

Hi, that's gonna be a pretty complex question. I'll try to be as short and clear as I can.

I want to implement a "Scaling Out" strategy in C# within multicharts, i.e. I want to buy a future every day at the first bar and sell it after 5 days always at the first available bar.

The strategy is powered by a CSV generated by me that contains the following structure:

Dates, Decision, ExitDate, UniqueID

where:

- Dates is the day I want to open a position.
- Decision contains the operation to be carried out (2: long, 0: short or 1: hold)
- ExitDate corresponds to the day I want to close the operation
- UniqueID contains a unique ID for that operation.

Here you can find an example of the csv: https://pastebin.com/vPs1RVJH


I'll skip the CSV reading part for simplicity because it's widely tested on other strategies, I know it works great.

Okay, let's get to the hard part. https://pastebin.com/WaZUitEn

Starting from line 13 to line 53, I create all the variables I need to manage the strategy. In particular 19-22 I create List<IOrderPriced> in order to contain in each element of the list the operations I wants to perform and from 45 to 50 I fill the list.

From line 101 to 119 there is all the part relating to the purchase of a future (entry condition). Basically for each line of the CSV I control which operation to do and in the bar corresponding to that date I perform the operation. So if my csv contains the following line:
2015-01-02, 2, 2015-01-09, 0
I will open a Long position on 2015-01-02 with ID 0. The exact line of code is 107: this.buy_order_long[Line.UniqueID].Send(Bars.Open[0], 1);

From line 80 to 93 I do the opposite, I check the output conditions. So referring to the previous example:

2015-01-02, 2, 2015-01-09, 0

on 2015-01-09 I close my Long operation and I do it at line 86 with the command: this.sell_order_long[LineExit.UniqueID].Send(Bars.Open[0], 1);


Basically the scaling out seems to work, positions are opened and closed according to CSV guidelines. However, there is an underlying problem: IOrderPriced wants as a Send parameter the price that must match to open or close the position. Looking at my code it is indicate as

this.buy_order_short[Line.UniqueID].Send(Bars.Open[0], 1); ---> Bars.Open[0] is the price to match.

So it happens that if the price does not match the position is never opened or maybe never closed.

So let's get to my question: is there any way not to use IOrderPriced but another class to always enter the first bar (whatever the price) and always exit at the first bar of another day (whatever the price)?

In the past I tried IOrderMarket but with terrible results. When I try to close a potion IOrderMarket closes all the other open positions, so I can't implement a real scaling out.

Can someone help me? If I haven't been clear enough, I'm willing to give more explanation.

Thank You

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: Scaling Out Strategy with IOrderMarket instead of IOrderPriced

Postby Vlada MultiCharts » 25 Aug 2020

Hello AsoStrife,

During auto trading, MultiCharts sends only one market order, so it will be required to modify your script. Instead of sending several orders simultaneously, you can calculate contracts for a single market order.

As a result, when you used market order, you encountered an issue with IOrderMarket.

We suggest the following.
Your system buys on the first bat and then sells after 5 days on the first bar. For IOrderMarket not to close all the positions, you can use the OrderExit.Total parameter for exit orders.

For example:

Code: Select all

protected override void Create() { SellShort = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified, "Enter", EOrderAction.SellShort)); buy_to_cover_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified, "Exit" , EOrderAction.BuyToCover, OrderExit.Total )); } protected override void StartCalc() { } protected override void CalcBar() { if (Bars.CurrentBar % 10 == 0) { SellShort.Send(20); } if (Bars.CurrentBar % 33 == 0) { //SellShort.Send(5); } if (Bars.CurrentBar % 100 == 0) { buy_to_cover_order.Send(5); } }

OrderExit.Total – closes the position on ONLY the specified quantity of contracts. The entries will be closed accordingly, starting from the first one, up to the last one, until the exit is executed upon the quantity of contracts specified.
For more info please check here.

Here is the idea.
In the next CSV file, create an additional column with a number of the orders in position that you would like to have as a result for a specific date.

For example,
On the first bar you have 20 orders open;
On the second you have 5;
Etc.
Let's suppose, the general position is 5+20 = 25.
On the 5th bar you need to close 20.

Then, you analyze the number of contracts in position. In the code you calculate how many orders should be closed to coincide with the CSV file. If in the CSV file you have position = 20 for the time of the 5th bar, you'll need to close 25-20=5 contracts. In this case you can use exit order for 5 contracts defined with OrderExit.Total parameter.

If according to CSV, the number of contracts is 35 for the moment of the 5th bar, and the current position is 25, it will be required to add 10 contracts. You can do this by sending a Buy Entry market order.


Return to “MultiCharts .NET”