Removing Random Command
Removing Random Command
07 Oct 2012, 18:06
Hi
I am trying to remove a random command from Sample Martiangle Robot and replace it with MA isFalling then Sell or MA isRising then Buy command but i have no luck with that. Can anyone please tell me where i am going wrong here is the sample code
using System; using cAlgo.API; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot] public class SampleMartingaleRobot : 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; } private Random random = new Random(); private Position position; protected override void OnStart() { ExecuteOrder(InitialVolume, GetRandomTradeCommand()); } private void ExecuteOrder(int volume, TradeType tradeType) { Trade.CreateMarketOrder(tradeType, Symbol, volume); } protected override void OnPositionOpened(Position openedPosition) { position = openedPosition; Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit)); } protected override void OnPositionClosed(Position closedPosition) { if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetRandomTradeCommand()); } else { ExecuteOrder((int) position.Volume * 2, position.TradeType); } } protected override void OnError(Error error) { if (error.Code == ErrorCode.BadVolume) Stop(); } private TradeType GetRandomTradeCommand() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } private double GetAbsoluteStopLoss(Position position, int stopLossInPips) { return position.TradeType == TradeType.Buy ? position.EntryPrice - Symbol.PipSize * stopLossInPips : position.EntryPrice + Symbol.PipSize * stopLossInPips; } private double GetAbsoluteTakeProfit(Position position, int takeProfitInPips) { return position.TradeType == TradeType.Buy ? position.EntryPrice + Symbol.PipSize * takeProfitInPips : position.EntryPrice - Symbol.PipSize * takeProfitInPips; } } }
Replies
b0risl33t
07 Oct 2012, 23:36
Cant figure this out what am i doin wrong
Cant figure out why i get else as an error here. And is closedPosition.TradeType will tell me what was the trade type on last position?
if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, closedPosition.TradeType); } else if (closedPosition.TradeType == TradeType.Buy); { ExecuteOrder((int) position.Volume * 2, TradeType.Sell); } else { ExecuteOrder((int) position.Volume * 2, TradeType.Buy); }
@b0risl33t
admin
08 Oct 2012, 11:47
Just in case here are the answers.
For the first post - Sample Martingale Robot modification to use Moving Average IsRising/IsFalling look at the following code:
using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { [Robot] public class MartingaleMA : Robot { [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)] public int InitialVolume { get; set; } [Parameter("Stop Loss", DefaultValue = 40, MinValue = 0)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 40, MinValue = 0)] public int TakeProfit { get; set; } [Parameter("Source")] public DataSeries Source { get; set; } [Parameter("Period", DefaultValue = 14, MinValue = 1)] public int Period { get; set; } [Parameter("Moving Average Type", DefaultValue = MovingAverageType.Simple)] public MovingAverageType MovingAverageType { get; set; } private Position _position; private MovingAverage _movingAverage; protected override void OnStart() { _movingAverage = Indicators.MovingAverage(Source, Period, MovingAverageType); ExecuteOrder(InitialVolume, GetTradeCommand()); } private void ExecuteOrder(int volume, TradeType tradeType) { Trade.CreateMarketOrder(tradeType, Symbol, volume); } protected override void OnPositionOpened(Position openedPosition) { _position = openedPosition; Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit)); } private double GetAbsoluteStopLoss(Position pos, int stopLossInPips) { return pos.TradeType == TradeType.Buy ? pos.EntryPrice - Symbol.PipSize * stopLossInPips : pos.EntryPrice + Symbol.PipSize * stopLossInPips; } private double GetAbsoluteTakeProfit(Position pos, int takeProfitInPips) { return pos.TradeType == TradeType.Buy ? pos.EntryPrice + Symbol.PipSize * takeProfitInPips : pos.EntryPrice - Symbol.PipSize * takeProfitInPips; } protected override void OnPositionClosed(Position closedPosition) { if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetTradeCommand()); } else { ExecuteOrder((int)_position.Volume * 2, _position.TradeType); } } private TradeType GetTradeCommand() { if (_movingAverage.Result.IsRising()) return TradeType.Buy; if (_movingAverage.Result.IsFalling()) return TradeType.Sell; return _position.TradeType; } protected override void OnError(Error error) { if (error.Code == ErrorCode.BadVolume) Stop(); } } }
For the second post, the TradeCommand() method returns TradeType, therefore:
private TradeType TradeCommand() { var lastIndex = MarketSeries.Close.Count - 1; double close = MarketSeries.Close[lastIndex - 1]; double lastClose = MarketSeries.Close[lastIndex - 2]; return close > lastClose ? TradeType.Buy : TradeType.Sell; }
And finally,
the semicolon at the end of the if-statement is producing the error, so remove the semicolon:
else if (closedPosition.TradeType == TradeType.Buy);
@admin
b0risl33t
07 Oct 2012, 21:07
Little Change
I have tryed to modify the code to execute orders Depending on the Last candle close value but still cant get it to work :( please help. Im Still a real noob at this but desperate to learn all this :D
@b0risl33t