ATR based TP
29 Mar 2014, 21:02
Hello Everyone,
I am new to the forum and to C# as well as cTrader coding but am looking forward to advancing my skills by making small cBots.
I have been editing the Sample MA cBot and so far have added a TP, SL, and modified the way it takes trades. However, I am also trying to add the option of a ATR based TP.
So far I have:
[Parameter("Enable ATR based TP", DefaultValue = false)]
public bool enableATR { get; set; }
[Parameter("ATR Period", DefaultValue = 14)]
public int ATRperiod { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
private AverageTrueRange atr;
private const string label = "Sample Trend cBot";
private int Profit;
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeriesMA, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeriesMA, SlowPeriods, MAType);
atr = Indicators.AverageTrueRange(ATRperiod, MAType);
}
protected override void OnBar()
{
var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
var previousSlowMa = slowMa.Result.Last(1);
var previousFastMa = fastMa.Result.Last(1);
if (enableATR = true)
{
Profit = (int)atr.Result.LastValue * 10000;
Print = (Profit);
}
else
{
Profit = TakeProfit;
}
However it does not seem to work for whatever reason. Also if enableATR is set to no, it still somehow displays the results as if it were set to yes.
I am rather confused. I would very much appreciate the help.
Also, are there any guides I should read to further improve my cAlgo skills? C# guides?
Thank you very much,
David
Replies
Tempestshade
29 Mar 2014, 23:23
Another question for you all,
Again as above, I have been modifying the same Trend sample EA.
I have modified so it works on new bars only and was wondering why my EA isn't taking a new trade every time the signal is still valid? For code I have:
if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, Profit);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa)
{
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, Profit);
}
I am probably missing something when it comes to the logic of the trades but for the life of me can't figure it out. I don't want to get rid of the functionality that when the MAs cross a trade is opened, but I also want it to keep 'stacking' trades in the direction of the trend.
I imagine if I added it would work:
if (currentSlowMa <= currentFastMa)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, Profit);
}
else if
*repeat for sells
}
I am going to try it, but would still like some input.
Thanks!
@Tempestshade
Tempestshade
29 Mar 2014, 23:30
RE:
Tempestshade said:
if (currentSlowMa <= currentFastMa) { ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, Profit); } else if *repeat for sells }
That ended up working! On to my next project.
I am looking to add a close on bar open as well. However, my code doesn't seem to want to compile for whatever reason.
var positions = Positions.FindAll(label);
foreach (var position in positions)
{
if (position.Pips >= Profit)
{
ClosePosition(position.Volume);
}
if (position.Pips <= StopLoss)
{
ClosePosition(position.Volume);
}
}
I get the errors : Error CS1502: The best overloaded method match for 'cAlgo.API.Robot.ClosePosition(cAlgo.API.Position)' has some invalid arguments
What exactly is causing this?
Thank you,
David
@Tempestshade
AimHigher
31 Mar 2014, 01:08
/api/reference/robot/closeposition-8635
ClosePosition(position, position.Volume);
or to close the entire position without specifying volume
ClosePosition(position);
@AimHigher

modarkat
29 Mar 2014, 21:53
instead of
you need to write
@modarkat