0 beğenilme 0 beğenilmeme
1,623 kez görüntülendi
Merhaba.

Grid trading yöntemi, ffp de zincir alarmlar sayesinde oldukça başarılı bir şekilde çalışıyor.İstediğimiz gibi ızgaralar arasını açıyoruz ya da kapatıyoruz v.b.  Programı bu kadar kullanışlı hale getiren yetkililere çok teşekkür ederiz.  Ancak bildiğiniz gibi ffp'de stratejileri 1 den fazla sembole uyarlamak mümkün değil.  Bunu yapmak için mutlaka Algo stratejisi kullanmak zorundayız.

Hazır stratejilerdeki kademeli stratejisi, Grid trading yapmamıza imkan verecek gibi görünüyor. Ancak ben şu aşamada grid tradingi açılışla kullanıyorum. İleride indikatörle de kullanabilirim.  Bu sebeple sizden ricam, Aşağıdaki kodu açılış fiyatıyla kullanabileceğim şekle getirmeniz. Yani diyelimki Bist 30 Nisan vade 2504 ile güne başladı, kademeler bunun üstüne ve altına doğru ilerlesin istiyorum.

Kodu , bu forma getirebilirmisiniz?  Bir de kademeler yukarı çıktıkça satış yapsın, aşağı indikçe alış yapsın şeklinde olursa çok iyi olur. FFPde ,zincir alarmlarla bu şekilde robotik olarak alım satım yapabiliyoruz. teşekkürler..

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Matriks.Data.Symbol;

using Matriks.Indicators;

using Matriks.Lean.Algotrader.AlgoBase;

using Matriks.Lean.Algotrader.Models;

using Matriks.Trader.Core;

using Matriks.Trader.Core.Fields;

 

namespace Matriks.Lean.Algotrader

{

    public class MultiLevelLimitOrders : MatriksAlgo

    {

        // Strateji çalıştırılırken kullanacağımız parametreler. Eğer sembolle ilgili bir parametre ise,

        // "SymbolParameter" ile, değilse "Parameter" ile tanımlama yaparız. Parantez içindeki değerler default değerleridir.

 

        [SymbolParameter("GARAN")]

        public string Symbol;

        [Parameter(SymbolPeriod.Min)]

        public SymbolPeriod SymbolPeriod;

        [Parameter(0)]

        public int CurrentStock;    //Algo calistirilmadan once portfolio'da bulunan mevcut stok buraya yazilabilir

                                    //Bu sayede acilista negatif sinyal olmasi durumunda stoktan satis yapilabilir

        [Parameter(2)]

        public int Quantity;

        [Parameter(1)]

        public int LevelQuantity; //The quantity to use when strategy starts adding to position

        [Parameter(4)]

        public int Levels; //Strategy will keep adding to position this many times

        [Parameter(1)]

        public decimal Percentage; //The percent amount from the buy order the strategy is allowed to buy

                                   //ie. if our initial buy is from 100, and we set percentage to 1, Levels=4 and LevelQuantity=50

                                   //the strategy will buy from 100 until 99, 50 shares at every 0.25 price movement

        [Parameter(2)]

        public int RoundTO;

 

        MOST most;

         

        public override void OnInit()

        {

            most = MOSTIndicator(Symbol, SymbolPeriod, OHLCType.Close, 3, 2, MovMethod.Exponential);

 

            AddSymbol(Symbol, SymbolPeriod);

            SetTimerInterval(5);

 

            //Eger backtestte emri bir al bir sat seklinde gonderilmesi isteniyor bu true set edilir.

            //Alttaki satırı silerek veya false geçerek emirlerin sirayla gönderilmesini engelleyebilirsiniz.

            SendOrderSequential(false);

            WorkWithPermanentSignal(false);

        }

 

        /// <summary>

        /// Init islemleri tamamlaninca, bardatalar kullanmaya hazir hale gelince bu fonksiyon tetiklenir. Data uzerinde bir defa yapilacak islemler icin kullanilir

        /// </summary>

        public override void OnInitCompleted()

        {

 

        }

 

        decimal TargetLevel = 0;

        decimal LastBuyOrderPrice = 0;

        decimal LastSellOrderPrice = 0;

