Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Linq;
- using System.IO;
- using System.Collections.Generic;
- using PowerLanguage.Function;
- using ATCenterProxy.interop;
- /**
- * @author Andrea Corriga
- */
- namespace PowerLanguage.Strategy {
- public class PredizioniCET : SignalObject {
- // Costruttore
- public PredizioniCET(object _ctx):base(_ctx){}
- // Longs, Short Orders, buy and sells
- private List<IOrderPriced> buy_order_long = new List<IOrderPriced>();
- private List<IOrderPriced> sell_order_long = new List<IOrderPriced>();
- private List<IOrderPriced> buy_order_short = new List<IOrderPriced>();
- private List<IOrderPriced> sell_order_short = new List<IOrderPriced>();
- // List with all CSV Rows
- static public List<Csv> Decisions = new List<Csv>();
- // To prevent Runtime Errors
- static int CountBar = 0;
- const int ShortValue = 0; // CSV Short Value
- const int HoldValue = 1; // CSV Hold Value
- const int LongValue = 2; // CSV Long Value
- private string LastDaySeen = "01/01/1970"; // Last day seen, used to avoid the problem to buy more futures in one day
- // CSV PATH
- private string CsvPath = @"D:/PhD-Market-Nets/experiments/050 - Prova GOLD predizioni 5 giorni/predictions/final_decisions/final_decision_prova_small.csv";
- /**
- *
- */
- protected override void Create() {
- ReadCsv(this.CsvPath); // Read the CSV
- // Foreach element of my CSV I create an Operation inside the list of long/short operations
- for (int i=0; i <= Decisions.Count; i++){
- buy_order_long.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Enty] Long " + i.ToString(), EOrderAction.Buy)) );
- sell_order_long.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Exit] Long " + i.ToString(), EOrderAction.Sell, OrderExit.FromEntry(buy_order_long[i]))) );
- sell_order_short.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Enty] Short " + i.ToString(), EOrderAction.SellShort)) );
- buy_order_short.Add( OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "[Exit] Short " + i.ToString(), EOrderAction.BuyToCover, OrderExit.FromEntry(sell_order_short[i]))) );
- }
- }
- // Default
- protected override void StartCalc() {
- CountBar = Bars.CurrentBar;
- }
- /**
- *
- */
- protected override void CalcBar(){
- // Get the current bar date in dd/mm/yyyy format
- string DateBar = Bars.Time[CountBar].ToShortDateString();
- /*
- * 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
- * only when the current day is different from the last visa since the dates are in order, I do not need any further checks.
- */
- if(!this.LastDaySeen.Equals(DateBar)){
- // I take the CSV line corresponding to the current day
- Csv Line = Decisions.Find(x => x.getDateItalian().Equals(DateBar));
- Csv LineExit = Decisions.Find(x => x.getExitDateItalian().Equals(DateBar));
- // Exit condition
- if(LineExit != null){
- if(LineExit.getExitDateItalian() == DateBar){
- switch(LineExit.Decision){
- case LongValue:
- Output.WriteLine("[EXIT] Long date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
- Bars.Time[CountBar].ToShortTimeString() + " con ID: " + LineExit.UniqueID.ToString());
- this.sell_order_long[LineExit.UniqueID].Send(Bars.Open[0], 1); // Chiudo una long
- break;
- case ShortValue:
- Output.WriteLine("[EXIT] Short date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
- Bars.Time[CountBar].ToShortTimeString() + " con ID: " + LineExit.UniqueID.ToString() );
- this.sell_order_short[LineExit.UniqueID].Send(Bars.Open[0], 1); // Chiudo una short
- break;
- } // Switch
- } // If Line.get_exit...
- } // if LineExit != null
- // Entry condition
- if(Line != null){
- switch(Line.Decision){
- case LongValue:
- Output.WriteLine("[ENTRY] Long date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
- Bars.Time[CountBar].ToShortTimeString() + " con ID: " + Line.UniqueID.ToString());
- this.buy_order_long[Line.UniqueID].Send(Bars.Open[0], 1); // Effettuo una long
- break;
- case ShortValue:
- Output.WriteLine("[ENTRY] Short date: " + Bars.Time[CountBar].ToShortDateString() + " at: " +
- Bars.Time[CountBar].ToShortTimeString() + " con ID: " + Line.UniqueID.ToString());
- this.buy_order_short[Line.UniqueID].Send(Bars.Open[0], 1); // Effettuo una short
- break;
- default:
- break;
- }
- } // Line != null
- // Update last day seen
- this.LastDaySeen = DateBar;
- } // if(!this.LastDaySeen.Equals(DateBar))
- }
- //Create Decisions Object by Reading the CSV FILE
- static protected void ReadCsv(string CsvPath){
- using(var reader = new StreamReader(CsvPath)){
- reader.ReadLine();
- while (!reader.EndOfStream){
- var Line = reader.ReadLine();
- Decisions.Add(new Csv(Line));
- }
- }
- }
- } // class PredizioniCET
- /**
- *
- * CSV Class
- */
- public class Csv{
- public string Date { get; set; }
- public int Decision { get; set; }
- public string ExitDate { get; set; }
- public int UniqueID {get; set; }
- public Csv(string line){
- var values = line.Split(',');
- this.Date = values[0]; // yyyy-mm-dd
- this.Decision = Int32.Parse(values[1]);
- this.ExitDate = values[2]; // yyyy-mm-dd
- this.UniqueID = Int32.Parse(values[3]);
- }
- // Get the dates in Italian Format dd/mm/yyyy
- public String getDateItalian () {
- var values = Date.Split('-');
- return values[2] + "/" + values[1] + "/" + values[0];;
- }
- public String getExitDateItalian () {
- var values = ExitDate.Split('-');
- return values[2] + "/" + values[1] + "/" + values[0];;
- }
- } // class Csv
- } // namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement