CurrentPosition sample code

Questions about MultiCharts .NET and user contributed studies.
webspiderc
Posts: 6
Joined: 26 Aug 2013
Has thanked: 1 time

CurrentPosition sample code

Postby webspiderc » 27 Oct 2013

I am newbie to MC and .Net. Is there any sample code on how to use CurrentPosition ? I just couldn't figure out how to use it.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: CurrentPosition sample code

Postby JoshM » 28 Oct 2013

I am newbie to MC and .Net. Is there any sample code on how to use CurrentPosition ? I just couldn't figure out how to use it.
Can you tell how you want to use it? There's not much of an example to give otherwise.

CurrentPosition is documented in the programming manual on page 43.

webspiderc
Posts: 6
Joined: 26 Aug 2013
Has thanked: 1 time

Re: CurrentPosition sample code

Postby webspiderc » 28 Oct 2013

Thx for reply, even this is a silly question.

What I wanted to do is to print out the CurrentPosition.Profit.Value. When I put it in the line

Output.WriteLine("Cur Bar: {0} Position {1}, P&L {2}", Bars.CurrentBar ,StrategyInfo.MarketPosition,CurrentPosition.Profit);

The compiler complains about:
The name 'CurrentPosition' does not exist in the current context

In the 4.6.3 it said "More detailed information upon current strategy parameters can be obtained through CurrentPosition(IMarketPosition)"

The point is I really doesn't catch what does "obtained through CurrentPosition(IMarketPosition)" means.

In contrast, StrategyInfo.MarketPosition is pretty straight forward like a ordinary variable.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 29 Oct 2013

webspiderc, it looks like you are trying to access this property from the place where it is not accessible, for ex. in your own new class (not in create and/or calcbar).

webspiderc
Posts: 6
Joined: 26 Aug 2013
Has thanked: 1 time

Re: CurrentPosition sample code

Postby webspiderc » 29 Oct 2013

Dear Henry

Can you help to drop me the most crucial line(s)? I have scratched my head for over weeks on this.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 29 Oct 2013

Please attach the complete code of the study you are having problem with or send it to my email (support@multicharts.com).

webspiderc
Posts: 6
Joined: 26 Aug 2013
Has thanked: 1 time

Re: CurrentPosition sample code

