DailyFX Home | Español | Français | 中文 | Deutsch | Italiano | العربية | Sverige | Ελληνικά


+ Reply to Thread
Page 2 of 4 FirstFirst 1 2 3 4 LastLast
Results 16 to 30 of 50
  1. #16
    KrazyOne is offline Member KrazyOne is an unknown quantity at this time
    Join Date
    Jul 2010
    Posts
    24
    Quote Originally Posted by Trading Gurus View Post
    Hi jlmh,



    I am deliberately trying to create an error. When an order is rejected by the server I want my code to know about it PDQ.

    So far I have been unable to discover how to do this.

    Thus far I have also been unable to discover how to successfully create a GTC OCO order with Quantity set to something more sensible, such as 1.

    Cheers,

    Jim
    To add to what jlmh said,

    Just call the following on every bar and you're orders are automatically a OCO.

    m_BuyLimit.Generate(TargetPrice,1);
    m_BuyStop.Generate(StopPrice,1);

    These must be IPriceOrder objects with the Lots parameter set to Lots.Variable.

    As long as those values stay the same, those orders will stay on the server.

    Make sure your chart has Strategy Automation on as well.

    If you want to see the thing work, make a 1min chart and let it run.

  2. #17
    Trading Gurus is offline Gold Member Trading Gurus is an unknown quantity at this time
    Join Date
    Jun 2009
    Posts
    92
    Hi KrazyOne,

    Quote Originally Posted by KrazyOne View Post
    To add to what jlmh said,

    Just call the following on every bar....
    That did the trick. However it seems an unnecessarily complicated way of doing what seems like it ought to be a very simple procedure. If you're dealing with market orders you might well want to apply your strategy to a tick chart, which means your code will be refreshing the OCO every tick too!

    I'm merely endeavouring to port an extremely simple MetaTrader 4 EA to Strategy Trader, and now I have yet another problem:

    I can access bid and ask prices via Bars.StatusLine.Bid (or Ask), which seems to work OK for live trading but is no good when backtesting. How can I access bid/ask prices on a tick by tick basis in a backtest?

    Sorry to sound like a broken record, but nobody's answered this question yet either:

    How do I trap an order rejection, or any other error condition, in Strategy Trader?

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  3. #18
    KrazyOne is offline Member KrazyOne is an unknown quantity at this time
    Join Date
    Jul 2010
    Posts
    24
    Quote Originally Posted by Trading Gurus View Post
    Hi KrazyOne,



    That did the trick. However it seems an unnecessarily complicated way of doing what seems like it ought to be a very simple procedure. If you're dealing with market orders you might well want to apply your strategy to a tick chart, which means your code will be refreshing the OCO every tick too!

    I'm merely endeavouring to port an extremely simple MetaTrader 4 EA to Strategy Trader, and now I have yet another problem:

    I can access bid and ask prices via Bars.StatusLine.Bid (or Ask), which seems to work OK for live trading but is no good when backtesting. How can I access bid/ask prices on a tick by tick basis in a backtest?

    Sorry to sound like a broken record, but nobody's answered this question yet either:

    How do I trap an order rejection, or any other error condition, in Strategy Trader?

    Jim
    For your bid and ask during backtesting, this can easily be done.

    Right click on your chart -> Format Strategy Advisors -> Backtesting tab

    From there you'll see two backtesting modes - classic and extended, you want extended.

    Create two charts within the same window, one with bid one with ask and reference them correctly.

    Then for tick by tick, do so on the second set of options on that same window.

    As for your error rejections, that does not seem possible to capture.

    But it's a minor issue, you could build a simple work around in your code for that.

  4. #19
    Trading Gurus is offline Gold Member Trading Gurus is an unknown quantity at this time
    Join Date
    Jun 2009
    Posts
    92
    Hi KrazyOne,

    Quote Originally Posted by KrazyOne View Post
    For your bid and ask during backtesting, this can easily be done.

    Right click on your chart -> Format Strategy Advisors -> Backtesting tab

    From there you'll see two backtesting modes - classic and extended, you want extended.

    Create two charts within the same window, one with bid one with ask and reference them correctly.

    Then for tick by tick, do so on the second set of options on that same window.
    Thanks for your suggestions, but I did all that. My SA received lots of ticks with identical bid/ask prices. What I meant was how can I modify my code to access the bid/ask at the time of the backtested tick, rather than the current quote, which is what my SA seems to be receiving at the moment.

    As for your error rejections, that does not seem possible to capture.

    But it's a minor issue, you could build a simple work around in your code for that.
    I have to disagree with you there. Seems like a major issue to me. If you're designing a trading system you need to concentrate on what's the worst that can happen, not what's the best that can happen.

    Similarly if you're coding an automated trading system you need to be able to catch and recover from everything you can think of that might possibly go wrong.

    I would anticipate that an object-oriented "institutional grade" automated trading platform would be able to inform me immediately and straightforwardly if one of my orders was rejected, along with a very good reason for so doing.

    If that's impossible in Strategy Trader what do you suggest as "a simple work around"?

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  5. #20
    KrazyOne is offline Member KrazyOne is an unknown quantity at this time
    Join Date
    Jul 2010
    Posts
    24
    How exactly are you obtaining the prices? If you're only grabbing it from the status line, then that will only be the current.

    If you want to get the current price at the time of the bar that is being run in backtesting, use Bars.CloseValue, Bars.HighValue, etc.

    As for the work around I was mentioning, you could simply take a look at the current and previous market positions. If you see a change in the market position, the trade was executed.

    The signals on the chart are based on real executed signals. So, if you were to send a market order and it was rejected, it will not appear on the chart. Keep in mind that this must be running with strategy automation on.. in other words, it has to be actually sending stuff and not running in a simulated mode.

  6. #21
    Trading Gurus is offline Gold Member Trading Gurus is an unknown quantity at this time
    Join Date
    Jun 2009
    Posts
    92
    Hi KrazyOne,

    Quote Originally Posted by KrazyOne View Post
    How exactly are you obtaining the prices? If you're only grabbing it from the status line, then that will only be the current.
    Like I said - "I can access bid and ask prices via Bars.StatusLine.Bid (or Ask)"

    If you want to get the current price at the time of the bar that is being run in backtesting, use Bars.CloseValue, Bars.HighValue, etc.
    There doesn't seem to be a Bars.AskClose or AskBars.Close! I presume I have to work with 2 sets of bars, one bid and one ask. How do I differentiate between the two? Preferably in a way that doesn't care which way up I have my 2 subcharts on the screen, and preferably in way that works equally well in backtesting and in live trading on a chart that doesn't have the second subchart!

    As for the work around I was mentioning, you could simply take a look at the current and previous market positions. If you see a change in the market position, the trade was executed.

    The signals on the chart are based on real executed signals. So, if you were to send a market order and it was rejected, it will not appear on the chart. Keep in mind that this must be running with strategy automation on.. in other words, it has to be actually sending stuff and not running in a simulated mode.
    Using your workaround how do I find out why the order was rejected? Phone up and ask nicely?

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  7. #22
    KrazyOne is offline Member KrazyOne is an unknown quantity at this time
    Join Date
    Jul 2010
    Posts
    24
    Quote Originally Posted by Trading Gurus View Post
    Hi KrazyOne,



    Like I said - "I can access bid and ask prices via Bars.StatusLine.Bid (or Ask)"



    There doesn't seem to be a Bars.AskClose or AskBars.Close! I presume I have to work with 2 sets of bars, one bid and one ask. How do I differentiate between the two? Preferably in a way that doesn't care which way up I have my 2 subcharts on the screen, and preferably in way that works equally well in backtesting and in live trading on a chart that doesn't have the second subchart!



    Using your workaround how do I find out why the order was rejected? Phone up and ask nicely?

    Cheers,

    Jim
    I understand that you used Bar.StatusLine, but just wanted to see if that was your only way of getting data, but it seems like it is...

    But yes, two charts, one bid and one ask and just do BarsOfData(int) for which you want to reference. This all depends on how you have your charts set up, 1 can be bid, 2 can be ask. Again, depends on how you have your data set up.

    Don't think you can tell the rejected reason, no. But, for a automated system, you just want to know if you're in or not and make sure you're not having any pending orders right? For pending orders, just go and access your orders table from the Fx2GoHost property.

    Code:
    foreach(IFxOrder ord in Fx2GoHost.OpenOrders)
    {
    }
    Use the above code and you can see the available property values for each order.

  8. #23
    jlmh is offline Member jlmh is on a distinguished road
    Join Date
    Aug 2008
    Posts
    66
    hi Jim,

    To get the tick-by-tick data, these might help you:
    1. use BarsOfData(1).Close[0] and BarsOfData(2).Close[0] instead of Bars.StatusLine.Bid
    2. right click the chart that you have the Strategy Advisor on, choose Format Strategy Advisor->Format, and then under the "Enable Intra-Bar Order Generation" tab, check the box.
    3. Format Strategy Advisor again, on Properties, under "Backtesting" tab, check the box "Use Bar Magnifier" at the middle, and choose 1 ticks

    This should allow you to get the tick-by-tick Bid and Ask data.

  9. #24
    firmusoft is offline Member firmusoft is an unknown quantity at this time
    Join Date
    Jul 2010
    Posts
    38

    Thumbs up Order related queries

    Guys;

    I am using this thread to post couple of queries since I dont have the permission to create new ones as yet. As I am new to this forum, I am also new to ST, so some of these questions may be naive.

    Question 1
    The Order.Generate function – Since there is no return code for this funtion, how do we know at runtime if the function failed or not. Does this function throw any exception?

    Question 2
    I am assuing the StrategyInfo.MarketPostion value and CurrentPosition object has to be updated when ever a trade occurs. After I invoke Order.Generate I don’t see these members getting updated immediately, but they are updated on the next bar. I tried both CreateMarketNextBar and CreateMarketThisBar order types, but same result in both cases.

    Question 3
    As I understand Buy and SellShort can be used to open new positions. Do they also close any open positions in the opposite direction?

    Sell and BuyToCover can used to exit the positions entered using Buy and Sellshort respectively. I assume we have to just invoke the opposite side order with the same number of Lots.

    If I open a Buy with 2 Lots first and then issue a Sell with 1 lot, does it do a partial close such the position is still long by 1 Lot?

    Can there be a situation where there are mutiple postions open at the same time? Like a long (1 Lot) and a short (3 Lots)? In that case what will be the value of StrategyInfo.MarketPostion (1-3 = -2) ?

    If there is already a long position open and I want to enter another position on the same side, what order type is to be used? Should it be Buy?

    Cheers
    Firmusoft

  10. #25
    jlmh is offline Member jlmh is on a distinguished road
    Join Date
    Aug 2008
    Posts
    66
    Question 1
    The Order.Generate function – Since there is no return code for this funtion, how do we know at runtime if the function failed or not. Does this function throw any exception?
    if the order is rejected, you'll see an error message pops up on the bottom right corner.

    Question 2
    I am assuing the StrategyInfo.MarketPostion value and CurrentPosition object has to be updated when ever a trade occurs. After I invoke Order.Generate I don’t see these members getting updated immediately, but they are updated on the next bar. I tried both CreateMarketNextBar and CreateMarketThisBar order types, but same result in both cases.
    it is because the Execute method is executed once in a bar. You can enable the intra-bar order generation by format strategy advisor, and check the box "Enable Intra-Bar Order Generation" under the "Intra-Bar Order Generation" tab.

    As I understand Buy and SellShort can be used to open new positions. Do they also close any open positions in the opposite direction?
    if you currently have a buy order on the chart, the upcoming sell short order will close the buy position, and open a new short position.


    If I open a Buy with 2 Lots first and then issue a Sell with 1 lot, does it do a partial close such the position is still long by 1 Lot?
    should be still one lot

    Can there be a situation where there are mutiple postions open at the same time? Like a long (1 Lot) and a short (3 Lots)? In that case what will be the value of StrategyInfo.MarketPostion (1-3 = -2) ?
    You can't hedge your positions in ST, so 1 long and 3 short positions would result 2 short position. and StrategyInfo.MarketPostion should be -2.


    If there is already a long position open and I want to enter another position on the same side, what order type is to be used? Should it be Buy?
    Yes, buy order. but you will need to enable multiple position limit by right-click the chart->Format Strategy Advisor->Properties, check the "Allow up to ......entry orders in the same direction as the currently held position" box.

  11. #26
    Trading Gurus is offline Gold Member Trading Gurus is an unknown quantity at this time
    Join Date
    Jun 2009
    Posts
    92
    Hi firmusoft,

    Quote Originally Posted by firmusoft View Post
    Guys;

    I am using this thread to post couple of queries since I dont have the permission to create new ones as yet. As I am new to this forum, I am also new to ST, so some of these questions may be naive.
    Your questions are not naive. However I've been tearing my hair out over them for the last few days. Consequently they are mostly already answered earlier in this very thread.

    Question 1
    The Order.Generate function – Since there is no return code for this funtion, how do we know at runtime if the function failed or not. Does this function throw any exception?
    I am given to understand that this function does not throw any exceptions. Consequently I am not a terribly happy bunny!

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  12. #27
    Trading Gurus is offline Gold Member Trading Gurus is an unknown quantity at this time
    Join Date
    Jun 2009
    Posts
    92
    Here is my latest OCO test SA:

    Code:
    using System;
    using Broker.StrategyLanguage.Function;
    
    namespace Broker.StrategyLanguage.Strategy
    {
        public class OcoTest : BaseStrategyAdvisor
        {
            private IMarketOrder m_BuyOrder;
    	private IPriceOrder m_BuyStop;
    	private IPriceOrder m_BuyLimit;
    	
            private int m_target = 100;
            private int m_stop = 100;
            private int m_lots = 2;
    	private bool Problem = false;
    	private double StopPrice, LimitPrice;
    		
    	public OcoTest(object ctx) :
                base(ctx) {}
    
            [Input]
            public int ProfitTarget {
                get { return m_target; }
                set { m_target = value; }
            }
    
            [Input]
            public int StopLoss {
                get { return m_stop; }
                set { m_stop = value; }
            }
    
            [Input]
            public int Quantity {
                get { return m_lots; }
                set { m_lots = value; }
            }
    		
            protected override void Construct(){
                 m_BuyOrder =
                    OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Variable, OrderAction.Buy));
    	     m_BuyStop =
    		OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Variable, OrderAction.Sell));
    	     m_BuyLimit =
    		OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Variable, OrderAction.Sell));
            }
    
            protected override void Initialize(){
    		Output.WriteLine("Launched OK");
    	}
    
            protected override void Destroy() {
    		Output.WriteLine("Self Destruct!");
    	}
    
            protected void InitializeOCO(double Target, double Stop) {
    		LimitPrice = Target;
    		StopPrice = Stop;
    		RefreshOCO();
    	}
    	
            protected void RefreshOCO() {
    		m_BuyLimit.Generate(LimitPrice, Quantity);
    		m_BuyStop.Generate(StopPrice, Quantity);
    	}
    		
            protected override void Execute(){
    		if (!Problem) {		// There seems to be no easy way in ST to check if there's a problem!
    			if (CurrentPosition.OpenTrades.Count <= 0) {
    				m_BuyOrder.Generate(Quantity);
    				InitializeOCO(Bars.StatusLine.Ask + (Bars.Point * ProfitTarget), Bars.StatusLine.Ask - (Bars.Point * StopLoss));
    			}
    			else {
    				RefreshOCO();
    			}		
    		}
            }
        }
    }
    I've removed all those redundant try/catch statements. I realise the backtest results are useless. However it does at long last seem to work as expected on 1 minute charts and up.

    My next naive question is this:

    How do I get it to work on a tick chart?

    Thanks in anticipation,

    Jim
    Last edited by Trading Gurus; 07-28-2010 at 12:03 PM. Reason: Tried to improve formatting
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  13. #28
    Gaurav is offline Gold Member Gaurav is an unknown quantity at this time
    Join Date
    Mar 2010
    Posts
    496
    Quote Originally Posted by Trading Gurus View Post
    Here is my latest OCO test SA:

    Code:
    using System;
    using Broker.StrategyLanguage.Function;
    
    namespace Broker.StrategyLanguage.Strategy
    {
        public class OcoTest : BaseStrategyAdvisor
        {
            private IMarketOrder m_BuyOrder;
    	private IPriceOrder m_BuyStop;
    	private IPriceOrder m_BuyLimit;
    	
            private int m_target = 100;
            private int m_stop = 100;
            private int m_lots = 2;
    	private bool Problem = false;
    	private double StopPrice, LimitPrice;
    		
    	public OcoTest(object ctx) :
                base(ctx) {}
    
            [Input]
            public int ProfitTarget {
                get { return m_target; }
                set { m_target = value; }
            }
    
            [Input]
            public int StopLoss {
                get { return m_stop; }
                set { m_stop = value; }
            }
    
            [Input]
            public int Quantity {
                get { return m_lots; }
                set { m_lots = value; }
            }
    		
            protected override void Construct(){
                 m_BuyOrder =
                    OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Variable, OrderAction.Buy));
    	     m_BuyStop =
    		OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Variable, OrderAction.Sell));
    	     m_BuyLimit =
    		OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Variable, OrderAction.Sell));
            }
    
            protected override void Initialize(){
    		Output.WriteLine("Launched OK");
    	}
    
            protected override void Destroy() {
    		Output.WriteLine("Self Destruct!");
    	}
    
            protected void InitializeOCO(double Target, double Stop) {
    		LimitPrice = Target;
    		StopPrice = Stop;
    		RefreshOCO();
    	}
    	
            protected void RefreshOCO() {
    		m_BuyLimit.Generate(LimitPrice, Quantity);
    		m_BuyStop.Generate(StopPrice, Quantity);
    	}
    		
            protected override void Execute(){
    		if (!Problem) {		// There seems to be no easy way in ST to check if there's a problem!
    			if (CurrentPosition.OpenTrades.Count <= 0) {
    				m_BuyOrder.Generate(Quantity);
    				InitializeOCO(Bars.StatusLine.Ask + (Bars.Point * ProfitTarget), Bars.StatusLine.Ask - (Bars.Point * StopLoss));
    			}
    			else {
    				RefreshOCO();
    			}		
    		}
            }
        }
    }
    I've removed all those redundant try/catch statements. I realise the backtest results are useless. However it does at long last seem to work as expected on 1 minute charts and up.

    My next naive question is this:

    How do I get it to work on a tick chart?

    Thanks in anticipation,

    Jim
    Jim,

    Would you like to run this strategy on a minute chart with it being called every tick OR would you like to just run it on a tick chart?

    To run it on a tick chart just apply it to a tick chart (not trying to be a smart ass here).

    To run the strategy Intrabar(every tick) on a minute (or any other) chart you will have to got to Format Strategy Advisor-> Intra-Bar Order Generation (tab) and check Enable Intra-bar Order Generation. All three items under that check mark has intrabar order generation on meaning your Execute() method will be called every tick for any of them.

  14. #29
    Trading Gurus is offline Gold Member Trading Gurus is an unknown quantity at this time
    Join Date
    Jun 2009
    Posts
    92
    Hi Gaurav,

    Quote Originally Posted by Gaurav View Post
    Jim,

    Would you like to run this strategy on a minute chart with it being called every tick OR would you like to just run it on a tick chart?

    To run it on a tick chart just apply it to a tick chart (not trying to be a smart ass here).

    To run the strategy Intrabar(every tick) on a minute (or any other) chart you will have to got to Format Strategy Advisor-> Intra-Bar Order Generation (tab) and check Enable Intra-bar Order Generation. All three items under that check mark has intrabar order generation on meaning your Execute() method will be called every tick for any of them.
    I certainly want to be able to run SAs on an n minute chart whilst their Execute() receives every tick. Thanks for clarifying how that works.

    This particular SA is solely for test purposes. I'm trying to discover the limitations of your platform. If I run it on a tick chart (thanks for clarifying how to do that ) I find that sometimes (when the ticks are arriving thick and fast?) my OCO orders get cancelled.

    This leads me to wonder if very occasionally the same problem might arise on an n minute chart too. Maybe if a previous OCO exit happened to be hit just as a bar was completed?

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  15. #30
    jlmh is offline Member jlmh is on a distinguished road
    Join Date
    Aug 2008
    Posts
    66
    hi Jim,

    In your code, it looks like you are trying to place the entry orders with price calculated based on the current Ask. I think it makes the platform to delete and replace the orders whenever a new tick comes in, that's why the OCO orders are cancelled. You can check the state of pending orders under the "Order" tab in the Account Manager section (it's defaulted on the bottom)

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.