not sure what is wrong with this script, i use it as indicator script, but it spits out data 5 times per second in my log file, the closing price is not in line with the closing price candle , not sure where the data is coming from. I used the same core part of the script as insurance script and the data was fine, i would get one log per minute>
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TradeServer.ScriptingDriver.DataObjects;
using TradeServer.ScriptingDriver.Interfaces;
namespace ExampleNameSpace
{
public class hllPrcPriceFall : IIndicatorScript
{
// hll from SimpleMovingAverage script
private int neededHistorryTicks = 1; // default field, used to store the needed depth
private int longLength = 35;
private int shortLength = 12;
private double ma_short = 0;
private double ma_long = 0;
// hll
// private double targetPriceChange = 10;
private double targetPriceFallPrc = 2;
private double LastCyclePrice = 0;
private double MaxPrice = 0;
private bool BuyOK = false;
// Properties
public string Name
{
get { return "hllPrcPriceFall"; }
}
public int TicksBack
{
get { return 1; }
}
public List<ScriptParameter> GetParameters()
{
List<ScriptParameter> res = new List<ScriptParameter>();
res.Add(new ScriptParameter("Change %", ScriptParameterType.Decimal, targetPriceFallPrc.ToString()));
return res;
}
public void SetParameters(Dictionary<string, object> parameters)
{
foreach (var item in parameters) // lets just loop them all
{
if (item.Key == "Change") { targetPriceFallPrc = Convert.ToDouble(item.Value); }
}
}
// Chart lines
public List<ChartRecord> GetChartLines()
{
List<ChartRecord> chartLines = new List<ChartRecord>();
chartLines.Add(new ChartRecord() { ChartIndex = 0, Name = "Short", HexLineColor = "#FF00FF" });
chartLines.Add(new ChartRecord() { ChartIndex = 0, Name = "Long", HexLineColor = "#00FFFF" });
return chartLines;
}
public List<decimal> GetChartData()
{
List<decimal> chartData = new List<decimal>();
chartData.Add(Convert.ToDecimal(ma_short));
chartData.Add(Convert.ToDecimal(ma_long));
// context.Logger.LogToFile(string.Format("Called short {0} and long {1}", ma_short, ma_long));
return chartData;
}
public void Init()
{
// do nothing
}
// This is the brain so the say, this is where the calculation is done and an output must be produced.
public IndicatorResult GetResult(IndicatorContext context)
{
// debug text
context.Logger.LogToFile("*****************************************************");
context.Logger.LogToFile("Current Date&Ttime= " + DateTime.Now.ToString("dd.MM h:mm:ss"));
context.Logger.LogToFile("PriceTick.Close= " + context.PriceTick.Close);
context.Logger.LogToFile("LastBuy= " + (double)context.LastBuyPrice);
context.Logger.LogToFile("LastBuy-2%= " + ((double)context.LastBuyPrice - (targetPriceFallPrc/100)*(double)context.LastBuyPrice));
context.Logger.LogToFile("LastCyclePrice= " + LastCyclePrice);
context.Logger.LogToFile("MaxPrice= " + MaxPrice);
if (context.PriceTick.Close < ((double)context.LastBuyPrice - (targetPriceFallPrc/100)*(double)context.LastBuyPrice)) // check if price below last buy by 2%
{
context.Logger.LogToFile(string.Format("Price < LastBuyPrice - 2%"));
BuyOK = true;
}
else
{
context.Logger.LogToFile(string.Format("Price > LastBuyPrice - 2%"));
if (context.PriceTick.Close > (LastCyclePrice)) // check if price greater than last cycle price
{
if (context.PriceTick.Close > (MaxPrice)) // check if price greater than MaxPrice
{
MaxPrice = context.PriceTick.Close;
context.Logger.LogToFile("MaxPrice= " + (MaxPrice));
LastCyclePrice = context.PriceTick.Close;
BuyOK = false;
context.Logger.LogToFile("Price>LastCyclePrice and Price>MaxPrice");
}
else
{
LastCyclePrice = context.PriceTick.Close;
BuyOK = false;
context.Logger.LogToFile("Price>LastCyclePrice and Price<MaxPrice");
}
}
else
{
if (context.PriceTick.Close < ((MaxPrice) - (targetPriceFallPrc/100)*(MaxPrice) )) // check if price smaller than MaxPrice -20%
{
context.Logger.LogToFile(string.Format("Price < MaxPrice - 2%"));
BuyOK = true;
}
else
{
context.Logger.LogToFile(string.Format("Price > MaxPrice - 2%"));
LastCyclePrice = context.PriceTick.Close;
BuyOK = false;
}
}
}
context.Logger.LogToFile("BuyOK= " + BuyOK.ToString());
try
{
if (BuyOK == false)
return IndicatorResult.SellShort;
else
return IndicatorResult.BuyLong;
}
catch (Exception ex) // This try catch block is an highly recommend practice
{
context.Logger.LogToFile(string.Format("Exception detected: {0}", ex.Message));
}
return IndicatorResult.StayNoPosition; // Default output
}
}
}