track market position

Questions about MultiCharts .NET and user contributed studies.
RobBoss

track market position

Postby RobBoss » 11 Jul 2016

Hello,

I have been working on a custom strategy for a few months now and need help from the experts.

Lets assume I want to place orders when the market price hits $.50 increments from the open market price. My strategy is a simple over/under that follows the Bars.Close() and reverses when those numbers are reached. I set limit orders and a stop order at each increment.

The only hurdle I have left is tracking my market position. I need my signal to wait for the market position to change (from one of the limit orders I placed), before it moves on and places new limit orders. Right now, there are instances when multiple criteria are met, and each places its own limit orders, which results in a new order being submitted and completed each bar.

I have tried the Trademanager example from the forums, but I believe it was either over my head, or if I was using it correctly, then it did not work for me.

I have used Environment.CalcReason, CalculationReason.MarketPositionChange, but recently found out it does not work with backtesting. Playback is a great tool, but much too slow.

I have also used a changing variable such as: (credit to Jos from Tradingcode.net for the idea)

Code: Select all

currentMP = StrategyInfo.MarketPosition
if(currentMP != prevMP){
code...
}
prevMP = currentMP
but this also has not worked completely.

I have also tried to trap the code in a while loop such as:

Code: Select all

while(StrategyInfo.MarketPosition > 0)
{
output.writeline(example);
}
but this is very demanding on my computer when backtesting or using Playback, and results in MC crashing.

I am looking for a different way to go about this, or perhaps a better understanding of some of these strategies. Is there something in MC.net I have overlooked? Does anyone have any advice?

I will gladly provide more information if it makes things clearer.

Thank you

User avatar
Henry MultiСharts
Posts: 9093
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2923 times

Re: track market position

Postby Henry MultiСharts » 12 Jul 2016

I have also used a changing variable such as: (credit to Jos from Tradingcode.net for the idea)

Code: Select all

currentMP = StrategyInfo.MarketPosition
if(currentMP != prevMP){
code...
}
prevMP = currentMP
but this also has not worked completely.
Hello RobBoss,

This approach should work. If it did not work then the code is incorrect.
The only alternative is the TradeManager, but it cannot be backtested.

eroleyikan
Posts: 22
Joined: 01 Jun 2016
Has thanked: 4 times
Been thanked: 6 times

Re: track market position

Postby eroleyikan » 12 Jul 2016

Bob,
I use Trademanager to track my market position real time. I cannot post full code without stripping strategy specific lines. But here is what I do in summary, and it works great. It would do what you need

Declare these class level variables
bool subscribed = false;
TradeManager.IPositions positionList;

Then in CalcBar() enter this code

Code: Select all

if (!subscribed)
{
TradeManager.TradingData.Positions.FinishChanging += new EventHandler(Positions_FinishChanging);
subscribed = true;
}t
Then create a method like this:

Code: Select all

