PT
Topics
01 Jun 2016, 19:27
5726
11
Replies
PTrader
02 Jun 2016, 14:14
RE:
Thank you. I tested cbot with changes, but don't start.
lucian said:
Sorry.
Change ( both OnStart and OnPositionsClosed)
if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))
with:
if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour))
@PTrader
PTrader
02 Jun 2016, 13:03
RE:
Thank you Lucian for your reply, but the cbot don't start.
lucian said:
Try something like this:
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; } private Random random = new Random(); protected override void OnStart() { if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour)) { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialVolume, GetRandomTradeType()); } } 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) { Print("Closed"); 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); } } else { OnStart(); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }
@PTrader
PTrader
06 Jun 2016, 12:09
RE:
Thank you Lucian, but not work.
When I click play the bot start, but at the stop loss or take profit the first position, it don't restart.
lucian said:
@PTrader