-
Notifications
You must be signed in to change notification settings - Fork 4
/
SignalTrader.JLS
7 lines (7 loc) · 17.6 KB
/
SignalTrader.JLS
1
2
3
4
5
6
7
[JLScript]
Version=1
Name=ScriptTrader - BTC
Notes=
Encrypted=false
LogRowsCount=1500
Data="var variablePath = \"C:\\\\Users\\\\Damjan\\\\Documents\\\\GitHub\\\\qt-bitcoin-trader-scripts-signal-trading\\\\BTC\\\\\";\nvar signal1StatusFile = variablePath + \"signal1Status.txt\";\nvar signal2StatusFile = variablePath + \"signal2Status.txt\";\nvar signal1Status = \"\";\nvar signal2Status = \"\";\nvar signal1StatusOld = \"\";\nvar signal2StatusOld = \"\";\nvar lastBuyPriceFile = variablePath + \"lastBuyPrice.txt\";\nvar lastSellPriceFile = variablePath + \"lastSellPrice.txt\";\nvar lastBuyPrice = 0;\nvar lastBuyPriceOld = 0;\nvar lastSellPrice = 0;\nvar lastSellPriceOld = 0;\nvar currencyPrimary = \"USD\";\nvar currencySecondary = \"BTC\";\nvar feeMaker = 0.001;\nvar feeTaker = 0.002;\nvar openAsksCountOld = 0;\nvar openBidsCountOld = 0;\nvar minPrimaryCurrencyBalanceForSale = 100;\nvar minSecondaryCurrencyBalanceForSale = 0.001;\nvar executeAsk = false;\nvar executeBid = false;\nvar lastCurrencySecondaryBalanceRight = 0;\ntrader.fileWrite(lastSellPriceFile, lastSellPrice);\ntrader.fileWrite(lastSellPriceFile, lastSellPrice);\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n/////////////// log to file or window ///////////////////\n//////////////////////////////////////////////////////////////////////////////////////////////\nvar logToFile = true;\nvar logToWindow = true;\nvar logFile = variablePath + \"SignalTraderLogger.txt\";\n\nfunction eventLogger(tempString) {\n if (logToFile)\n trader.fileAppend(logFile, trader.dateTimeString() + \": \" + tempString);\n if (logToWindow)\n trader.log(tempString);\n}\n//////////////////////////////////////////////////////////////////////////////////////////////\n/////////////// functions ///////////////////\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction checkTradeExecutionCondition() {\n var scriptName = \"checkTradeExecutionCondition()\";\n eventLogger(scriptName + \".START\");\n\n eventLogger(scriptName + \".executeAsk: \" + executeAsk);\n eventLogger(scriptName + \".executeBid: \" + executeBid);\n if (executeAsk == true) {\n makeSell();\n }\n\n if (executeBid == true) {\n makeBuy();\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction readSignalFiles() {\n var scriptName = \"readSignalFiles()\";\n eventLogger(scriptName + \".START\");\n\n signal1Status = trader.fileReadAll(signal1StatusFile).toString().trim(); \n signal2Status = trader.fileReadAll(signal2StatusFile).toString().trim();\n \n\n eventLogger(scriptName + \".signal1Status: \" + signal1Status); \n eventLogger(scriptName + \".signal2Status: \" + signal2Status); \n\n //signalCorrection();\n\n signal1StatusOld = signal1Status; \n signal2StatusOld = signal2Status;\n \n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction executeBuy() {\n var scriptName = \"executeBuy()\";\n eventLogger(scriptName + \".START\");\n\n eventLogger(scriptName + \".STEP 1\");\n var lastCurrencyPrimaryBalance = trader.get(\"Balance\", currencyPrimary);\n var lastCurrencySecondaryBalance = trader.get(\"Balance\", currencySecondary);\n eventLogger(scriptName + \".lastCurrencyPrimaryBalance: \" + lastCurrencyPrimaryBalance);\n eventLogger(scriptName + \".minPrimaryCurrencyBalanceForSale: \" + minPrimaryCurrencyBalanceForSale);\n\n if (lastCurrencyPrimaryBalance > minPrimaryCurrencyBalanceForSale) {\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n lastBuyPrice = trader.get(\"LastMyBuyPrice\");\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n var lastPrice = trader.get(\"LastPrice\");\n eventLogger(scriptName + \".lastPrice: \" + lastPrice);\n var buyAmount = lastCurrencyPrimaryBalance / lastPrice;\n buyAmount = buyAmount - 0.0001;\n eventLogger(scriptName + \".buyAmount: \" + buyAmount);\n\n if (lastSellPrice == 0) {\n trader.buy(currencySecondary + currencyPrimary, buyAmount, lastPrice);\n } else {\n lastSellPrice = trader.get(\"LastMySellPrice\");\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n var primaryBalanceBeforeLastSell = lastSellPrice / ((1 - feeTaker) * 1000) * 1000;\n eventLogger(scriptName + \".primaryBalanceBeforeLastSell: \" + primaryBalanceBeforeLastSell);\n var primaryBalanceOnePromile = primaryBalanceBeforeLastSell / ((1 - feeTaker) * 1000);\n eventLogger(scriptName + \".primaryBalanceOnePromile: \" + primaryBalanceOnePromile);\n var newWantedPrimaryBalance = primaryBalanceOnePromile * 1000 + primaryBalanceOnePromile * feeMaker * 1000;\n eventLogger(scriptName + \".newWantedPrimaryBalance: \" + newWantedPrimaryBalance);\n\n var buyPrice = newWantedPrimaryBalance;\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n eventLogger(scriptName + \".buyPrice: \" + buyPrice);\n\n trader.buy(currencySecondary + currencyPrimary, buyAmount, lastPrice);\n }\n resetSignalSellFilesStatus();\n executeBid = true;\n }\n resetSignalBuyFilesStatus();\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction makeBuy() {\n var scriptName = \"makeBuy()\";\n eventLogger(scriptName + \".START\");\n\n readSignalFiles();\n\n if (canMakeBuy()) {\n trader.cancelBids(currencySecondary + currencyPrimary);\n trader.delay(5, \"executeBuy()\");\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction canMakeSell() {\n var scriptName = \"canMakeSell()\";\n eventLogger(scriptName + \".START\");\n\n var makeSellCondition = false;\n var openAsks = trader.get(\"OpenAsksCount\");\n eventLogger(scriptName + \".openAsks: \" + openAsks);\n if (openAsks > 0) {\n eventLogger(scriptName + \".STEP 1\");\n return true;\n }\n\n eventLogger(scriptName + \".signal2Status: \" + signal2Status);\n \n if (signal2Status == \"TRUE\") {\n eventLogger(scriptName + \".STEP 2\");\n return true;\n }\n\n var lastCurrencySecondaryBalance = trader.get(\"Balance\", currencySecondary);\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n eventLogger(scriptName + \".executeAsk: \" + executeAsk);\n if (executeAsk == true && lastCurrencySecondaryBalance > minSecondaryCurrencyBalanceForSale) {\n eventLogger(scriptName + \".STEP 3\");\n return true;\n }\n\n var lastCurrencyPrimaryBalance = trader.get(\"Balance\", currencyPrimary);\n\n eventLogger(scriptName + \".lastCurrencyPrimaryBalance: \" + lastCurrencyPrimaryBalance);\n eventLogger(scriptName + \".minPrimaryCurrencyBalanceForSale: \" + minPrimaryCurrencyBalanceForSale);\n\n if (executeAsk == true && lastCurrencySecondaryBalance < minSecondaryCurrencyBalanceForSale && openAsks == 0 && lastCurrencyPrimaryBalance > minPrimaryCurrencyBalanceForSale) {\n eventLogger(scriptName + \".STEP 4\");\n executeBid = false;\n executeAsk = false;\n lastSellPrice = trader.get(\"LastMySellPrice\");\n return false;\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction executeSell() {\n var scriptName = \"executeSell()\";\n eventLogger(scriptName + \".START\");\n\n eventLogger(scriptName + \".STEP 1\");\n var lastCurrencyPrimaryBalance = trader.get(\"Balance\", currencyPrimary);\n var lastCurrencySecondaryBalance = trader.get(\"Balance\", currencySecondary);\n\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n eventLogger(scriptName + \".minSecondaryCurrencyBalanceForSale: \" + minSecondaryCurrencyBalanceForSale);\n\n if (lastCurrencySecondaryBalance > minSecondaryCurrencyBalanceForSale) {\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n lastSellPrice = trader.get(\"LastMySellPrice\");\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n var lastPrice = trader.get(\"LastPrice\");\n eventLogger(scriptName + \".lastPrice: \" + lastPrice);\n var sellAmount = lastCurrencySecondaryBalance;\n sellAmount = sellAmount - 0.0001;\n eventLogger(scriptName + \".sellAmount: \" + sellAmount);\n\n if (lastBuyPrice == 0) {\n eventLogger(scriptName + \".lastBuyPrice == 0\");\n trader.sell(currencySecondary + currencyPrimary, sellAmount, lastPrice);\n } else {\n lastBuyPrice = trader.get(\"LastMyBuyPrice\");\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n var primaryBalanceBeforeLastBuy = lastBuyPrice / ((1 - feeMaker) * 1000) * 1000;\n eventLogger(scriptName + \".primaryBalanceBeforeLastBuy: \" + primaryBalanceBeforeLastBuy);\n var primaryBalanceOnePromile = primaryBalanceBeforeLastBuy / ((1 - feeMaker) * 1000);\n eventLogger(scriptName + \".primaryBalanceOnePromile: \" + primaryBalanceOnePromile);\n var newWantedPrimaryBalance = primaryBalanceOnePromile * 1000 + primaryBalanceOnePromile * feeTaker * 1000;\n eventLogger(scriptName + \".newWantedPrimaryBalance: \" + newWantedPrimaryBalance);\n var sellPrice = newWantedPrimaryBalance;\n\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n eventLogger(scriptName + \".lastPrice: \" + lastPrice);\n eventLogger(scriptName + \".sellPrice: \" + sellPrice);\n\n trader.sell(currencySecondary + currencyPrimary, sellAmount, lastPrice);\n }\n resetSignalBuyFilesStatus();\n executeAsk = true;\n }\n resetSignalSellFilesStatus();\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction makeSell() {\n var scriptName = \"makeSell()\";\n eventLogger(scriptName + \".START\");\n readSignalFiles();\n if (canMakeSell()) {\n trader.cancelAsks(currencySecondary + currencyPrimary);\n trader.delay(5, \"executeSell()\");\n }\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction resetSignalBuyFilesStatus() {\n var scriptName = \"resetSignalBuyFilesStatus()\";\n eventLogger(scriptName + \".START\");\n\n trader.fileWrite(signal1StatusFile, \"FALSE\"); \n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction resetSignalSellFilesStatus() {\n var scriptName = \"resetSignalSellFilesStatus()\";\n eventLogger(scriptName + \".START\");\n\n trader.fileWrite(signal2StatusFile, \"FALSE\"); \n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction canMakeBuy() {\n var scriptName = \"canMakeBuy()\";\n eventLogger(scriptName + \".START\");\n\n var makeBuyCondition = false;\n var openBids = trader.get(\"OpenBidsCount\");\n eventLogger(scriptName + \".openBids: \" + openBids);\n\n if (openBids > 0) {\n eventLogger(scriptName + \".STEP 1\");\n return true;\n }\n\n\n eventLogger(scriptName + \".signal1Status: \" + signal1Status);\n \n if (signal1Status == \"TRUE\") {\n eventLogger(scriptName + \".STEP 2\");\n return true;\n }\n\n var lastCurrencyPrimaryBalance = trader.get(\"Balance\", currencyPrimary);\n eventLogger(scriptName + \".lastCurrencyPrimaryBalance: \" + lastCurrencyPrimaryBalance);\n eventLogger(scriptName + \".executeBid: \" + executeBid);\n if (executeBid == true && lastCurrencyPrimaryBalance > minPrimaryCurrencyBalanceForSale) {\n eventLogger(scriptName + \".STEP 3\");\n return true;\n }\n\n var lastCurrencySecondaryBalance = trader.get(\"Balance\", currencySecondary);\n eventLogger(scriptName + \".lastCurrencySecondaryBalance: \" + lastCurrencySecondaryBalance);\n eventLogger(scriptName + \".minSecondaryCurrencyBalanceForSale: \" + minSecondaryCurrencyBalanceForSale);\n\n if (executeBid == true && lastCurrencyPrimaryBalance < minPrimaryCurrencyBalanceForSale && openBids == 0 && lastCurrencySecondaryBalance > minSecondaryCurrencyBalanceForSale) {\n eventLogger(scriptName + \".STEP 4\");\n executeBid = false;\n executeAsk = false;\n lastBuyPrice = trader.get(\"LastMyBuyPrice\");\n return false;\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction writeLastBuyPrice() {\n var scriptName = \"writeLastBuyPrice()\";\n eventLogger(scriptName + \".START\");\n\n lastBuyPrice = trader.get(\"LastMyBuyPrice\");\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n trader.fileWrite(lastBuyPriceFile, lastBuyPrice);\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction readLastBuyPrice() {\n var scriptName = \"readLastBuyPrice()\";\n eventLogger(scriptName + \".START\");\n\n lastBuyPrice = parseFloat(trader.fileReadAll(lastBuyPriceFile).toString().trim());\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n if (lastBuyPrice != 0) {\n lastBuyPrice = trader.get(\"LastMyBuyPrice\");\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n }\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction writeLastSellPrice() {\n var scriptName = \"writeLastSellPrice()\";\n eventLogger(scriptName + \".START\");\n\n lastSellPrice = trader.get(\"LastMySellPrice\");\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n trader.fileWrite(lastSellPriceFile, lastSellPrice);\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction readLastSellPrice() {\n var scriptName = \"readLastSellPrice()\";\n eventLogger(scriptName + \".START\");\n\n lastSellPrice = parseFloat(trader.fileReadAll(lastSellPriceFile).toString().trim());\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n if (lastSellPrice != 0) {\n lastSellPrice = trader.get(\"LastMySellPrice\");\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction compareLastBuyPrice() {\n var scriptName = \"compareLastBuyPrice()\";\n eventLogger(scriptName + \".START\");\n\n if (lastBuyPrice != 0) {\n lastBuyPrice = trader.get(\"LastMyBuyPrice\");\n if (lastBuyPrice != lastBuyPriceOld) {\n resetSignalBuyFilesStatus();\n lastBuyPriceOld = lastBuyPrice;\n writeLastBuyPrice();\n }\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction compareLastSellPrice() {\n var scriptName = \"compareLastSellPrice()\";\n eventLogger(scriptName + \".START\");\n\n if (lastSellPrice != 0) {\n lastSellPrice = trader.get(\"LastMySellPrice\");\n if (lastSellPrice != lastSellPriceOld) {\n resetSignalSellFilesStatus();\n lastSellPriceOld = lastSellPrice;\n writeLastSellPrice();\n }\n }\n\n eventLogger(scriptName + \".END\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n\nfunction startEverything() {\n var scriptName = \"startEverything()\";\n eventLogger(scriptName + \".START --------------------------------------\");\n\n readSignalFiles();\n\n eventLogger(scriptName + \".lastBuyPrice: \" + lastBuyPrice);\n if (lastBuyPrice == 0) {\n readLastBuyPrice();\n }\n\n eventLogger(scriptName + \".lastSellPrice: \" + lastSellPrice);\n if (lastSellPrice == 0) {\n readLastSellPrice();\n }\n\n compareLastBuyPrice();\n compareLastSellPrice();\n\n makeBuy();\n makeSell();\n\n eventLogger(scriptName + \".END --------------------------------------\");\n}\n\n//////////////////////////////////////////////////////////////////////////////////////////////\n/////////////// main program ///////////////////\n//////////////////////////////////////////////////////////////////////////////////////////////\n\ntrader.timer(30, \"startEverything()\");\ntrader.timer(15, \"checkTradeExecutionCondition()\");"