-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from GeorgeFreelanceDeveloper/feature/turtle_c…
…trader Feature/turtle ctrader
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
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("Benchmark Symbol", Group ="Filter settings", DefaultValue ="US500")] | ||
public string BenchmarkSymbol {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 | ||
DataSeries benchmarkSymbolClosePrices = MarketData.GetBars(TimeFrame.Daily, BenchmarkSymbol).ClosePrices; | ||
double benchmarkSymbolClose = MarketData.GetBars(TimeFrame.Daily, BenchmarkSymbol).ClosePrices.LastValue; | ||
bool filter = EnableFilter ? benchmarkSymbolClose >= Indicators.SimpleMovingAverage(benchmarkSymbolClosePrices, 200).Result.LastValue: 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(); | ||
} | ||
} | ||
} | ||
} |