Robot that doesn't trade, why?
Robot that doesn't trade, why?
22 Sep 2012, 11:47
Hello,
I have an indicator, kind of looking like the Super Trend Plus indicator in the Algorithms > Indicators list here. It has 2 dotted lines (up and down) that are used as entry/exit point : up signify an up trend, the dotted line is below the price. If the price crosses the up line, it's a trend reversal signal, and then the down dotted line is in use.
During an up trend only up got data, and during a down trend only down. As I was not sure if the indicator is calculated first and feared that when a reversal occure only down will be feed, I used upPrev, which is the previous value (that cannot be different from up when the trend is changing). It is also used to find if up as changed and to change the stop loss on my current order.
So the principle is to open a buy position when the down line is crossed. Then to set the SL at the open line value. If up is crossed, open a sell position and set SL at down value. The buy trade would have been automatically closed by its SL.
SO this is for the theory. I made the following Robot, but it doesn't trade... Can someone help me on this?
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot] public class TrendTrader : Robot { [Parameter("Standard Deviation Multiplier", DefaultValue = 1.2, MinValue = 0.2)] public double StdDevMultiplier { get; set; } [Parameter("Standard Deviation Period", DefaultValue = 80, MinValue = 1)] public int StdDevPeriod { get; set; } [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)] public MovingAverageType MAType { get; set; } [Parameter("Position's size", DefaultValue = 10000, MinValue = 10000)] public int OrderSize { get; set; } private TrendFinder trendFinder; private double upPrev; private double downPrev; private Position position; protected override void OnStart() { trendFinder = Indicators.GetIndicator(StdDevMultiplier, StdDevPeriod, MAType); } protected override void OnTick() { var up = trendFinder.UpTrend.LastValue; var down = trendFinder.DownTrend.LastValue; if(Trade.IsExecuting) //No operation if waiting for a trade to be executed { return; } if (up > 0) { if (up != upPrev) // if the barrier has changed, change the stop loss { if(position != null && position.TradeType == TradeType.Buy) { Trade.ModifyPosition(position, up, null); } upPrev = up; } if (upPrev > Symbol.Bid) // if up is crossed, open sell { Trade.CreateMarketOrder(TradeType.Sell, Symbol, OrderSize); } } else if (down > 0) { if (down != downPrev) // if the barrier has changed, change the stop loss { if(position != null && position.TradeType == TradeType.Sell) { Trade.ModifyPosition(position, down, null); } downPrev = down; } if (downPrev < Symbol.Bid) // if down is crossed, open buy { Trade.CreateMarketOrder(TradeType.Buy, Symbol, OrderSize); } } } protected override void OnStop() { // Put your deinitialization logic here } protected override void OnPositionOpened(Position openedPosition) { position = openedPosition; //Change position state if a position is opened and confirmed. } } }
Replies
PsykotropyK
25 Sep 2012, 08:55
upPrev & downPrev are set as the last existing up or down line. They are set up at every changes in the line values, and should be kept (if my code is good) for the next tick.
Anyway, Ill try with Print.
About print, I have a problem, on a OnBar, I try to get open and close values of the bar. So I use the following code :
protected override void OnBar() { double cl = MarketSeries.Close[MarketSeries.Close.Count - 1]; double op = MarketSeries.Open[MarketSeries.Open.Count - 1]; Print("cl: {0}", cl); Print("op: {0}", op); }
The result is that cl and op got same values, the open price (see screen). How can it be???? To be more precise :
@PsykotropyK
admin
25 Sep 2012, 09:47
The close is equal to the open because OnBar is executed at the begining of the current bar, so the close value is the same as the open, as well as the high and the low. What you need to do is investigate the previous bar.
You can test it with this code:
// Previous Bar Index int previousIndex = MarketSeries.Close.Count - 2; // Previous Bar Open Time Print("Time: {0}", MarketSeries.OpenTime[previousIndex]); double cl = MarketSeries.Close[previousIndex]; double op = MarketSeries.Open[previousIndex]; double high = MarketSeries.High[previousIndex]; double low = MarketSeries.Low[previousIndex]; Print("cl: {0}", cl); Print("op: {0}", op); Print("high: {0}", high); Print("low: {0}", low); // Current Bar // The Bar just started so all values are equal to open cl = MarketSeries.Close.LastValue; op = MarketSeries.Open.LastValue; high = MarketSeries.High.LastValue; low = MarketSeries.Low.LastValue; Print("Time: {0}", MarketSeries.OpenTime.LastValue); Print("cl: {0}", cl); Print("op: {0}", op); Print("high: {0}", high); Print("low: {0}", low);
@admin
admin
24 Sep 2012, 19:03
Hello,
We will probably have to take a look at the indicator so that we can properly assist you.
Nonetheless, if based on the indicator SuperTrend uploaded on our site, I can tell that these two conditions will never be met:
if (upPrev > Symbol.Bid) -> upPrev is set to the UpTrend of SuperTrend which is always below the price. UpTrend serves as a stop loss in a Buy Trade.
if (downPrev < Symbol.Bid) -> downPrev is set to the DownTrend of SuperTrend which is always above the price. DownTrend serves as a stop loss in a Sell Trade.
In general, in order to troubleshoot your robots/indicators, you can add Print() statements in between your other code statements, so that for example, you can see if a specific condition is met or if a variable contains the value that it is intended to.
@admin