Skip to content

Commit

Permalink
Merge pull request #12 from GeorgeFreelanceDeveloper/feature/turtle_c…
Browse files Browse the repository at this point in the history
…trader

Feature/turtle ctrader
  • Loading branch information
GeorgeFreelanceDeveloper authored Jun 9, 2024
2 parents d02a641 + 225b28e commit cd9d351
Showing 1 changed file with 91 additions and 0 deletions.
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();
}
}
}
}

0 comments on commit cd9d351

Please sign in to comment.