This Cbot isn't executing Any trades
This Cbot isn't executing Any trades
03 Oct 2024, 13:48
Hi, Guys,
first of all, this trading strategy is great, you're welcome to use it, it can help you a lot, it worked for me manually
I just learned programming to automate it, and my cbot is not executing any trades.
Here's the code (the price execution part is in Bold) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots {
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class Algorithm: Robot
//sma20
{
[Parameter("Source")]
public DataSeries SourceSma20 {
get;
set;
}
[Parameter("Trade Volume (in units)", DefaultValue = 2000, MinValue = 2000, Step = 2000)]
public double TradeVolume {
get;
set;
} // Trade volume parameter in increments of 1000
[Parameter("Partial Take Profit (in units)", DefaultValue = 1000, MinValue = 1000, Step = 1000)]
public double VolumeToClose {
get;
set;
} // Amount of the volume you want to close
[Parameter("SMA1 Period", Group = "Moving Averages", DefaultValue = 20)]
public int Sma1Period {
get;
set;
}
private SimpleMovingAverage sma20;
//sma100
[Parameter("Source")]
public DataSeries SourceSma100 {
get;
set;
}
[Parameter("SMA2 Period", Group = "Moving Averages", DefaultValue = 100)]
public int Sma2Period {
get;
set;
}
private SimpleMovingAverage sma100;
//Atr
[Parameter("ATR Period", Group = "ATR", DefaultValue = 14)]
public int AtrPeriod {
get;
set;
}
private AverageTrueRange atr;
// Volume Oscillator Parameters
[Parameter("VO Short Term Period", Group = "VO's Volume", DefaultValue = 5)]
public int VoShortTermPeriod {
get;
set;
}
[Parameter("VO Long Term Period", Group = "VO's Volume", DefaultValue = 14)]
public int VoLongTermPeriod {
get;
set;
}
private VolumeOscillator volumeOscillator;
//Didi S
[Parameter("Source", DefaultValue = "Close")]
public string DidiSource { get; set; }
[Parameter("Short MA Period", DefaultValue = 3, Group = "Didi's Confirmation")]
public int ShortMAPeriod {
get;
set;
}
[Parameter("Long MA Period", DefaultValue = 25, Group = "Didi's Confirmation")]
public int LongMAPeriod {
get;
set;
}
DidiS _didiS;
//Vortex
[Parameter("Periods", DefaultValue = 14, Group = "Vortex's Confirmation")]
public int per {
get;
set;
}
[Parameter("HighLight Bubbles", DefaultValue = false)]
public bool bubble { get; set; }
Vortex _vortex;
//Squeeze break
[Parameter("Periods", Group = "Bollinger Bands", DefaultValue = 10)]
public int BollingerPeriod {
get;
set;
}
[Parameter("Standard Dev", Group = "Bollinger Bands", DefaultValue = 2, MinValue = 0)]
public double StDeviation {
get;
set;
}
[Parameter("MA Type", Group = "Bollinger Bands", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType BollingerMaType {
get;
set;
}
[Parameter("MA Period", Group = "Keltner Channels", DefaultValue = 10)]
public int KeltnerPeriod {
get;
set;
}
[Parameter("MA Type", Group = "Keltner Channels", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType KeltnerMaType {
get;
set;
}
[Parameter("ATR Period", Group = "Keltner Channels", DefaultValue = 10)]
public int AtrPeriods {
get;
set;
}
[Parameter("ATR MA Type", Group = "Keltner Channels", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType AtrMaType {
get;
set;
}
[Parameter("BandDistance", Group = "Keltner Channels", DefaultValue = 1.5)]
public double BandDistance {
get;
set;
}
[Parameter("Momentum Periods", DefaultValue = 12)]
public int MomentumPeriods {
get;
set;
}
SqueezeBreak _squeezeBreak;
private Position BuyPosition;
private Position SellPosition;
private double GlobalAtrValueInPips;
protected override void OnStart() {
sma20 = Indicators.SimpleMovingAverage(SourceSma20, Sma1Period);
sma100 = Indicators.SimpleMovingAverage(SourceSma100, Sma2Period);
atr = Indicators.AverageTrueRange(AtrPeriod, MovingAverageType.Simple);
volumeOscillator = Indicators.VolumeOscillator(VoShortTermPeriod, VoLongTermPeriod);
_didiS = Indicators.GetIndicator < DidiS > (DidiSource,ShortMAPeriod, LongMAPeriod);
_vortex = Indicators.GetIndicator < Vortex > (per,bubble);
_squeezeBreak = Indicators.GetIndicator < SqueezeBreak > (BollingerPeriod, StDeviation,
BollingerMaType, KeltnerPeriod, KeltnerMaType, AtrPeriods, AtrMaType, BandDistance, MomentumPeriods);
}
protected override void OnBarClosed() {
// Access previous values for conditions
double vmpLast = _vortex.vmp.Last(1); // VMP of the last candle
double vmmLast = _vortex.vmm.Last(1); // VMM of the last candle
double vmp2ago = _vortex.vmp.Last(2); // VMM of the candle before the last
double vmm2ago = _vortex.vmm.Last(2); // VMM of the candle before the last
double vmp3ago = _vortex.vmp.Last(3); // VMM of the candle before the last
double vmm3ago = _vortex.vmm.Last(3); // VMM of the candle before the last
double didiOutputLast = _didiS.DidiOutput.Last(1); // Didi Output of the last candle
double didiBaseLast = _didiS.DidiBase.Last(1); // Didi Base of the last candle
double didiOutput2ago = _didiS.DidiOutput.Last(2); // Didi Base of the candle before the last
double didiBase2ago = _didiS.DidiBase.Last(2); // Didi Base of the candle before the last
double didiOutput3ago = _didiS.DidiOutput.Last(3); // Didi Base of the candle before the last
double didiBase3ago = _didiS.DidiBase.Last(3); // Didi Base of the candle before the last
double volumeOscillatorValue = volumeOscillator.Result.Last(1); // Volume Oscillator value
double closingPrice = Bars.ClosePrices.Last(0); // Closing price of the last candle
double lastsqueeze = _squeezeBreak.Up.Last(1);
double LastCandleAtr = atr.Result.Last(1) * Symbol.PipSize;
// Check conditions for buying
if (vmpLast > vmmLast &&
(vmm2ago > vmp2ago || vmm3ago > vmp3ago) &&
didiOutputLast > didiBaseLast &&
(didiBase2ago > didiOutput2ago || didiBase3ago > didiOutput3ago) &&
lastsqueeze >= 1 && // Ensure this evaluates to boolean
volumeOscillatorValue > 1 &&
closingPrice > sma20.Result.LastValue &&
closingPrice > sma100.Result.LastValue && Symbol.Spread < 0.9 && Math.Abs(closingPrice - sma20.Result.LastValue) < 1.15 * LastCandleAtr) {
// Execute buy order
TradeResult BuyResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeVolume, "Buy Signal", LastCandleAtr * 1.5, null);
BuyPosition = BuyResult.Position;
GlobalAtrValueInPips = atr.Result.Last(1) * Symbol.PipSize;
}
// Check conditions for selling
else if (vmpLast < vmmLast &&
(vmm2ago < vmp2ago || vmm3ago < vmp3ago) &&
didiOutputLast < didiBaseLast &&
(didiBase2ago < didiOutput2ago || didiBase3ago < didiOutput3ago) &&
lastsqueeze >= 1 && // Ensure this evaluates to boolean
volumeOscillatorValue > 1 &&
closingPrice < sma20.Result.LastValue &&
closingPrice < sma100.Result.LastValue && Symbol.Spread < 0.9 && Math.Abs(closingPrice - sma20.Result.LastValue) < 1.15 * LastCandleAtr ) {
// Execute sell order
TradeResult SellResult = ExecuteMarketOrder(TradeType.Sell, SymbolName, TradeVolume, "Sell Signal", LastCandleAtr * 1.5, null);
SellPosition = SellResult.Position;
GlobalAtrValueInPips = atr.Result.Last(1) * Symbol.PipSize;
}
}
protected override void OnTick() {
ManageOpenPositions();
}
private void ManageOpenPositions() {
if (BuyPosition != null && BuyPosition.Quantity > 0) {
// break even
if (Bars.ClosePrices.Last(0) >= BuyPosition.EntryPrice + (1.0 * GlobalAtrValueInPips)) {
ModifyPosition(BuyPosition, BuyPosition.EntryPrice, null);
}
// Close part of the buy position
if (Bars.ClosePrices.Last(0) >= BuyPosition.EntryPrice + (1.0 * GlobalAtrValueInPips) && BuyPosition.Quantity > VolumeToClose)
{
ClosePosition(BuyPosition, VolumeToClose);
}
// Check for trailing stop loss condition
if (Bars.ClosePrices.Last(0) >= BuyPosition.EntryPrice + (1.5 * GlobalAtrValueInPips)) {
// Set trailing stop loss
ModifyPosition(BuyPosition, null, null, true,StopTriggerMethod.Trade);
}
}
if (SellPosition != null && SellPosition.Quantity > 0) {
//double atrValueInPips = atr.Result.Last(1) * Symbol.PipSize;
// Break even
if (Bars.ClosePrices.Last(0) <= SellPosition.EntryPrice - (1.0 * GlobalAtrValueInPips)) {
ModifyPosition(SellPosition, SellPosition.EntryPrice, null);
}
// Close part of the buy position
if (Bars.ClosePrices.Last(0) <= SellPosition.EntryPrice - (1.0 * GlobalAtrValueInPips) && BuyPosition.Quantity > VolumeToClose)
{
ClosePosition(SellPosition, VolumeToClose);
// trailing stop loss condition
if (Bars.ClosePrices.Last(0) <= SellPosition.EntryPrice + (1.5 * GlobalAtrValueInPips)) {
// Set trailing stop loss
ModifyPosition(SellPosition, null, null,true,StopTriggerMethod.Trade);
}
}
}
}
protected override void OnStop() {
// Handle cBot stop here
}
}
}
Replies
samyelzallat
07 Oct 2024, 07:39
( Updated at: 07 Oct 2024, 12:05 )
RE: This Cbot isn't executing Any trades
PanagiotisCharalampous said:
Hi there,
It will be hard to get help by just throwing dozens of lines of code and ask for people why it doesn't work. This is because readers don't know what you expect the code to do and what it does instead. Even if they spend time interrogating you, then they will need to spend hours to figure out what you did wrong in your logic. Not many people have this surplus of time.
Did you try debugging your strategy first? Did you place a break point at the line where the conditions are evaluated to check what are the values when a trade is expected to be placed? It will help you understand why the trade is not placed when you expect it to do so.
You need to narrow down the problem to a question that can be answered by somebody in 10-15 minutes before asking for help.
Best regards,
Panagiotis
Thanks a lot,
I will make an other post with issues Iam still facing and keep it as clear as I can
@samyelzallat
PanagiotisCharalampous
04 Oct 2024, 05:26
Hi there,
It will be hard to get help by just throwing dozens of lines of code and ask for people why it doesn't work. This is because readers don't know what you expect the code to do and what it does instead. Even if they spend time interrogating you, then they will need to spend hours to figure out what you did wrong in your logic. Not many people have this surplus of time.
Did you try debugging your strategy first? Did you place a break point at the line where the conditions are evaluated to check what are the values when a trade is expected to be placed? It will help you understand why the trade is not placed when you expect it to do so.
You need to narrow down the problem to a question that can be answered by somebody in 10-15 minutes before asking for help.
Best regards,
Panagiotis
@PanagiotisCharalampous