        string LastBuyOrderID = "";

        string LastSellOrderID = "";

 

        List<decimal> buyprice = new List<decimal>();

        List<decimal> sellprice = new List<decimal>();

        List<string> buyOrderID = new List<string>();

        List<string> sellOrderID = new List<string>();

        int BuyFlag = 0, SellFlag = 0, SellAllQuantity;

 

        Dictionary<string, Dictionary<string, IOrder>> orders = new Dictionary<string, Dictionary<string, IOrder>>();

        string lastBUYorderid = "NULL";

        string lastSELLorderid = "NULL";

        int realposition = 0;

        decimal inAlgoQuantity = 0;

 

        /// <summary>

        /// Eklenen sembollerin bardata'ları ve indikatorler güncellendikçe bu fonksiyon tetiklenir.

        /// </summary>

        /// <param name="barData">Bardata ve hesaplanan gerçekleşen işleme ait detaylar</param>

        public override void OnDataUpdate(BarDataCurrentValues barDataCurrentValues)

        {

            var lastUpdate = barDataCurrentValues.LastUpdate;

            var close = barDataCurrentValues.LastUpdate.Close;

            var pct = Percentage / 100;

            IDictionary<string, decimal> portfolio = GetPortfolio();

            var positionflag = portfolio.ContainsKey(Symbol) && portfolio[Symbol] != 0;

 

            if (positionflag)

            {

                inAlgoQuantity = portfolio[Symbol];

            }

 

            if (most.ExMOV.CurrentValue > most.CurrentValue && BuyFlag == 0)

            {

                Debug("Most.ExMov: " + Math.Round(most.ExMOV.CurrentValue, 2) + " is ABOVE Most: " + Math.Round(most.CurrentValue, 2));

                if (SellFlag == 0) //daha once satis yapilmadiysa

                {

                    SendMarketOrder(Symbol, Quantity, (OrderSide.Buy));

                    LastBuyOrderPrice = close + 0.01m;

                    Debug("Buy order sent @" + LastBuyOrderPrice); //ASSUMED! fill @ close+0.01

 

                    TargetLevel = LastBuyOrderPrice * (1 - pct);

                    decimal division = (LastBuyOrderPrice - TargetLevel) / Levels;

 

                    for (int i = 0; i < Levels; i++)

                    {

                        decimal newprice = Math.Round((LastBuyOrderPrice - division * (i + 1)), RoundTO);

                        LastBuyOrderID = SendLimitOrder(Symbol, LevelQuantity, OrderSide.Buy, newprice);

                        buyprice.Add(newprice);

                        buyOrderID.Add(LastBuyOrderID);

                        Debug("Sent BUY limit order at level " + (i + 1) + " with price = " + newprice);

                    }

                }

                if (SellFlag == 1) //satistan donulduyse

                {

                    for (int i = 0; i < Levels; i++) //list index i=0, levels index i+1=1

                    {

                        SendCancelOrder(sellOrderID[i]);

                        Debug("Cancelled SELL limit order at level " + (i + 1) + " with price = " + sellprice[i]);

                    }

                    sellOrderID.Clear();

                    sellprice.Clear();

                    SendMarketOrder(Symbol, Quantity, (OrderSide.Buy));

                    LastBuyOrderPrice = close + 0.01m;

                    Debug("Buy order sent @" + LastBuyOrderPrice); //ASSUMED! fill @ close+0.01

                    TargetLevel = LastBuyOrderPrice * (1 - pct);

                    decimal division = (LastBuyOrderPrice - TargetLevel) / Levels;

 

                    for (int i = 0; i < Levels; i++)

                    {

                        decimal newprice = Math.Round((LastBuyOrderPrice - division * (i + 1)), RoundTO);

                        buyprice.Add(newprice);

                        LastBuyOrderID = SendLimitOrder(Symbol, LevelQuantity, OrderSide.Buy, newprice);

                        buyOrderID.Add(LastBuyOrderID);

                        Debug("Sent BUY limit order at level " + (i + 1) + " with price = " + newprice);

                    }

                    SellFlag = 0;

                }

                BuyFlag = 1;

            }

            if (most.ExMOV.CurrentValue < most.CurrentValue && SellFlag == 0)

            {

                Debug("Most.ExMov: " + Math.Round(most.ExMOV.CurrentValue, 2) + " is BELOW Most: " + Math.Round(most.CurrentValue, 2));

                if (BuyFlag == 0)

                {

                    if (CurrentStock + inAlgoQuantity >= Quantity)

                    {

                        SendMarketOrder(Symbol, Quantity, (OrderSide.Sell));

                        LastSellOrderPrice = close;

                        Debug("SELL order sent @" + LastSellOrderPrice); //ASSUMED! fill @ close

                        TargetLevel = LastSellOrderPrice * (1 + pct);

                        decimal division = (TargetLevel - LastSellOrderPrice) / Levels;

                        for (int i = 0; i < Levels; i++)

                        {

                            decimal newprice = Math.Round((LastSellOrderPrice + division * (i + 1)), RoundTO);

                            sellprice.Add(newprice);

                            LastSellOrderID = SendLimitOrder(Symbol, LevelQuantity, OrderSide.Sell, newprice);

                            sellOrderID.Add(LastSellOrderID);

                            Debug("Sent limit SELL order at level " + (i + 1) + " with price = " + newprice);

                        }

                        SellFlag = 1;

                    }

                    if (CurrentStock + inAlgoQuantity < Quantity) Debug("Satis durumu gerceklesti. Stok olmadigi icin emir gonderilmedi.");

                }

                if (BuyFlag == 1)

                {

                    for (int i = 0; i < Levels; i++) //list index i=0, levels index i+1=1

                    {

                        SendCancelOrder(buyOrderID[i]);

                        Debug("Cancelled BUY limit order at level " + (i + 1) + " with price = " + buyprice[i]);

                    }

                    buyOrderID.Clear();

                    buyprice.Clear();

 

                    SellAllQuantity = Quantity + (realposition - 1) * LevelQuantity;

                    SendMarketOrder(Symbol, SellAllQuantity, (OrderSide.Sell));

                    LastSellOrderPrice = close;

                    Debug("SELL order sent. Exited LONG position");

                    TargetLevel = LastSellOrderPrice * (1 + pct);

                    decimal division = (TargetLevel - LastSellOrderPrice) / Levels;

 

                    if (CurrentStock + inAlgoQuantity >= Quantity)

                    {

                        Debug("SELL Signal received. Stock available = " + (CurrentStock + inAlgoQuantity) + ". Sell order will be sent.");

                        for (int i = 0; i < Levels; i++)

                        {

                            decimal newprice = Math.Round((LastSellOrderPrice + division * (i + 1)), RoundTO);

                            sellprice.Add(newprice);

                            LastSellOrderID = SendLimitOrder(Symbol, LevelQuantity, OrderSide.Sell, newprice);

                            sellOrderID.Add(LastSellOrderID);

                            Debug("Sent limit SELL order at level " + (i + 1) + " with price = " + newprice);

                        }

                        SellFlag = 1;

                    }

                    BuyFlag = 0;

                }

            }

        }

 

        /// <summary>

        /// Gönderilen emirlerin son durumu değiştikçe bu fonksiyon tetiklenir.

        /// </summary>

        /// <param name="barData">Emrin son durumu</param>

        public override void OnOrderUpdate(IOrder order)

        {

            if (!orders.ContainsKey(order.Symbol)) orders.Add(order.Symbol, new Dictionary<string, IOrder>());

            orders[order.Symbol][order.CliOrdID] = order;

 

            if (order.OrdStatus.Obj == OrdStatus.Filled && order.Side.Obj == Side.Buy)

            {

                LastBuyOrderPrice = order.LastPx;

                Debug("Buy Order filled @ " + LastBuyOrderPrice);

                realp
Algoritmik Trading kategorisinde (184 puan) tarafından | 1,623 kez görüntülendi
0 0
bunu prime için de yayınlarmısınız.

1 cevap

0 beğenilme 0 beğenilmeme
merhaba,

bu tür formüller Prime tarafına çevrilememektedir,

bilgilerinize
(40,139 puan) tarafından
7,509 soru
7,511 cevap
4,405 yorum
8,716 kullanıcı