Topics
Replies
ChannelTrader
26 Feb 2021, 11:48
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi ChannelTrader,
Your problem is here
if (Positions.Count <= 1 && positions.Last().Pips >= TargetPips)
You cannot call Last() method if there are no elements in the collection.
Best Regards,
Panagiotis
Dear Panagiotis, I edited this reply.
I modified the parameter to
if (Positions.Count <= 1 && positioned.Pips >= TargetPips)
var positioned = Positions.Find(botlabel, Symbol.Name);.
Now I'm getting this error.
It says"The reference for the object is not defined to a object instancy"
@ChannelTrader
ChannelTrader
26 Feb 2021, 11:25
RE:
PanagiotisCharalampous said:
Hi ChannelTrader,
Can you provide your symbol and backtesting dates?
Best Regards,
Panagiotis
Hi Panagiotis, how are you?
Thank you for your reply.
I'm testing on GBPUSD, 20/03/2019, with IfBuyOpen Parameter set as true.
The bot closes all the positions,only on CTrader 3.8, but it still get this warning in log.
@ChannelTrader
ChannelTrader
25 Feb 2021, 15:31
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi ChannelTrader,
I tried this on backtesting and seems to close positions just fine before it stops
Best Regards,
Panagiotis
Mine is throwing an exception Ontick and sometimes OnStop, but I'm using spotware Ctrader 4.0 beta to do the backtest, should I test it on cTrader 3.8 only?
@ChannelTrader
ChannelTrader
25 Feb 2021, 14:39
( Updated at: 25 Feb 2021, 15:14 )
RE:
PanagiotisCharalampous said:
Hi ChannelTrader,
To help you solve it, you need to help me reproduce it. So I need the following
1) The complete cBot code
2) cBot parameters
3) Backtesting dates and the date that this happens.
Best Regards,
Panagiotis
Hi Panagiotis, how are you?
The parameters I'm using is set by default an I'm trying to use it on a demo account at live market , but it also happens at any day backtesting, but you can try on 20/03/2019, GBPUSD with parameter IfOpenBuy as true to get the error.
I'm also using a copy of these one for buying with the parameter IfOpenBuy set as true.
I tryed to remove the Stop() method from CloseningPositions() and add if (Positions.Count < 1)
{
Stop();
}
but I'm having the same error.
Here follows the code:
using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;
using System;
using System.Net;
using Telegram.Bot;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class VENDEDOR : Robot
{
[Parameter("Long position?", Group = "Basic Setup", DefaultValue = false)]
public bool IfOpenBuy { get; set; }
[Parameter("First order volume", Group = "Basic Setup", DefaultValue = 1000, MinValue = 1, Step = 1)]
public double InitialVolume { get; set; }
[Parameter("Distance of trades", Group = "Basic Setup", DefaultValue = 10, MinValue = 1)]
public int GridSizeInPoints { get; set; }
[Parameter("Target Pips", DefaultValue = 10)]
public double TargetPips { get; set; }
[Parameter("Profit Factor", Group = "Basic Setup", DefaultValue = 0.4, Step = 0.1)]
public double _profitfactor { get; set; }
//Sets the minimum distance between a pending order and an open position.
[Parameter("Minimum Trade Separation", DefaultValue = 10)]
public double MinPipDistance { get; set; }
[Parameter("Bot Token", DefaultValue = "", Group = "Telegram Notifications")]
public string BotToken { get; set; }
[Parameter("Chat ID", DefaultValue = "", Group = "Telegram Notifications")]
public string ChatID { get; set; }
[Parameter("Message", DefaultValue = "", Group = "Telegram Notifications")]
public string Message { get; set; }
Telegram telegram;
public double TotalLoss { get; set; }
private string botlabel;
double _minEquity;
protected override void OnStart()
{
telegram = new Telegram();
// Set position label to cBot name
botlabel = GetType().Name;
// Normalize volume in case a wrong volume was entered
if (InitialVolume != (InitialVolume = Symbol.NormalizeVolumeInUnits(InitialVolume)))
{
Print("Initial volume entered incorrectly, volume has been changed to ", InitialVolume);
}
_minEquity = Account.Equity;
}
protected override void OnTick()
{
if (IfOpenBuy)
{
if (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == botlabel) == 0)
ExecuteMarketOrder(TradeType.Buy, SymbolName, InitialVolume, botlabel, null, null);
else if (Symbol.Ask <= Positions.FindAll(botlabel, SymbolName, TradeType.Buy).Last().EntryPrice - GridSizeInPoints * Symbol.PipSize)
ExecuteMarketOrder(TradeType.Buy, SymbolName, InitialVolume, botlabel, null, null);
}
else
{
if (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == botlabel) == 0)
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1 * InitialVolume, botlabel, null, null);
else if (Symbol.Bid >= Positions.FindAll(botlabel, SymbolName, TradeType.Sell).Last().EntryPrice + GridSizeInPoints * Symbol.PipSize)
ExecuteMarketOrder(TradeType.Sell, SymbolName, InitialVolume, botlabel, null, null);
}
if (Account.Equity < _minEquity)
_minEquity = Account.Equity;
ClosingPositions();
if (Positions.Count < 1)
{
Stop();
}
}
public void ClosingPositions()
{
var positions = this.Positions.FindAll(botlabel, Symbol.Name);
double netProfit = positions.Sum(x => x.NetProfit);
if (Positions.Count >= 2 && netProfit >= _profitfactor * (Account.Balance - _minEquity))
{
foreach (var position in positions)
{
ClosePosition(position);
telegram.SendTelegram(ChatID, BotToken, Message);
}
}
// if (Positions.Count <= 1 && netProfit >= 1.5 * (Account.Balance - _minEquity))
if (Positions.Count <= 1 && positions.Last().Pips >= TargetPips)
{
foreach (var position in positions)
{
ClosePosition(position);
telegram.SendTelegram(ChatID, BotToken, Message);
}
}
}
protected override void OnBar()
{
}
protected override void OnStop()
{
telegram.SendTelegram(ChatID, BotToken, Message);
ClosingPositions();
}
}
}
@ChannelTrader
ChannelTrader
24 Feb 2021, 17:30
( Updated at: 24 Feb 2021, 17:31 )
RE:
ChannelTrader said:
I'm trying to stop my cBot when all the positions are closed, but sometimes it stop before close all the positions
I'm trying with this code:
public void ClosingPositions()
{
var positions = this.Positions.FindAll(botlabel, Symbol.Name);
double netProfit = positions.Sum(x => x.NetProfit);if (Positions.Count >= 2 && netProfit >= 0.3 * (Account.Balance - _minEquity))
{
foreach (var position in positions)
{
ClosePosition(position);telegram.SendTelegram(ChatID, BotToken, Message);
}
Stop();
}
// if (Positions.Count <= 1 && netProfit >= 1.5 * (Account.Balance - _minEquity))if (Positions.Count <= 1 && positions.Last().Pips >= TargetPips)
{
foreach (var position in positions)
{
ClosePosition(position);telegram.SendTelegram(ChatID, BotToken, Message);
}
Stop();
}
}
Is that wrong?
Dear Panagiotis, thx for your quickly repply.
It is happening when I have 2 or more positions openend. It closes only the first position and the others keep opened.
Can you help me to solve it?
@ChannelTrader
ChannelTrader
22 Feb 2021, 16:31
( Updated at: 22 Feb 2021, 18:02 )
RE:
PanagiotisCharalampous said:
Hi ChannelTrader,
See an example below
double _minEquity; protected override void OnStart() { _minEquity = Account.Equity; } protected override void OnTick() { if (Account.Equity < _minEquity) _minEquity = Account.Equity; }
Best Regards,
Panagiotis
Hi Mr. Panagiotis, thank you for your repply.
Worked perfectly
@ChannelTrader
ChannelTrader
29 May 2020, 17:23
( Updated at: 21 Dec 2023, 09:22 )
The las transaction I did before the money disappear.
I tried for more than 1 week to contact ICmarkets support and I got this answer:
Then I tried to deposit more money on the strategy to solve the problem by myself:
But it still shows only the last deposit.
@ChannelTrader
ChannelTrader
29 May 2020, 17:02
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi diegoyann19,
If you have invested money into a strategy then the funds are moved into a subaccount which is dedicated to following this strategy. You can see the subaccount in the cTrader Copy section. You can read more information here.
Best Regards,
Panagiotis
The problem is, I have more than AUD600 invested in the strategy, but I can only manage half.
The rest of the money disappeared.
@ChannelTrader
ChannelTrader
26 Feb 2021, 12:18
RE:
PanagiotisCharalampous said:
Hi again,
I removed the parameter ClosingPositions() from OnStop() and now I have no error.
Thank you for your help and dedication.
Best regards.
@ChannelTrader