Variable issue - unexpected outcome during the calculation  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
~Zola~
Posts: 20
Joined: 28 Nov 2016
Has thanked: 5 times
Been thanked: 1 time

Variable issue - unexpected outcome during the calculation

Postby ~Zola~ » 04 Dec 2019

I suspected I am using the wrong method to define the variable in the program, so that the calculation is in error. Here is the output as below:
Current Time:1/2/2001 9:57:00, Bars.High:15911, TempL:15890, Testing TempL
Current Time:2/2/2001 11:02:00, Bars.High:16140, TempL:16105, After Shortsell
Current Time:2/2/2001 11:02:00, Bars.High:16140, TempL:15890, After Buy
The variable m_TempL.Value was assigned to 16105 at 2/2/2001 11:02:00, but suddenly m_TempL.Value was reassigned to 15890 at next tick/bar. I have investigated this problem for a few weeks. Could I have any idea to solve this problem? Thank you.
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy
{
[IOGMode(IOGMode.Enabled)]
public class W_KK_Wave_V6 : SignalObject
{
private VariableObject<Double> m_TempL, m_TempH;

public W_KK_Wave_V6(object _ctx):base(_ctx)
{
buyPct = 0.011;
sellPct = 0.011;
}
[Input]
public double buyPct { get; set; }
[Input]
public double sellPct { get; set; }

private IOrderMarket m_TrailingLE;
private IOrderMarket m_TrailingSE;

protected override void Create()
{
// create variable objects, function objects, order objects etc.
m_TrailingLE = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "TrailingLE", EOrderAction.Buy));
m_TrailingSE = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "TrailingSE", EOrderAction.SellShort));
m_TempL = new VariableObject<double>(this);
m_TempH = new VariableObject<double>(this);
Output.Clear();
}
protected override void StartCalc()
{
// assign inputs
m_TempL.DefaultValue = 99999;
m_TempH.DefaultValue = 0;
}
protected override void CalcBar()
{
// strategy logic
if (Bars.Low[0] < m_TempL.Value)
{
m_TempL.Value = Bars.Low[0];
Output.WriteLine("Current Time:{0}, Bars.High:{1}, TempL:{2}, Testing TempL", Bars.BarUpdateTime, Bars.High[0], m_TempL.Value);
}
if (Bars.High[0] > m_TempH.Value)
{
m_TempH.Value = Bars.High[0];
}
if (Bars.High[0] > (m_TempL.Value * (1 + buyPct)) && (StrategyInfo.MarketPosition == 0))
{
m_TrailingLE.Send();
}
if (Bars.Low[0] < (m_TempH.Value * (1 - sellPct)) && (StrategyInfo.MarketPosition == 0))
{
m_TrailingSE.Send();
}
if (Bars.High[0] > (m_TempL.Value * (1 + buyPct)) && (StrategyInfo.MarketPosition < 0))
{
m_TrailingLE.Send();
m_TempH.Value = Bars.High[0];
Output.WriteLine("Current Time:{0}, Bars.High:{1}, TempL:{2}, After Buy", Bars.BarUpdateTime, Bars.High[0], m_TempL.Value);
}
if (Bars.Low[0] < (m_TempH.Value * (1 - sellPct)) && (StrategyInfo.MarketPosition > 0))
{
m_TrailingSE.Send();
m_TempL.Value = Bars.Low[0];
Output.WriteLine("Current Time:{0}, Bars.High:{1}, TempL:{2}, After Shortsell", Bars.BarUpdateTime, Bars.High[0], m_TempL.Value);
}
}
}
}
Last edited by ~Zola~ on 05 Dec 2019, edited 3 times in total.

User avatar
dataheck
Posts: 9
Joined: 19 Nov 2019
Location: Toronto, Ontario
Contact:

Re: Variable issue - unexpected outcome during the calculation

Postby dataheck » 04 Dec 2019

I'm not sure (new to MultiCharts), but I think this happened in the same bar, not the next bar. I think your code allows for your buy signal to be triggered, and then the sell signal to be triggered on the same bar. Try making that second "if" an "else if" to force only one to be executed per bar and see if that changes the behaviour.

Naturally you'll have to think about if that's the behaviour you want or not, but that'll at least identify it.

~Zola~
Posts: 20
Joined: 28 Nov 2016
Has thanked: 5 times
Been thanked: 1 time

Re: Variable issue - unexpected outcome during the calculation  [SOLVED]

Postby ~Zola~ » 05 Dec 2019

MarketPosition had not been real established under IOGMode, so that the program not work


Return to “MultiCharts .NET”