6
6
// profit of any kind. Use it at your own risk.
7
7
//
8
8
// -------------------------------------------------------------------------------------------------
9
+
9
10
using System ;
10
11
using cAlgo . API ;
11
12
using cAlgo . API . Indicators ;
12
13
using cAlgo . API . Internals ;
13
- using cAlgo . API ;
14
- using cAlgo . API . Indicators ;
15
14
16
15
namespace cAlgo . Robots
17
16
{
@@ -24,7 +23,7 @@ public class GoldTrendStrategy : Robot
24
23
private RelativeStrengthIndex _rsi ;
25
24
private Supertrend _supertrend ;
26
25
private MacdCrossOver _macd ;
27
-
26
+
28
27
[ Parameter ( "Fast MA Source" , Group = "Fast MA" ) ]
29
28
public DataSeries FastMaSource { get ; set ; }
30
29
@@ -76,9 +75,6 @@ public class GoldTrendStrategy : Robot
76
75
[ Parameter ( "Trailing Stop (Pips)" , DefaultValue = 50 , Group = "Trade" , MinValue = 0 ) ]
77
76
public double TrailingStopInPips { get ; set ; }
78
77
79
- [ Parameter ( "Volume (Lots)" , DefaultValue = 0.01 , Group = "Trade" ) ]
80
- public double VolumeInLots { get ; set ; }
81
-
82
78
[ Parameter ( "Stop Loss (Pips)" , DefaultValue = 100 , Group = "Trade" , MinValue = 1 ) ]
83
79
public double StopLossInPips { get ; set ; }
84
80
@@ -95,15 +91,12 @@ protected override void OnStart()
95
91
96
92
if ( ! UseDynamicVolume )
97
93
_volumeInUnits = Symbol . QuantityToVolumeInUnits ( VolumeInLots ) ;
94
+
98
95
_fastMa = Indicators . SimpleMovingAverage ( FastMaSource , FastMaPeriod ) ;
99
96
_slowMa = Indicators . SimpleMovingAverage ( SlowMaSource , SlowMaPeriod ) ;
100
97
_rsi = Indicators . RelativeStrengthIndex ( Bars . ClosePrices , RsiPeriod ) ;
101
98
_supertrend = Indicators . Supertrend ( SupertrendPeriods , SupertrendMultiplier ) ;
102
99
_macd = Indicators . MacdCrossOver ( Bars . ClosePrices , MacdLongCycle , MacdShortCycle , MacdSignalPeriods ) ;
103
- _volumeInUnits = Symbol . QuantityToVolumeInUnits ( VolumeInLots ) ;
104
- _fastMa = Indicators . SimpleMovingAverage ( FastMaSource , FastMaPeriod ) ;
105
- _slowMa = Indicators . SimpleMovingAverage ( SlowMaSource , SlowMaPeriod ) ;
106
- _rsi = Indicators . RelativeStrengthIndex ( MarketSeries . Close , RsiPeriod ) ;
107
100
108
101
_fastMa . Result . Line . Color = Color . Gold ;
109
102
_slowMa . Result . Line . Color = Color . DarkOrange ;
@@ -136,14 +129,14 @@ protected override void OnBarClosed()
136
129
137
130
protected override void OnTick ( )
138
131
{
132
+ if ( TrailingStopInPips <= 0 )
133
+ return ;
134
+
139
135
foreach ( var position in Positions . FindAll ( Label ) )
140
136
{
141
- double ? newStopLoss ;
142
-
143
- if ( position . TradeType == TradeType . Buy )
144
- newStopLoss = Symbol . Bid - TrailingStopInPips * Symbol . PipSize ;
145
- else
146
- newStopLoss = Symbol . Ask + TrailingStopInPips * Symbol . PipSize ;
137
+ double ? newStopLoss = position . TradeType == TradeType . Buy
138
+ ? Symbol . Bid - TrailingStopInPips * Symbol . PipSize
139
+ : Symbol . Ask + TrailingStopInPips * Symbol . PipSize ;
147
140
148
141
if ( position . TradeType == TradeType . Buy && ( position . StopLoss == null || newStopLoss > position . StopLoss ) )
149
142
ModifyPosition ( position , newStopLoss , position . TakeProfit ) ;
@@ -164,29 +157,12 @@ private double GetTradeVolume()
164
157
return Symbol . NormalizeVolumeInUnits ( units , RoundingMode . ToNearest ) ;
165
158
}
166
159
167
- var inUptrend = _fastMa . Result . LastValue > _slowMa . Result . LastValue ;
168
- var inDowntrend = _fastMa . Result . LastValue < _slowMa . Result . LastValue ;
169
-
170
- if ( inUptrend && _rsi . Result . LastValue < Oversold )
171
- {
172
- ClosePositions( TradeType . Sell ) ;
173
- ExecuteMarketOrder( TradeType . Buy , SymbolName , _volumeInUnits , Label , StopLossInPips , TakeProfitInPips ) ;
174
- }
175
- else if ( inDowntrend && _rsi . Result . LastValue > Overbought )
176
- {
177
- ClosePositions ( TradeType . Buy ) ;
178
- ExecuteMarketOrder ( TradeType . Sell , SymbolName , _volumeInUnits , Label , StopLossInPips , TakeProfitInPips ) ;
179
- }
180
- }
181
-
182
160
private void ClosePositions ( TradeType tradeType )
183
161
{
184
162
foreach ( var position in Positions . FindAll ( Label ) )
185
163
{
186
- if ( position . TradeType != tradeType )
187
- continue ;
188
-
189
- ClosePosition ( position ) ;
164
+ if ( position . TradeType == tradeType )
165
+ ClosePosition ( position ) ;
190
166
}
191
167
}
192
168
}
0 commit comments