Right, so in the code below my cbot now only executes a buy order, when lastBarBullish = true and when position.count == 0. Now i want to add an extra condition to this. That would be that the cbot only executes a new buy market order if the last trade it took was a sell market order. How would one code this?
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class NewcBot : Robot
{
public enum RENKO_PRESET
{
None = 0,
// None
Renko,
// Renko
MedianRenko,
// Median Renko
PointO,
// PointO
TurboRenko,
// Turbo Renko
HybridRenko
// Hybrid Renko
}
//
// UltimateRenko indicator settings
//
[Parameter(DefaultValue = 5)]
public double BarSizePips { get; set; }
[Parameter(DefaultValue = RENKO_PRESET.None)]
public RENKO_PRESET Preset { get; set; }
[Parameter(DefaultValue = 50)]
public int OpenOffset { get; set; }
[Parameter(DefaultValue = 50)]
public int ReversalOpenOffset { get; set; }
[Parameter(DefaultValue = 150)]
public int ReversalBarSize { get; set; }
[Parameter(DefaultValue = true)]
public bool ShowWicks { get; set; }
[Parameter(DefaultValue = 300)]
public int BricksToShow { get; set; }
[Parameter(DefaultValue = false)]
public bool ResetOpenOnNewTradingDay { get; set; }
[Parameter(DefaultValue = 0)]
public double FixedOpenPriceForFirstRenko { get; set; }
[Parameter(DefaultValue = false)]
public bool ApplyOffsetToFirstBar { get; set; }
[Parameter(DefaultValue = 1)]
public int OffsetValue { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("TP", DefaultValue = 2)]
public int TP { get; set; }
[Parameter("SL", DefaultValue = 100)]
public int SL { get; set; }
//
// Declare and create an instance of MedianRenko indicator
//
private UltimateRenko renko_indi;
protected override void OnStart()
{
object[] parameterValues =
{
BarSizePips,
// Bar Size (Pips)
Preset,
// Renko preset to use
OpenOffset,
// Open offset % (0 to ...)
ReversalOpenOffset,
// Reversal Open offset % (0 to ...)
ReversalBarSize,
// Reversal bar size % (0 to ...)
ShowWicks,
// Show wicks
BricksToShow,
// Maximum Bars
ResetOpenOnNewTradingDay,
// Reset Open on new trading day
FixedOpenPriceForFirstRenko,
// Use fixed Open price for 1st renko
ApplyOffsetToFirstBar,
// Truncate trailing digits on the first renko
OffsetValue
// Number of digits to truncate
};
// Initialize the UltimateRenko indicator
renko_indi = Indicators.GetIndicator<UltimateRenko>(parameterValues);
// Put your initialization logic here
}
protected override void OnTick()
{
//Buy Position
{
bool lastBarBullish = (renko_indi.Open.Last(1) < renko_indi.Close.Last(1)) && (renko_indi.Open.Last(2) < renko_indi.Close.Last(2));
bool BullishtoBearish = (renko_indi.Open.Last(1) > renko_indi.Close.Last(1)) && (renko_indi.Open.Last(2) < renko_indi.Close.Last(2));
if (lastBarBullish == true && (Positions.Count == 0))
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, "Buy", SL, TP);
//Close Logic
if (BullishtoBearish == true)
{
foreach (Position p in Positions)
{
ClosePosition(p);
}
}
}
// Sell Position
{
bool lastBarBearish = (renko_indi.Open.Last(1) > renko_indi.Close.Last(1)) && (renko_indi.Open.Last(2) > renko_indi.Close.Last(2));
bool BearishtoBullish = (renko_indi.Open.Last(1) < renko_indi.Close.Last(1)) && (renko_indi.Open.Last(2) > renko_indi.Close.Last(2));
if (lastBarBearish == true && (Positions.Count == 0))
ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell", SL, TP);
// Close logic
if (BearishtoBullish == true)
{
foreach (Position p in Positions)
{
ClosePosition(p);
}
}
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
}
}