Пример #1
0
bool OrderItem::isValid()
{
	bool isVal=date>0&&price>0.0&&symbol.size()==6;
	if(isVal)
	{
        QDateTime itemDate=QDateTime::fromTime_t(date);
        if(baseValues_->use24HourTimeFormat){
            dateStr=itemDate.toString(baseValues.dateTimeFormat);
        } else {
            QString mmssTemp=itemDate.toString("mm:ss");
            QString hTemp=itemDate.toString("H");
            qint16 hTempInt=hTemp.toInt();
            QString timeStr;
            if(hTempInt<=12)timeStr=hTemp+':'+mmssTemp+" am";
            else timeStr=QString::number(hTempInt-12)+':'+mmssTemp+" pm";
            dateStr=itemDate.toString("dd.MM.yyyy")+' '+timeStr;
        }

		QString priceSign=baseValues.currencyMap.value(symbol.right(3),CurencyInfo("$")).sign;
        amountStr=baseValues.currencyMap.value(symbol.left(3),CurencyInfo("$")).sign+textFromDouble(amount);
        priceStr=priceSign+textFromDouble(price);
		total=price*amount;
        totalStr=priceSign+textFromDouble(total,baseValues.currentPair.currBDecimals);
	}
	return isVal;
}
void Exchange_Bitfinex::sell(QString symbol, double apiBtcToSell, double apiPriceToSell)
{
	if(tickerOnly)return;

    CurrencyPairItem pairItem;
    pairItem=baseValues.currencyPairMap.value(symbol.toUpper(),pairItem);
    if(pairItem.symbol.isEmpty())return;

	QByteArray orderType="limit";
    if(pairItem.currRequestSecond=="exchange")
		orderType.prepend("exchange ");
    QByteArray params=", \"symbol\": \""+pairItem.currRequestPair+"\", \"amount\": \"";
    params+=textFromDouble(apiBtcToSell,pairItem.currADecimals);
    params+="\", \"price\": \"";
    params+=textFromDouble(apiPriceToSell,pairItem.priceDecimals);
    params+="\", \"exchange\": \"all\", \"side\": \"sell\", \"type\": \""+orderType+"\"";
    if(debugLevel)logThread->writeLog("Sell: "+params,1);
	sendToApi(307,"order/new",true,true,params);
}
void TradesItem::cacheStrings()
{
	QDateTime itemDate=QDateTime::fromTime_t(date);

    if(baseValues_->use24HourTimeFormat){
        timeStr=itemDate.toString(baseValues.timeFormat);
        dateStr=itemDate.toString(baseValues.dateTimeFormat);
    } else {
        QString mmssTemp=itemDate.toString("mm:ss");
        QString hTemp=itemDate.toString("H");
        qint16 hTempInt=hTemp.toInt();
        if(hTempInt<=12)timeStr=hTemp+':'+mmssTemp+" am";
        else timeStr=QString::number(hTempInt-12)+':'+mmssTemp+" pm";
        dateStr=itemDate.toString("dd.MM.yyyy")+' '+timeStr;
    }

    if(price>0.0)priceStr=textFromDouble(price,baseValues.decimalsPriceLastTrades);
    if(amount>0.0)amountStr=textFromDouble(amount,baseValues.decimalsAmountLastTrades);
    if(amount>0.0&&price>0.0)totalStr=textFromDouble(price*amount,qMin(baseValues.currentPair.currBDecimals,baseValues.decimalsTotalLastTrades));
}
Пример #4
0
void HistoryItem::cacheStrings()
{
	QDateTime cachedDateTime=QDateTime::fromTime_t(dateTimeInt);

    if(baseValues_->use24HourTimeFormat){
        timeStr=cachedDateTime.toString(baseValues.timeFormat);
        dateTimeStr=cachedDateTime.toString(baseValues.dateTimeFormat);
    } else {
        QString mmssTemp=cachedDateTime.toString("mm:ss");
        QString hTemp=cachedDateTime.toString("H");
        qint16 hTempInt=hTemp.toInt();
        if(hTempInt<=12)timeStr=hTemp+':'+mmssTemp+" am";
        else timeStr=QString::number(hTempInt-12)+':'+mmssTemp+" pm";
        dateTimeStr=cachedDateTime.toString("dd.MM.yyyy")+' '+timeStr;
    }

    cachedDateTime.setTime(QTime(0,0,0,0));
	dateInt=cachedDateTime.toTime_t();

	QString usdSign=baseValues.currencyMap.value(symbol.right(3),CurencyInfo("$")).sign;
    if(price>0.0)priceStr=usdSign+textFromDouble(price,baseValues.decimalsPriceMyTransactions);
	if(volume>0.0)
	{
        volumeStr=baseValues.currencyMap.value(symbol.left(3),CurencyInfo("BTC")).sign+textFromDouble(volume,baseValues.decimalsAmountMyTransactions);
	}
	if(volume>0.0&&price>0.0)
    {
        totalStr=textFromDouble(price*volume,baseValues.decimalsTotalMyTransactions);

		if(!baseValues.forceDotInSpinBoxes)
		{
			priceStr.replace(".",",");
			volumeStr.replace(".",",");
			totalStr.replace(".",",");
		}
	}
}
QString RuleScriptParser::holderToScript(RuleHolder &holder, bool testMode)
{
    bool execImmediately=holder.variableACode==QLatin1String("IMMEDIATELY");
    bool eventIsTrade=holder.variableACode==QLatin1String("LastTrade")||holder.variableACode==QLatin1String("MyLastTrade");

    QString script="function executeRule()\n{\n";
    if(!execImmediately)script="var executed=false;\n"+script+" executed=true;\n";
    if(!testMode)
    {
        if(holder.isTradingRule())
        {
            script+=
            " if(trader.get(\"ApiLag\")>10)\n"
            " {\n"
            " trader.log(\"Api lag is to high\");\n"
            " trader.delay(1,\"executeRule()\");\n"
            " return;\n"
            " }\n\n";
        }
        if(holder.thanTypeIndex<4)
        {
            double amount=holder.thanAmount;
            if(holder.thanAmountPercentChecked)
            {
                amount/=100.0;
                if(holder.thanTypeIndex==0)
                    script+=" var amount = trader.get(\"Balance\",\""+baseValues.currentPair.currAStr+"\");\n";
                else
                    script+=" var amount = trader.get(\"Balance\",\""+baseValues.currentPair.currBStr+"\");\n";
                if(amount!=0.0&&amount!=1.0)
                    script+=" amount *= "+textFromDouble(amount)+";\n";
            }
            else script+=" var amount = "+textFromDouble(amount)+";\n";

            if(amount!=0.0)
            {
                if(holder.thanAmountFeeIndex==1)script+=" amount *= (1.0 + trader.get(\"Fee\") / 100.0);\n";
                if(holder.thanAmountFeeIndex==2)script+=" amount *= (1.0 - trader.get(\"Fee\") / 100.0);\n";
            }
            if(!script.isEmpty())script+="\n";

            if(holder.thanPriceTypeCode=="EXACT")script+=" var price = "+textFromDouble(holder.thanPrice)+";\n";
            else
            {
                script+=" var price = trader.get(\""+holder.tradeSymbolCode+"\" , \""+holder.thanPriceTypeCode+"\");\n";

                if(holder.thanPricePercentChecked)script+=" price "+holder.thanPricePlusMinusText+"= price * "+textFromDouble(holder.thanPrice/100.0)+";\n";
                else if(holder.thanPrice!=0.0)script+=" price "+holder.thanPricePlusMinusText+"= "+textFromDouble(holder.thanPrice)+";\n";

                if(holder.thanPriceFeeIndex==1)script+=" price *= ( 1.0 + trader.get(\"Fee\") / 100.0 );\n";
                if(holder.thanPriceFeeIndex==2)script+=" price *= ( 1.0 - trader.get(\"Fee\") / 100.0 );\n";
            }
            if(!script.isEmpty())script+="\n";
            switch(holder.thanTypeIndex)
            {
            case 0: //Sell
                script+=" trader.sell(\""+holder.tradeSymbolCode+"\" , amount , price)";
                break;
            case 1: //Buy
                if(holder.thanAmountPercentChecked)
                    script+=" trader.buy(\""+holder.tradeSymbolCode+"\" , amount / price , price)";
                else
                    script+=" trader.buy(\""+holder.tradeSymbolCode+"\" , amount , price)";
                break;
            case 2: //Receive
                script+=" trader.sell(\""+holder.tradeSymbolCode+"\" , amount / price , price)";
                break;
            case 3: //Spend
                script+=" trader.buy(\""+holder.tradeSymbolCode+"\" , amount / price , price)";
                break;
            default: break;
            }
            if(!script.isEmpty())script+=";\n";
        }
        else
        {
            switch(holder.thanTypeIndex)
            {
            case 4: //Cancel all Orders
                script+=" trader.cancelOrders();\n";
                break;
            case 5: //Cancel Asks
                script+=" trader.cancelAsks();\n";
                break;
            case 6: //Cancel Bids
                script+=" trader.cancelBids();\n";
                break;
            case 7://Start group
                script+=" trader.groupStart(\""+holder.thanText+"\");\n";
                break;
            case 8://Stop group
                script+=" trader.groupStop(\""+holder.thanText+"\");\n";
                break;
            case 9://Beep
                script+=" trader.beep();\n";
                break;
            case 10://Play Sound
                script+=" trader.playWav(\""+holder.thanText+"\");\n";
                break;
            case 11://Start app
                script+=" trader.startApp(\""+holder.thanText+"\");\n";
                break;
            case 12://Say text
                {
                QString sayText;
                if(!holder.sayCode.isEmpty())sayText=", trader.get(\""+holder.sayCode+"\")";
                script+=" trader.say(\""+holder.thanText+"\""+sayText+");\n";
                }
                break;
            }
        }
        script+=" trader.groupDone();\n";
    }
    else script+=" trader.test(1);\n trader.groupStop();\n";
    script+="}";

    bool haveDelay=holder.delayMilliseconds>0.0001&&!testMode;

    QString executeRuleLine;

    if(haveDelay)executeRuleLine=" trader.delay("+textFromDouble(holder.delayMilliseconds,3,0)+",\"executeRule()\");";
            else executeRuleLine=" executeRule();";

    if(execImmediately)
    {
        script+="\n"+executeRuleLine;
    }
    else
    {
    QString indicatorBValue;
	QString indicatorB;
    QString realtime;
	QString ifLine;

	QString comparationText=holder.comparationText;
	if(comparationText==QLatin1String("="))comparationText=QLatin1String("==");
	else
	if(comparationText==QLatin1String("<>"))comparationText=QLatin1String("!=");

    if(!eventIsTrade)
    {
        if(holder.variableBCode=="EXACT")
        {
            ifLine=" if(value "+comparationText+" "+textFromDouble(holder.variableBExact)+")";
        }
        else
        {
            indicatorBValue="trader.get(\""+holder.variableBSymbolCode+"\" , \""+holder.variableBCode+"\")";
            indicatorB=indicatorBValue+";\n";
            if(holder.variableBPercentChecked)
                indicatorB+=" baseVariable "+holder.variableBplusMinus+"= baseVariable*"+textFromDouble(holder.variableBExact/100.0)+";\n";
            else
                if(holder.variableBExact!=0.0)indicatorB+=" baseVariable "+holder.variableBplusMinus+"= "+textFromDouble(holder.variableBExact)+";\n";

            if(holder.variableBFeeIndex>0)
            {
                QString sign=(holder.variableBFeeIndex==1?"+":"-");
                indicatorB+=" baseVariable "+sign+"= baseVariable*trader.get(\""+holder.valueBSymbolCode+"\" , \"Fee\");\n";
            }

            if(holder.variableBModeIndex==0)realtime=" calcBaseVariable();\n";
            else
                if(holder.variableBModeIndex==2)
                {
                    bool haveLessThan=comparationText.contains("<");
                    bool haveMoreThan=comparationText.contains(">");
                    if(haveLessThan)realtime=" if(value > "+indicatorBValue+")calcBaseVariable();\n";
                    if(haveMoreThan)realtime=" if(value < "+indicatorBValue+")calcBaseVariable();\n";
                }
            ifLine=" if(value "+comparationText+" baseVariable)";
            script+="\n\nvar baseVariable = calcBaseVariable();\n"
                    "function calcBaseVariable()\n"
                    "{\n"
                    " baseVariable = "+indicatorB+
                    " return baseVariable;\n"
                    "}";
        }
    }

    QString eventName=holder.variableACode;
    if(!eventIsTrade&&eventName.startsWith(QLatin1String("Balance"),Qt::CaseInsensitive))
    {
        if(eventName.endsWith("A",Qt::CaseInsensitive))eventName="Balance\",\""+holder.valueASymbolCode.left(3);
        else
        if(eventName.endsWith("B",Qt::CaseInsensitive))eventName="Balance\",\""+holder.valueASymbolCode.right(3);
    }
    script+="\n\ntrader.on(\""+eventName+"\").changed()\n"
    "{\n"
    " if(executed)return;\n"
    " if(symbol != \""+holder.valueASymbolCode+"\")return;\n";
    script+=realtime+
    ifLine+executeRuleLine;
    if(testMode)script+="\n else { trader.test(2); trader.stopGroup(); }\n";
    script+="\n}\n";
    }
    return script;
}
Пример #6
0
void DepthModel::calculateSize()
{
	if(!somethingChanged)return;
	somethingChanged=true;

    double maxPrice=0.0;
    double maxVolume=0.0;
    double maxTotal=0.0;

    double totalSize=0.0;
    double totalPrice=0.0;

	if(originalIsAsk)
	{
		for(int n=0;n<priceList.count();n++)
		{
			int currentRow=n;
			if(!originalIsAsk)currentRow=priceList.count()-currentRow-1;

			totalSize+=volumeList.at(currentRow);
            totalPrice+=volumeList.at(currentRow)*priceList.at(currentRow);

            sizeListAt(currentRow)=totalSize;
            sizePriceList[currentRow]=totalPrice;
            sizeListStr[currentRow]=textFromDouble(totalSize,qMin(baseValues.currentPair.currADecimals,baseValues.decimalsTotalOrderBook));

			maxPrice=qMax(maxPrice,priceList.at(currentRow));
			maxVolume=qMax(maxVolume,volumeList.at(currentRow));
            maxTotal=qMax(maxTotal,sizeListAt(currentRow));
		}
	}
	else
	{
		for(int n=priceList.count()-1;n>=0;n--)
		{
			int currentRow=n;
			if(originalIsAsk)currentRow=priceList.count()-currentRow-1;
			totalSize+=volumeList.at(currentRow);
            totalPrice+=volumeList.at(currentRow)*priceList.at(currentRow);
            sizeListAt(currentRow)=totalSize;
            sizePriceList[currentRow]=totalPrice;
            sizeListStr[currentRow]=textFromDouble(totalSize,qMin(baseValues.currentPair.currADecimals,baseValues.decimalsTotalOrderBook));

			maxPrice=qMax(maxPrice,priceList.at(currentRow));
			maxVolume=qMax(maxVolume,volumeList.at(currentRow));
            maxTotal=qMax(maxTotal,sizeListAt(currentRow));
		}
	}

    widthPrice=10+textFontWidth(textFromDouble(maxPrice,baseValues.currentPair.priceDecimals));
    widthVolume=10+textFontWidth(textFromDouble(maxVolume,baseValues.currentPair.currADecimals));
    widthSize=10+textFontWidth(textFromDouble(maxTotal,baseValues.currentPair.currADecimals));
	
	widthPrice=qMax(widthPrice,widthPriceTitle);
	widthVolume=qMax(widthVolume,widthVolumeTitle);
	widthSize=qMax(widthSize,widthSizeTitle);

	int sizeColumn=2;
	if(isAsk)sizeColumn=1;
	emit dataChanged(index(0,sizeColumn),index(priceList.count()-1,sizeColumn));
}
Пример #7
0
QVariant DepthModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())return QVariant();
	int currentRow=index.row();

	if(role==Qt::WhatsThisRole)
	{
		return baseValues.currentPair.currBSign+priceListStr.at(currentRow)+" "+baseValues.currentPair.currASign+volumeListStr.at(currentRow)+" "+baseValues.currentPair.currASign+sizeListStr.at(currentRow);
	}

	if(role!=Qt::DisplayRole&&role!=Qt::ToolTipRole&&role!=Qt::StatusTipRole&&role!=Qt::ForegroundRole&&role!=Qt::BackgroundRole&&role!=Qt::TextAlignmentRole)return QVariant();

	int indexColumn=index.column();
	if(isAsk)indexColumn=columnsCount-indexColumn-1;

	if(role==Qt::TextAlignmentRole)
	{
		if(indexColumn==0)return 0x0081;
		if(indexColumn==1)return 0x0082;
		if(indexColumn==3)return 0x0082;
		return 0x0084;
	}

	if(grouped&&currentRow<2)
	{
		if(role==Qt::ForegroundRole)return baseValues.appTheme.black;
		if(currentRow==1||groupedPrice==0.0)return QVariant();
		QString firstRowText;
		switch(indexColumn)
		{
		case 0: //Price
                firstRowText=textFromDouble(groupedPrice);
				if(role==Qt::ToolTipRole)firstRowText.prepend(baseValues.currentPair.currBSign);	
				break; 
		case 1: //Volume
                firstRowText=textFromDouble(groupedVolume,baseValues.currentPair.currADecimals);
				if(role==Qt::ToolTipRole)firstRowText.prepend(baseValues.currentPair.currASign);
				break;
		}
		if(firstRowText.isEmpty())return QVariant();
		return firstRowText;
	}

	if(grouped)currentRow-=grouped;
	if(currentRow<0||currentRow>=priceList.count())return QVariant();

	if(!originalIsAsk)currentRow=priceList.count()-currentRow-1;

	if(role==Qt::StatusTipRole)
	{
		QString direction;

		switch(directionList.at(currentRow))
		{
		case -1: direction=downArrowStr+"\t"; break;
		case 1: direction=upArrowStr+"\t"; break;
		}
		return baseValues.currentPair.currBSign+priceListStr.at(currentRow)+"\t"+baseValues.currentPair.currASign+volumeListStr.at(currentRow)+"\t"+direction+baseValues.currentPair.currASign+sizeListStr.at(currentRow);
	}

	if(role==Qt::ForegroundRole)
	{
		if(indexColumn==1)
		{
            double volume=volumeList.at(currentRow);
            double smallValue=baseValues.currentPair.currAInfo.valueSmall;
			if(volume<=smallValue)return baseValues.appTheme.gray; smallValue*=10.0;
			if(volume<=smallValue)return baseValues.appTheme.black; smallValue*=10.0;
			if(volume<=smallValue)return baseValues.appTheme.darkGreen; smallValue*=10.0;
			if(volume<=smallValue)return baseValues.appTheme.darkRedBlue;
			return baseValues.appTheme.red;
		}
		return baseValues.appTheme.black;
	}

    double requestedPrice=priceList.at(currentRow);
	if(requestedPrice<=0.0)return QVariant();

	if(role==Qt::BackgroundRole)
	{
		if(originalIsAsk)
		{
			if(mainWindow.ordersModel->currentAsksPrices.value(requestedPrice,false))return baseValues.appTheme.lightGreen;
		}
		else
		{
			if(mainWindow.ordersModel->currentBidsPrices.value(requestedPrice,false))return baseValues.appTheme.lightGreen;
		}
		return QVariant();
	}

	QString returnText;

	switch(indexColumn)
	{
	case 0://Price
		if(role==Qt::ToolTipRole)baseValues.currentPair.currBSign+priceListStr.at(currentRow);
		return priceListStr.at(currentRow);
		break;
	case 1:
		{//Volume
		if(volumeList.at(currentRow)<=0.0)return QVariant();
		if(role==Qt::ToolTipRole)baseValues.currentPair.currASign+volumeListStr.at(currentRow);
		return volumeListStr.at(currentRow);
		}
		break;
	case 2:
		{//Direction
			switch(directionList.at(currentRow))
			{
			case -1: return downArrowStr;
			case 1: return upArrowStr;
			default: return QVariant();
			}
		}
	case 3:
		{//Size
        if(sizeListGet(currentRow)<=0.0)return QVariant();
		if(role==Qt::ToolTipRole)baseValues.currentPair.currASign+sizeListStr.at(currentRow);
		return sizeListStr.at(currentRow);
		}
		break;
	default: break;
	}
	if(!returnText.isEmpty())return returnText;
	return QVariant();
}