![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
Hello,
I have Bid data with one price in each line imported in ninjatrader. In strategy I use GetCurrentBid() and it outputs different values like those in historical data manager. And also entry price and exit price in backtesting trades window arent identical to prices in historical data manager. Help me with getting right prices in strategy, please Thanks. Synax |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Hello,
GetCurrentBid() is a real time method. When backtesting, it will substitute the closing price of the bar. If you would like to backtest using the historical bid data in your historical data manager, you will need to Add this data series to your strategy in the Initialize() method: Code:
protected override void Initialize()
{
Add("ES 06-12", PeriodType.Minute, 1, MarketDataType.Bid);
Add("ES 06-12", PeriodType.Minute, 1, MarketDataType.Ask);
}
Code:
protected override void OnBarUpdate()
{
double bidData = BarsArray[1][0];
double askData = BarsArray[2][0];
Print("Bid Price" + bidData);
Print("Ask Price" + askData);
}
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
now its OK, but everything prints and executes twice in row. method OnBarUpdate() is called twice in row i think. can you help me to avoid this double execution ?
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Hello,
Which BarsInProgress are you running the order methods in? If you're not famialir with BIP, please take a look at the following link: http://www.ninjatrader.com/support/h...inprogress.htm Also see the following link for additional information on running multi-bars scripts. If this is not clear, can you please provide me with a sample of your OBU() code that is resulting in the double executions?
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
Please, check the code. I really hope you help me. Thank you very much.
protected override void Initialize() { CalculateOnBarClose = true; Add("$EURUSD", PeriodType.Tick, 1, MarketDataType.Bid); Add("$EURUSD", PeriodType.Tick, 1, MarketDataType.Ask); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { double lastProfit = 0; int tradesCount = myInput0==1?Performance.LongTrades.Count:Performan ce.ShortTrades.Count; if (tradesCount > 0) { // Get the last completed real-time trade (at index 0) Trade lastTrade = myInput0==1?Performance.LongTrades[tradesCount - 1]:Performance.ShortTrades[tradesCount - 1]; // Calculate the PnL for the last completed real-time trade lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity; cash += lastProfit; account += lastProfit; Print(lastTrade.ProfitCurrency +" "+ lastTrade.Quantity); } else { lastProfit = 0; } Print(myInput0+": "+account+": "+cash + " "+lastBet+" ; "+lastProfit); bet = 1; if(myInput0==1) ExitLong(); if(myInput0==2) ExitShort(); if(myInput0==1) EnterLong((int)(bet*10000)); if(myInput0==2) EnterShort((int)(bet*10000)); lastBet = bet; } |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Hello,
When myInput0 == 1 or 2, you are placing an exit order and an entry order at the same time. This is going to place two orders. If you simply want to reverse the position, you can just use EnterLong and EnterShort. You do not need to use the ExitLong/ExitShort to get out of the position, as this will be handled by the EnterLong/EnterShort methods.
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
exitlong means exit from last position. when i removed it only one trade was executed.
|
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Yes, that is true but at the same time you are placing an EnterLong. If you are in a short position and envoke an EnterLong, it should exit the last short position and enter a new long position.
Can you clarify what you are attempting to do with this method. How many orders are you looking to place?
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
on every barupdate i want to exit from last and place new position.
if input is 1 i trade only long with bid prices. if input is 2 i trade only short with ask prices. |
|
|
|
|
|
#10 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
so how can i do that ???
|
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Hello,
From your code, I'm not seeing where you set myInput0 to 2 Are you doing this from the strategy property? Under what condition is myInput0 at 2?
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
when i run backtest in strategy analyzer i run it with myinput0 with value 1 with bid prices and when i run backtest with myinput value 2 i run it with ask prices
|
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Ok, I understand. At any rate, you are still asking the strategy to do twice as much as you need.
Currently it's only looking for MyInput1 == 1, which would submit 2 orders in every instance. You can try using MarketPosition to ensure that your strategy is flat, long, or short before placing an exit order or a entry order. Try something such as: Code:
if(myInput0==1 && Position.MarketPosition == MarketPosition.Long) ExitLong();
if(myInput0==2 && Position.MarketPosition ==MarketPosition.Short) ExitShort();
else if(myInput0==1 && Position.MarketPosition == MarketPosition.Flat) EnterLong((int)(bet*10000));
else if(myInput0==2 && Position.MarketPosition == MarketPosition.Flat) EnterShort((int)(bet*10000));
Matthew
NinjaTrader Customer Service
Last edited by NinjaTrader_Matthew; 04-25-2012 at 11:57 AM.
|
|
|
|
|
|
#14 |
|
Junior Member
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
|
i did that with exitlong and exitshort with that conditions and its not working.
i think whole method is called twice because also printing is outputed twice in row. same values. help me please |
|
|
|
|
|
#15 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
|
Did you end up adding a BarsInProgress condition to your script?
If you only want this to fire on the primary data series, please make sure your script is only working in the context of BIP 0 Code:
protected override void OnBarUpdate()
{
if(BarsInProgress == 0)
{
//strategy logic goes here
}
}
http://www.ninjatrader.com/support/h...nstruments.htm
Matthew
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can a strategy or indicator have more than a single source file? | alex.nt | General Programming | 7 | 11-30-2011 04:11 AM |
| Primary data source | pehon | Connecting | 2 | 07-14-2011 10:38 AM |
| External data source | nicknamed | Connecting | 6 | 08-10-2010 04:05 PM |
| Any way to specify data source? | heech | Connecting | 2 | 03-16-2009 04:29 PM |
| strategy using picks from a different source | j0dan | Strategy Development | 3 | 04-23-2008 01:38 PM |