Max Consecutive Loses Function
Max Consecutive Loses Function
08 Oct 2012, 04:32
Hi
Is there a function available to get how many consecutive loses been made.
Example:
If (consecutiveloses < specifiedconsecutiveloses)
{
execute
}
else
stop()
Thanks
Replies
admin
08 Oct 2012, 15:32
Hello,
There is no internal method for counting consecutive losses but you may code one similar to this post: /forum/calgo-reference-samples/62
Let us know if this helped.
@admin
b0risl33t
08 Oct 2012, 15:40
Thanks for quick reply.
I will try and make that work, i will post my results here
thanks
@b0risl33t
b0risl33t
08 Oct 2012, 16:39
Ok i made this but not sure what i done wrong :( i get a member modefier protected must procede member name and type error. here is 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("Max Losses", DefaultValue = 4, MinValue = 1, MaxValue = 10)] public int consecutiveLosses {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; private _consecutiveLosses 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) { _consecutiveLosses++; } else if (closedPosition.GrossProfit > 0) { _consecutiveLosses = 0; } else if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetTradeCommand()); } else if (_consecutiveLosses < consecutiveLosses) { ExecuteOrder((int)_position.Volume * 2, GetTradeCommand()); } else { Stop() } } private TradeType GetTradeCommand() { var lastIndex = MarketSeries.Close.Count - 1; double close = MarketSeries.Close[lastIndex - 1]; double lastClose = MarketSeries.Close[lastIndex - 2]; if (_movingAverage.Result.IsRising() && close > lastClose) return TradeType.Buy; if (_movingAverage.Result.IsFalling() && close < lastClose) return TradeType.Sell; return _position.TradeType; } protected override void OnError(Error error) { if (error.Code == ErrorCode.BadVolume) Stop(); } } }
Any ideas?
Thanks
@b0risl33t
admin
08 Oct 2012, 17:51
On Line 33:
private _consecutiveLosses
this is not a proper definition because it is missing the type (i.e. int)
Fix to:
private int _consecutiveLosses;
Line 92:
Stop() // missing semicolon
Fix to:
Stop();
Logical error on Line 73:
else if (closedPosition.GrossProfit > 0) { _consecutiveLosses = 0; } else if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetTradeCommand()); }
Please make sure you understand the meaning of an if-else statement:
if(conditionA) { // conditionA is true // if conditionA is not true execution of the program will not reach this line } else if(conditionB) { // this means conditionA is not true and conditionB is true // if conditionA is true or conditionB is not true execution of the program will not reach this line } else { // this means that neither conditionA is true nor conditionB is true // if conditionA or conditionB is true execution of the program will not reach this line }
If you need more help understanding if-else statements please refer to this article: http://msdn.microsoft.com/en-us/library/5011f09h.aspx
Alternatively, you may Google search if-else statements, there are ample examples online.
You may also post a description of what you are trying to accomplish with the if-else statement here.
@admin
b0risl33t
08 Oct 2012, 18:08
Thank you for the reply
I am still learning c++ :P i never even looked at it before 4 days ago. Im still a real newbie but i am willing to learn so thanks for pointing me in the right direction.
What i am trying to achive is for the robot to restart its multiplyer if it has reached say 5 losses, but i want to be able to modify how many losses required with a parameter.
Example
InitialVolume
- 1 lot - Loss
- 2 lots - Loss
- 4 lots - Loss
- 8 Lots - Loss
4 Lots reached now the robot should reset and restart with InitialVolume
Hope this makes sense.
In mean time i will try and figure it out.
Thank You
@b0risl33t
b0risl33t
08 Oct 2012, 19:18
Done It
Maybe not the best but it works :d
[Parameter("Max Losses", DefaultValue = 4, MinValue = 1, MaxValue = 10)] public int Losses {get; set; } private int _consecutiveLosses; protected override void OnPositionClosed(Position closedPosition) { { if (closedPosition.GrossProfit <= closedPosition.Commissions) { _consecutiveLosses++; } else { _consecutiveLosses = 0; } } if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetTradeCommand()); } else if (_consecutiveLosses < Losses) { ExecuteOrder((int)_position.Volume * 2, GetTradeCommand()); } else { OnStart(); } }
@b0risl33t
admin
09 Oct 2012, 11:38
You should be able to accomplish the same by just calling ExecuteOrder with the InitialVolume.
if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, GetTradeCommand()); } else if (_consecutiveLosses < consecutiveLosses) { ExecuteOrder((int)_position.Volume * 2, GetTradeCommand()); } else { ExecuteOrder(InitialVolume, GetTradeCommand()); }
@admin
b0risl33t
08 Oct 2012, 14:47
bump
any ideas yet?
@b0risl33t