Hi,
Less than a week into coding, so please be gentle with me! I've set up a bot based on some data from youTube videos and carried on the game by adding my own parameters and logic along the way.
The trading logic has mostly come from the sample trend cBot where the terms cancel each other out so that there is only ever one trade in action at a time, for some reason however it's not working this time around and opens a new position each bar, which as I'm sure you can imagine gets ridiculous quite quickly! (Note: a lot of MACD dead wood I've just left dormant)
What am I missing? Here's the full code. Thanks in advance!
using System;
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 NNFXBot6 : Robot
{
[Parameter("Stop Loss ATR Multiplier", Group = "Risk Structure", DefaultValue = 1.5)]
public int SLATRM { get; set; }
[Parameter("Take Profit ATR Multiplier", Group = "Risk Structure", DefaultValue = 1.0)]
public int TPATRM { get; set; }
/*
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
*/
[Parameter("Slow MA Type", Group = "Slow Moving Average")]
public MovingAverageType SMAType { get; set; }
[Parameter("Fast MA Type", Group = "Fast Moving Average")]
public MovingAverageType FMAType { get; set; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSeries { get; set; }
[Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
public int FastPeriods { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
//Create Indicator Variables
private AverageTrueRange atr;
private MacdCrossOver macd;
private const string label = "Sample Trend cBot";
protected override void OnStart()
{
//Load indicators on startup
atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
macd = Indicators.MacdCrossOver(26, 12, 9);
//My own additions
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, FMAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, SMAType);
}
protected override void OnBar()
{
//Calculate trade amount based on ATR
var PrevATR = Math.Round(atr.Result.Last(1) / Symbol.PipSize);
var TradeAmount = (Account.Equity * 0.02) / (1 * PrevATR * Symbol.PipValue);
TradeAmount = Symbol.NormalizeVolumeInUnits(TradeAmount, RoundingMode.Down);
//Two Line Cross Example
/*
var MACDLine = macd.MACD.Last(1);
var PrevMACDLine = macd.MACD.Last(2);
var Signal = macd.Signal.Last(1);
var prevSignal = macd.Signal.Last(2);
*/
var curSlowMa = slowMa.Result.Last(0);
var curFastMa = fastMa.Result.Last(0);
var prevFastMa = fastMa.Result.Last(1);
var prevSlowMa = slowMa.Result.Last(1);
var StopLoss = SLATRM;
var TakeProfit = TPATRM;
var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);
//Check for Continuation Signal
if (curFastMa > curSlowMa && longPosition == null)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeAmount, "EMA x Up SMA", StopLoss * PrevATR, TakeProfit * PrevATR);
}
else if (curFastMa < curSlowMa && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, SymbolName, TradeAmount, "EMA x Down SMA", StopLoss * PrevATR, TakeProfit * PrevATR);
}
}
protected override void OnStop()
{
// Handle cBot stop here
}
}
}