#include <QCoreApplication>
#include <QDate>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDate startDate = QDate(2013,6,1);
QDate endDate = QDate(2013,6,12);
int b = startDate.daysTo(endDate);
qDebug() << startDate;
for(int i=1;i<b;++i){
qDebug() << startDate.addDays(i);
}
qDebug() << endDate;
return a.exec();
}
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
void readFromEnd(QString path)
{
QFile file(path);
file.open(QIODevice::ReadOnly);
long len = file.size();
long start = file.pos();
long nextend = start + len - 1;
file.seek(nextend);
qint8 c;
while(nextend>start){
c = file.read(1).at(0);
if(c=='\n'){
qDebug() << file.readLine();
}
nextend--;
file.seek(nextend);
if(nextend==0){
qDebug() << file.readLine();
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
readFromEnd("d:/AL01.csv");
return a.exec();
}
#include <QCoreApplication>
#include "src/SmtpMime"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SmtpClient smtp("smtp.139.com");
smtp.setUser("888888");
smtp.setPassword("888888");
MimeMessage message;
message.setSender(new EmailAddress("888888@139.com", "888888"));
message.addRecipient(new EmailAddress("888888@139.com", "888888"));
message.setSubject("888888");
MimeText text;
text.setText("888888");
message.addPart(&text);
smtp.connectToHost();
smtp.login();
smtp.sendMail(message);
smtp.quit();
}
#include <QCoreApplication>
#include <QDebug>
#include <QSettings>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QFile>
//测试注册表读写
void testRegistry(){
QString path = "software\\weisoft\\金字塔\\settings";
QSettings settings(path,QSettings::NativeFormat);
settings.setValue("myid",12345);
qDebug() << settings.value("myid").toInt();
}
//测试INI文件读写
void testINI(){
QString path = "d:\\mysettings.ini";
QSettings settings(path,QSettings::IniFormat);
settings.setValue("myid",12345);
qDebug() << settings.value("myid").toInt();
}
//测试全局变量读写
void testExtData(){
QSettings settings("上海中期","北京营业部");
settings.setValue("myid",12345);
qDebug() << settings.value("myid").toInt();
}
//测试数据库读写
void testDatabase(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mytest.db");
if(!db.open()){
qDebug() << "db is not open";
}else{
QSqlQuery query;
query.exec("create table main(id varchar)");
query.exec("insert into main values(12345)");
query.exec("select * from main");
while(query.next()){
qDebug() << query.value(0).toInt();
}
}
}
//测试CSV文件读写
void testCSV(){
QFile csvFile("d:\\mytest.csv");
if(csvFile.open(QIODevice::WriteOnly)){
csvFile.write("1,2,3,4,5");
csvFile.close();
}
if(csvFile.open(QIODevice::ReadOnly)){
QTextStream in(&csvFile);
QString result = in.readLine();
qDebug() << result;
csvFile.close();
}
}
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
testRegistry();
testExtData();
testINI();
testDatabase();
testCSV();
return a.exec();
}
#include "mytraderspi.h"
#include <QDebug>
#include <QString>
#include <QTextCodec>
char FRONT_ADDR[] = "tcp://sim-front1.ctp.shcifco.com:31205";
TThostFtdcBrokerIDType BROKER_ID = "1002";
TThostFtdcInvestorIDType INVESTOR_ID = "00000050";
TThostFtdcPasswordType PASSWORD_ID = "123456";
MyTraderSpi::MyTraderSpi()
{
traderApi = CThostFtdcTraderApi::CreateFtdcTraderApi();
traderApi->RegisterSpi(this);
traderApi->SubscribePublicTopic(THOST_TERT_RESTART);
traderApi->SubscribePrivateTopic(THOST_TERT_RESTART);
traderApi->RegisterFront(FRONT_ADDR);
traderApi->Init();
traderApi->Join();
}
void MyTraderSpi::OnFrontConnected()
{
CThostFtdcReqUserLoginField req;
memset(&req,0,sizeof(req));
strcpy(req.BrokerID,BROKER_ID);
strcpy(req.UserID,INVESTOR_ID);
strcpy(req.Password,PASSWORD_ID);
traderApi->ReqUserLogin(&req,0);
}
void MyTraderSpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if(bIsLast)
{
CThostFtdcSettlementInfoConfirmField req;
memset(&req,0,sizeof(req));
strcpy(req.BrokerID,BROKER_ID);
strcpy(req.InvestorID,INVESTOR_ID);
traderApi->ReqSettlementInfoConfirm(&req,0);
}
}
void MyTraderSpi::OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if(bIsLast)
{
CThostFtdcQryInstrumentField req;
memset(&req,0,sizeof(req));
traderApi->ReqQryInstrument(&req,0);
}
}
void MyTraderSpi::OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
qDebug() << QString::fromLocal8Bit(pInstrument->InstrumentName);
}
#include <QCoreApplication>
#include "mytraderspi.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyTraderSpi spi;
return a.exec();
}
#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
#include <QByteArray>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QByteArray encodedString;
encodedString += "合约代码,";
encodedString += "交易所代码,";
encodedString += "合约名称,";
encodedString += "合约在交易所的代码,";
encodedString += "产品代码,";
encodedString += "产品类型,";
encodedString += "交割年份,";
encodedString += "交割月,";
encodedString += "市价单最大下单量,";
encodedString += "市价单最小下单量,";
encodedString += "限价单最大下单量,";
encodedString += "限价单最小下单量,";
encodedString += "合约数量乘数,";
encodedString += "最小变动价位,";
encodedString += "创建日,";
encodedString += "上市日,";
encodedString += "到期日,";
encodedString += "开始交割日,";
encodedString += "结束交割日,";
encodedString += "合约生命周期状态,";
encodedString += "当前是否交易,,";
encodedString += "持仓类型,";
encodedString += "持仓日期类型,";
encodedString += "多头保证金率,";
encodedString += "空头保证金率";
QTextCodec *codec = QTextCodec::codecForName("GBK");
QString string = codec->toUnicode(encodedString);
qDebug() << string;
return a.exec();
}
#include "mytraderspi.h"
#include <QDebug>
#include <QByteArray>
#include <QString>
#include <QFile>
char FRONT_ADDR[] = "tcp://sim-front1.ctp.shcifco.com:31205";
TThostFtdcBrokerIDType BROKER_ID = "1002";
TThostFtdcInvestorIDType INVESTOR_ID = "00000050";
TThostFtdcPasswordType PASSWORD_ID = "123456";
int numOfContracts =0;
TraderSpi::TraderSpi()
{
traderApi = CThostFtdcTraderApi::CreateFtdcTraderApi();
traderApi->RegisterSpi(this);
traderApi->SubscribePublicTopic(THOST_TERT_RESTART);
traderApi->SubscribePrivateTopic(THOST_TERT_RESTART);
traderApi->RegisterFront(FRONT_ADDR);
traderApi->Init();
traderApi->Join();
}
void TraderSpi::OnFrontConnected()
{
CThostFtdcReqUserLoginField req;
memset(&req,0,sizeof(req));
strcpy(req.BrokerID,BROKER_ID);
strcpy(req.UserID,INVESTOR_ID);
strcpy(req.Password,PASSWORD_ID);
traderApi->ReqUserLogin(&req,0);
}
void TraderSpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if(bIsLast)
{
CThostFtdcSettlementInfoConfirmField req;
memset(&req,0,sizeof(req));
strcpy(req.BrokerID,BROKER_ID);
strcpy(req.InvestorID,INVESTOR_ID);
traderApi->ReqSettlementInfoConfirm(&req,0);
}
}
void TraderSpi::OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if(bIsLast)
{
CThostFtdcQryInstrumentField req;
memset(&req,0,sizeof(req));
traderApi->ReqQryInstrument(&req,0);
}
}
void TraderSpi::OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
numOfContracts++;
QFile file("d:/ooo.csv");
if(file.open(QFile::Append))
{
if (numOfContracts==1)
{
QByteArray title;
title += "合约代码,";
title += "交易所代码,";
title += "合约名称,";
title += "合约在交易所的代码,";
title += "产品代码,";
title += "产品类型,";
title += "交割年份,";
title += "交割月,";
title += "市价单最大下单量,";
title += "市价单最小下单量,";
title += "限价单最大下单量,";
title += "限价单最小下单量,";
title += "合约数量乘数,";
title += "最小变动价位,";
title += "创建日,";
title += "上市日,";
title += "到期日,";
title += "开始交割日,";
title += "结束交割日,";
title += "合约生命周期状态,";
title += "当前是否交易,";
title += "持仓类型,";
title += "持仓日期类型,";
title += "多头保证金率,";
title += "空头保证金率";
qDebug() << QString::fromLocal8Bit(title);
title += "\n";
file.write(title);
}
QByteArray content;
content += pInstrument->InstrumentID;
content +=",";
content += pInstrument->ExchangeID;
content +=",";
content += pInstrument->InstrumentName;
content += ",";
content += pInstrument->ExchangeInstID;
content += ",";
content += pInstrument->ProductID;
content += ",";
content += pInstrument->ProductClass;
content += ",";
content += QByteArray::number(pInstrument->DeliveryYear);
content += ",";
content += QByteArray::number(pInstrument->DeliveryMonth);
content += ",";
content += QByteArray::number(pInstrument->MaxMarketOrderVolume);
content += ",";
content += QByteArray::number(pInstrument->MinMarketOrderVolume);
content += ",";
content += QByteArray::number(pInstrument->MaxLimitOrderVolume);
content += ",";
content += QByteArray::number(pInstrument->MinLimitOrderVolume);
content += ",";
content += QByteArray::number(pInstrument->VolumeMultiple);
content += ",";
content += QByteArray::number(pInstrument->PriceTick);
content += ",";
content += pInstrument->CreateDate;
content += ",";
content += pInstrument->OpenDate;
content += ",";
content += pInstrument->ExpireDate;
content += ",";
content += pInstrument->StartDelivDate;
content += ",";
content += pInstrument->EndDelivDate;
content += ",";
content += pInstrument->InstLifePhase;
content += ",";
content += QByteArray::number(pInstrument->IsTrading);
content += ",";
content += pInstrument->PositionType;
content += ",";
content += pInstrument->PositionDateType;
content += ",";
content += QByteArray::number(pInstrument->LongMarginRatio);
content += ",";
content += QByteArray::number(pInstrument->ShortMarginRatio);
qDebug() << QString::fromLocal8Bit(content);
content += "\n";
file.write(content);
}
file.close();
}