Forum Topics not found
chocoboy 16 Sep 2014, 16:41
This is my first using the cAlgo platform.
In this code, I am trying to code a simple trend following strategy. However, I must be missing something because I get the following error message:
15/03/2013 01:33:30.000 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
Can someone help me and tell me what I am doing wrong please? Thank you
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 Trend : Robot { public int LotSize = 10; protected override void OnStart() { } protected override void OnTick() { double y_range = (MarketSeries.Close.Last(1) - MarketSeries.Open.Last(1)); double trail_stop = 0.2 * Math.Abs(y_range); double hardstop = 0.5 * Math.Abs(y_range); int total_positions = Positions.Count; if (total_positions < 1) { //Buy market entry with hard stop at 50% of t-1 if (y_range > 0 && Symbol.Bid > MarketSeries.Close.Last(1)) { var b_signal = ExecuteMarketOrder(TradeType.Buy, Symbol, LotSize); if (b_signal.IsSuccessful) { var position = b_signal.Position; var stopLoss = position.EntryPrice - hardstop; ModifyPosition(position, stopLoss, null); } } //Sell market entry with hard stop at 50% of t-1 else if (MarketSeries.Open.Last(1) < MarketSeries.Close.Last(1) && y_range < 0) { var s_signal = ExecuteMarketOrder(TradeType.Sell, Symbol, LotSize); if (s_signal.IsSuccessful) { var position = s_signal.Position; var stopLoss = position.EntryPrice + hardstop; ModifyPosition(position, stopLoss, null); } } } if (total_positions > 0) { var position_b = Positions.Find(null, null, TradeType.Buy); var position_s = Positions.Find(null, null, TradeType.Sell); var trailer_b = Symbol.Ask - trail_stop; var trailer_s = Symbol.Bid + trail_stop; if (position_b != null && Symbol.Ask - trail_stop > position_b.StopLoss) { ModifyPosition(position_b, trailer_b, null); } if (position_s != null && Symbol.Bid + trail_stop > position_s.StopLoss) { ModifyPosition(position_s, trailer_s, null); } } //Buy market entry //Sell market entry } protected override void OnStop() { // Put your deinitialization logic here } } }
chocoboy
16 Sep 2014, 16:41
This is my first using the cAlgo platform.
In this code, I am trying to code a simple trend following strategy. However, I must be missing something because I get the following error message:
15/03/2013 01:33:30.000 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
Can someone help me and tell me what I am doing wrong please? Thank you
@chocoboy