CurrentPosition.Side == EMarketPositionSide.Short

Questions about MultiCharts .NET and user contributed studies.
lewisthegood
Posts: 21
Joined: 08 Mar 2021
Been thanked: 1 time

CurrentPosition.Side == EMarketPositionSide.Short

Postby lewisthegood » 07 Feb 2022

I have a short position x1 of an index Futures, and trying to use "CurrentPosition.Side == EMarketPositionSide.Short" condition to assign a variable to when a short position is opened. However, it appears that the scripts did not run the statement for this condition when I already have a short position opened (did not print the "testing").

Please advise if the following coding is correct, or is there another way to do it? (I am building a spread trading strategy - Long A and Short B simultaneously once the conductions are met, hence I need to pass some variable when A has been shorted / B has been longed for global variables)

....
protected override void Create() {
short_order = OrderCreator.Limit(new SOrderParameters(Contracts.Default, EOrderAction.SellShort));
}

protected override void StartCalc() {
Output.Clear();
}
protected override void CalcBar(){
...
if(CurrentPosition.Side == EMarketPositionSide.Short)
{
int testing = -1;
Output.WriteLine("{0}",testing);
}

lewisthegood
Posts: 21
Joined: 08 Mar 2021
Been thanked: 1 time

Re: CurrentPosition.Side == EMarketPositionSide.Short

Postby lewisthegood » 07 Feb 2022

I was thinking about using "StrategyInfo.MarketPosition < 0" but it will not work as both short A and long B will net off the StrategyInfo.MarketPosition which will become 0

User avatar
Tammy MultiCharts
Posts: 200
Joined: 06 Aug 2020
Has thanked: 6 times
Been thanked: 65 times

Re: CurrentPosition.Side == EMarketPositionSide.Short

Postby Tammy MultiCharts » 28 Feb 2022

Hello lewisthegood,

Please check the code sample below that might help you achieve your goal.

Code: Select all

using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; namespace PowerLanguage.Strategy { public class _Test_ : SignalObject { public _Test_(object _ctx):base(_ctx){} private IOrderPriced short_order; protected override void Create() { short_order = OrderCreator.Limit(new SOrderParameters(Contracts.Default, EOrderAction.SellShort)); } protected override void StartCalc() { Output.Clear(); } protected override void CalcBar() { if (Bars.CurrentBar == 10) short_order.Send(Bars.Close[0] - 10.0); Output.WriteLine("BarUpdateTime: {0}", Bars.BarUpdateTime); if(CurrentPosition.Side == EMarketPositionSide.Short) { int testing = -1; Output.WriteLine("{0}",testing); } } } }


Return to “MultiCharts .NET”