-
Notifications
You must be signed in to change notification settings - Fork 5
/
TurtleTrendFollowStrategy.cs
94 lines (70 loc) · 3.44 KB
/
TurtleTrendFollowStrategy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class TurtleTrendFollow_cBot : Robot
{
// ********************************
// User defined inputs
// ********************************
// Basic settings
[Parameter("Entry Length", Group ="Basic settings", DefaultValue =20)]
public int EntryLength {get; set;}
[Parameter("Exit Length", Group ="Basic settings", DefaultValue =10)]
public int ExitLength {get; set;}
[Parameter("Risk Percentage", Group ="Basic settings", DefaultValue =1)]
public double RiskPercentage {get; set;}
[Parameter("Atr Multiplier", Group ="Basic settings", DefaultValue =2)]
public int AtrMultiplier {get; set;}
[Parameter("Atr Length", Group ="Basic settings", DefaultValue =20)]
public int AtrLength {get;set;}
// Filter settings
[Parameter("Enable Filter", Group ="Filter settings", DefaultValue =false)]
public bool EnableFilter {get; set;}
[Parameter("Price above SMA(X)", Group ="Filter settings", DefaultValue =200)]
public int SmaLength {get;set;}
[Parameter("RSI > X", Group ="Filter settings", DefaultValue = 0)]
public int RsiValue {get;set;}
protected override void OnStart()
{
}
protected override void OnBarClosed()
{
// **********************************
// Perform calculations and analysis
// **********************************
// Basic
double upperChannel = Indicators.DonchianChannel(EntryLength).Top.LastValue;
double lowerChannel = Indicators.DonchianChannel(ExitLength).Bottom.LastValue;
double closePrice = Bars.ClosePrices.LastValue;
string label = $"TurtleTrendFollow_cBot-{Symbol.Name}";
// Trade amount
double qty = ((RiskPercentage/100) * Account.Balance) / (AtrMultiplier * Indicators.AverageTrueRange(20,MovingAverageType.Simple).Result.LastValue);
double qtyInLots = ((int)(qty /Symbol.VolumeInUnitsStep)) * Symbol.VolumeInUnitsStep;
// Filter
bool priceAboveSMA = closePrice > Indicators.SimpleMovingAverage(Bars.ClosePrices, SmaLength).Result.LastValue;
bool rsiAboveValue = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14).Result.LastValue > RsiValue;
bool filter = EnableFilter ? priceAboveSMA && rsiAboveValue : true;
Position position = Positions.Find(label);
bool isOpenPosition = position != null;
bool buyCondition = closePrice > upperChannel && !isOpenPosition && filter;
bool sellCondition = closePrice < lowerChannel && isOpenPosition;
// ********************************
// Manage trade
// ********************************
// Entry
if(buyCondition)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, qtyInLots, label);
}
// Exit
if(sellCondition)
{
position.Close();
}
Print("Sucessful call OnBarClosed() method.");
}
}
}