Help with my code
Help with my code
30 Mar 2014, 01:35
Hello again,
I figured I would create a whole new topic dedicated to my endeavors in learning to code with cAlgo. So far I really like the language. Very easy to understand.
Again I would really like to know if there are any free resources out there to learn the base language of cAlgo (C# I believe?).
On to my problem.
My current code:
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class MelioraCircle : Robot { [Parameter("TP", DefaultValue = 20)] public int TakeProfit { get; set; } [Parameter("SL", DefaultValue = 50)] public int StopLoss { get; set; } [Parameter("Volume", DefaultValue = 10000)] public int Volume { get; set; } [Parameter("MA Period", DefaultValue = 75)] public int MAPeriod { get; set; } [Parameter("MA Source")] public DataSeries MAsource { get; set; } [Parameter("MA Type")] public MovingAverageType MAType { get; set; } [Parameter("RSI Period", DefaultValue = 14)] public int RSIPeriod { get; set; } private const string label = "Meliora Circle"; private MovingAverage MA; private RelativeStrengthIndex RSI; protected override void OnStart() { // Put your initialization logic here MA = Indicators.MovingAverage(MAsource, MAPeriod, MAType); RSI = Indicators.RelativeStrengthIndex(MAsource, RSIPeriod); } protected override void OnBar() { // Put your core logic here var currentma = MA.Result.Last(0); var previousma = MA.Result.Last(1); var currentrsi = RSI.Result.Last(0); var previousrsi = RSI.Result.Last(1); var longposition = Positions.FindAll(label, Symbol, TradeType.Buy); var sellposition = Positions.FindAll(label, Symbol, TradeType.Sell); var positions = Positions.FindAll(label); foreach (var position in positions) { if (position.Pips >= TakeProfit) { ClosePosition(position); } if (position.Pips <= StopLoss) { ClosePosition(position); } } if (currentrsi > previousrsi && Symbol.Ask > previousma && Symbol.Ask > currentma) { if (longposition == null) ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label); } else if (currentrsi < previousrsi && Symbol.Bid < previousma && Symbol.Ask < currentma) { if (sellposition == null) ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label); } } protected override void OnStop() { // Put your deinitialization logic here } } }
The idea I am trying to put out is that when price breaches above/below the MA and when the RSI is rising/falling and above/below 50 it would take a buy or sell respectively. And go on to TP or SL. However at the moment all my cBot is doing is opening a trade, and then closing the bar after....
Can anyone point me in the right direction? Any help would be greatly appreciated.
Cheers,
David
Replies
Tempestshade
30 Mar 2014, 15:26
( Updated at: 21 Dec 2023, 09:20 )
RE:
Hello again all,
I still can't figure out the problem with my above code so instead I went ahead and tried to code another strategy I found. Very simple
Indicators - 21 EMA
TF - H4
BUY conditions - Place buy orders on bar open when price is above the EMA and when the opening price of bar is greater than the average price (Median?) of the previous bar.
SELL conditions - Place sell orders on bar open when price is below the EMA and when the opening price of bar is less than the average price (Median?) of the previous bar.
namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter("TP", DefaultValue = 40)] public int TakeProfit { get; set; } [Parameter("SL", DefaultValue = 40)] public int StopLoss { get; set; } [Parameter("Volume", DefaultValue = 10000)] public int Volume { get; set; } [Parameter("MA Period", DefaultValue = 21)] public int MAPeriod { get; set; } [Parameter("MA Source")] public DataSeries MAsource { get; set; } private MovingAverage EMA; private const string label = "4H Scalping"; private MedianPrice _Price; private int index; private double avgprice; protected override void OnStart() { // Put your initialization logic here EMA = Indicators.ExponentialMovingAverage(MAsource, MAPeriod); _Price = Indicators.MedianPrice(); avgprice = _Price.Result[index]; } protected override void OnBar() { // Put your core logic here var currentema = EMA.Result.Last(0); _Price = Indicators.MedianPrice(); if (Symbol.Bid > currentema && Symbol.Bid > avgprice) { ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit); avgprice = _Price.Result[index]; } else if (Symbol.Ask < currentema && Symbol.Ask < avgprice) { ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit); avgprice = _Price.Result[index]; } } protected override void OnStop() { // Put your deinitialization logic here } } }
The problem I am having right now is that even when the median price isn't greater/lower it is still taking buy/sells.
In a buy example it should only be taking a trade every time there is a previous bull bar. As you can see by the example I have made a green dot where a trade should have been taken. You will see that when the previous bar is a bearish bar a trade is not taken. As you can see on the right, this was not the case.
As always any help pointing me to where I went wrong would be appreciated.
Cheers,
David
@Tempestshade
Tempestshade
30 Mar 2014, 20:10
Hello again,
I attempted to recode the above but am now receiving an error: = Backtesting stopped: Error #16309803 occurred
As always I have no idea where I am going wrong. I feel like I streamlined the code quite a bit.
namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class fourHScalping : Robot { [Parameter("Trade Comment", DefaultValue = "4H Scalper")] public string label { get; set; } [Parameter("Take Profit", DefaultValue = 20)] public int TakeProfit { get; set; } [Parameter("Stop Loss", DefaultValue = 20)] public int StopLoss { get; set; } [Parameter("Volume", DefaultValue = 10000)] public int Volume { get; set; } [Parameter("Datasource")] public DataSeries Datasource { get; set; } [Parameter("MA Type")] public MovingAverageType typeMA { get; set; } [Parameter("MA Period", DefaultValue = 21)] public int periodMA { get; set; } private MovingAverage mavg; protected override void OnStart() { // Put your initialization logic here mavg = Indicators.MovingAverage(Datasource, periodMA, typeMA); } protected override void OnBar() { // Put your core logic here var cavg = mavg.Result.Last(0); int index = MarketSeries.Median.Count - 1; var avgprice = MarketSeries.Median[index - 1]; if (Symbol.Bid > cavg && Symbol.Bid > avgprice) { ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit); } else if (Symbol.Ask < cavg && Symbol.Ask < avgprice) { ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit); } } protected override void OnStop() { // Put your deinitialization logic here } } }
Thanks,
David
@Tempestshade
Tempestshade
31 Mar 2014, 02:29
Am now having another problem with my code:
Throwing errors:
Error CS1502: The best overloaded method match for 'cAlgo.API.Robot.ClosePosition(cAlgo.API.Position)' has some invalid arguments
Error CS1503: Argument 1: cannot convert from 'cAlgo.API.Position[]' to 'cAlgo.API.Position'
var longposition = Positions.FindAll(label, Symbol, TradeType.Buy); var shortposition = Positions.FindAll(label, Symbol, TradeType.Sell); if (fSignal.Result.HasCrossedAbove(sSignal.Result.LastValue, checkbars) == true && currentrsi > 50) { if (shortposition != null) ClosePosition(shortposition); ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label); } else if (fSignal.Result.HasCrossedBelow(sSignal.Result.LastValue, checkbars) == true && currentrsi < 50) { if (longposition != null) ClosePosition(longposition); ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label); }
@Tempestshade
Tempestshade
31 Mar 2014, 02:41
Stupid mistake changed Positions.FindAll.... to Positions.Find
@Tempestshade
Tempestshade
30 Mar 2014, 04:17
I should add it is doing this repeatedly so every bar it is closing the previous trade, and opening a new trade. Next bar is closes the trade it just opened and opens a new trade....
Thanks,
David
@Tempestshade