Trade FOREX with FXCM

  • Award-Winning Platform
  • 24/7 Customer Support
  • Trade Directly on Charts
  • Free $50K Practice Account
Register


Results 1 to 2 of 2

Thread: NULL pointer Error stack in ST coding - need help

  1. #1
    santhoshml is offline Member
    Join Date
    Aug 2011
    Posts
    24

    NULL pointer Error stack in ST coding - need help

    Hello,

    I am new ST and just learning how to program it. I am trying to code the below stratergy and

    if adx < 20, buy when price goes below lower BB with trailing SL=ATR and limit =SMA(15)
    if adx < 20, sell when price goes above upper BB with trailing SL=ATR and limit =SMA(15)

    I have written the below code, which to the best of my knowledge is correct (I am a fulltime programmer myself). But its throwing an exception, Not sure from which line. Can you help me on this.

    Code *************************
    using System;
    using Broker.StrategyLanguage.Function;
    namespace Broker.StrategyLanguage.Strategy
    {
    public class BBRangeTrading : BaseStrategyAdvisor
    {
    private AvgTrueRange m_avgtruerange1;
    private ISeries<Double> m_price;
    private AverageFC m_averagefc1;
    private SeriesVar<Double> m_fastavg;
    private ISeries<Double> m_bollingerprice;
    private ISeries<Double> m_testpricelband;
    private ISeries<Double> m_testpriceuband;
    private BollingerBand m_bollingerband1;
    private BollingerBand m_bollingerband2;
    private bool m_condition1;
    private SeriesVar<Double> m_lowerband;
    private SeriesVar<Double> m_higherband;
    private double m_TP0;
    private double m_SL0;
    private double m_TP1;
    private double m_SL1;
    private double m_price0, m_price1;
    private IPriceOrder m_Order0;
    private IPriceOrder m_Order0_TP;
    private IPriceOrder m_Order0_SL;
    private IPriceOrder m_Order1;
    private IPriceOrder m_Order1_TP;
    private IPriceOrder m_Order1_SL;
    private Function.ADX m_adx1;
    private SeriesVar<Double> m_adxvalue;
    public BBRangeTrading(object ctx) :
    base(ctx) {}
    private ISeries<Double> price{
    get { return m_price; }
    }
    private ISeries<Double> bollingerprice{
    get { return m_bollingerprice; }
    }
    private ISeries<Double> testpricelband{
    get { return m_testpricelband; }
    }
    private ISeries<Double> testpriceuband{
    get { return m_testpriceuband; }
    }

    protected override void Construct(){
    Output.Clear();
    Output.WriteLine("I am entering construct method");

    m_avgtruerange1 = new AvgTrueRange(this);
    m_averagefc1 = new AverageFC(this);
    m_fastavg = new SeriesVar<Double>(this);
    m_adx1 = new Function.ADX(this);
    m_adxvalue = new SeriesVar<Double>(this);
    m_bollingerband1 = new BollingerBand(this);
    m_bollingerband2 = new BollingerBand(this);
    m_lowerband = new SeriesVar<Double>(this);
    m_higherband = new SeriesVar<Double>(this);
    m_Order0 = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "BBandLE", OrderAction.Buy));
    m_Order0_TP = OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Default, "BBandLE_TP", OrderAction.Sell));
    m_Order0_SL = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "BBandLE_SL", OrderAction.Sell));
    m_Order1 = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "BBandSE", OrderAction.SellShort));
    m_Order1_TP = OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Default, "BBandSE_TP", OrderAction.BuyToCover));
    m_Order1_SL = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "BBandSE_SL", OrderAction.BuyToCover));

    Output.WriteLine("I am leaving construct method");
    }
    protected override void Initialize(){
    Output.WriteLine("I am entering initialize method");
    m_avgtruerange1.length = new Serie---pression<Int32>(delegate { return 14; });
    m_averagefc1.price = price;
    m_averagefc1.length = new Serie---pression<Int32>(delegate { return 15; });
    m_fastavg.DefaultValue = 0;
    m_bollingerprice = Bars.Close;
    m_testpricelband = Bars.Close;
    m_testpriceuband = Bars.Close;
    m_bollingerband1.price = bollingerprice;
    m_bollingerband1.length = new Serie---pression<Int32>(delegate { return 20; });
    m_bollingerband1.numdevs = new Serie---pression<Int32>(delegate { return (-1*2); });
    m_bollingerband2.price = bollingerprice;
    m_bollingerband2.length = new Serie---pression<Int32>(delegate { return 20; });
    m_bollingerband2.numdevs = new Serie---pression<Int32>(delegate { return 2; });
    m_condition1 = default(bool);
    m_lowerband.DefaultValue = 0;
    m_higherband.DefaultValue = 0;
    m_adx1.length = new Serie---pression<Int32>(delegate { return 14; });
    m_adxvalue.DefaultValue = 0;

    Output.WriteLine("I am leaving initialize method");
    }
    protected override void Destroy() {}
    protected override void Execute() {
    Output.WriteLine("I am entering execute method");

    Output.WriteLine("Bars.TimeValue="+Bars.TimeValue);
    Output.WriteLine("Bars.CurrentBar="+Bars.CurrentBar);
    Output.WriteLine("Bars.CloseValue="+Bars.CloseValue);
    Output.WriteLine("Bars.OpenValue="+Bars.OpenValue);
    Output.WriteLine("Bars.HighValue="+Bars.HighValue);
    Output.WriteLine("Bars.LowValue="+Bars.LowValue);

    m_adxvalue.Value = m_adx1[0];
    Output.WriteLine("m_adxvalue.Value="+m_adxvalue.Value);


    // m_fastavg.Value = m_averagefc1[0];
    // Output.WriteLine("m_fastavg.Value="+m_fastavg.Value);

    CheckTrailingStop();
    Output.WriteLine("I am done with CheckTrailingStop");

    CheckExitLogic();
    Output.WriteLine("I am done with CheckExitLogic");

    m_lowerband.Value = m_bollingerband1[0];
    Output.WriteLine("m_lowerband.Value="+m_lowerband.Value);

    m_condition1 = (Functions.DoubleGreater(Bars.CurrentBar, 1) &&
    Functions.CrossesOver(this, testpricelband, m_lowerband) &&
    Functions.DoubleLessEquals(m_adxvalue.Value, 20));
    if (m_condition1){
    m_price0 = m_lowerband.Value;
    m_SL0 = m_price0 - m_avgtruerange1[0] * Point;
    m_Order0.Generate(m_price0);
    }

    m_higherband.Value = m_bollingerband2[0];
    m_condition1 = (Functions.DoubleGreater(Bars.CurrentBar, 1) &&
    Functions.CrossesUnder(this, testpriceuband, m_higherband) &&
    Functions.DoubleLessEquals(m_adxvalue.Value, 20));
    if (m_condition1){
    m_price1 = m_higherband.Value;
    m_SL1 = m_price1 + m_avgtruerange1[0] * Point;
    m_Order1.Generate(m_price1);
    }
    }

    private void CheckExitLogic() {
    if(StrategyInfo.MarketPosition == 0)
    return;

    m_fastavg.Value = m_averagefc1[0];

    if(StrategyInfo.MarketPosition > 0) {
    m_Order0_TP.Generate(m_fastavg.Value);

    if(m_SL0 > 0)
    m_Order0_SL.Generate(m_SL0);
    }

    if(StrategyInfo.MarketPosition < 0) {
    m_Order1_TP.Generate(m_fastavg.Value);

    if(m_SL1 > 0)
    m_Order1_SL.Generate(m_SL1);
    }
    }

    private void CheckTrailingStop() {
    if(StrategyInfo.MarketPosition > 0) {
    if(m_SL0 >0 && (Bars.Close[0] - m_SL0) > (m_avgtruerange1[0] * Point))
    m_SL0 = Bars.Close[0] - m_avgtruerange1[0] * Point;
    }
    if(StrategyInfo.MarketPosition < 0) {
    if(m_SL1 > 0 && (m_SL1 - Bars.Close[0]) > (m_avgtruerange1[0] * Point))
    m_SL1 = Bars.Close[0] + m_avgtruerange1[0] * Point;
    }
    }

    private double Point //Returns the point value
    {
    get{
    if(Bars.Point < 0.01)
    {
    return(Bars.Point * 10);
    }
    else
    {
    return Bars.Point;
    }
    }
    }
    }
    }
    Attached Thumbnails Attached Thumbnails NULL pointer Error stack in ST coding -  need help-st-bb-range-error.jpg  


  2. #2
    jimmylmh is offline FXCM Automated Platform Specialist
    Join Date
    Dec 2010
    Posts
    964
    Hi santhoshml,

    Please try to change the line
    Code:
    m_averagefc1.price = price;
    to
    Code:
    m_averagefc1.price = Bars.Close;
    Hope this helps.

    Regards,
    Jimmy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Disclaimer: Trading foreign exchange on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts. Any opinions, news, research, analyses, prices, or other information contained on this website is provided as general market commentary and does not constitute investment advice. Forex Capital Markets LLC. will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.