Skip to content

Instantly share code, notes, and snippets.

@michaelnagy
Last active January 26, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelnagy/5d5a944d2a361e686c3fd237f92ca6e1 to your computer and use it in GitHub Desktop.
Save michaelnagy/5d5a944d2a361e686c3fd237f92ca6e1 to your computer and use it in GitHub Desktop.
cAlgo grid hedge bot
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class gridhedge : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
public double balance;
public double lastTradePrice;
public TradeResult lastposition;
public bool opened;
protected override void OnStart()
{
// Get the initial balance
balance = Account.Balance;
Print(balance);
// Start hedge grid
lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "buy", 0, 2);
ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "sell", 0, 2);
Print(lastposition.Position.TakeProfit);
Print(Symbol.Bid);
Print(Symbol.Ask);
Positions.Closed += PositionsClosed;
}
private void PositionsClosed(PositionClosedEventArgs args)
{
var position = args.Position;
Print("Position closed with {0} profit", position.GrossProfit);
if ((position.TakeProfit >= Symbol.Bid) & (position.TakeProfit <= Symbol.Ask))
{
ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "buy", 0, 2);
ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "sell", 0, 2);
Print("last position closed by takeprofits");
}
}
protected override void OnTick()
{
// Check if equity is in profit then shut down the grid cycle
if (Account.Equity >= balance + 2)
{
foreach (var position in Positions)
{
ClosePosition(position);
}
balance = Account.Balance;
Print(balance);
// Start hedge grid
ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "buy", 0, 2);
ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "sell", 0, 2);
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment