Martingale doesnt check the strategy
20 Jun 2018, 00:00
Hi
When he starts the martingale, he doesnt check the strategy. Hitting the stoploss he open a new order without check if strategy = true (In the example below, it doesnt check the crossing of moving averages when loss a trade).
How to give order only to open martingale when there is crossing of moving averages and the loss of the previous trade?
thank you so much
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class TrenDMartingale : Robot
{
[Parameter(DefaultValue = " TrenD ")]
public string cBotLabel { get; set; }
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter()]
public DataSeries SourceSeries { get; set; }
[Parameter("Slow Periods", DefaultValue = 10)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", DefaultValue = 5)]
public int FastPeriods { get; set; }
[Parameter(DefaultValue = 3)]
public int MaxPositions { get; set; }
[Parameter(DefaultValue = 5000)]
public int Volume { get; set; }
[Parameter("Stop Loss 1 (pips)", DefaultValue = 120)]
public int sl1 { get; set; }
[Parameter("Take Profit 1 (pips)", DefaultValue = 30)]
public int tp1 { get; set; }
////////////////////////////////martingale///////////////////////////////////////
[Parameter("Start martingale ", DefaultValue = true)]
public bool Startmartingale { get; set; }
[Parameter("Stop Loss martingale ", DefaultValue = 120)]
public double sl2 { get; set; }
[Parameter("Take Profit martingale ", DefaultValue = 120)]
public double tp2 { get; set; }
[Parameter("Multiplier Ordre ", DefaultValue = 2.1)]
public double Multiplier { get; set; }
[Parameter(" Max Run ", DefaultValue = 41000)]
public int MaxRun { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
private const string label2 = " TrenD.X ";
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
Positions.Closed += OnPositionsClosed;
}
protected override void OnBar()
{
var cBotPositions = Positions.FindAll(cBotLabel);
if (cBotPositions.Length > MaxPositions)
return;
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 (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa)
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, cBotLabel, sl1, tp1);
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa)
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, cBotLabel, sl1, tp1);
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
if (Startmartingale == true)
{
Print("Closed");
var position = args.Position;
if (position.Label != cBotLabel || position.SymbolCode != Symbol.Code)
if (position.Label != label2 || position.SymbolCode != Symbol.Code)
return;
if (position.GrossProfit < 0)
{
{
ExecuteOrder(getNewVolume((int)position.Volume), position.TradeType);
}
}
}
}
private void ExecuteOrder(long volume, TradeType tradeType)
{
var result = ExecuteMarketOrder(tradeType, Symbol, volume, label2, sl2, tp2);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private int getNewVolume(int posVol)
{
var newVol = posVol * 2;
newVol = Math.Min(newVol, MaxRun);
if (newVol == MaxRun)
newVol = Volume;
Print("newVol = {0}", newVol);
return newVol;
}
}
}
Replies
DelFonseca
22 Jun 2018, 21:06
Thank you for your feedback.
I'm not very good with English. The problem is that the code below doesnt cheeck the strategy (crossing moving averages) when a trade comes from the martingale.
private void ExecuteOrder(long volume, TradeType tradeType)
{
var result = ExecuteMarketOrder(tradeType, Symbol, volume, label2, sl2, tp2);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
I wan the position of the martingale's also open at the crossing of moving averages.
Thank you
@DelFonseca

nakw
22 Jun 2018, 20:53
What you ask for is not clear.
but if you want martingale to check if last trade was lose then use "History"
var p= History.FindLast("Label",Symbol); var isLose = (p != null && p.Pips<0); if(isLose) { // do something }@nakw