Postby webspiderc » 29 Oct 2013

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
public class Donchian_Channel : IndicatorObject
{
private Function.HighestFC m_channelup;
private Function.LowestFC m_channeldown;
private Function.HighestFC m_failsafeup;
private Function.LowestFC m_failsafedown;
private Function.HighestFC m_longexit;
private Function.LowestFC m_shortexit;


public VariableSeries<double> m_ChannelUp;
public VariableSeries<double> m_ChannelDown;
public VariableSeries<double> m_FailsafeUp;
public VariableSeries<double> m_FailsafeDown;
public VariableSeries<double> m_LongExit;
public VariableSeries<double> m_ShortExit;
private VariableSeries<int> m_position;

private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;
private IPlotObject Plot4;
private IPlotObject Plot5;
private IPlotObject Plot6;




public Donchian_Channel(object _ctx):base(_ctx)
{
channelup_range=20;
channeldown_range=20;
failsafeup_range=55;
failsafedown_range=55;
longexit_range=10;
shortexit_range=10;

}



// private ISeries<double> donchianprice { get; set; }

// private ISeries<double> upperchannel { get; set; }


[Input]
public int channelup_range { get; set; }

[Input]
public int channeldown_range { get; set; }

[Input]
public int failsafeup_range { get; set; }

[Input]
public int failsafedown_range { get; set; }

[Input]
public int longexit_range { get; set; }

[Input]
public int shortexit_range { get; set; }

private IPlotObject plot1;
private IPlotObject plot2;
private IPlotObject plot3;
private IPlotObject plot4;
private IPlotObject plot5;
private IPlotObject plot6;


protected override void Create()
{
// create variable objects, function objects, plot objects etc.
m_channelup = new HighestFC(this);
m_channeldown = new LowestFC(this);
m_failsafeup = new HighestFC(this);
m_failsafedown = new LowestFC(this);
m_longexit = new HighestFC(this);
m_shortexit = new LowestFC(this);

m_ChannelUp = new VariableSeries<double>(this);
m_ChannelDown = new VariableSeries<double>(this);
m_FailsafeUp = new VariableSeries<double>(this);
m_FailsafeDown = new VariableSeries<double>(this);
m_LongExit = new VariableSeries<double>(this);
m_ShortExit = new VariableSeries<double>(this);

m_position = new VariableSeries<int>(this);



plot1 = AddPlot(new PlotAttributes("Dochian_up", EPlotShapes.Line, Color.Red, Color.Empty, 1, 0, true));
plot2 = AddPlot(new PlotAttributes("Dochian_down", EPlotShapes.Line, Color.Blue, Color.Empty, 1, 0, true));
plot3 = AddPlot(new PlotAttributes("Failsafe_up", EPlotShapes.Line, Color.Cyan, Color.Empty, 0, 2, true));
plot4 = AddPlot(new PlotAttributes("FailSafe_down", EPlotShapes.Line, Color.Yellow, Color.Empty, 0, 2, true));
plot5 = AddPlot(new PlotAttributes("Short_Exit", EPlotShapes.Line, Color.Purple, Color.Empty, 0, 2, true));
plot6 = AddPlot(new PlotAttributes("Long_Exit", EPlotShapes.Line, Color.Green, Color.Empty, 0, 2, true));


}
protected override void StartCalc()
{
m_channelup.pricevalue=Bars.High;
m_channelup.len=channelup_range;
m_channeldown.pricevalue=Bars.Low;
m_channeldown.len=channeldown_range;

m_failsafeup.pricevalue=Bars.High;
m_failsafeup.len=failsafeup_range;
m_failsafedown.pricevalue=Bars.Low;
m_failsafedown.len=failsafedown_range;

m_longexit.pricevalue=Bars.High;
m_longexit.len=longexit_range;
m_shortexit.pricevalue=Bars.Low;
m_shortexit.len=shortexit_range;

}
protected override void CalcBar()
{
// indicator logic
m_ChannelUp.Value=m_channelup[1];
m_ChannelDown.Value=m_channeldown[1];
m_FailsafeUp.Value=m_failsafeup[1];
m_FailsafeDown.Value=m_failsafedown[1];
m_ShortExit.Value=m_shortexit[1];
m_LongExit.Value=m_longexit[1];

// #if DEBUG
m_position.Value=this.StrategyInfo.MarketPosition;
Output.WriteLine("Cur Bar: {0} Position {1}, Avg Price {2}, OpenEquity {3}, ClosedEquity {4}, IMarket {5}", Bars.CurrentBar ,StrategyInfo.MarketPosition, StrategyInfo.AvgEntryPrice,StrategyInfo.OpenEquity,StrategyInfo.ClosedEquity,CurrentPosition.OpenTrades.Count);
// Output.WriteLine("Cur Bar: {0} Position {1}, Current Value {2}, P/L {3}", Bars.CurrentBar ,m_position.Value, CurrentPosition.OpenTrades,CurrentPosition.Profit);
// #endif

if(m_channelup[1] != 0) //to remove the first zero record caused by
{
plot1.Set(m_ChannelUp.Value);
plot2.Set(m_ChannelDown.Value);
plot3.Set(m_FailsafeUp.Value);
plot4.Set(m_FailsafeDown.Value);
plot5.Set(m_ShortExit.Value);
plot6.Set(m_LongExit.Value);
}
}
}
}

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: CurrentPosition sample code

Postby JoshM » 29 Oct 2013

Code: Select all

m_position.Value=this.StrategyInfo.MarketPosition;
Output.WriteLine("Cur Bar: {0} Position {1}, Avg Price {2}, OpenEquity {3}, ClosedEquity {4}, IMarket {5}", Bars.CurrentBar ,StrategyInfo.MarketPosition, StrategyInfo.AvgEntryPrice,StrategyInfo.OpenEquity,StrategyInfo.ClosedEquity,CurrentPosition.OpenTrades.Count);
// Output.WriteLine("Cur Bar: {0} Position {1}, Current Value {2}, P/L {3}", Bars.CurrentBar ,m_position.Value, CurrentPosition.OpenTrades,CurrentPosition.Profit);
In this case the question becomes: how to create a signal object in an indicator so that this signal object can be called to access its values in an indicator?

I'd love to see an example for this, MC Support.

Earlier Dru gave an example here, but I never got that code running fine with multiple updates per bar (and given that he only got one thanks for that post with 100+ downloads, I suppose other people also didn't got it working like they wished).

(Edit: that is of course not to say that Dru's example wasn't helpful. I'm saying that another example, that addresses those points, would be welcomed).

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 31 Oct 2013

webspiderc,

Positions information is available for signals only. At the moment there is no direct way to access the information about the positions, in particular CurrentPosition, from the indicator code. You need to pass this information into the indicator code. There are multiple ways to do that:
1) Transfer the values with the help of Global Variables or Native C# Solution for Global Storage.
2) Receive the values you need with the help of TradeManager. Examples: auto trading from market scanner, Trade manager example.
3) Another example of transferring the values between the studies using a static class has been provided by Dru
in the following thread.

