|
|
-
 Originally Posted by Trading Gurus
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.
-
Hi KrazyOne,
 Originally Posted by KrazyOne
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
-
 Originally Posted by Trading Gurus
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.
-
Hi KrazyOne,
 Originally Posted by KrazyOne
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
-
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.
-
Hi KrazyOne,
 Originally Posted by KrazyOne
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
-
 Originally Posted by Trading Gurus
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.
-
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.
-
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 dont 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
-
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 dont 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.
-
Hi firmusoft,
 Originally Posted by firmusoft
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
-
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
-
 Originally Posted by Trading Gurus
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.
-
Hi Gaurav,
 Originally Posted by Gaurav
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
-
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)
|