Topics
Replies
MinMin
01 Aug 2018, 15:17
RE:
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 SampleTrendcBot : Robot { [Parameter("MA Type")] public MovingAverageType MAType { get; set; } [Parameter("Open")] public DataSeries SourceSeries { get; set; } //Chose Open only [Parameter("Close")] public DataSeries SourceSeriest { get; set; } [Parameter("PeriodsAR", DefaultValue = 50)] public int PeriodsAR { get; set; } [Parameter("PeriodsRSI", DefaultValue = 14)] public int PeriodsRSI { get; set; } [Parameter("Slow Periods", DefaultValue = 1)] public int SlowPeriods { get; set; } [Parameter("Fast Periods", DefaultValue = 1)] public int FastPeriods { get; set; } [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)] public double Quantity { get; set; } private MovingAverage openMa; private MovingAverage closeMa; private const string label = "Sample Trend cBot"; private AverageTrueRange AverageTR; private RelativeStrengthIndex rsi; protected override void OnStart() { openMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType); closeMa = Indicators.MovingAverage(SourceSeriest, SlowPeriods, MAType); AverageTR = Indicators.AverageTrueRange(PeriodsAR, MAType); rsi = Indicators.RelativeStrengthIndex(SourceSeriest, PeriodsRSI); } protected override void OnTick() { var longPosition = Positions.Find(label, Symbol, TradeType.Buy); var shortPosition = Positions.Find(label, Symbol, TradeType.Sell); var SoLenh = Positions.Count(x => x.SymbolCode == Symbol.Code); var currentOpenMa = openMa.Result.Last(0); var currentCloseMa = closeMa.Result.Last(0); var previousOpenMa = openMa.Result.Last(1); var previousCloseMa = closeMa.Result.Last(1); var previousOpenMa1 = openMa.Result.Last(2); var previousCloseMa1 = closeMa.Result.Last(2); var KhoangCach = AverageTR.Result.Last(1); var RSI = rsi.Result.Last(1); if ((RSI > 65 && ((previousOpenMa1 < previousCloseMa1 && previousOpenMa < previousCloseMa && currentOpenMa > previousCloseMa && currentCloseMa < previousOpenMa && ((previousCloseMa1 - previousOpenMa1) > (previousCloseMa - previousOpenMa)) && Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa - previousOpenMa) < (0.5 * KhoangCach)) || (Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa1 - previousOpenMa1) > KhoangCach && Math.Abs(previousCloseMa - previousOpenMa) < (0.1 * KhoangCach) && previousOpenMa1 < previousCloseMa1 && currentCloseMa < currentOpenMa))) && shortPosition == null) { Print("1"); Print(longPosition); Print("bien long"); Print(shortPosition); Print("bien short"); if (longPosition != null) { Print("2"); ClosePosition(longPosition); Print("3"); } Print("4"); ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label); Print(shortPosition); Print("End test 1"); } else if ((RSI < 35 && ((previousOpenMa1 > previousCloseMa1 && previousOpenMa > previousCloseMa && currentOpenMa < previousCloseMa && currentCloseMa > previousOpenMa && (Math.Abs(previousCloseMa1 - previousOpenMa1) > Math.Abs(previousCloseMa - previousOpenMa)) && Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa - previousOpenMa) < (0.5 * KhoangCach)) || (Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa1 - previousOpenMa1) > KhoangCach && Math.Abs(previousOpenMa - previousCloseMa) < (0.1 * KhoangCach) && previousOpenMa1 > previousCloseMa1 && currentCloseMa > currentOpenMa))) && longPosition == null) { Print("a"); Print(longPosition); Print("bien longa"); Print(shortPosition); Print("bien shorta"); if (shortPosition != null) { Print("test b"); ClosePosition(shortPosition); Print("test c"); } Print("test d"); ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label); Print(shortPosition); Print("End Test 2"); } if (RSI < 30 && shortPosition != null) { Print("test a1"); ClosePosition(shortPosition); Print("test a2"); } else if (RSI > 70 && longPosition != null) { Print("test b1"); ClosePosition(longPosition); Print("test b2"); } foreach (var position in Positions) { if (position.NetProfit < -4) { Print("C1"); ClosePosition(position); } } } private long VolumeInUnits { get { return Symbol.QuantityToVolume(Quantity); } } } }
It run perfectly creat orders and close them but after few hours maybe more, this problem happened and it stop. I hope you can help!
Thanks
@MinMin
MinMin
01 Aug 2018, 10:47
RE:
Panagiotis Charalampous said:
Hi vancong140293,
If the method returns null, it means there is no Position with such label for that Symbol.
Best Regards,
Panagioti
Hi,
You see there is a Position have been created before:
31/07/2018 21:48:56.432 | New cBot, EURGBP, m1 | Executing Market Order to Sell 6000 EURGBP
31/07/2018 21:48:57.229 | New cBot, EURGBP, m1 | → Executing Market Order to Sell 6000 EURGBP SUCCEEDED
I think Positions.Find must find to this position which just have been created?
Thanks,
@MinMin
MinMin
17 Dec 2018, 16:25
RE:
Panagiotis Charalampous said:
Sorry I was missing ")" when i copy . But I used the same code like you and the problem happend!
@MinMin