Advertisement
Guest User

Scaling Out Strategy

a guest
Apr 27th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. using PowerLanguage.Function;
  7. using ATCenterProxy.interop;
  8.  
  9.  
  10. /**
  11.  * @author Andrea Corriga
  12.  */
  13. namespace PowerLanguage.Strategy {
  14.     public class PredizioniCET : SignalObject {
  15.         // Costruttore
  16.         public PredizioniCET(object _ctx):base(_ctx){}
  17.        
  18.         // Longs, Short Orders, buy and sells
  19.         private List<IOrderPriced> buy_order_long = new List<IOrderPriced>();
  20.         private List<IOrderPriced> sell_order_long = new List<IOrderPriced>();
  21.         private List<IOrderPriced> buy_order_short = new List<IOrderPriced>();
  22.         private List<IOrderPriced> sell_order_short = new List<IOrderPriced>();
  23.        
  24.         // List with all CSV Rows
  25.         static public List<Csv> Decisions = new List<Csv>();
  26.  
  27.         // To prevent Runtime Errors
  28.         static int CountBar = 0;
  29.        
  30.         const int ShortValue = 0; // CSV Short Value
  31.         const int HoldValue = 1; // CSV Hold Value
  32.         const int LongValue = 2; // CSV Long Value
  33.  
  34.         private string LastDaySeen = "01/01/1970"; // Last day seen, used to avoid the problem to buy more futures in one day
  35.         // CSV PATH
  36.         private string CsvPath = @"D:/PhD-Market-Nets/experiments/050 - Prova GOLD predizioni 5 giorni/predictions/final_decisions/final_decision_prova_small.csv";
  37.        
  38.         /**
  39.          *
  40.          */
  41.         protected override void Create() {
  42.             ReadCsv(this.CsvPath); // Read the CSV
  43.            
  44.             // Foreach element of my CSV I create an Operation inside the list of long/short operations
  45.             for (int i=0; i <= Decisions.Count; i++){
  46.                 buy_order_long.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Enty] Long " + i.ToString(), EOrderAction.Buy)) );
  47.                 sell_order_long.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Exit] Long " + i.ToString(), EOrderAction.Sell, OrderExit.FromEntry(buy_order_long[i]))) );
  48.                
  49.                 sell_order_short.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Enty] Short " + i.ToString(), EOrderAction.SellShort)) );
  50.                 buy_order_short.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Exit] Short " + i.ToString(), EOrderAction.BuyToCover, OrderExit.FromEntry(sell_order_short[i])))  );
  51.             }
  52.            
  53.         }
  54.        
  55.         // Default
  56.         protected override void StartCalc() {
  57.             CountBar = Bars.CurrentBar;
  58.         }
  59.        
  60.         /**
  61.          *
  62.          */
  63.         protected override void CalcBar(){
  64.  
  65.             // Get the current bar date in dd/mm/yyyy format
  66.             string DateBar = Bars.Time[CountBar].ToShortDateString();
  67.            
  68.             /*
  69.              * Since the resolution of the datasets is 5 minutes, many bars will have the same date with this if I enter to make an operation
  70.              * only when the current day is different from the last visa since the dates are in order, I do not need any further checks.
  71.  */
  72.             if(!this.LastDaySeen.Equals(DateBar)){
  73.                
  74.                 // I take the CSV line corresponding to the current day
  75.                 Csv Line = Decisions.Find(x => x.getDateItalian().Equals(DateBar));
  76.                
  77.                 Csv LineExit = Decisions.Find(x => x.getExitDateItalian().Equals(DateBar));
  78.                
  79.                 // Exit condition
  80.                 if(LineExit != null){
  81.                     if(LineExit.getExitDateItalian() == DateBar){
  82.                         switch(LineExit.Decision){
  83.                             case LongValue:
  84.                                 Output.WriteLine("[EXIT] Long date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
  85.                                     Bars.Time[CountBar].ToShortTimeString() + " con ID: " + LineExit.UniqueID.ToString());
  86.                                 this.sell_order_long[LineExit.UniqueID].Send(Bars.Open[0], 1); // Chiudo una long
  87.                                 break;
  88.                             case ShortValue:
  89.                                 Output.WriteLine("[EXIT] Short date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
  90.                                     Bars.Time[CountBar].ToShortTimeString() + " con ID: " + LineExit.UniqueID.ToString() );
  91.                                 this.sell_order_short[LineExit.UniqueID].Send(Bars.Open[0], 1); // Chiudo una short
  92.                                 break;
  93.                         } // Switch
  94.                        
  95.    
  96.                     } // If Line.get_exit...
  97.                 } // if LineExit != null
  98.                
  99.                
  100.                
  101.                 // Entry condition
  102.                 if(Line != null){
  103.                     switch(Line.Decision){
  104.                         case LongValue:
  105.                             Output.WriteLine("[ENTRY] Long date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
  106.                                 Bars.Time[CountBar].ToShortTimeString() + " con ID: " + Line.UniqueID.ToString());
  107.                                 this.buy_order_long[Line.UniqueID].Send(Bars.Open[0], 1); // Effettuo una long
  108.                             break;
  109.                         case ShortValue:
  110.                             Output.WriteLine("[ENTRY] Short date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
  111.                                 Bars.Time[CountBar].ToShortTimeString() + " con ID: " + Line.UniqueID.ToString());
  112.                                 this.buy_order_short[Line.UniqueID].Send(Bars.Open[0], 1); // Effettuo una short
  113.                             break;
  114.                         default:
  115.                             break;
  116.                     }
  117.                    
  118.  
  119.                 } // Line != null
  120.                
  121.                 // Update last day seen
  122.                 this.LastDaySeen = DateBar;
  123.                
  124.             } // if(!this.LastDaySeen.Equals(DateBar))
  125.  
  126.         }
  127.        
  128.        
  129.         //Create Decisions Object by Reading the CSV FILE
  130.         static protected void ReadCsv(string CsvPath){
  131.             using(var reader = new StreamReader(CsvPath)){
  132.                 reader.ReadLine();
  133.                 while (!reader.EndOfStream){
  134.                     var Line = reader.ReadLine();
  135.                     Decisions.Add(new Csv(Line));
  136.                 }
  137.             }
  138.         }
  139.  
  140.     } // class PredizioniCET
  141.    
  142.     /**
  143.      *
  144.      * CSV Class
  145.      */
  146.     public class Csv{
  147.         public string Date { get; set; }
  148.         public int Decision { get; set; }
  149.         public string ExitDate { get; set; }
  150.         public int UniqueID {get; set; }
  151.        
  152.         public Csv(string line){
  153.             var values = line.Split(',');
  154.             this.Date = values[0]; // yyyy-mm-dd
  155.             this.Decision = Int32.Parse(values[1]);
  156.             this.ExitDate = values[2]; // yyyy-mm-dd
  157.             this.UniqueID = Int32.Parse(values[3]);
  158.         }
  159.  
  160.         // Get the dates in Italian Format dd/mm/yyyy
  161.         public String getDateItalian () {
  162.             var values = Date.Split('-');
  163.             return values[2] + "/" + values[1] + "/" + values[0];;         
  164.         }
  165.         public String getExitDateItalian () {
  166.             var values = ExitDate.Split('-');
  167.             return values[2] + "/" + values[1] + "/" + values[0];;         
  168.         }
  169.        
  170.     } // class Csv
  171. } // namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement