Trade FOREX with FXCM

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


Results 1 to 15 of 16
Page 1 of 2 1 2 LastLast

Thread: Sample code: multiple stages of profit taking & Multi-time frame set-up

  1. #1
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272

    Sample code: multiple stages of profit taking & Multi-time frame set-up

    Hi,

    I want to get started with MarketScope strategy design.

    I have read a bit through some stuff and scanned a little bit of code from examples of the code base.

    Conceptually, I need to do the following:
    1. Order management:
    For all my strategies I need two-stage and three-stage limit profit taking with stop moves. I guess I have all order types available from TradeStation. Is there sample code out there to do this? I guess this must be used by other designers already. If not, what is the set-up for both backtesting and LIVE trading, i.e. do I need to subscribe tick data to implement this as to monitor real-time price inputs? Is it better to open 2 or 3 positions, respectively, and control them separately or is it better to open one position and close out partial profits?

    2. Use of indicators:
    MACD of standard & custom settings for 1H, 4H, 1D, 1W time frames. For these, I basically only look at updates at respective bar close of the time frame. Do I need to construct higher time frames from 1H, or does it work to subscribe to all time frames?`

    3. Bid & Ask series:
    I use indicators on both Ask and Bid series separately, how would I do this in MarketScope?

    4. Backtest:
    Is it correct that the backtester uses bid and ask series automatically, i.e. does the backtest account for the real spread?
    What is the granularity of the price data on backtest, i.e. tick, 1M or something else?

    Sorry for a lot of questions but some inital help will save me a tremendous amount of time to get something up and running.

    Thanks in advance,
    Martin

  2. #2
    Natalya is online now Member
    Join Date
    Apr 2009
    Posts
    744
    Hi Martin,

    Thanks for your post.

    Quote Originally Posted by mstreck View Post
    4. Backtest:
    Is it correct that the backtester uses bid and ask series automatically, i.e. does the backtest account for the real spread?
    What is the granularity of the price data on backtest, i.e. tick, 1M or something else?
    Backtester and Optimizer use data from the quotes manager. It provides historical bid and ask prices. Backtester/Optimizer simulates the market executing buy orders at ask and sell orders at bid prices. That is s the real historical spread that was on the market is used.
    I'll answer other questions a bit later.

  3. #3
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Hi Natalya,

    thanks for the answer on 4), that is good news to start.

    Will wait for more answers from you.

    I am currently thinking on doing 1) & 2) with one order entry with stop & limit (which I see from documentation is an option) and doing partial close-outs by monitoring live tick feed. I will need both tick data and the bars data from plural time frames. I guess I will need to subscribe for all these price sources then, right? But maybe you have a better concept.

    Reg 3) I have not seen anything in the docu yet on how to do this, but I have not yet read the part on how to use standard indicators. I need definitely daily pivot levels and MACD.

    Cheers,
    Martin

  4. #4
    Natalya is online now Member
    Join Date
    Apr 2009
    Posts
    744
    Quote Originally Posted by mstreck View Post
    1. Order management:
    For all my strategies I need two-stage and three-stage limit profit taking with stop moves. I guess I have all order types available from TradeStation. Is there sample code out there to do this? I guess this must be used by other designers already. If not, what is the set-up for both backtesting and LIVE trading, i.e. do I need to subscribe tick data to implement this as to monitor real-time price inputs? Is it better to open 2 or 3 positions, respectively, and control them separately or is it better to open one position and close out partial profits?
    I think it's easier to open one position instead of number of positions. We will prepare the sample.

    Quote Originally Posted by mstreck View Post
    2. Use of indicators:
    MACD of standard & custom settings for 1H, 4H, 1D, 1W time frames. For these, I basically only look at updates at respective bar close of the time frame. Do I need to construct higher time frames from 1H, or does it work to subscribe to all time frames?`
    You should subscribe to all necessary time frames.

    Quote Originally Posted by mstreck View Post
    3. Bid & Ask series:
    I use indicators on both Ask and Bid series separately, how would I do this in MarketScope?
    You can write an indicator which requests Ask and Bid prices and use them for calculations.
    See execute("getHistory", ...)

  5. #5
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Thank you, Natalya, I am looking forward to the sample code to manage one order with partial close-outs and stop move.

    I guess I understand the update mechanism for an indicator (actually I will write some of my custom indicators first since this appears to be less complex).

    For the strategy, I see that I can subscribe to all time frames I want for both ask and bid using ExtSubscribe function and create afterwards e.g. MACD instances for all these price source streams. I think that I also need to subscribe with ExtSubscribe function to a tick stream since I will need tick data for the order management outlined above.

    What I do not yet understand is how to distinguish in the ExtUpdate function then wether an incoming tick is just tick between a bar of any time frame, and when a tick belongs to a bar close of one or more of the time frames. Could you please elaborate on this question also, maybe also in view of how I update then e.g. a MACD of 1H time frame on a 1H bar close but not the other MACDs if the remaining MACDs are not on a bar close of their respective time frames?

    Cheers,
    Martin

  6. #6
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Hi,

    I have started to implement my order management already, but still would be nice to see an example if you have.

    What I need to know right now is following:

    I have subscribed to all bar streams I need.

    Do I need to subscribe to a tick stream as well in order to be able to have ExtUpdate being called on all ticks (since I need to monitor instance.bid[NOW] and instance.ask[NOW] when I have an open position) or do I not need to subscribe to a tick stream for this?

    Also, still have no clue upon how to identify in ExtUpdate when a bar of a time frame has closed or not.

    These are my two most important issues right now.

    Thanks,
    Martin

  7. #7
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Hi Natalya,

    I have read a little more through some documentation and examples, so I know how to distinguish between the bar processing of time frames using the id.

    1. From what I read, I have to subscribe to a tick stream as well to have access of tick data for the current candle being built up, is this correct? That would make sense then to use an id for this stream to do the tick processing separate from the bar processing.
    If I do have to subscribe to tickstreams, is it still true that the strategy will automatically load 300 bars for each barstream subscribed?
    If so, how do I detect that the history has been successfully loaded? If not, how do I load this (ExtHistory like in indicator?)?

    I also wonder whether ExtUpdate is called e.g. 10 times in sequence when I have ask & bid ticks, 1H, 4H, 1D, and 1W being closed at the same time?

    2. For the order processing, I have some conceptual questions:
    I decided to open one position as you recommend. From that moment on, since I have a UK based account where hedging is allowed, I have to set initial stop and limits.
    a) Is it possible to trigger both stop & limit in the same call of ExtUpdate? Or do I have to do this in sequence and wait until stop is finished and set limit afterwards.
    b) Is it possible to set partial limits, e.g. 2 limit orders with different amounts, and if so, can I trigger one stop and plural limits by the same ExtUpdate() call?
    If partial limits are not possible, I can work with bracket order that I will modify monitoring incoming ticks for limit reaching detection.


    I apologise for all these questions...I am just new to the platform trying to understand how things work for me.

    Thanks,
    Martin

  8. #8
    Natalya is online now Member
    Join Date
    Apr 2009
    Posts
    744
    Hi Martin,

    Please see my responses below.

    Quote Originally Posted by mstreck View Post
    1. From what I read, I have to subscribe to a tick stream as well to have access of tick data for the current candle being built up, is this correct?
    Yes, this is correct.

    Quote Originally Posted by mstreck View Post
    That would make sense then to use an id for this stream to do the tick processing separate from the bar processing.
    If I do have to subscribe to tickstreams, is it still true that the strategy will automatically load 300 bars for each barstream subscribed?
    This is true.

    Quote Originally Posted by mstreck View Post
    If so, how do I detect that the history has been successfully loaded? If not, how do I load this (ExtHistory like in indicator?)?
    Please use AsyncOperationFinished

    Quote Originally Posted by mstreck View Post
    I also wonder whether ExtUpdate is called e.g. 10 times in sequence when I have ask & bid ticks, 1H, 4H, 1D, and 1W being closed at the same time?
    ExtUpdate is called for each period being closed (that is it can be called e.g. 10 times).

    Quote Originally Posted by mstreck View Post
    a) Is it possible to trigger both stop & limit in the same call of ExtUpdate? Or do I have to do this in sequence and wait until stop is finished and set limit afterwards.
    You can set both stop and limit at once.

    Quote Originally Posted by mstreck View Post
    b) Is it possible to set partial limits, e.g. 2 limit orders with different amounts, and if so, can I trigger one stop and plural limits by the same ExtUpdate() call?
    If partial limits are not possible, I can work with bracket order that I will modify monitoring incoming ticks for limit reaching detection.
    Partial limits/stops are not allowed. But it's possible to emulate them in the strategy code.
    Actually, you can also open a number of positions with different stops/limits. This approach has some disadvantages. E.g. your positions could be opened at different prices.
    I'll upload the example with emulated partial stops/limits tomorrow.

  9. #9
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Thank you, Natalya,

    For the history loading in a strategy, in case I use the automatic loading of 300 bars, each time frame will load 300 bars, what is the cookie value used in ExtAsyncOperationFinished then (since I do not trigger it with getHistory where I would define my own cookie value)?
    Also, in case I need more than 300 bars for some reason, I guess I would use then getHistory myself, which would not be in conflict with the 'automatic' loading of 300 bars, is this correct?

    My strategies also require knowledge about past order execution (time of entries) and whether a trigger has occurred on an entry condition or not, as well as on-going trades not yet being closed, such that proper initialisation is guaranteed at the start of LIVE trading.
    I am currently thinking of running a backtest, and do a context save of all variables, write these into a file, so I can read them back when starting a LIVE mode, which I can control by a user input setting, I think.
    Do you know any other way how to do this, or do you have other recommendations to implement this?

    Thank you,
    Martin

  10. #10
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Natalya,

    I would need to know the cookie value used for the automated loading of 300 bars of plural time frames, or how to set a cookie for this automatic loading myself.

    For the context save, I plan to use persistent storage example, but I need to know how to detect the end of the backtest. Is source:isAlive() a reliable way to detect the processing of the last (i.e. current bar) of the backtest operation?

    Thanks in advance,
    Martin

  11. #11
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Just happened to (finally) scan through the lua helper file which makes my questions for the history loading of a strategy obsolete. I see that the 300 bars are loaded automatically for bar sources I can subscribe at the first call of ExtUpdate for a tick update, and I can even change the 'from' value in this file in case I need a longer history to correctly initialise values taken e.g. from indicator values of a bar series, so this is all fine.

    However, I am still not sure about the initialisation issue as to correctly take into account the history of previous trades of the past. I would appreciate help with this before I waste time with writing a lot of test code, in particular as whether it is feasible to run a backtest, detect the last closed candle depending on real current date/time, and do a context save in persistent storage which I can load back in Prepare() or probably better at a first call of ExtUpdate().

    Thanks,
    Martin

  12. #12
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Hi,

    Will you still provide some sample code for the order management?

    Cheers,
    Martin

  13. #13
    Natalya is online now Member
    Join Date
    Apr 2009
    Posts
    744
    Hi Martin,

    Sorry for the delay.
    Please see the sample code for the order management in the attachment.
    When the sample condition is met (crossing fast and slow moving averages), the sample strategy opens 3 positions with different stops/limits . The strategy will not open new positions while there is at least one trade.

    I wrote above that it's easier to implement opening one position instead of number of positions. Actually that's not true for accounts with hedging (as in your case). If one position is created, the strategy has to simulate stops and limits. So the strategy must be always running (otherwise stops and limits won't be executed).
    Attached Files Attached Files
    Last edited by Natalya; 06-18-2012 at 03:25 AM.

  14. #14
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Thank you, Natalya. Code examples are always helpful.

    Cheers,
    Martin

  15. #15
    mstreck is offline Member
    Join Date
    Jan 2011
    Posts
    272
    Hi,

    I have a question which relates to edit order command.

    I have so far implemented my order lib to open one position with attached stop and limit orders. That works fine. I can also close out a part of a profit with a market exit order. This also works fine. After that, I see that the stop and limit orders attached are updated to a reduced amount, in line with what has been closed out.
    What does not work from there is to edit the stop and limit orders with the still open amount. I get an error that the amount cannot be changed, which appears to relate to the original amount of the entire position before I close out a part.
    This is a bit surprising, as I appear reference the still open amount correctly, so I am NOT doing partial stops nor partial limits.

    Is it a must to first delete the stop and limit orders attached to the open position once I have closed out a part, and then set new stops and limits, or should this work and I need to look for another source of error?
    In case I would need to first delete the old stop and limits, I may change my concept to your example by managing three separate positions, where I guess I can edit stops and limits, as the amount is never changed partially of any of those positions.

    Thanks again,
    Martin
    Last edited by mstreck; 06-22-2012 at 12:22 PM.

Page 1 of 2 1 2 LastLast

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.