RightEdge Forums
Main     Home          Members     Calendar     Who's On

Welcome Guest
        



Live trading at Market Close (End of Day) Expand / Collapse
Message
Posted 4/17/2012 3:04:05 PM Post #14275
 

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie
We have created multiple systems for back testing strategies at EOD prices. We're now starting to convert the strategies to live systems using IB (TWS) and are running into some issues.

The first system is the simplest one. We are trading two ETFs, with a SMA on an Index (third Symbol) that we use to make decisions on which of the two ETFs to hold. The first issue is that at 4:00PM the New Bar method is never called. Frequency we used was "Daily" with Live Options set to "ignore data outside trading hours" and also "Daily Frequency with filter based on time" both set to 9:30AM-4:00PM. We tested the same code with Minute Bars and everything works, but our system needs to trade based on prices at close.

The second issue is that if we leave RightEdge and IB TWS open, at around 11:45pm there is an error message in RE that data feed disconnect, and the IB TWS application is not open the next day.

Thanks for taking a look and any help. Here is some sample code from the MySystem class


public override void NewBar()
{
//Call NewBar in the SymbolScripts
base.NewBar();

//Load Symbols
MySymbolScript ETF1 = SymbolScripts["ETF1"];
MySymbolScript ETF2 = SymbolScripts["ETF2"];
MySymbolScript INDEX = SymbolScripts["INDEX"];

INDEX.OutputMessage("INDEX: " + INDEX.Bars.Current.Close + " SMA: " + INDEX.SMAFast.Current);

//Calculate Target Ratio
if (INDEX.SMAFast.Current < INDEX.Bars.Current.Close)
{
ETF1TargetRatio = 1.00;
ETF2TargetRatio = 0.00;
}
else
{
ETF1TargetRatio = 0.00;
ETF2TargetRatio = 1.00;
}

//Calculate Number of Shares
ETF1NewShares = (int)((SystemData.AccountValue) * ETF1TargetRatio / ETF1.Bars.Current.Close);
ETF2NewShares = (int)((SystemData.AccountValue) * ETF2TargetRatio / ETF2.Bars.Current.Close);

bool positionChanged = true;

if(PositionManager.GetOpenPositions().Count > 0)
{
positionChanged = false;

if(PositionManager.GetOpenPositions()[0].Symbol.Name == "ETF2" & ETF1NewShares > 0)
{
positionChanged = true;
PositionManager.GetOpenPositions()[0].CloseAtMarket();
}

if(PositionManager.GetOpenPositions()[0].Symbol.Name == "ETF1" & ETF2NewShares > 0)
{
positionChanged = true;
PositionManager.GetOpenPositions()[0].CloseAtMarket();
}
}

if(positionChanged)
{
//Trade ETF1
if(ETF1NewShares > 0)
{
Position p = ETF1.OpenPosition(PositionType.Long, OrderType.Limit, ETF1.Bars.Current.Close, ETF1NewShares);
}

//Trade ETF2
if(ETF2NewShares > 0)
{
Position p = ETF2.OpenPosition(PositionType.Long, OrderType.Limit, ETF2.Bars.Current.Close, ETF2NewShares);
}
}
}
Posted 4/23/2012 12:32:48 AM Post #14300
 

DeveloperDeveloperDeveloperDeveloperDeveloperDeveloperDeveloperDeveloper
A bar won't be sent out until a tick is received for a time after the end of that bar. Since you are using the Live option to filter out data after 4:00, the system never receives a tick to let it know that the current time has passed 4:00 and the NewBar isn't raised. So you should disable the option to ignore live data outside of trading hours.

Of course it doesn't make much sense that you can't use these two options together. I've added a work item to change it so that when the option to ignore live data is enabled, when data is filtered outside of trading hours it will still send time updates so that this issue won't happen.

TWS has a "feature" where it automatically closes itself each day. If you want to keep it running you need a tool that prevents this from happening. I recommend this one: IBController

Thanks,
Daniel
Posted 4/23/2012 12:03:38 PM Post #14306
 

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie
Hi Daniel,

Thank you for the reply. I wasn't very clear, but we tried using the following 4 options:

Minute Bars - WORKED
Daily Bars with "ignore data outside trading hours" ENABLED - NOT WORKING
Daily Bars with "ignore data outside trading hours" DISABLED - NOT WORKING
Daily Frequency filter based on time with "ignore data outside trading hours" DISABLED - NOT WORKING

Are there any other reasons that the System NewBar event would not fire for those?

Thanks,
Serge
Posted 4/24/2012 1:48:36 AM Post #14309
 

DeveloperDeveloperDeveloperDeveloperDeveloperDeveloperDeveloperDeveloper
Can you clarify how you were testing this? Were you just checking to see if NewBar() was called at all?

If you are using vanilla daily bars, then NewBar() wouldn't be called until you received data from the broker after midnight. Since TWS was shutting down before then this wouldn't have happened.

With the Daily Frequency filter, were you getting any data after 4:00PM? If so then I would expect NewBar() to be called.

Are you using build 42? It had some fixes to the FilteredDailyFrequency. There will also be more fixes in the next build. I don't think they are related to your problems but you can try using the updated version of FilteredDailyFrequency available here.

Thanks,
Daniel
Posted 4/24/2012 10:24:05 AM Post #14311
 

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie
Hi Daniel,

I used OutputMessage to print out some info at the beginning of NewBar(), which did not print anything when using Daily bars, so I can be sure it was never called.

Testing was done with Build 42 (updated last week).

I tried several different frequency filter times (9:30-4:00, 1:00-1:10, 3:00-4:00, etc) to see if I could get anything to work, but no luck. Data was coming in from IB during all the times. Testing with Minute bars everything would work though.

Thanks again,
Serge
Posted 4/25/2012 1:50:49 PM Post #14324
 

DeveloperDeveloperDeveloperDeveloperDeveloperDeveloperDeveloperDeveloper
Hi serge,

I've discovered two bugs with the FilteredDailyFrequency. One is that the EndTime is not saved correctly when you use the frequency editor. The other is that when you use a FilteredDailyFrequency as your main system frequency, you will get a "Partial bar null" exception for ticks that are sent outside of the times specified in the frequency.

These will be fixed in a future build. For now, I recommend you set the main system frequency to Daily, and create a FilteredDailyFrequency in code. Here is a sample of how to do this:


Frequency _fdf;

public override void Startup()
{
FilteredDailyFrequency fdf = new FilteredDailyFrequency();
fdf.StartTime = TimeSpan.FromHours(9.5);
fdf.EndTime = TimeSpan.FromHours(10).Add(TimeSpan.FromMinutes(40));

_fdf = GetFrequency(fdf);

_fdf.NewBar += _fdf_NewBar;
}

void _fdf_NewBar(object sender, SingleBarEventArgs e)
{
OutputMessage("New filtered daily frequency bar. Close price: " + _fdf.Bars.Current.Close);
}


If you are using indicators, you can set them to use the FilteredDailyFrequency like this:

_sma = new SMA(20, _fdf.Close);
SystemData.IndicatorManager.SetFrequency(_sma, _fdf);

Thanks,
Daniel
« Prev Topic | Next Topic »


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 12:19pm

2005-2007 © RightEdge Systems