In MultiCharts .Net 9.0 we are going to add a built-in ability of accessing the signal values from the studies applied to the same chart.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 31 Oct 2013

Earlier Dru gave an example here, but I never got that code running fine with multiple updates per bar (and given that he only got one thanks for that post with 100+ downloads, I suppose other people also didn't got it working like they wished).

(Edit: that is of course not to say that Dru's example wasn't helpful. I'm saying that another example, that addresses those points, would be welcomed).
JoshM, in the mentioned thread you have found the right answer yourself:
I'm supposed to call a Dispose() or anything on a certain object to prevent these steps.
Were you unable to implement that logic?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: CurrentPosition sample code

Postby JoshM » 31 Oct 2013

I'm supposed to call a Dispose() or anything on a certain object to prevent these steps.
Were you unable to implement that logic?
Yes, I implemented it as:

Code: Select all

protected override void Destroy()
{
StrategiesPool<TestStrategy2>.UnRegister(this);
base.Destroy();
}
But that did not help the issues I was having with it. I'll try it again now, perhaps now I better see where I went wrong.
In MultiCharts .Net 9.0 we are going to add a built-in ability of accessing the signal values from the studies applied to the same chart.
That's great. :)

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: CurrentPosition sample code

Postby JoshM » 31 Oct 2013

I'm supposed to call a Dispose() or anything on a certain object to prevent these steps.
Were you unable to implement that logic?
It works, yes, but even after implementing that logic I still run into this:
- Perhaps this is MC .NET related, but if I change one line in the source code of the indicator (even if it's a enter to make some whitespace) and save the indicator, the indicator line returns back to a horizontal zero line.

To then get the correct indicator line, I need to remove the indicator and strategy, then re-apply the strategy and change it settings to my preference, than re-apply the indicator and change it settings, and then the indicator plots the correct values.
On MultiCharts .NET64 Version 8.7 Release (Build 7636), after a change to either the indicator or strategy, I need to remove them both from the chart and re-add then, since just saving and letting them automatically reload does not work.

That makes this, for me, not a doable 'solution' since I program incrementally and change little things as I go. It's not workable to every time having to manually remove the indicator and strategy, then re-add them and re-enter the inputs.

So, that bring me back to the previous question: does someone has a good example for this issue? (Though I don't mind waiting for MC9 for this feature; but hopefully that MC9 feature will be implemented in a productive manner).

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 01 Nov 2013

This case is being analyzed by our engineers. I will keep the board posted.

webspiderc
Posts: 6
Joined: 26 Aug 2013
Has thanked: 1 time

Re: CurrentPosition sample code

Postby webspiderc » 04 Nov 2013

Is there any tentative timeline on MC 9?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 04 Nov 2013

Is there any tentative timeline on MC 9?
There is no ETA for this version yet.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: CurrentPosition sample code

Postby Henry MultiСharts » 05 Nov 2013

I'm supposed to call a Dispose() or anything on a certain object to prevent these steps.
Were you unable to implement that logic?
It works, yes, but even after implementing that logic I still run into this:
- Perhaps this is MC .NET related, but if I change one line in the source code of the indicator (even if it's a enter to make some whitespace) and save the indicator, the indicator line returns back to a horizontal zero line.

To then get the correct indicator line, I need to remove the indicator and strategy, then re-apply the strategy and change it settings to my preference, than re-apply the indicator and change it settings, and then the indicator plots the correct values.
On MultiCharts .NET64 Version 8.7 Release (Build 7636), after a change to either the indicator or strategy, I need to remove them both from the chart and re-add then, since just saving and letting them automatically reload does not work.

That makes this, for me, not a doable 'solution' since I program incrementally and change little things as I go. It's not workable to every time having to manually remove the indicator and strategy, then re-add them and re-enter the inputs.

So, that bring me back to the previous question: does someone has a good example for this issue? (Though I don't mind waiting for MC9 for this feature; but hopefully that MC9 feature will be implemented in a productive manner).
JoshM,

Our programmer has modified the study. Now you do not need to delete/reapply it to the chart. The study values will be flushed automatically once you save the study and updated on the chart once new ticks are received.
Attachments
FromStrategyToIndicatorDataTransfer.pln
(3.38 KiB) Downloaded 551 times


Return to “MultiCharts .NET”