Trade FOREX with FXCM

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


Results 1 to 7 of 7
  1. #1
    Micoud19 is offline Member
    Join Date
    Feb 2012
    Posts
    8

    Unhappy Accessing real time data from BarsOfData(2)

    I would appreciate a little help with the following:

    I am trying to code a strategy that evaluates and executes an SMA and EMA crossover on a 1H chart. The first problem I found was that even though I had configured the IOG to execute on each tick, the order was always placed on the next bar while I needed it to enter the order at the crossing event. I then learned that I should use a sub chart to control this, so I placed the 1Min as the primary chart, and where the order entry took place and I inserted the 1hour chart as chart 2 with the corresponding SMA and EMA and accessed the data via BarsOfData(2). The issue I am having is that I am always getting the same EMA & SMA data during the length of the hour. As soon as the hour changes, I get a new data, but this puts me back in the same position I was before I introduced the sub-chart where I enter or exit the trade too late. I have attempted this with both IOG on and off and the results are the same.

    Below is the relevant part of the code and if anyone can provide some guidance, that would be greatly appreciated.

    Thanks,

    private ISeries<Double> m_price;
    private SeriesVar<Double> m_sma, m_ema;
    private XAverage m_emaTmp;
    private Average m_smaTmp;
    private int m_emaLength;
    private int m_smaLength;
    private ISeries<Double> price {
    get { return m_price; }
    }
    [Input]
    public int emaLength {
    get ( return m_emaLength; )
    set ( m_emaLength=value; )
    }
    [Input]
    public int smaLength {
    get ( return m_smaLength; )
    set ( m_smaLength=value; )
    }

    //Construct()
    m_emaTmp=new XAverage(this,2);
    m_smaTmp=new Average(this,2);
    m_ema=new SeriesVar<Double>(this,0,2);
    m_sma=new SeriesVar<Double>(this,0,2);

    //Initialize()
    m_price=BarsOdData(2).Close;
    m_emaTmp.price=m_emaTmp[0];
    m_smaTmp.price=m_smaTmp[0];
    m_emaTmp.length=new Serie---pression<Int32>(delegate ´return emaLength; });
    m_smaTmp.length=new Serie---pression<Int32>(delegate ´return smaLength; });
    m_ema.DefaultValue=0;
    m_sma.DefaultValue=0;


    //Execute()
    m_ema.Value=m_emaTmp[0];
    m_sma.Value=m_emaTmp[0];

    rest of the logic....

  2. #2
    robocod's Avatar
    robocod is offline Member
    Join Date
    Mar 2011
    Posts
    323
    I am trying to code a strategy that evaluates and executes an SMA and EMA crossover on a 1H chart. The first problem I found was that even though I had configured the IOG to execute on each tick, the order was always placed on the next bar while I needed it to enter the order at the crossing event. I then learned that I should use a sub chart to control this
    Actually, the correct solution to your problem is to use CreateMarketNextBar rather than CreateMarketThisBar. There is a lot of confusion how to use these two, and what the difference is, especially with respect to IOG, etc.

    I will try to summarise.

    If IOG is off, the your strategy only gets 1 "tick" (i.e. when your Execute() method is called) per bar, and that is at the bar close. If you use CreateMarketThisBar, then the order is place at the close of this bar, which means it only appears on the chart on the next bar. Whereas, CreateMarketNextBar , which should really be called CreateMarketNextTick in my opinion, will cause an order to be placed on the next tick, which will be the end of the next bar, and the actual order only appears on the following bar - so it looks like it is 2 bars delay.

    With IOG on, CreateMarketThisBar behaves the same, but CreateMarketNextBar will place the order on the next tick, which usually means it will be placed and filled inside the current bar. I think this is what you were trying to achieve, and it is a lot easier to do this than messing with multiple charts, etc.

    The other issue that you have with BarsOfData(2) is because the IOG mode only applies to the primary chart/symbol, so the EMA&SMA, which are computed from BarsOfData(2) still only get 1 tick of data per bar.

    (Caveat : I am away from my normal PC just now, so the above is from memory - but I think I got it right. You can always verify by experiment).

  3. #3
    Micoud19 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by robocod View Post
    With IOG on, CreateMarketThisBar behaves the same, but CreateMarketNextBar will place the order on the next tick, which usually means it will be placed and filled inside the current bar. I think this is what you were trying to achieve, and it is a lot easier to do this than messing with multiple charts, etc.
    Thanks for your advice; I appreciate it.

    I actually did try using both CreateMarketNextBar and CreateMarketThisBar (before I attempted using a second chart) with IOG on, but in both cases, the order was placed on the next or the following bar. One thing though is that I have not tried this going forward, so perhaps there is another configuration that I am missing somewhere. I did enable the Bar Magnifier for backtesting on both Tick and 1 minute, but the order was still placed on the next or following bars. Any Ideas?

    I would indeed prefer to only use one chart as you advise and your reasoning there couples 100% with my results with the second chart.

    Thanks,

  4. #4
    robocod's Avatar
    robocod is offline Member
    Join Date
    Mar 2011
    Posts
    323
    I wrote very simple EA with SMA and EMA cross and tested with with 4 options:

    CreateMarketNextBar and IOG off
    CreateMarketNextBar and IOG on
    CreateMarketThisBar and IOG off
    CreateMarketThisBar and IOG on

    From the charts, it seems to concur with what I described.

    screenshots.zip

    PS: although the attachment is called screenshots the zip file also includes the strategy code for reference.

    If you look at the setting with CreateMarketNextBar and IOG on, you will see that sometimes orders are placed apparently before the cross - and sometimes where there was no cross at all! This is ok. There was a cross during the formation of the bar, and the order is placed on the 1st such cross, but by the close of the bar the lines might have un-crossed if you see what I mean. This is why it is often common practise to wait for a close of the bar.

    For IOG on, I am using the following setting...

    Name:  iog.png
Views: 255
Size:  29.5 KB

    You didn't post all of your logic - perhaps you had some other logic in your EA where it waits for the bar to close before placing a trade? In this case it is working like IOG = off, except that you do still get Execute() called each tick - which is useful for managing stops, etc.

  5. #5
    Micoud19 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks for this. I do have a buffer between crossing and placing the order but even if I reduce it to 0 pips, the order was placed on the next or following bar. However, more than likely I must have something else in the code that is creating this issue. Unfortunately I cannot access your file today as I will be away from my computer until tomorrow. Therefore, I will review your file at that time and post my entire code so you can take a look.

    Once again, thanks for all your help!

  6. #6
    Micoud19 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    I looked at your code and it was pretty much the same as what I had other than I was passing the values of Xaverage and Average variables to a SeriesVar before calling the crossover/crossunder functions. Not sure why that would have created the issue, but your code was a lot cleaner so I took out those extra variables and now the orders are getting executed within the bars. Now it is a question of fiddling with the parameters and perhaps some additional logic to enhance performance.

    Attached is the code.



    Thanks again for all the help.
    ไฟล์แนบข้อความ ไฟล์แนบข้อความ

  7. #7
    cloudes is offline Registered User
    Join Date
    May 2012
    Posts
    4
    Mi dispiace, non lo so.

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.