Konu programın kapanması sonucu nasıl bir işlem yapabilirim? ile başlayıp IQ sorununa dönmüş. İki konu hakkında da yazacaklarım olacak
Bu sorun için iki çözüm var.
Biri bir txt dosyasına değerleri tutmak ve sonra program çalıştığında okutmak.
Diğeri verileri db'de tutmak.
Ben ilk olarak txt'de tuttum ve şu an SQL Server'de tutuyorum. IQ güzel bir program fakat donma veya kullanılan symbol'de data keslimesi sorunu sık görüyorum. Bu soruna karşı bu çözümleri buldum. 4-5 farklı pc de sistem çalıştırıyorum. Ryzen, i5, i7 işlemci farketmeksizin bu sorunlarla karşılaşıyorum. Özellikle 1 gün sonra sistem inanılmaz şişiyor. Yani hiç donmasa bile 1-2 gün çalıştıktan sonra ister istemez IQ kapatılıp tekrar çalıştırılması gerekiyor.
Size txt ile çalışan bir yapı paylaşıyorum. Bu robot ilk çalıştığında var olan tüm parametreleri ve değişkenleri kaydedilen dosyadan okur. Dosya çalışan sybol ismi olarak masaüstüne kaydedilir. Yaptığım stratejide pozisyon takibini kendim yaptığımdan o bilgileri de kaydediyorum. Herhangi bir problem kaldığı yerden devam etmesini sağlıyorum.
Yapı olarak hantal fakat iş görür. Kodu kendinize göre uyarlamanız gerekir.
public class IzSuren_Close : MatriksAlgo
{
[SymbolParameter("XBT_USD_BMEX")]
public string Symbol;
[Parameter(SymbolPeriod.Min5)]
public SymbolPeriod SymbolPeriod;
[Parameter(50)]
public decimal BuySellQuantity;
[Parameter(10)]
public int MovPeriod;
[Parameter(19)]
public int DmiPeriod;
[Parameter(40)]
public int DmiUpLevel;
[Parameter(30)]
public int DmiDownLevel;
MOV movDmi;
DemandIndex demandIndex;
[Parameter(true)]
public bool Kar_AL;
[Parameter(4)]
public decimal Kar_AL_Yuzde;
[Parameter(true)]
public bool Zarar_Durdur;
[Parameter(2)]
public decimal Zarar_Durdur_Yuzde;
decimal stock = 0;
int SystemPosition = 0;
public bool DosyadanOku;
public string FolderPath = "C:\\Users\\user\\Desktop\\Pozisyonlar\\DMI";
public string DosyaAdi;
decimal sellPrice = 0;
decimal buyPrice = 0;
decimal karAlFiyat = 0;
decimal shortZararDurdurFiyat = 0;
decimal longZararDurdurFiyat = 0;
public override void OnInit()
{
DosyaAdi = Symbol;
PozisyonOku();
AddSymbol(Symbol, SymbolPeriod);
demandIndex = DemandIndexIndicator(Symbol, SymbolPeriod, DmiPeriod);
movDmi = MOVIndicator(demandIndex, MovPeriod, MovMethod.Exponential);
WorkWithPermanentSignal(true);
SendOrderSequential(false);
}
public override void OnInitComplated()
{
PozisyonYaz();
Debug("########################################");
Debug("Parametreler ve Pozisyonlar :");
Debug("Symbol=" + Symbol);
Debug("SystemPosition=" + SystemPosition);
Debug("BuySellQuantity=" + BuySellQuantity);
Debug("MovPeriod=" + MovPeriod);
Debug("DmiPeriod=" + DmiPeriod);
Debug("DmiUpLevel=" + DmiUpLevel);
Debug("DmiDownLevel=" + DmiDownLevel);
Debug("Kar_AL_Yuzde=" + Kar_AL_Yuzde);
Debug("Zarar_Durdur_Yuzde=" + Zarar_Durdur_Yuzde);
Debug("stock=" + stock);
Debug("sellPrice=" + sellPrice);
Debug("buyPrice=" + buyPrice);
Debug("shortZararDurdurFiyat=" + shortZararDurdurFiyat);
Debug("longZararDurdurFiyat=" + longZararDurdurFiyat);
Debug("#########################################");
}
public override void OnTimer()
{
}
public override void OnNewsReceived(int newsId, List<string> relatedSymbols)
{
}
public override void OnDataUpdate(BarDataCurrentValues barDataCurrentValues)
{
var lastPrice = barDataCurrentValues.LastUpdate.Close;
if (SystemPosition == 1)
{
if (lastPrice>buyPrice)
{
Debug("******************************************************");
Debug("Yeni LongStoploss Noktaları Hesaplanıyor...");
Debug("Önceki Fiyat :" + buyPrice);
Debug("Güncel Fiyat : " + lastPrice);
longZararDurdurFiyat = lastPrice - ((lastPrice * Zarar_Durdur_Yuzde) / 100);
buyPrice = lastPrice;
Debug("Long Pozisyonda Stoploss Fiyatı:" + longZararDurdurFiyat);
Debug("********************************************************");
}
}
if (SystemPosition == -1)
{
if (lastPrice<sellPrice)
{
Debug("******************************************************");
Debug("Yeni ShortStoploss Noktaları Hesaplanıyor...");
Debug("Önceki Fiyat :" + sellPrice);
Debug("Güncel Fiyat : " + lastPrice);
shortZararDurdurFiyat = lastPrice + ((lastPrice * Zarar_Durdur_Yuzde) / 100);
sellPrice = lastPrice;
Debug("Short Pozisyonda Stoploss Fiyatı:" + shortZararDurdurFiyat);
Debug("********************************************************");
}
}
if (Al Koşulu)
{
Debug("CrossAbove");
if (SystemPosition == 0)
{
SystemPosition = 1;
} else if (SystemPosition == -1)
{
SystemPosition = 1;
}
}
if (Sat Koşulu)
{
if (SystemPosition == 0)
{
SystemPosition = -1;
} else if (SystemPosition == 1)
{
SystemPosition = -1;
}
}
if (SystemPosition == 1)
{
if (lastPrice <= longZararDurdurFiyat)
{
Debug("||||||||||||||||||||||||||||||||||||||||");
Debug("LongStoploss tetiklendi : Satış Fiyatı = " + lastPrice);
SendMarketOrder(Symbol, BuySellQuantity, OrderSide.Sell, ChartIcon.PositionClose);
stock = 0;
SystemPosition = 0;
Debug("||||||||||||||||||||||||||||||||||||||||");
}
}
if (SystemPosition == -1)
{
if (lastPrice >= shortZararDurdurFiyat)
{
Debug("||||||||||||||||||||||||||||||||||||||||");
Debug("ShortStoploss tetiklendi : Alış Fiyatı = " + lastPrice);
stock = 0;
SendMarketOrder(Symbol, BuySellQuantity, OrderSide.Buy, ChartIcon.StopLoss);
SystemPosition = 0;
Debug("||||||||||||||||||||||||||||||||||||||||");
}
}
}
public override void OnOrderUpdate(IOrder order)
{
}
public void PozisyonYaz()
{
if (!System.IO.Directory.Exists(FolderPath))
Directory.CreateDirectory(FolderPath);
string fileName = Path.Combine(FolderPath, DosyaAdi + ".txt");
try
{
StreamWriter sw = new StreamWriter(fileName);
sw.WriteLine(Symbol.ToString());
sw.WriteLine(SystemPosition.ToString());
sw.WriteLine(BuySellQuantity.ToString());
sw.WriteLine(MovPeriod.ToString());
sw.WriteLine(DmiPeriod.ToString());
sw.WriteLine(DmiUpLevel.ToString());
sw.WriteLine(DmiDownLevel.ToString());
sw.WriteLine(Kar_AL_Yuzde.ToString());
sw.WriteLine(Zarar_Durdur_Yuzde.ToString());
sw.WriteLine(stock.ToString());
sw.WriteLine(sellPrice.ToString());
sw.WriteLine(buyPrice.ToString());
sw.WriteLine(shortZararDurdurFiyat.ToString());
sw.WriteLine(longZararDurdurFiyat.ToString());
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
Debug(fileName + " dosyasina yazildi");
}
public void PozisyonOku()
{
String line;
int say = 0;
var dosya = new List<object>();
if (!System.IO.Directory.Exists(FolderPath))
Directory.CreateDirectory(FolderPath);
string fileName = Path.Combine(FolderPath, DosyaAdi + ".txt");
try
{
StreamReader sr = new StreamReader(fileName);
while ((line = sr.ReadLine()) != null)
{
dosya.Add(line);
say++;
}
sr.Close();
Symbol = Convert.ToString(dosya.ElementAt(0));
SystemPosition = Convert.ToInt32(dosya.ElementAt(1));
BuySellQuantity = Convert.ToDecimal(dosya.ElementAt(2));
MovPeriod = Convert.ToInt32(dosya.ElementAt(3));
DmiPeriod = Convert.ToInt32(dosya.ElementAt(4));
DmiUpLevel = Convert.ToInt32(dosya.ElementAt(5));
DmiDownLevel = Convert.ToInt32(dosya.ElementAt(6));
Kar_AL_Yuzde = Convert.ToDecimal(dosya.ElementAt(7));
Zarar_Durdur_Yuzde = Convert.ToDecimal(dosya.ElementAt(8));
stock = Convert.ToDecimal(dosya.ElementAt(9));
sellPrice = Convert.ToDecimal(dosya.ElementAt(10));
buyPrice = Convert.ToDecimal(dosya.ElementAt(11));
shortZararDurdurFiyat = Convert.ToDecimal(dosya.ElementAt(12));
longZararDurdurFiyat = Convert.ToDecimal(dosya.ElementAt(13));
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
Debug(fileName + " dosyasindan okundu");
}
}