Optimization Problem
Optimization Problem
05 May 2020, 14:04
Hi Panagiotis
I am trying to optimise my Algo but every time I start the process, the application freezes. This started after the last update. Before this everything was fine (However this could just be a coincidence, I don't know). Any help would be much appreciated, thanks.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class GwaveAI : Robot
{
[Parameter("PeriodMALong", DefaultValue = 23)]
// The period of the average required to determine the trend
public int Period_MA_Long { get; set; }
[Parameter("PeriodBB", DefaultValue = 22)]
// Period of average bollinger
public int Period_BB { get; set; }
[Parameter("Deviation", DefaultValue = 1.5)]
// Deviation Bollinger Bands
public double Deviation { get; set; }
[Parameter("Reserve", DefaultValue = 28)]
// indent (in points) from the boundaries of the bollinger bands to set the stop loss
public int Reserve { get; set; }
[Parameter("Volume", DefaultValue = 20000)]
// opening position volume
public int vol { get; set; }
[Parameter("Take Profit", DefaultValue = 163)]
public int TakeProfit { get; set; }
[Parameter("Stop Loss", DefaultValue = 139)]
public int StopLoss { get; set; }
[Parameter("MovingAverage Type", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType _paramMAType { get; set; }
[Parameter("KeltnerChannels MA Period", DefaultValue = 24)]
public int _paramKeltnerChannlesMAPeriod { get; set; }
[Parameter("KeltnerChannels ATR Period", DefaultValue = 9)]
public int _paramKeltnerChannlesAtrPeriod { get; set; }
[Parameter("KeltnerChannels BandDistance", DefaultValue = 2.1)]
public double _paramKeltnerChannlesBandDistance { get; set; }
[Parameter("STO %K Periods", DefaultValue = 8, MinValue = -21)]
public int KPeriods { get; set; }
[Parameter("STO %K Slowing", DefaultValue = 1, MinValue = -21)]
public int KSlowing { get; set; }
[Parameter("STO %D Periods", DefaultValue = 2, MinValue = -21)]
public int DPeriods { get; set; }
[Parameter("x1", DefaultValue = 32)]
public int x1 { get; set; }
[Parameter("x2", DefaultValue = -155)]
public int x2 { get; set; }
[Parameter("x3", DefaultValue = -215)]
public int x3 { get; set; }
[Parameter("x4", DefaultValue = 138)]
public int x4 { get; set; }
[Parameter("FastMA", DefaultValue = 5)]
public int FastMA { get; set; }
[Parameter("SlowMA", DefaultValue = 20)]
public int SlowMA { get; set; }
[Parameter("Step", DefaultValue = 12)]
public int Step { get; set; }
private StochasticOscillator _SOC;
private KeltnerChannels _indicatorKeltnerChannels;
private BollingerBands bb;
private MovingAverage sma;
private Position pos;
private bool IsOpenPos = false;
private double sl;
private MacdHistogram macd;
protected override void OnStart()
{
string text = "EAI by Gwave";
base.ChartObjects.DrawText("EAI by Gwave", text, StaticPosition.TopLeft, new Colors?(Colors.Lime));
sma = Indicators.MovingAverage(MarketSeries.Close, Period_MA_Long, MovingAverageType.Simple);
bb = Indicators.BollingerBands(MarketSeries.Close, Period_BB, Deviation, MovingAverageType.Simple);
macd = Indicators.MacdHistogram(SlowMA, FastMA, 3);
_indicatorKeltnerChannels = Indicators.KeltnerChannels(_paramKeltnerChannlesMAPeriod, _paramMAType, _paramKeltnerChannlesAtrPeriod, _paramMAType, _paramKeltnerChannlesBandDistance);
_SOC = Indicators.StochasticOscillator(KPeriods, KSlowing, DPeriods, MovingAverageType.Simple);
}
protected override void OnTick()
{
var top = bb.Top.Last(1);
var bottom = bb.Bottom.Last(1);
var main = bb.Main.Last(1);
var ktop = _indicatorKeltnerChannels.Top.Last(1);
var kbottom = _indicatorKeltnerChannels.Bottom.Last(1);
var kmain = _indicatorKeltnerChannels.Main.Last(1);
var currentD = _SOC.PercentD.Last(0);
var currentK = _SOC.PercentK.Last(0);
var previousD = _SOC.PercentD.Last(1);
var previousK = _SOC.PercentK.Last(1);
double per = percertron();
int last = MarketSeries.Close.Count - 1;
int tradeCount = 0;
tradeCount++;
string Label = tradeCount.ToString();
if (!(MarketSeries.Open[last] == MarketSeries.High[last] && MarketSeries.Open[last] == MarketSeries.Low[last]))
return;
if (IsOpenPos)
{
if ((pos.TradeType == TradeType.Buy && Symbol.Ask > bb.Top[last]) || (pos.TradeType == TradeType.Sell && Symbol.Bid < bb.Bottom[last]))
Trade.Close(pos);
// opening a long position
if (per > 0 && currentK >= currentD && previousK < previousD && currentK < 20)
{
Trade.CreateBuyMarketOrder(Symbol, vol);
IsOpenPos = true;
}
// opening a short position
if (per < 0 && currentK <= currentD && previousK > previousD && currentK > 80)
{
Trade.CreateSellMarketOrder(Symbol, vol);
IsOpenPos = true;
}
}
if (!IsOpenPos)
{
// opening a long position
if (per > 0 && currentK >= currentD && previousK < previousD && currentK < 20)
{
Trade.CreateBuyMarketOrder(Symbol, vol);
IsOpenPos = true;
}
// opening a short position
if (per < 0 && currentK <= currentD && previousK > previousD && currentK > 80)
{
Trade.CreateSellMarketOrder(Symbol, vol);
IsOpenPos = true;
}
}
if (pos != null)
{
foreach (var position in Positions.FindAll("Label"))
{
if (pos.TradeType == TradeType.Buy && Symbol.Ask > top)
{
Trade.Close(pos);
}
else
{
if ((Symbol.Ask > main) && Symbol.Ask > sl + StopLoss * 2 * Symbol.PointSize)
Trade.ModifyPosition(pos, Symbol.Ask - StopLoss * Symbol.PointSize, 50.5);
}
if (pos.TradeType == TradeType.Sell && Symbol.Bid < bottom)
{
Trade.Close(pos);
}
else
{
if ( (Symbol.Bid < main) && Symbol.Bid < sl - StopLoss * 2 * Symbol.PointSize)
Trade.ModifyPosition(pos, Symbol.Bid + StopLoss * Symbol.PointSize, -50.5);
}
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
private double percertron()
{
int last = MarketSeries.Close.Count - 1;
double w1 = x1 - 100;
double w2 = x2 - 100;
double w3 = x3 - 100;
double w4 = x4 - 100;
double a1 = macd.Histogram[last - 1];
double a2 = macd.Histogram[last - 1 - Step];
double a3 = macd.Histogram[last - 1 - Step * 2];
double a4 = macd.Histogram[last - 1 - Step * 3];
return w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4;
}
protected override void OnPositionOpened(Position openedPosition)
{
pos = openedPosition;
Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit));
if (pos.TradeType == TradeType.Buy)
Trade.ModifyPosition(pos, bb.Bottom[bb.Bottom.Count - 1] - Reserve * Symbol.PointSize, 0);
if (pos.TradeType == TradeType.Sell)
Trade.ModifyPosition(pos, bb.Top[bb.Top.Count - 1] + Reserve * Symbol.PointSize, 0);
}
private double GetAbsoluteStopLoss(Position openedPosition, int stopLossInPips)
{
return pos.TradeType == TradeType.Buy ? pos.EntryPrice - Symbol.PipSize * stopLossInPips : pos.EntryPrice + Symbol.PipSize * stopLossInPips;
}
private double GetAbsoluteTakeProfit(Position openedPosition, int takeProfitInPips)
{
return pos.TradeType == TradeType.Buy ? pos.EntryPrice + Symbol.PipSize * takeProfitInPips : pos.EntryPrice - Symbol.PipSize * takeProfitInPips;
}
}
}
Replies
halwaraj723
09 May 2020, 20:12
Awesome article. One can read this kind of article. At the same time, give a second to know about ACMarket Latest APK
@halwaraj723
domesticprojects16
13 May 2020, 21:41
RE:
Great article and learnt alot, Latest Topstore VIP on Ios thank you
@domesticprojects16
PanagiotisCharalampous
05 May 2020, 14:32
Hi Gwave,
Please also provide us with your broker, the optimized cBot parameters ranges and your optimization settings. We need to have all the required information to reproduce the problem.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous