RightEdge Forums
Main     Home          Members     Calendar     Who's On

Welcome Guest
        


««123»»

IB paper trading: Stop Loss and Take Profit... Expand / Collapse
Message
Posted 5/15/2009 20:36:10 Post #8492
 

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member
dplaisted (4/15/2009)
Hmm, I can't figure out how to submit a GTC order to TWS using their API.  There is a time in force value, but setting it to GTC didn't work. 

I have successfully set GTC orders using IB's IOrder interface as suggested by PHG. I did it through an old Matlab trading system I created that uses the ActiveX API. Unfortunately, I don't know how to access this interface through RE. I would really like to work with RE's PositionManager, but keeping orders open is essential.

I also tried the code suggested by dplaisted, but for many reasons have been unable to make it (or numerous variants) work reliably.

Posted 5/16/2009 06:40:57 Post #8493
 

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being
I think this translates to GTC:
PositionSettings set = new PositionSettings();
set.BarsValid = -
1;   // <--- GTC



-Pete
(See also rightedge-ats Yahoo group )

Posted 5/16/2009 14:06:48 Post #8494
 

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member
phg (5/16/2009)
I think this translates to GTC:
PositionSettings set = new PositionSettings();
set.BarsValid = -
1;   // <--- GTC

Pete,

This doesn't seem to be the problem. I just examined LiveOpenPositions.xml, and all pending orders have BarsValid == -1, yet TWS displays them as day orders, and will cancel them before the beginning of the next trading day. The stop orders are cancelled within 15 minutes of the end of RTH, and the limit orders are cancelled after the conclusion of extended trading hours. Following is a typical position (as recorded in LiveOpenPositions.xml) in case it provides any clues:

    <PositionDataXml>
      <
PosID>69</PosID>
      <
Symbol>
        <
CurrencyType>USD</CurrencyType>
        <
StrikePrice>0</StrikePrice>
        <
ExpirationDate>0001-01-01T00:00:00</ExpirationDate>
        <
ContractType>NoContract</ContractType>
        <
Name>INTC</Name>
        <
Exchange />
        <
SymbolInformation>
          <
Margin>0</Margin>
          <
TickSize>0</TickSize>
          <
ContractSize>0</ContractSize>
          <
DecimalPlaces>2</DecimalPlaces>
        </
SymbolInformation>
        <
AssetClass>Stock</AssetClass>
      </
Symbol>
      <
PositionType>Short</PositionType>
      <
Description>Breakout GTC</Description>
      <
Trades>
        <
TradeInfo>
          <
FilledTime>2009-05-15T12:54:03</FilledTime>
          <
TransactionType>Short</TransactionType>
          <
Price>
            <
SymbolPrice>15.24</SymbolPrice>
            <
AccountPrice>15.24</AccountPrice>
          </
Price>
          <
Size>2775</Size>
          <
OrderType>Limit</OrderType>
          <
TradeType>OpenPosition</TradeType>
          <
Commission>0</Commission>
          <
Description>Open Position</Description>
          <
BuyingPowerChange>-21145.5</BuyingPowerChange>
          <
MarginChange>21145.5</MarginChange>
          <
ShortedCashChange>42291</ShortedCashChange>
          <
ProfitConversionRate>0</ProfitConversionRate>
          <
OrderID>2665</OrderID>
        </
TradeInfo>
      </
Trades>
      <
PendingOrders>
        <
TradeOrderXml>
          <
OrderID>2666</OrderID>
          <
PosID>69</PosID>
          <
TradeType>ProfitTarget</TradeType>
          <
Description>ProfitTarget</Description>
          <
BarsValid>-1</BarsValid>
          <
CancelPending>false</CancelPending>
        </
TradeOrderXml>
        <
TradeOrderXml>
          <
OrderID>2667</OrderID>
          <
PosID>69</PosID>
          <
TradeType>StopLoss</TradeType>
          <
Description>StopLoss</Description>
          <
BarsValid>-1</BarsValid>
          <
CancelPending>false</CancelPending>
        </
TradeOrderXml>
      </
PendingOrders>
      <
ProfitTarget>0.006</ProfitTarget>
      <
ProfitTargetPrice>15.14856</ProfitTargetPrice>
      <
ProfitTargetType>Percentage</ProfitTargetType>
      <
StopLoss>0.045</StopLoss>
      <
StopLossPrice>15.925799999999999</StopLossPrice>
      <
StopLossType>Percentage</StopLossType>
      <
TrailingStop>0</TrailingStop>
      <
TrailingStopType>None</TrailingStopType>
      <
