Scalper Trade Bot

General

The following example shows how to make a simple bot like a scalper-bot. It shows the usage of technologies related to the price calculation and coin positioning.

Installation

  • Open the My documents\HTS\ScriptBots folder.

  • Create a new text file, name it scalper.cs

  • Open the file and paste the source code inside it

  • Save the file and start the HaasBot

  • Create a new custom scripted bot and you should be able to select an item called scalper

Source code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TradeServer.ScriptingDriver.DataObjects;
using TradeServer.ScriptingDriver.Interfaces;
namespace BotScripts
{
public class ScalperBot : IScriptBot
{
private decimal _stoploss = 5.0M;
public string Name
{
get { return "Spot Scalper-bot"; }
}
public ScriptBotType BotType
{
get { return ScriptBotType.SpotBot; }
}
public List<ScriptParameter> GetParameters()
{
List<ScriptParameter> res = new List<ScriptParameter>();
res.Add(new ScriptParameter("Stop-loss", ScriptParameterType.Double, _stoploss.ToString()));
return res;
}
public void SetParameters(Dictionary<string, object> parameters)
{
foreach (var item in parameters) // lets just loop them all
{
if (item.Key == "Stop-loss") { _stoploss = Convert.ToDecimal(item.Value); }
}
}
public void Init()
{
}
public void DoUpdate(ScriptBotContext context)
{
// Check for open orders
if (context.OpenOrders.Count > 0)
{
context.Logger.Log("Open orders detected, update skipped.");
return;
}
// enable the following lines if you like to see run-time details
context.Logger.Log("Last buy: " + context.LastBuyPrice.ToString());
context.Logger.Log("Last sell: " + context.LastSellPrice.ToString());
context.Logger.Log("Fee: " + context.Fee.ToString());
context.Logger.Log("Stop-loss: " + _stoploss.ToString());
// Based on the position, lets see what we can do...
if (context.BotPosition == BotsPosition.BuyLong)
{
context.Logger.Log("The bot its position is: Bought");
if (context.LastBuyPrice == 0.0M)
{
context.Logger.Log("No last prices are known, approving first selling order.");
context.API.PlaceSellOrder(context.TradeAmount, (decimal)context.PriceTick.Bid);
}
else
{
var targetPrice = context.LastBuyPrice;
if (context.Fee > 0.0M)
targetPrice = context.LastBuyPrice + (context.LastBuyPrice / 100.0M * (context.Fee * 2.0M));
context.Logger.Log("Last buying price was " + context.LastBuyPrice.ToString() + " the target sell price is: " +
targetPrice.ToString() + " (now: " + context.PriceTick.Bid.ToString() + ")");
// Normal trading procedure
if (targetPrice < (decimal)context.PriceTick.Bid)
{
context.Logger.Log("Approving selling order.");
context.API.PlaceSellOrder(context.TradeAmount, (decimal)context.PriceTick.Bid);
}
if (_stoploss > 0.0M)
{
targetPrice = context.LastBuyPrice - (context.LastBuyPrice / 100.0M * _stoploss);
if (targetPrice > (decimal)context.PriceTick.Bid)
{
context.Logger.Log("Triggering stop-loss...");
context.API.PlaceSellOrder(context.TradeAmount, (decimal)context.PriceTick.Bid);
}
}
}
}
else if (context.BotPosition == BotsPosition.SellShort)
{
context.Logger.Log("The bot its position is: Sold");
if (context.LastSellPrice == 0.0M)
{
context.Logger.Log("No last prices are known, approving first buying order.");
context.API.PlaceBuyOrder(context.TradeAmount, (decimal)context.PriceTick.Ask);
}
else
{
var targetPrice = context.LastSellPrice;
if (context.Fee > 0.0M)
targetPrice = context.LastSellPrice - (context.LastSellPrice / 100.0M * (context.Fee * 2.0M));
context.Logger.Log("Last selling price was " + context.LastSellPrice.ToString() + " the target buy price is: " +
targetPrice.ToString() + " (now: " + context.PriceTick.Ask.ToString() + ")");
// Normal trading procedure
if (targetPrice > (decimal)context.PriceTick.Ask)
{
context.Logger.Log("Approving buying order.");
context.API.PlaceBuyOrder(context.TradeAmount, (decimal)context.PriceTick.Ask);
}
if (_stoploss > 0.0M)
{
targetPrice = context.LastSellPrice + (context.LastSellPrice / 100.0M * _stoploss);
if (targetPrice < (decimal)context.PriceTick.Bid)
{
context.Logger.Log("Triggering stop-loss...");
context.API.PlaceBuyOrder(context.TradeAmount, (decimal)context.PriceTick.Ask);
}
}
}
}
}
}
}