152 lines
4.9 KiB
C#
152 lines
4.9 KiB
C#
using System;
|
|
using sharp.json;
|
|
using System.IO;
|
|
using sharp.trading;
|
|
using sharp.trading.cache;
|
|
using sharp.extensions;
|
|
using sharp.json.attributes;
|
|
using sharp.tradebot;
|
|
|
|
namespace BigBot
|
|
{
|
|
[JSONClassPolicy(Policy = JSONPolicy.ATTRIBUTED)]
|
|
public class TargetValueBot : TradingBot<SetupClass>
|
|
{
|
|
[JSONField]
|
|
public string CurrentOrderID;
|
|
|
|
public override void Prepare()
|
|
{
|
|
base.Prepare();
|
|
|
|
TradingBotEnvironment.RegisterPeriodic(Trade,10);
|
|
}
|
|
|
|
public override void Unprepare()
|
|
{
|
|
TradingBotEnvironment.UnregisterPeriodic(Trade);
|
|
|
|
base.Unprepare();
|
|
}
|
|
|
|
|
|
public void Trade()
|
|
{
|
|
TradeBotBalance balanceBase = getBalance(BasicSetup.BaseSymbol);
|
|
TradeBotBalance balanceMarket = getBalance(BasicSetup.MarketSymbol);
|
|
|
|
if (BasicSetup.Enabled){
|
|
Log("TargetValueBot V2 is trading...");
|
|
OrderBook orderBook = BotMarket.getOrderBook();
|
|
|
|
double testVolume = balanceMarket.CurrentBalance;
|
|
if (testVolume == 0){
|
|
testVolume = 1;
|
|
}
|
|
|
|
System.Tuple<double,double> currentValues = orderBook.getVolumePrice(testVolume);
|
|
|
|
// Log("Current Market Values: REBUY: {0,11:####0.00000000} {2} SELL: {1,11:####0.00000000} {2}", currentValues.Item1, currentValues.Item2, BasicSetup.BaseSymbol);
|
|
//1 Log("Current Market Spread: {0,6:##0.00}%", ((currentValues.Item1 / currentValues.Item2)-1)*100);
|
|
double currentMarketValue = currentValues.Item2;
|
|
|
|
double currentUnitValue = currentMarketValue / testVolume;
|
|
double alpha = BasicSetup.TargetPrice / currentUnitValue;
|
|
double beta = Math.Pow(alpha, BasicSetup.TargetExponent);
|
|
|
|
double finalTargetValue = BasicSetup.TargetValue * beta;
|
|
|
|
if (balanceMarket.CurrentBalance <= 0){
|
|
currentMarketValue = 0;
|
|
}
|
|
|
|
|
|
double currentAbsDiff = finalTargetValue - currentMarketValue;
|
|
double currentDiff = currentAbsDiff / finalTargetValue;
|
|
|
|
Log("Current Market Balance: {0,10:###0.0000} {1} Current Market Value: {2,10:###0.0000} {3}", balanceMarket.CurrentBalance, balanceMarket.Currency, currentMarketValue, balanceBase.Currency);
|
|
Log("Current Market Price (bid): {0,10:###0.0000} {1}",currentUnitValue,balanceBase.Currency);
|
|
Log("Current Target Value: {0,10:###0.0000} {1} Target Value Diff: {2,10:###0.0000} {1} [ {3,6:##0.00}% ]", finalTargetValue, balanceBase.Currency, currentAbsDiff,currentDiff * 100);
|
|
|
|
if (!CurrentOrderID.IsNull())
|
|
{
|
|
Order order = getOrder(CurrentOrderID);
|
|
if (!order.IsOpen)
|
|
{
|
|
CurrentOrderID = null;
|
|
}
|
|
}
|
|
|
|
if ((currentDiff > BasicSetup.MarginBuy) || (-currentDiff > BasicSetup.MarginSell))
|
|
{
|
|
if (!CurrentOrderID.IsNull()){
|
|
Log("Canceling old Order before creating new one.");
|
|
TradingEnvironment.DefaultConnection.cancelOrder(CurrentOrderID);
|
|
} else
|
|
{
|
|
double diffMarket = currentAbsDiff / orderBook.CurrentBid;
|
|
|
|
if (diffMarket > 0){
|
|
// Buy
|
|
double window = orderBook.getVolumePrice(diffMarket).Item1 - orderBook.getVolumePrice(diffMarket).Item2;
|
|
double price = orderBook.getVolumePrice(diffMarket).Item1 + (BasicSetup.WindowLevelToBuy * window);
|
|
|
|
if (price >= balanceBase.CurrentBalance){
|
|
diffMarket *= balanceBase.CurrentBalance / price;
|
|
price = orderBook.getVolumePrice(diffMarket).Item1;
|
|
}
|
|
Log("Will buy {0,11:####0.00000000} {1} @ {2,11:####0.00000000} {3} = {4,11:####0.00000000} {3}", diffMarket, BasicSetup.MarketSymbol, orderBook.CurrentAsk, BasicSetup.BaseSymbol, price);
|
|
Order order = createLimitOrder(OrderType.BUY, BasicSetup.MarketSymbol, BasicSetup.BaseSymbol, diffMarket, orderBook.CurrentAsk);
|
|
if (!order.IsNull()){
|
|
CurrentOrderID = order.OrderID;
|
|
}
|
|
} else {
|
|
// SELL
|
|
diffMarket = -diffMarket;
|
|
double window = orderBook.getVolumePrice(diffMarket).Item1 - orderBook.getVolumePrice(diffMarket).Item2;
|
|
double price = orderBook.getVolumePrice(diffMarket).Item1 + (BasicSetup.WindowLevelToBuy * window);
|
|
|
|
Log("Will sell {0,11:####0.00000000} {1} @ {2,11:####0.00000000} {3} = {4,11:####0.00000000} {3}", diffMarket, BasicSetup.MarketSymbol, orderBook.CurrentBid, BasicSetup.BaseSymbol, price);
|
|
Order order = createLimitOrder(OrderType.SELL, BasicSetup.MarketSymbol, BasicSetup.BaseSymbol, diffMarket, orderBook.CurrentBid);
|
|
if (!order.IsNull()){
|
|
CurrentOrderID = order.OrderID;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DumpBalances();
|
|
Save();
|
|
}
|
|
}
|
|
|
|
public override int SchedulingIntervall()
|
|
{
|
|
return 15;
|
|
}
|
|
|
|
|
|
|
|
public class WeightedIndicator{
|
|
public double Volume;
|
|
public double Weight;
|
|
}
|
|
|
|
}
|
|
|
|
public class SetupClass : BasicBotSetup
|
|
{
|
|
public double TargetValue = 0;
|
|
public double TargetPrice = 0;
|
|
public double TargetExponent = 3;
|
|
|
|
public double MarginSell = 0.02;
|
|
public double MarginBuy = 0.02;
|
|
|
|
public double WindowLevelToBuy = 0.1;
|
|
public double WindowLevelToSell = 0.8;
|
|
}
|
|
}
|