BarCountExit>212</BarCountExit>
      <
PendingClose>false</PendingClose>
      <
IsPending>false</IsPending>
    </
PositionDataXml>

Thanks for your help,

David

Posted 5/17/2009 06:45:49 Post #8496
 

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being
Hi David. Hmm. The RE API reference says this: "By default, an order to open a position will be canceled after 1 bar if it is not filled. This setting allows you to specify the number of bars before it will be canceled. If this value is set to -1, the order will not be automatically canceled at all." TWS Extended Order Attributes lists string m_tif with possible values 'Day, GTC, IOC, GTD'.

The words say things should work, so there's a bug someplace.

-Pete
(See also rightedge-ats Yahoo group )

Posted 5/17/2009 14:25:45 Post #8497
 

Lead DeveloperLead DeveloperLead DeveloperLead DeveloperLead DeveloperLead DeveloperLead DeveloperLead Developer
Guys, we think the problem may be on this end. We're setting the GTC flag in the library that we use to communicate with IB, it's acknowledged, but then the order does not show up in IB as GTC. It's a high priority item.
Posted 5/19/2009 11:57:47 Post #8514
 

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member
While the diligent folks at RE work the problem, I've managed to retool Daniel's code a bit so that it works pretty reliably. Rather than try to sort out which orders have been canceled, I simply renew everything. Place a call to RenewOrders() at the beginning of your NewBar method using code something like (set tenewPositions to true in your Startup method):

      public override void NewBar()

      {

            if (this.renewPositions)

            {

                  OutputMessage("Update pending orders for " + this.Symbol.Name);

                  this.RenewOrders();

                  this.renewPositions = false;

            }

           

            // your trading code

           

            }

      }



      private void RenewOrders()

      {

            double profitTarget;

            TargetPriceType profitTargetType;

            double stopLoss;

            TargetPriceType stopLossType;

           

            foreach (Position position in this.PositionManager.GetOpenPositions(this.Symbol))

            {

                  profitTarget = position.ProfitTarget;

                  profitTargetType = position.ProfitTargetType;

                  stopLoss = position.StopLoss;

                  stopLossType = position.StopLossType;

 

                  position.SetProfitTarget(2.0d * profitTarget, profitTargetType);  // force new order by changing price

                  position.SetProfitTarget(profitTarget, profitTargetType);               // assert desired price

                  OutputMessage("TP renewed for " + position.CurrentSize + " " + this.Symbol.Name);

                 

                  position.SetStopLoss(2.0d * stopLoss, stopLossType);                    // force new order by changing price

                  position.SetStopLoss(stopLoss, stopLossType);                                 // assert desired price

                  OutputMessage("SL renewed for " + position.CurrentSize + " " + this.Symbol.Name);

            }

      }

Posted 5/19/2009 14:49:40 Post #8515
 

Lead DeveloperLead DeveloperLead DeveloperLead DeveloperLead DeveloperLead DeveloperLead DeveloperLead Developer
Wow, thanks for sharing, it's much appreciated.

The best way we've found to maintain formatting is to paste the code snippet directly from Visual Studio or RightEdge into Microsoft Word. Then copy from Word into the forum message. The formatting is maintained a little better that way. It's a bit roundabout.
Posted 5/20/2009 08:55:46 Post #8530
 

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member
Bill,

Glad to help. Please see corrected (and cleaned up) post, above. The call to RenewOrders must be placed in NewBar. It doesn't work if placed in the Startup method. My bad.

David

Posted 8/24/2009 03:26:49 Post #9181
 

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie
Hey guys,

I don't really know if you found a solution for the GTC orders, but maybe this can help:

- Download the source code for the TWSplugin for RE:
http://www.rightedgesystems.com/forums/Topic8819-16-1.aspx?Highlight=tws
- In the 'SubmitOrder' method, replace the code for condition 'if (order.GoodTillCanceled)' with this line:
apiOrder.Tif = TimeInForce.GoodTillCancel;
- Now, in your RE system code, BarsValid for a PositionSettings object should execute a GTC order when value is -1

And that's it
Posted 11/24/2009 17:48:23 Post #10075
 

Forum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum Guru
Hello,

Is there an update to this high priority issue as I am facing the same issue and would rather use a fix if available than the workaround?

Actually I wonder what is the real problem, as I never had issues placing GTC orders via the API.

Regards

Amin

« Prev Topic | Next Topic »

««123»»

Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: billb, young, dplaisted

Permissions Expand / Collapse

All times are GMT -5:00, Time now is 1:08am

2005-2007 © RightEdge Systems