Profile Picture

PositionOrder Examples

Posted By phg 10 Years Ago
Message
phg
Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)
Posted Wednesday October 17 2007
Are there any examples of the use of PositionOrder objects in any of the sample strategies or ones uploaded to the site? Am I right that you have to use one if you want to set a BarsValid number?

-Pete

See also Yahoo group about applying RE.

Posted Wednesday October 17 2007
You don't need to set a BarsValid, it will default to 1.  I'm not sure any of our examples use it, but here is something I wrote today:

PositionOrder posOrder = new PositionOrder();

posOrder.TrailingStop = 0.05;

posOrder.OrderSymbol = symbol;

posOrder.OrderType = OrderType.Market;

posOrder.PositionType = PositionType.Short;

posOrder.ProfitTarget = 0.05;

posOrder.BarCountExit = 30;

posOrder.NumShares = 100;

 

PositionManager.OpenPosition(posOrder);

Thanks,
Daniel

phg
Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)Supreme Being (44,695 reputation)
Posted Thursday October 18 2007
Thank you! Would it be good form to follow that with:
dispose posOrder;
or is that not needed?
If one wants BarsValid to be greater than 1 it looks like using the object is the way to accomplish that?

-Pete

See also Yahoo group about applying RE.

Posted Friday October 19 2007
PositionOrder does not implement IDispose, so you could not actually do this.  You would not want to if it could, though Smile

That would be the way to go to set barsvalid.

DoQ
Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)
Posted Thursday January 24 2008
It seems that my existing order entry code doesn't seem to work anymore in 1.1 but did in 1.0. I wrote a quick system that just buys or sells every bar to verify. OpenPosition(...) works fine but PositionOrder does nothing (at IB TWS).  Actually, just tried with paperbroker and same result. Whats changed?

Here is the code:

public class SystemMain : SystemBase

{

PositionOrder Order = new PositionOrder();

public override void Startup()

{

// Perform initialization or set system wide options here

}

public override void NewSymbolBar(Symbol symbol, BarData bar)

{

//OpenPosition(symbol,PositionType.Long,OrderType.Market);

SystemData.Output.OutputLines.Clear();

SystemData.Output.Add(OutputSeverityLevel.Warning, bar.PriceDateTime.ToString() + "___" + "Going Short to Open", symbol);

PositionManager.OpenPosition(SetupOrderToOpen(symbol,PositionType.Short,OrderType.Market,bar.Close,1000,"Going Short"));

// This line of code runs the actions you have set up in in the Project Form

Actions.RunActions(symbol);

}

//Setup OrderToOpen here

private PositionOrder SetupOrderToOpen(Symbol symbol,PositionType PType,OrderType OType,double Limit,ulong TradeSize, string Comment)

{

//defaults

Order.BarCountExit = 0;//no timed exit

Order.ProfitTarget = double.NaN;//this is a % tgt

Order.StopLoss = double.NaN;

Order.TrailingStop = double.NaN;

//details

Order.OrderSymbol = symbol;

Order.PositionType = PType;

Order.NumShares = TradeSize;

Order.OrderType = OrderType.Market;//StopLimit doesn't work right now in RE

Order.StopPrice = double.NaN;

Order.LimitPrice = Limit;

Order.Description = Comment;

Order.BarsValid = -1; //indefinite

SystemData.Output.Add(OutputSeverityLevel.Warning, "Order Sent " + PType.ToString() + TradeSize.ToString() + DateTime.Now.ToString());

return Order;

}

}

 

thx


DoQ_Indicators
"better is the enemy of good enough"
Posted Thursday January 24 2008
You should check the return value of OpenPosition to see if it succeeded.  If it did not (which I think is most likely), check the Message property for the reason it failed.  The following code demonstrates how to do this.

ReturnValue<string> retVal = OpenPosition(symbol, PositionType.Long, OrderType.Limit, price);

if (!retVal.Success)
{
    // Add message to output window with the reason the position could not be opened
    SystemData.Output.Add(OutputSeverityLevel.Warning, retVal.ReturnCode.Message, symbol);
}
else
{
    string ID = retVal.Value;
    // Store or process position ID here, if necessary

}

Thanks,
Daniel

DoQ
Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)
Posted Thursday January 24 2008
dplaisted (1/24/2008)
You should check the return value of OpenPosition to see if it succeeded.  If it did not (which I think is most likely), check the Message property for the reason it failed.  The following code demonstrates how to do this.

ReturnValue<string> retVal = OpenPosition(symbol, PositionType.Long, OrderType.Limit, price);