private void Positions_FinishChanging(object sender, EventArgs e)
{
TradeManager.IPositions pos = sender as TradeManager.IPositions;
foreach (TradeManager.Position p in pos.Items)
{ //your custom code here
//p.value gives you current position}
}
Positions_FinishChanging gets triggered when any of your positions change. So you would have to check the symbol you are in. p.SymbolStr gives you the symbol above.

let me know if this helps.
Erol

RobBoss

Re: track market position

Postby RobBoss » 12 Jul 2016

Thank you for the responses.

Erol, that is essentially how I was using Trademanager, and that makes me feel better about my understanding of the software. However, without the ability to backtest, I would be hesitant to try any of my strategies.

I would actually like to ask about using while loops. It seem every time I use one in a strategy, as soon as I load it onto a chart (or try playback) MC locks up and usually results in me having to close the program the hard way.

while(StrategyInfo.MarketPosition > 0)
{
output.writeline(example);
}

This loop in particular would solve all over my problems, but it seems to be too much for my computer. Three years ago my comp was top of the line and i can't imagine it is having trouble with this. Is there a setting I need to check?

I only have a chart of 1-2 days open and a 2 second bar. I also use IOG and when using playback, I tried going as far down as 5 updates per tick. Same result.

Any clue what could be happening? Is this a common problem?

User avatar
ABC
Posts: 629
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: track market position

Postby ABC » 13 Jul 2016

while(StrategyInfo.MarketPosition > 0)
{
output.writeline(example);
}
RobBoss,

I never had an issue with while loops in MC.NET, but the above would become an infinite loop as soon you are long, which might explain why your computer locks up. MC doesn't detect infinite loops for you during execution (unlike TS for example).

Regards,

ABC

RobBoss

Re: track market position

Postby RobBoss » 13 Jul 2016

Hi ABC,

I see what you are saying. The example loop is more accurately a temporary infinite loop as I only use them when it has the ability to leave, in this case, for the market position to change. A thought just occurred to me, would I have to call the market position within the loop to help it leave? Such As:

currentMP = StrategyInfo.MarketPosition;

while( currentMP > 0)
{
output.writeline(example);
currentMP = StrategyInfo.MarketPosition;
}

This seems redundant now that I write it out... Also, a side question if anyone reads this far:
Is there any way to retract or cancel a stop or limit order?

Thanks

eroleyikan
Posts: 22
Joined: 01 Jun 2016
Has thanked: 4 times
Been thanked: 6 times

Re: track market position

Postby eroleyikan » 13 Jul 2016

Rob,
I agree with ABC, that's a guaranteed infinite loop. Even with the exit attempt, it would be infinite, because if you have IOG enabled, your calcbar() code is running every tick anyways.
Without knowing exactly what you are trying to do, I would think that using an if statement would work just fine.
if( currentMP > 0)
{
output.writeline(example);
currentMP = StrategyInfo.MarketPosition;
}

on your question on cancelling an order... yes there is a way. There are some other threads on this topic though. i found them a while back and implemented my version. Search for TradeManager.ITradingProfile CancelOrder() method. If you cannot find what you are looking for post a separate new topic, and I will try to help.
Thanks
Erol

RobBoss

Re: track market position

Postby RobBoss » 19 Jul 2016

Thank you Erol and ABC,

I am convinced I can not use a while loop like that now.

I have a question about using Trademanager to track: If I used the Positions.FinishChanging event trigger as a condition for the code that holds order submissions, is the event considered 'triggered' for only one pass-through of the script?

EX:
long(1)...long(2)...long(3)...event trigger- now short(4)...short(5)...short(6)...short(7)

Because if I want to use limit orders, they would only be submitted on one iteration of the script, and as I have come to learn, if a limit order is not sent through on every bar, it is not considered at the broker.

Also, with market orders, the price would need time to reach certain price points to trigger.

To rephrase: How long is TradeManager.TradingData.Positions considered FinishChanging?

The same goes for Environment.CalcReason, CalculationReason.MarketPositionChange.

Thanks

eroleyikan
Posts: 22
Joined: 01 Jun 2016
Has thanked: 4 times
Been thanked: 6 times

Re: track market position

Postby eroleyikan » 20 Jul 2016

Rob,
This is beyond my knowledge of how MC works. I will leave this to someone from MC to respond/resolve.
Thanks
Erol

User avatar
Henry MultiСharts
Posts: 9093
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2923 times

Re: track market position

Postby Henry MultiСharts » 20 Jul 2016

When TradeManager.ProcessEvents is called - MultiCharts starts checking for changes in the Order and Position Tracker data. Once changes are found - the corresponding event is triggered.

If you want to get the information regarding changes in the TradeManager faster - you need to call the TradeManager.ProcessEvents more often in your code.

CalculationReason.MarketPositionChange is triggered once broker sends the info about position change.


Return to “MultiCharts .NET”