Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Code to generate variable for Ratio of two stocks in a pairs trade
Collapse
X
-
Code to generate variable for Ratio of two stocks in a pairs trade
I need 2 simultaneous orders to use as close to the same nominal cash value as each other in the morning. I coded my entries but I have no experience coding. The formula would be something like (HigherPricedStock/LowerPriceStock)=Variable, then sell short 1000 of the Higherpricestock and 1000 X Variable of the Lower Price stock so that the cash value of the positions is as close to equal as possible. Does anyone have the basic code that would generate this variable? I want it calculated byt the 9:30 price.Tags: None
-
Hello Vt2009,
Thank you for your post.
You can use principles from the help guide article below to create a multiseries strategy.
Closes is used to reference the last price of a secondary series.
You can get the time stamp of a bar using ToTime(Time[0]). The reference sample below can help with working with DateTime in NinjaScript.
http://www.ninjatrader-support2.com/...ad.php?t=19292
The snippet below will capture the value of the primary series / secondary series if time stamp is 9:30.
Code:if ((ToTime(Time[0]) == 93000)) myVariable = (Closes[0][0] / Closes[1][0]);
Ryan M.NinjaTrader Customer Service
-
That's not how it works. You need to add a secondary series in the initialize method.
Code:Add("SKF", PeriodType.Minute, 1);
Then this secondary series is accessed with index [1] . The current close of this series is Closes[1][0]
http://www.ninjatrader-support.com/HelpGuideV6/MultiTimeFrameInstruments.html
That link is relevant to Multiseries strategies and your question. As Tim suggested in your other thread you can see a sample multi instrument strategy by clicking Tools > Edit NinjaScript > Strategy and viewing the code for Sample Multi Instrument.
Please post your questions only once so that we remain efficient and aren't duplicating efforts.Ryan M.NinjaTrader Customer Service
Comment
-
Thanks
Sorry for posting multiple questions- thanks for all the help:
Would this probably work then?
Add("SKF",PeriodType.Minute, 1)
if ((ToTime(Time[0])==930000))
myVariable= (Closes[1][0]/Closes[0][0])
Since the Primary series is UYG (Closes[0][0]) and I want SKF/UYG to be the variable?
Comment
-
Make sure you have the "Add" statement in the initialize method and include the missing semicolon ;
Then that code should return SKF / UYG.
You can work with print statements and view the output window to verify the results are what you expect.
Print (myVariable);Ryan M.NinjaTrader Customer Service
Comment
-
Placement in Code
This is how I tried to enter this code, could yall help me clean it up a little bit?
privateint shortRatio = 1; // Default setting for ShortRatio
// User defined variables (add any user defined variables below)
protectedoverridevoid Initialize ()
{
Add("skf",PeriodType.Minute, 1)
}
{ if ((ToTime(Time[0]==93000));
myVariable=(Closes[1][0]/Closes[0][0])
}
#endregion
///<summary>
Comment
-
Here's some items I noticed:- Never see variable shortRatio used in code.
- shortRatio is declared as an int so should not be used for prices. Use type double for this.
- Might need to capitalize SKF
- Still missing Semicolon on Add statement
- Semicolon doesn't belong at end of if statement
- Variables are declared in variables region.
- Adding series is done in the Initialize() method.
- Calculation logic should be in OnBarUpdate() method.
Ryan M.NinjaTrader Customer Service
Comment
-
Multi instruments
I'm not able to find correct syntax for BarsSinceEntry () == 0
for multi instruments. Wihout this the strategy works. I put the condition
if (BarsInProgress == 0) but doesnt hels.
Can you please write down how it should be.
BarsSinceEntry (0,,) == 0 Doesnt works.
Thanks
BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by rtwave, 04-12-2024, 09:30 AM
|
4 responses
29 views
0 likes
|
Last Post
by rtwave
Today, 12:50 AM
|
||
Started by yertle, Yesterday, 08:38 AM
|
7 responses
28 views
0 likes
|
Last Post
by yertle
Today, 12:15 AM
|
||
Started by bmartz, 03-12-2024, 06:12 AM
|
2 responses
21 views
0 likes
|
Last Post
by bmartz
Today, 12:11 AM
|
||
Started by funk10101, Today, 12:02 AM
|
0 responses
6 views
0 likes
|
Last Post
by funk10101
Today, 12:02 AM
|
||
Started by gravdigaz6, Yesterday, 11:40 PM
|
1 response
9 views
0 likes
|
Last Post Yesterday, 11:49 PM |
Comment