Topics
Replies
MATRIXTRADER
18 Nov 2021, 15:13
RE:
PanagiotisCharalampous said:
Hi MATRIXTRADER,
One of the improvements you can make to the code is to execute the logic of the cBot on each symbol's BarOpened event. OnBar is executed differently for each symbol and this can cause discrepancies to the results.
Best Regards,
Panagiotis
Thank you very much for your answer
I think this must be the right solution.
unfortunately I looked in the API and looked in the forum and I did not find a concrete example to make my task easier.
Do you have if possible a piece of code to execute the logic on the BarOpened event of a symbol
with many thanks.
@MATRIXTRADER
MATRIXTRADER
16 Nov 2021, 14:22
RE: RE:
MATRIXTRADER said
Hello
does anyone have an answer regarding the difference
backtesting (Tick mode) when changing chart symbol.
where is the error in my code?
Regards
@MATRIXTRADER
MATRIXTRADER
15 Nov 2021, 13:32
RE:
PanagiotisCharalampous said:
Hi MATRIXTRADER,
As far as I can see, you are using the current chart symbol for your indicators.
MA = Indicators.MovingAverage(Bars.ClosePrices, MA_period, MAType); _macd = Indicators.MacdHistogram(LongCycle, ShortCycle, MACDPeriod);
So for every symbol the indicators will be different. Why do you expect the results to be the same?
Best Regards,
Panagiotis
Hello
thank you for your reply
I misspoke
so I give you another example to make me understand.
if I have several instances with different symbols in the same robot then necessarily the graphic symbol will be different for some instances.
for my example I built a simplified robot with 2 identical indicators but 3 different symbols (or 4 ... or 10 ...) because I want to multiply the opportunities with the same robot, necessarily 2 instances will be disconnected from the symbol of the graph.
the backtesting corresponds to reality only for the instance which opens with the correct graphic, but it is false for the 2 other instances which do not open with the graphic which should correspond to their symbol.
and if you interchange the chart or if you choose 1 symbol different from the robot's choice you will never have the same backtesting.
so I think that something important is missing from my code to create a robot with symbols that work independently of each other. (and a backtesting that corresponds for each of the symbols since we cannot open a chart at the same time to each)
thanks in advance.
// ROBOT TRADERMATRIX
//
// \ /
// \ /
// \ /
// (0)!(0)
// --------(@)--------
// / \
// / \
// / \
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using Microsoft.Win32;
using cAlgo.API.Requests;
using System.Text;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class MultyTraDinGPro : Robot
{
[Parameter("Use CHOOSE 1", DefaultValue = true)]
public bool useCHOOSE1 { get; set; }
[Parameter("CHOOSE SYMBOL 1", DefaultValue = "GER40")]
public string Symbol1 { get; set; }
[Parameter("Another Timeframe", DefaultValue = "HOUR")]
public TimeFrame AnotherTimeFrame1 { get; set; }
[Parameter("Label 1", DefaultValue = "dax")]
public string RobotID1 { get; set; }
[Parameter("Volume", DefaultValue = 0.6, MinValue = 0)]
public double Volume1 { get; set; }
[Parameter("SL ", DefaultValue = 60)]
public double SL1 { get; set; }
[Parameter("TP ", DefaultValue = 10)]
public double TP1 { get; set; }
[Parameter("MA_period", DefaultValue = 24)]
public int MA_period1 { get; set; }
[Parameter("MAType", DefaultValue = MovingAverageType.VIDYA)]
public MovingAverageType MAType1 { get; set; }
[Parameter("--------MACD-----------------", DefaultValue = "MACD")]
public string Separator6 { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 30, MinValue = 1)]
public int LongCycle1 { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 2, MinValue = 1)]
public int ShortCycle1 { get; set; }
[Parameter("MACD Period", DefaultValue = 20, MinValue = 1)]
public int MACDPeriod1 { get; set; }
[Parameter("__________________________", DefaultValue = "___________")]
public string Separator1 { get; set; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////
[Parameter("Use CHOOSE 2", DefaultValue = true)]
public bool useCHOOSE2 { get; set; }
[Parameter("CHOOSE SYMBOL 2", DefaultValue = "GBPUSD")]
public string Symbol2 { get; set; }
[Parameter("Another Timeframe", DefaultValue = "HOUR")]
public TimeFrame AnotherTimeFrame2 { get; set; }
[Parameter("Label 1", DefaultValue = "gbp")]
public string RobotID2 { get; set; }
[Parameter("Volume", DefaultValue = 5000, MinValue = 0)]
public double Volume2 { get; set; }
[Parameter("SL ", DefaultValue = 110)]
public double SL2 { get; set; }
[Parameter("TP ", DefaultValue = 20)]
public double TP2 { get; set; }
[Parameter("MA_period", DefaultValue = 100)]
public int MA_period2 { get; set; }
[Parameter("MAType", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType MAType2 { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 21, MinValue = 1)]
public int LongCycle2 { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
public int ShortCycle2 { get; set; }
[Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
public int MACDPeriod2 { get; set; }
[Parameter("__________________________", DefaultValue = "___________")]
public string Separator2 { get; set; }
//////////////////////////////////////////////////////////////////////////////////////////////////////
[Parameter("Use CHOOSE 3", DefaultValue = true)]
public bool useCHOOSE3 { get; set; }
[Parameter("CHOOSE SYMBOL 3", DefaultValue = "XAUUSD")]
public string Symbol3 { get; set; }
[Parameter("Another Timeframe", DefaultValue = "HOUR")]
public TimeFrame AnotherTimeFrame3 { get; set; }
[Parameter("Label 3", DefaultValue = "gold")]
public string RobotID3 { get; set; }
[Parameter("Volume", DefaultValue = 3, MinValue = 0)]
public double Volume3 { get; set; }
[Parameter("SL ", DefaultValue = 120)]
public double SL3 { get; set; }
[Parameter("TP ", DefaultValue = 15)]
public double TP3 { get; set; }
[Parameter("MA_period", DefaultValue = 200)]
public int MA_period3 { get; set; }
[Parameter("MAType", DefaultValue = MovingAverageType.Exponential)]
public MovingAverageType MAType3 { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 32, MinValue = 1)]
public int LongCycle3 { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 14, MinValue = 1)]
public int ShortCycle3 { get; set; }
[Parameter("MACD Period", DefaultValue = 30, MinValue = 1)]
public int MACDPeriod3 { get; set; }
[Parameter("__________________________", DefaultValue = "___________")]
public string Separator20E { get; set; }
private MovingAverage MA1;
private MacdHistogram _macd1;
private MovingAverage MA2;
private MacdHistogram _macd2;
private MovingAverage MA3;
private MacdHistogram _macd3;
private Symbol _symbol1;
private Symbol _symbol2;
private Symbol _symbol3;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
private void InitializeSeries(string symbolCode)
{
_symbol1 = Symbols.GetSymbol(Symbol1);
_symbol2 = Symbols.GetSymbol(Symbol2);
_symbol3 = Symbols.GetSymbol(Symbol3);
}
protected override void OnStart()
{
InitializeSeries(Symbol1);
InitializeSeries(Symbol2);
InitializeSeries(Symbol3);
{
var Series1 = MarketData.GetSeries(_symbol1, AnotherTimeFrame1);
MA1 = Indicators.MovingAverage(Series1.Close, MA_period1, MAType1);
_macd1 = Indicators.MacdHistogram(Series1.Close, LongCycle1, ShortCycle1, MACDPeriod1);
var Series2 = MarketData.GetSeries(_symbol2, AnotherTimeFrame2);
MA2 = Indicators.MovingAverage(Series2.Close, MA_period2, MAType2);
_macd2 = Indicators.MacdHistogram(Series2.Close, LongCycle2, ShortCycle2, MACDPeriod2);
var Series3 = MarketData.GetSeries(_symbol3, AnotherTimeFrame3);
MA3 = Indicators.MovingAverage(Series3.Close, MA_period3, MAType3);
_macd3 = Indicators.MacdHistogram(Series3.Close, LongCycle3, ShortCycle3, MACDPeriod3);
}
}
protected override void OnBar()
{
ManagePositions1();
ManagePositions2();
ManagePositions3();
}
private void ManagePositions1()
{
if (useCHOOSE1 == true)
{
var cBotPositions = Positions.FindAll(RobotID1);
if (cBotPositions.Length >= 1)
return;
{
if (MA1.Result.IsRising() && _macd1.Signal.IsRising())
{
ExecuteMarketOrder(TradeType.Buy, _symbol1, Volume1, RobotID1, SL1, TP1);
{
if (MA1.Result.IsFalling() && _macd1.Signal.IsFalling())
{
ExecuteMarketOrder(TradeType.Sell, Symbol1, Volume1, RobotID1, SL1, TP1);
}
}
}
}
}
}
//////////////////////////////////////////////////////////////////
private void ManagePositions2()
{
if (useCHOOSE2)
{
var cBotPositions = Positions.FindAll(RobotID2);
if (cBotPositions.Length >= 1)
return;
{
if (MA2.Result.IsRising() && _macd2.Signal.IsRising())
{
ExecuteMarketOrder(TradeType.Buy, _symbol2, Volume2, RobotID2, SL2, TP2);
{
if (MA2.Result.IsFalling() && _macd2.Signal.IsFalling())
{
ExecuteMarketOrder(TradeType.Sell, _symbol2, Volume2, RobotID2, SL2, TP2);
}
}
}
}
}
}
//////////////////////////////////////////////////////////////////
private void ManagePositions3()
{
//
if (useCHOOSE3 == true)
{
var cBotPositions = Positions.FindAll(RobotID3);
if (cBotPositions.Length >= 1)
return;
{
if (MA3.Result.IsRising() && _macd3.Signal.IsRising())
{
ExecuteMarketOrder(TradeType.Buy, _symbol3, Volume3, RobotID3, SL3, TP3);
{
if (MA3.Result.IsFalling() && _macd3.Signal.IsFalling())
{
ExecuteMarketOrder(TradeType.Sell, _symbol3, Volume3, RobotID3, SL3, TP3);
}
}
}
}
}
}
}
}
@MATRIXTRADER
MATRIXTRADER
14 Jul 2020, 15:17
RE: RE:
I found this solution and after several tests and comparison it works well.
Add .ClosePrices !!
MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2).ClosePrices, LongCycle, ShortCycle, MACDPeriod);
Thank you for your support.
@MATRIXTRADER
MATRIXTRADER
14 Jul 2020, 13:40
RE:
PanagiotisCharalampous said:
Hi MATRIXTRADER,
Please post the complete cBot code that is generating these errors.
Best Regards,
Panagiotis
Thanks for your help
here is a code snippet
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.None)]
public class NewcBot : Robot
{
[Parameter("Volume", DefaultValue = 10000)]
public double Volume { get; set; }
[Parameter("SL ", DefaultValue = 0)]
public int SL { get; set; }
[Parameter("TP ", DefaultValue = 0)]
public int TP { get; set; }
[Parameter("Another Timeframe MovingAverage", DefaultValue = "Hour4")]
public TimeFrame AnotherTimeFrame1 { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Period", DefaultValue = 125)]
public int Period { get; set; }
[Parameter("Moving Average Type", DefaultValue = MovingAverageType.TimeSeries)]
public MovingAverageType MovingAverageType { get; set; }
[Parameter("----------MACD------------", DefaultValue = "MACD")]
public string Separator1 { get; set; }
[Parameter("Another Timeframe MacdHistogram", DefaultValue = "Hour")]
public TimeFrame AnotherTimeFrame2 { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
public int LongCycle { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
public int ShortCycle { get; set; }
[Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
public int MACDPeriod { get; set; }
private MacdHistogram MACD;
private MovingAverage MA;
protected override void OnStart()
{
MA = Indicators.MovingAverage(MarketData.GetBars(AnotherTimeFrame1).ClosePrices, Period, MovingAverageType);
// MACD = Indicators.MacdHistogram(LongCycle, ShortCycle, MACDPeriod);
MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2), LongCycle, ShortCycle, MACDPeriod);
}
protected override void OnTick()
{
double MA2 = MA.Result[MA.Result.Count - 2];
int bars = MarketSeries.Close.Count - 1;
double cl = MarketSeries.Close[bars - 1];
if (MA2 < cl && MACD.Signal.IsRising())
{
Close(TradeType.Sell);
Open(TradeType.Buy);
}
else if (MA2 > cl && MACD.Signal.IsFalling())
{
Close(TradeType.Buy);
Open(TradeType.Sell);
}
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll("MAMACD", Symbol, tradeType))
//"SampleRSI"
ClosePosition(position);
}
private void Open(TradeType tradeType)
{
var position = Positions.Find("MAMACD", Symbol, tradeType);
if (position == null)
ExecuteMarketOrder(tradeType, Symbol, Volume, "MAMACD", SL, TP);
}
}
}
@MATRIXTRADER
MATRIXTRADER
14 Jul 2020, 09:55
RE:
PanagiotisCharalampous said:
Hi MATRIXTRADER,
Try
MACD = Indicators.MacdHistogram(MarketData.GetBars(AnotherTimeFrame2), LongCycle, ShortCycle, MACDPeriod);
Best Regards,
Panagiotis
Thanks for your help,
I had already tried this method that I had apply to MA in the example above and which works ... unfortunately for MacdHistogram the method does not want to build.Error CS1502: The overloaded method best matches 'cAlgo.API.Internals.IIndicatorsAccessor.MacdHistogram (cAlgo.API.DataSeries, int, int, int)' has invalid arguments
Error CS1503: Argument 1: unable to convert from 'cAlgo.API.Bars' to 'cAlgo.API.DataSeries'
@MATRIXTRADER
MATRIXTRADER
18 Nov 2021, 16:10
RE:
you are great
thanks to you it works perfectly, I will be able to continue to develop my strategy
Thank you for you precious help
Matrix
@MATRIXTRADER