SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Aug 15, 2020 2:17 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Jul 07, 2020 4:28 am 
Offline

Joined: Wed Jul 01, 2020 7:19 am
Posts: 14
I am using exact same code as SMACrossOver with TakeProfit&StopLoss for the TP and SL for my strategy, somehow the TakeProfit works fine but StopLoss doesn't work correctly as it is.
The StopLoss executed at better price and killing the opportunity of the TakeProfit.
Anton Could you help me to solve?
Regards,

Code:
      protected override void OnOrderFilled(Order order)
      {
         if (order == Entry)
         {
            double DailyATRTakeProfitSize = NormalizePrice(DailyATR[DailyATR.Count - 1] * (double)DailyATRTakeProfitPercent / 100, Instrument.PriceFormat);
            double DailyATRStopLossSize = NormalizePrice(DailyATR[DailyATR.Count - 1] * (double)DailyATRStopLossPercent / 100, Instrument.PriceFormat);
            
            // Send take profit and stop loss orders.
            if (Position.Side == PositionSide.Long)
            {
               // Calculate prices.
               double takeProfitPrice = NormalizePrice(Position.EntryPrice + DailyATRTakeProfitSize,Instrument.PriceFormat);
               double stopLossPrice = NormalizePrice(Position.EntryPrice - DailyATRStopLossSize,Instrument.PriceFormat);
               
                    Console.WriteLine(Clock.DateTime+"    "+takeProfitPrice+"   "+stopLossPrice);

               // Create orders.
               TakeProfit = SellLimitOrder(Instrument, Qty, takeProfitPrice, "TakeProfit "+ Position.EntryDate);
               StopLoss = SellStopOrder(Instrument, Qty, stopLossPrice, "StopLoss"+ Position.EntryDate);

               // Send orders.
               Send(StopLoss);
               Send(TakeProfit);
            }
            else
            {
               // Calculate prices.
               double takeProfitPrice = NormalizePrice(Position.EntryPrice - DailyATRTakeProfitSize,Instrument.PriceFormat);
               double stopLossPrice = NormalizePrice(Position.EntryPrice + DailyATRStopLossSize,Instrument.PriceFormat);
               
               Console.WriteLine(Clock.DateTime+"    "+takeProfitPrice+"   "+stopLossPrice);

               // Create orders.
               TakeProfit = BuyLimitOrder(Instrument, Qty, takeProfitPrice, "TakeProfit "+ Position.EntryDate);
               StopLoss = BuyStopOrder(Instrument, Qty, stopLossPrice, "StopLoss "+ Position.EntryDate);

               // Send orders.
               Send(StopLoss);
               Send(TakeProfit);
            }
         }
         else if (order == StopLoss)
         {
            // Cancel take profit order.
            if (!TakeProfit.IsDone)
               Cancel(TakeProfit);
         }
         else if (order == TakeProfit)
         {
            // Cancel stop loss order.
            if (!StopLoss.IsDone)
               Cancel(StopLoss);
         }
      }


Attachments:
Screenshot 2020-07-07 at 9.25.51 AM.png
Screenshot 2020-07-07 at 9.25.51 AM.png [ 193.19 KiB | Viewed 116 times ]
Top
 Profile  
 
PostPosted: Tue Jul 07, 2020 10:04 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 577
Hello,
ExecutionSimulator has settings for executions. For your case, you can set FillAtStopPrice = true and then stop will be executed right at this price.
These settings can be found in the Providers window.


Top
 Profile  
 
PostPosted: Tue Jul 07, 2020 10:14 am 
Offline

Joined: Wed Jul 01, 2020 7:19 am
Posts: 14
Hi Thanks for the information, skuvv

I'm using Backtest to run this, and my scenario file looks like this.
Somehow I added the code you mentioned me, it seems it has something wrong.

Code:
using SmartQuant;
using System;

namespace OpenQuant
{
    public partial class MyScenario : Scenario
    {
        public MyScenario(Framework framework)
            : base(framework)
        {
        }

        public override void Run()
        {
            Instrument instrument = InstrumentManager.Instruments["EURUSD.FXCM"];

            strategy = new MyStrategy(framework, "DailyATRPercentMomentum");

            strategy.AddInstrument(instrument);

            DateTime DateTime1 = new DateTime(2010, 01, 01);
            DateTime DateTime2 = new DateTime(2020, 01, 01);

            BarSeries Series1 = DataManager.GetHistoricalBars(instrument, DateTime1, DateTime2, BarType.Time, 3600);
            BarSeries Series2 = DataManager.GetHistoricalBars(instrument, DateTime1, DateTime2, BarType.Time, 86400);

            DataSimulator.Series.Add(Series1);
            DataSimulator.Series.Add(Series2);

            DataSimulator.SubscribeAll = false;
            ExecutionSimulator.FillOnBar = true;
       ExecutionSimulator.FillAtStopPrice = true;
         
            StartStrategy();
        }
    }
}




Is it because I am using the GetHistoricalBars?


Attachments:
Screenshot 2020-07-07 at 3.10.43 PM.png
Screenshot 2020-07-07 at 3.10.43 PM.png [ 26.35 KiB | Viewed 112 times ]
Top
 Profile  
 
PostPosted: Tue Jul 07, 2020 11:16 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 577
You are using daily bars (size = 86400) and ExecutionSimulator.FillOnBar.
It triggered the execution of your stop order and stop order was executed at stop price.

Tips:
1)You can avoid using daily bars.
2)You can fill orders on tick market data.
3)You can specify BarType/BarSize which will be used for executions:
Code:
DataSimulator.BarFilter.Add(BarType.Time, 3600);


Top
 Profile  
 
PostPosted: Tue Jul 07, 2020 1:47 pm 
Offline

Joined: Wed Jul 01, 2020 7:19 am
Posts: 14
Hello thank you for your information.
Following the FAQ, to work with multi timeframe, I added the DailyBar and assign the BarSeires at OnBar Like this

Code:
            if (bar.Size == 86400)
            { DailyBarSeries.Add(bar); return; }


But Let me try avoiding the BarSize of 86400 to test. Thank you.


Top
 Profile  
 
PostPosted: Tue Jul 07, 2020 5:48 pm 
Offline

Joined: Wed Jul 01, 2020 7:19 am
Posts: 14
Seems it is working correctly now, but working with FillOnBar and multi timeframe without tick data will give some bug, any way thanks a lot!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group