Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.36 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using System.Globalization;
  8. using cAlgo.API;
  9. using cAlgo.API.Indicators;
  10. using cAlgo.API.Internals;
  11. using cAlgo.Indicators;
  12.  
  13. namespace cAlgo.Robots
  14. {
  15.     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
  16.     public class SS : Robot
  17.     {
  18.         [Parameter("Label Name)", DefaultValue = "stc")]
  19.         public string inputlabel { get; set; }
  20.  
  21.  
  22.         [Parameter("Time Lookback N (min) ", DefaultValue = 300)]
  23.         public int N_ { get; set; }
  24.  
  25.         [Parameter("Num of High/Low (K) to look ", DefaultValue = 5)]
  26.         public int K_ { get; set; }
  27.  
  28.  
  29.         [Parameter("stDevIndex ", DefaultValue = 3)]
  30.         public int stDevIndex { get; set; }
  31.  
  32.  
  33.         [Parameter("Diff (%) ", DefaultValue = 20)]
  34.         public double diff { get; set; }
  35.  
  36.         [Parameter("Pip Rate (pips) ", DefaultValue = 20)]
  37.         public double pipRate { get; set; }
  38.  
  39.  
  40.         [Parameter("Volume", DefaultValue = 10000)]
  41.         public int Volume { get; set; }
  42.  
  43.         [Parameter("Stop loss pips", DefaultValue = 5)]
  44.         public int stoppips { get; set; }
  45.  
  46.         [Parameter("Take Profit pips", DefaultValue = 50)]
  47.         public int profitpips { get; set; }
  48.  
  49.  
  50.         private bool buysafe = true, sellsafe = true, tradesafe = true;
  51.  
  52.  
  53.         private string label = "";
  54.  
  55.  
  56.  
  57.  
  58. //=======calc variable
  59.         int bar = 1;
  60.  
  61.         List<double> hpt = new List<double>
  62.         {
  63.                     };
  64.  
  65.         List<double> lpt = new List<double>
  66.         {
  67.                     };
  68.  
  69.         protected override void OnStart()
  70.         {
  71.             // Put your initialization logic here
  72.             label = inputlabel + Symbol.Code + TimeFrame;
  73.             Positions.Closed += PositionsClosed;
  74.             Positions.Opened += PositionsOpened;
  75.  
  76.  
  77.  
  78.  
  79. //find bars...
  80.  
  81.             var idxtm = Time.Date;
  82.  
  83.             for (var i = 0; i < MarketSeries.OpenTime.Count; i++)
  84.             {
  85.                 idxtm = MarketSeries.OpenTime.Last(i);
  86.                 if (idxtm.DayOfWeek == DayOfWeek.Wednesday)
  87.                     break;
  88.             }
  89.  
  90.             var lookb = idxtm - TimeSpan.FromMinutes(N_);
  91.             bar = MarketSeries.OpenTime.GetIndexByTime(idxtm) - MarketSeries.OpenTime.GetIndexByTime(lookb);
  92.  
  93.  
  94.         }
  95.  
  96.         protected override void OnTick()
  97.         {
  98.  
  99.  
  100.             var price = MarketSeries.Close.LastValue;
  101.  
  102.  
  103.             //====exit strategy
  104.  
  105.             var allpos = Positions.FindAll(label);
  106.  
  107.             if (allpos.Length > 0)
  108.             {
  109.  
  110.                 foreach (var pos in allpos)
  111.                 {
  112.  
  113.                     if (pos.NetProfit < 0)
  114.                         continue;
  115.                 }
  116.             }
  117.         }
  118.  
  119.  
  120.  
  121.         protected override void OnBar()
  122.         {
  123.             // Put your core logic here
  124.  
  125.  
  126.             // Put your core logic here
  127.  
  128.             var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
  129.             var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
  130.             var price = MarketSeries.Close.LastValue;
  131.  
  132.             buysafe = true;
  133.             sellsafe = true;
  134.  
  135.  
  136. //find highest-series
  137.             List<double> hx = new List<double>
  138.             {
  139.                             };
  140.             for (var i = 0; i < bar; i++)
  141.                 hx.Add(MarketSeries.High.Last(i));
  142.  
  143.             hx.Sort((x, y) => y.CompareTo(x));
  144.  
  145.             hx.RemoveRange(K_ - 1, hx.Count - K_);
  146.  
  147. //find lowest-series
  148.  
  149.             List<double> lx = new List<double>
  150.             {
  151.                             };
  152.             for (var i = 0; i < bar; i++)
  153.                 lx.Add(MarketSeries.Low.Last(i));
  154.  
  155.             lx.Sort((x, y) => x.CompareTo(y));
  156.  
  157.             lx.RemoveRange(K_ - 1, hx.Count - K_);
  158.  
  159.  
  160.  
  161. //average
  162.  
  163.             var avgh = nd(avg(hx));
  164.             var avgl = nd(avg(lx));
  165.  
  166. //stdev
  167.  
  168.  
  169.  
  170.             var sth = nd(stddev(hx));
  171.             var stl = nd(stddev(lx));
  172.             var avgStdev = nd((sth + stl) / 2);
  173.  
  174.  
  175.  
  176. //pipdiff
  177.  
  178.             var pipdiff = Math.Round(((avgh - avgl) / Symbol.PipSize), 1);
  179.  
  180.  
  181. //entry
  182.  
  183.  
  184.  
  185.             if (pipdiff >= pipRate && avgStdev <= stDevIndex)
  186.             {
  187.                 //////////////////////////////////////////////////////////////////////////////////////////////////
  188.  
  189.                 if (1 - (price / avgh) <= diff / 100 && 1 - (price / avgh) > 0)
  190.                 {
  191.  
  192.                     //if (longPosition != null)
  193.                     //    ClosePosition(longPosition);
  194.                     if (shortPosition == null)
  195.                         sell();
  196.                 }
  197.  
  198.                 if (1 - (avgl / price) <= diff / 100 && 1 - (avgl / price) > 0)
  199.                 {
  200.                     //if (shortPosition != null)
  201.                     //    ClosePosition(shortPosition);
  202.  
  203.                     if (longPosition == null)
  204.                         buy();
  205.  
  206.                 }
  207.  
  208.  
  209.  
  210.  
  211.             }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.         }
  221.         protected override void OnStop()
  222.         {
  223.             // Put your deinitialization logic here
  224.         }
  225.  
  226.         //===============
  227.  
  228.  
  229.  
  230.         private void buy()
  231.         {
  232.  
  233.             if (buysafe)
  234.             {
  235.                 var pos = Positions.Find(label, Symbol, TradeType.Sell);
  236.  
  237.                 ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, stoppips, profitpips);
  238.             }
  239.  
  240.         }
  241.  
  242.         private void sell()
  243.         {
  244.             if (sellsafe)
  245.             {
  246.  
  247.                 var pos = Positions.Find(label, Symbol, TradeType.Buy);
  248.  
  249.                 ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, stoppips, profitpips);
  250.             }
  251.         }
  252.  
  253.  
  254.  
  255.         private void PositionsClosed(PositionClosedEventArgs args)
  256.         {
  257.  
  258.             if (args.Position.SymbolCode == Symbol.Code && args.Position.Label == label)
  259.             {
  260.                 //
  261.  
  262.                 tradesafe = true;
  263.             }
  264.         }
  265.  
  266.  
  267.         private void PositionsOpened(PositionOpenedEventArgs args)
  268.         {
  269.             if (args.Position.SymbolCode == Symbol.Code && args.Position.Label == label)
  270.             {
  271.                 cancelallpendingorder();
  272.                 tradesafe = false;
  273.             }
  274.         }
  275.  
  276.         private void cancelallpendingorder()
  277.         {
  278.  
  279.  
  280.             foreach (var order in PendingOrders)
  281.             {
  282.                 if (order.SymbolCode == Symbol.Code && order.Label == label)
  283.                 {
  284.                     Print("cancel existing order");
  285.                     CancelPendingOrder(order);
  286.                 }
  287.             }
  288.         }
  289.  
  290.         private double avg(List<double> series)
  291.         {
  292.             var sum = 0.0;
  293.  
  294.             foreach (var a in series)
  295.                 sum += a;
  296.  
  297.             return (sum / series.Count);
  298.  
  299.         }
  300.  
  301.  
  302.         private double nd(double value)
  303.         {
  304.  
  305.             return Math.Round(value, Symbol.Digits);
  306.  
  307.         }
  308.         //==============
  309.  
  310.  
  311.  
  312.  
  313.         private double stddev(List<double> series)
  314.         {
  315.  
  316.             var avgx = avg(series);
  317.             var sum = 0.0;
  318.  
  319.             for (var i = 0; i < series.Count; i++)
  320.                 sum += Math.Pow(series[i] - avgx, 2.0);
  321.             ;
  322.  
  323.             return (Math.Sqrt(sum / series.Count));
  324.  
  325.         }
  326.     }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement