Description
sss
using cAlgo.API;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class GridTrader : Robot
{
private double mLevelSize;
private bool mClosing;
private double mBuyPrice;
private double mSellPrice;
private double mExitPrice;
private double mLevelCount;
private long magic = 123456; // Set your desired magic number
[Parameter("Level Points", DefaultValue = 100)]
public int LevelPoints { get; set; }
[Parameter("Order Size", DefaultValue = 0.01)]
public double OrderSize { get; set; }
protected override void OnStart()
{
string tradeComment = "GridTrader";
mLevelSize = Symbol.PipSize * LevelPoints;
ResetCounters();
ExecuteGrid();
}
protected override void OnTick()
{
if (mClosing)
{
CloseGrid();
return;
}
if (mBuyPrice == 0 && mSellPrice == 0)
{
ExecuteGrid();
return;
}
double bid = Symbol.Bid;
double ask = Symbol.Ask;
if (mExitPrice > 0)
{
if ((mBuyPrice > 0 && ask <= mExitPrice) || (mSellPrice > 0 && bid >= mExitPrice))
{
CloseGrid();
return;
}
}
if (mBuyPrice > 0 && bid >= mBuyPrice)
{
mLevelCount++;
mSellPrice = 0;
if (mLevelCount >= 4)
{
CloseGrid();
Print("Hit SL, stopping expert");
Stop();
}
else
{
if (ClosePosition(TradeType.Buy))
{
mBuyPrice = OpenPosition(TradeType.Buy);
mExitPrice = OpenPosition(TradeType.Sell);
if (mBuyPrice == 0 || mExitPrice == 0)
CloseGrid();
else
{
mBuyPrice += mLevelSize;
mExitPrice -= mLevelSize;
}
DisplayLevels();
}
}
return;
}
if (mSellPrice > 0 && ask <= mSellPrice)
{
mLevelCount++;
mBuyPrice = 0;
if (mLevelCount >= 4)
{
CloseGrid();
Print("Hit SL, stopping expert");
Stop();
}
else
{
if (ClosePosition(TradeType.Sell))
{
mSellPrice = OpenPosition(TradeType.Sell);
mExitPrice = OpenPosition(TradeType.Buy);
if (mSellPrice == 0 || mExitPrice == 0)
CloseGrid();
else
{
mSellPrice -= mLevelSize;
mExitPrice += mLevelSize;
}
DisplayLevels();
}
}
return;
}
}
private void ResetCounters()
{
mClosing = false;
mBuyPrice = 0;
mSellPrice = 0;
mExitPrice = 0;
mLevelCount = 0;
DisplayLevels();
}
private void ExecuteGrid()
{
ResetCounters();
mBuyPrice = OpenPosition(TradeType.Buy);
if (mBuyPrice == 0)
{
Print("Failed to open grid on buy");
CloseGrid();
return;
}
mBuyPrice += mLevelSize;
mSellPrice = OpenPosition(TradeType.Sell);
if (mSellPrice == 0)
{
Print("Failed to open grid on sell");
CloseGrid();
return;
}
mSellPrice -= mLevelSize;
DisplayLevels();
}
private void CloseGrid()
{
mClosing = !(ClosePosition(TradeType.Buy) && ClosePosition(TradeType.Sell));
if (!mClosing)
ResetCounters();
}
private double OpenPosition(TradeType tradeType)
{
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Symbol.NormalizeVolume(Symbol.QuantityToVolumeInUnits(1)));
var result = ExecuteMarketOrder(tradeType, Symbol, 1000, "GridTrader", magic,30);
if (result.IsSuccessful)
return result.Position.EntryPrice;
return 0;
}
private bool ClosePosition(TradeType tradeType)
{
foreach (var position in Positions)
{
if (position.Label == "GridTrader" && position.TradeType == tradeType)
{
var result = ClosePosition(position);
if (result.IsSuccessful)
return true;
}
}
return false;
}
private void DisplayLevels()
{
DisplayLevel("BuyAt", mBuyPrice, "Buy At");
DisplayLevel("SellAt", mSellPrice, "Sell At");
DisplayLevel("ExitAt", mExitPrice, "Exit At");
}
private void DisplayLevel(string name, double value, string text)
{
string textName = name + "_text";
ChartObjects.RemoveObject(name);
ChartObjects.RemoveObject(textName);
if (value == 0)
return;
var time0 = MarketSeries.OpenTime.LastValue;
var time1 = MarketSeries.OpenTime.Last(1);
ChartObjects.DrawLine(name, time1, value, time0, value, Colors.Yellow);
ChartObjects.DrawText(textName, text + " " + value, StaticPosition.TopLeft, Colors.Yellow);
}
}
}
DA
davidmwabuka
Joined on 07.03.2023
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: TRUE grid .algo
- Rating: 0
- Installs: 976
- Modified: 01/07/2023 12:18
Warning! Running cBots downloaded from this section may lead to financial losses. Use them at your own risk.
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
No comments found.