Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Globalization;
- using cAlgo.API;
- using cAlgo.API.Indicators;
- using cAlgo.API.Internals;
- using cAlgo.Indicators;
- namespace cAlgo.Robots
- {
- [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
- public class SS : Robot
- {
- [Parameter("Label Name)", DefaultValue = "stc")]
- public string inputlabel { get; set; }
- [Parameter("Time Lookback N (min) ", DefaultValue = 300)]
- public int N_ { get; set; }
- [Parameter("Num of High/Low (K) to look ", DefaultValue = 5)]
- public int K_ { get; set; }
- [Parameter("stDevIndex ", DefaultValue = 3)]
- public int stDevIndex { get; set; }
- [Parameter("Diff (%) ", DefaultValue = 20)]
- public double diff { get; set; }
- [Parameter("Pip Rate (pips) ", DefaultValue = 20)]
- public double pipRate { get; set; }
- [Parameter("Volume", DefaultValue = 10000)]
- public int Volume { get; set; }
- [Parameter("Stop loss pips", DefaultValue = 5)]
- public int stoppips { get; set; }
- [Parameter("Take Profit pips", DefaultValue = 50)]
- public int profitpips { get; set; }
- private bool buysafe = true, sellsafe = true, tradesafe = true;
- private string label = "";
- //=======calc variable
- int bar = 1;
- List<double> hpt = new List<double>
- {
- };
- List<double> lpt = new List<double>
- {
- };
- protected override void OnStart()
- {
- // Put your initialization logic here
- label = inputlabel + Symbol.Code + TimeFrame;
- Positions.Closed += PositionsClosed;
- Positions.Opened += PositionsOpened;
- //find bars...
- var idxtm = Time.Date;
- for (var i = 0; i < MarketSeries.OpenTime.Count; i++)
- {
- idxtm = MarketSeries.OpenTime.Last(i);
- if (idxtm.DayOfWeek == DayOfWeek.Wednesday)
- break;
- }
- var lookb = idxtm - TimeSpan.FromMinutes(N_);
- bar = MarketSeries.OpenTime.GetIndexByTime(idxtm) - MarketSeries.OpenTime.GetIndexByTime(lookb);
- }
- protected override void OnTick()
- {
- var price = MarketSeries.Close.LastValue;
- //====exit strategy
- var allpos = Positions.FindAll(label);
- if (allpos.Length > 0)
- {
- foreach (var pos in allpos)
- {
- if (pos.NetProfit < 0)
- continue;
- }
- }
- }
- protected override void OnBar()
- {
- // Put your core logic here
- // Put your core logic here
- var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
- var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
- var price = MarketSeries.Close.LastValue;
- buysafe = true;
- sellsafe = true;
- //find highest-series
- List<double> hx = new List<double>
- {
- };
- for (var i = 0; i < bar; i++)
- hx.Add(MarketSeries.High.Last(i));
- hx.Sort((x, y) => y.CompareTo(x));
- hx.RemoveRange(K_ - 1, hx.Count - K_);
- //find lowest-series
- List<double> lx = new List<double>
- {
- };
- for (var i = 0; i < bar; i++)
- lx.Add(MarketSeries.Low.Last(i));
- lx.Sort((x, y) => x.CompareTo(y));
- lx.RemoveRange(K_ - 1, hx.Count - K_);
- //average
- var avgh = nd(avg(hx));
- var avgl = nd(avg(lx));
- //stdev
- var sth = nd(stddev(hx));
- var stl = nd(stddev(lx));
- var avgStdev = nd((sth + stl) / 2);
- //pipdiff
- var pipdiff = Math.Round(((avgh - avgl) / Symbol.PipSize), 1);
- //entry
- if (pipdiff >= pipRate && avgStdev <= stDevIndex)
- {
- //////////////////////////////////////////////////////////////////////////////////////////////////
- if (1 - (price / avgh) <= diff / 100 && 1 - (price / avgh) > 0)
- {
- //if (longPosition != null)
- // ClosePosition(longPosition);
- if (shortPosition == null)
- sell();
- }
- if (1 - (avgl / price) <= diff / 100 && 1 - (avgl / price) > 0)
- {
- //if (shortPosition != null)
- // ClosePosition(shortPosition);
- if (longPosition == null)
- buy();
- }
- }
- }
- protected override void OnStop()
- {
- // Put your deinitialization logic here
- }
- //===============
- private void buy()
- {
- if (buysafe)
- {
- var pos = Positions.Find(label, Symbol, TradeType.Sell);
- ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, stoppips, profitpips);
- }
- }
- private void sell()
- {
- if (sellsafe)
- {
- var pos = Positions.Find(label, Symbol, TradeType.Buy);
- ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, stoppips, profitpips);
- }
- }
- private void PositionsClosed(PositionClosedEventArgs args)
- {
- if (args.Position.SymbolCode == Symbol.Code && args.Position.Label == label)
- {
- //
- tradesafe = true;
- }
- }
- private void PositionsOpened(PositionOpenedEventArgs args)
- {
- if (args.Position.SymbolCode == Symbol.Code && args.Position.Label == label)
- {
- cancelallpendingorder();
- tradesafe = false;
- }
- }
- private void cancelallpendingorder()
- {
- foreach (var order in PendingOrders)
- {
- if (order.SymbolCode == Symbol.Code && order.Label == label)
- {
- Print("cancel existing order");
- CancelPendingOrder(order);
- }
- }
- }
- private double avg(List<double> series)
- {
- var sum = 0.0;
- foreach (var a in series)
- sum += a;
- return (sum / series.Count);
- }
- private double nd(double value)
- {
- return Math.Round(value, Symbol.Digits);
- }
- //==============
- private double stddev(List<double> series)
- {
- var avgx = avg(series);
- var sum = 0.0;
- for (var i = 0; i < series.Count; i++)
- sum += Math.Pow(series[i] - avgx, 2.0);
- ;
- return (Math.Sqrt(sum / series.Count));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement