Hello guys,
I'm totally clueless... it all seems to work as it should but when called from a batch file and the scheduler my orders are not always transmitted to IB Gateway.
I run a few different batches files each morning:
- update norgate DB at 08:45
- check TWS or IB Gateway is running at 08:55 and 09:05
- clean daily persistent variables at 09:10
- the autotrading script at 09:15
Somehow, sometimes the last script will NOT generate orders when called from the batch file by the scheduler... but running that batch file manually will work...
I know the scheduler does run the script (it sends an email saying so) but the orders are not sent reliably to IB...
Here is the autotrading part of the script:
// AUTOTRADING
ibc = GetTradingInterface("IB");
if( ibc.IsConnected() AND LastValue(Buy) AND LastValue(rank <= MaxPositions)) // check if connection to IB was successfull
{
// Prepating Contracts : SYMBOL-EXCHANGE-TYPE-CURRENCY
//StockMarketName = StrToUpper( MarketID(1) );
//StockMarketName = WriteIf(StockMarketName=="NASDAQ","ISLAND",StockMarketName);
//StockMarketName = WriteIf(StockMarketName=="NYSE","SMART",StockMarketName);
StockMarketName = "SMART";
ContractFullName = Name() + "-" + StockMarketName + "-STK-USD";
ContractSmartName = Name() + "-SMART-STK-USD";
// retrieve BUYSTP_OrdID from previous run of THIS strategy, will be empty if no order was placed before
BUYSTP_OrdID = StaticVarGetText(StrategyName+"BUYSTP_OrdID_"+Name());
_TRACE("###BUYSTP_OrdID = StaticVarGetText:" + BUYSTP_OrdID);
// Create Order if it's before 10:00 AND the order ID is empty
if( Now(4) < 154000 AND BUYSTP_OrdID=="")
{
// It's not too late to place the order
BUYSTP_OrdID = ibc.ModifyOrder(BUYSTP_OrdID, ContractFullName, "BUY", LastValue(BuyQuantity), "STP", 0, LastValue(LimitPrice), "DAY", True ); // place order but do not transmit yet
// Error handling
BUYSTP_Error = ibc.GetLastError(BUYSTP_OrdID);
if (BUYSTP_Error != "")
{
SendEmail(StrategyName+"-ERROR-BUYSTP for Ticker: "+Name()+" Name: "+FullName(), BUYSTP_Error);
Say("Error with Ticker "+Name()+" Name "+FullName()+" API Error Message is "+BUYSTP_Error);
// Pushover
messageurl = url + "Error BUYSTP with Ticker "+Name()+" API Error Message is "+BUYSTP_Error;
internetconnection = InternetOpenURL(messageurl);
InternetClose(internetconnection);
}
else
{
SendEmail(StrategyName+"-SUCCESS-BUYSTP for Ticker: "+Name()+" Name: "+FullName(),"Sent Buy Order for "+LastValue(BuyQuantity)+" of :"+Name());
Say("Sent "+LastValue(BuyQuantity)+" "+Name());
// Pushover
messageurl = url + "Sent order for: "+LastValue(BuyQuantity)+" "+Name();
internetconnection = InternetOpenURL(messageurl);
InternetClose(internetconnection);
}
MOC_OrdID = StaticVarGetText(StrategyName+"MOC_OrdID_"+Name());
_TRACE("###MOC_OrdID = StaticVarGetText:" + MOC_OrdID);
MOC_OrdID = ibc.ModifyOrder(MOC_OrdID, ContractFullName, "SELL", LastValue(BuyQuantity), "MOC", 0, 0, "DAY", True, 100, "", BUYSTP_OrdID );
// Error handling
MOC_Error = ibc.GetLastError(MOC_OrdID);
if (MOC_Error != "")
{
SendEmail(StrategyName+"-ERROR-MOC for Ticker: "+Name()+" Name: "+FullName(), MOC_Error);
Say("Error with Ticker "+Name()+" Name "+FullName()+" API Error Message is "+MOC_Error);
// Pushover
messageurl = url + "Error MOC with Ticker "+Name()+" API Error Message is "+MOC_Error;
internetconnection = InternetOpenURL(messageurl);
InternetClose(internetconnection);
}
// store BUYSTP_OrdID for next run so we know which order to modify, set as Persistent to be available when the script
// is called later in the day to generate MOC orders
StaticVarSetText(StrategyName+"BUYSTP_OrdID_"+Name(), BUYSTP_OrdID, True);
StaticVarSetText(StrategyName+"MOC_OrdID_"+Name(), MOC_OrdID, True);
}
}
As you guess the persistent variables "cleaning" done every morning is deleting the variables from the previous day (it's a daily MOO and MOC strategy).
From what I can see, the only plausible problem could be that ibc.IsConnected is NOT TRUE at the time the script is run... which would be super annoying since I'm checking that 10 and 20 minutes before running the script...
Amibroker is running on a VPS so I would expect the connection to IB to be a lot more reliable than from my home wifi (where I get IB TWS saying disconnected/reconnected a few times a day).
Anybody has an idea of what could be going wrong ?
Have you had the problem of intermitent ibc.IsConnected "deconnections" ?
Thanks a lot for any idea,