if (!retVal.Success)
{
    // Add message to output window with the reason the position could not be opened
    SystemData.Output.Add(OutputSeverityLevel.Warning, retVal.ReturnCode.Message, symbol);
}
else
{
    string ID = retVal.Value;
    // Store or process position ID here, if necessary

}

Thanks,
Daniel

I never had a problem with OpenPosition(symbol,xx,xxx,xxx) but rather with OpenPosition(PositionOrder), so I tried to get the return string if pass or fail and oddly enough RE doesn't flag a fail at all. So I output to screen both cases to make sure the Return Code exists. Sure enough RE thinks it has sent the order but I get nothing.  Here is the code:

public class SystemMain : SystemBase

{

PositionOrder Order = new PositionOrder();

public override void Startup()

{

// Perform initialization or set system wide options here

}

public override void NewSymbolBar(Symbol symbol, BarData bar)

{

//OpenPosition(symbol,PositionType.Long,OrderType.Market);

//SystemData.Output.OutputLines.Clear();

SystemData.Output.Add(OutputSeverityLevel.Warning, bar.PriceDateTime.ToString() + "___" + "Going Short to Open", symbol);

ReturnValue<string> retVal = PositionManager.OpenPosition(SetupOrderToOpen(symbol,PositionType.Short,OrderType.Market,bar.Close,1000,"Going Short"));

if (!retVal.Success)

{

// Add message to output window with the reason the position could not be opened

SystemData.Output.Add(OutputSeverityLevel.Warning, retVal.ReturnCode.Message, symbol);

}

else

{

SystemData.Output.Add(OutputSeverityLevel.Warning, retVal.ReturnCode.Message, symbol);

string ID = retVal.Value;

// Store or process position ID here, if necessary

}

// This line of code runs the actions you have set up in in the Project Form

Actions.RunActions(symbol);

}

//Setup OrderToOpen here

private PositionOrder SetupOrderToOpen(Symbol symbol,PositionType PType,OrderType OType,double Limit,ulong TradeSize, string Comment)

{

//defaults

Order.BarCountExit = 0;//no timed exit

Order.ProfitTarget = double.NaN;//this is a % tgt

Order.StopLoss = double.NaN;

Order.TrailingStop = double.NaN;

//details

Order.OrderSymbol = symbol;

Order.PositionType = PType;

Order.NumShares = TradeSize;

Order.OrderType = OrderType.Market;//StopLimit doesn't work right now in RE

Order.StopPrice = double.NaN;

Order.LimitPrice = Limit;

Order.Description = Comment;

Order.BarsValid = -1; //indefinite

SystemData.Output.Add(OutputSeverityLevel.Warning, "Order Sent " + PType.ToString() + TradeSize.ToString() + DateTime.Now.ToString());

return Order;

}

}

 

thx


DoQ_Indicators
"better is the enemy of good enough"
DoQ
Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)
Posted Thursday January 24 2008
Made a mistake with the sample broker test system, the capital was set too low. So Paper Broker does correctly accept and execute orders but the initial problem remains, IB doesn't receive anything (which worked in 1.0 but not 1.1). The Live System Window shows no orders on any pane and TWS shows no orders. Also, OpenPosition(PositionOrder) doesn't return a fail....

thx


DoQ_Indicators
"better is the enemy of good enough"

Edited: Thursday January 24 2008 by DoQ
DoQ
Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)Supreme Being (11,415 reputation)
Posted Friday January 25 2008
Any luck with this? Should I start another more appropriate thread?

As you know OpenPosition(PositionOrder) is the only way to send an order without having it timed out on every bar. At the moment Re doesn't send anything to TWS even though it thinks it has. So how can I send orders thru RE to IB (i.e. is there a workaround)?

thx


DoQ_Indicators
"better is the enemy of good enough"
Posted Saturday January 26 2008
When you use OpenPosition(symbol,xx,xxx,xxx), does the order get sent to IB (and then of course get cancelled the next time a bar comes in)?

The other OpenPosition methods just create a PositionOrder object and pass it to OpenPosition(PositionOrder).  So I'm not sure what the problem is but hopefully we can figure it out.

You are declaring your PositionOrder variable as a member of your class, and reusing the same one each time you call OpenPosition.  I don't see any reason this would cause a problem, but try moving the following line to the start of your SetupOrderToOpen() method:

PositionOrder Order = new PositionOrder();

Are you using the new C# TWS plugin, or the old one that uses ActiveX?

Have you looked at the TWS log files to see if there is any error information in them?  They should be located in your TWS installation directory (C:\Jts probably) under the names log.Mon.txt, log.Tue.txt, etc.

Thanks,
Daniel



Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems