AM
Topics
Replies
amantalpur007@gmail.com
09 Jul 2017, 19:09
RE:
HI, i want sample martingale cbot to stop doubling my position after 5 consecutive loses. please tell me the coding to do so. i shall be really really grateful to you lucian.
lucian said:
Try backtest with:
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 PTrader : Robot { [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)] public int InitialVolume { get; set; } [Parameter("Stop Loss", DefaultValue = 40)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 40)] public int TakeProfit { get; set; } [Parameter("Start hour", DefaultValue = 11)] public int _start_hour { get; set; } [Parameter("End Hour", DefaultValue = 18)] public int _end_hour { get; set; } public bool trigger; private Random random = new Random(); protected override void OnStart() { trigger = false; Positions.Closed += OnPositionsClosed; } protected override void OnTick() { if ((Server.Time.Hour == _start_hour) && (Positions.Count == 0) && (!trigger)) { ExecuteOrder(InitialVolume, GetRandomTradeType()); trigger = true; } } private void ExecuteOrder(long volume, TradeType tradeType) { var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { if (Server.Time.Hour > _start_hour) { trigger = false; } var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour)) { if (position.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetRandomTradeType()); } else { ExecuteOrder((int)position.Volume * 2, position.TradeType); } } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }
@amantalpur007@gmail.com
amantalpur007@gmail.com
09 Jul 2017, 19:30
RE: RE:
HI, i use Sample MArtingale Cbot. in that cbot. i want this cbot to stop doubling my postion after 5 consecutive loses. please tell me the code to do so. i saw your comment and in that comment you've added MAs. i just want to add a setting so that cbot stops doubling after 5 consecutive loses. my email is amantalpur007@gmail.com. i've inserted code of that cbot. please write the whole coding including my need. i shall be really grateful to you.
// ------------------------------------------------------------------------------------------------- // // This code is a cAlgo API sample. // // This cBot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new // order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will // continue to double the volume amount for all orders created until one of them hits the take Profit. // After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount. // // ------------------------------------------------------------------------------------------------- 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 SampleMartingalecBot : Robot { [Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)] public double InitialQuantity { get; set; } [Parameter("Stop Loss", DefaultValue = 40)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 40)] public int TakeProfit { get; set; } private Random random = new Random(); protected override void OnStart() { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialQuantity, GetRandomTradeType()); } private void ExecuteOrder(double quantity, TradeType tradeType) { var volumeInUnits = Symbol.QuantityToVolume(quantity); var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, GetRandomTradeType()); } else { ExecuteOrder(position.Quantity * 2, position.TradeType); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }tradermatrix said:
@amantalpur007@gmail.com