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
Making the code for buy and sell commands
Collapse
X
-
Making the code for buy and sell commands
I am trying to make a strategy where the code triggers a buy $0.01 below the moving average and sells when it goes back up $0.01.Tags: None
-
Hello Jacobornelas,
Welcome to the NinjaTrader forums!
Below is a link to a forum post with helpful information about getting started with NinjaScript.
As an example:
if (SMA(14)[0] - .01 < GetCurrentAsk())
{
EnterLongLimit(SMA(14)[0] - .01);
}Chelsea B.NinjaTrader Customer Service
-
Thank you so much for helping me with my original question, it worked very well. I do have one more question regarding the quantity that I want the code to buy when entering a long limit. For example, If I buy $100 worth of stock and make 10% on it when I sell it, how do I write a code so that the ninjatrader will know to buy the amount of quantity that I want it to buy with the new amount I received from the previous sale. If i buy $100 worth of stock and make 10% and now have $110, how do I make a code that tells the program to buy the maximum amount as possible with the $110. Then if repeats after that if I were to make another 10% and turn the $110 into $121 and make the program buy the maximum amount of stock as possible with the $121. I know my wording was not the best with this question so if there is any confusion just let me know. Thank you so much!
Comment
-
Hello Jacobornelas,
This would come down to your custom logic and calculations.
You can get information such as the Unrealized PnL from the Position.
The RealizedPnL comes from the Performance collection (which is a TradeCollection collection).
Order fills and executions will cause OnOrderUpdate() and OnExecution() to run.
The quantity can be an int variable that allows you to do a custom calculation. This variable can be supplied to an entry order call.
You could start with an account size, and then add or subtract the pnl of the last trade.
private int myAccountSize = 1;
myAccountSize = AccountSize;
myAccountSize += Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency // <-- your custom calculation to track the account size here
myQuantity = (int)Math.Floor(myAccountSize / Close[0]); // <-- your custom calculation to make a quantity here
EnterLong(myQuantity, "longEntry1");
Below is a link to an example that might give you some ideas.
Chelsea B.NinjaTrader Customer Service
Comment
-
Thank you so much for all of the help. I'm sure that you can tell I am new to all of this coding stuff and C#. I tried my best to implement your sample code lines into my strategy but I'm not sure I did it 100% correctly. I have attached screenshots of my code to this post. Do you think you could look over them please and let me know of any of the mistakes I have made and how I could fix them? Thank you so much and have a wonderful week!Attached Files
Comment
-
Hello Jacobornelas,
Private variables will need to be declared in the scope of the class.
Trying using the Strategy Builder to add variables and take a look at where it puts them in the code. Also, be sure to watch the NinjaScript Editor 401 Training video.Last edited by NinjaTrader_ChelseaB; 05-20-2020, 08:50 AM.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by geddyisodin, 04-25-2024, 05:20 AM
|
8 responses
61 views
0 likes
|
Last Post Yesterday, 10:45 AM | ||
Started by jxs_xrj, 01-12-2020, 09:49 AM
|
4 responses
3,288 views
1 like
|
Last Post
by jgualdronc
Today, 10:02 AM
|
||
Can someone turn this thinkscript into a ninjascript. 5PM CST OHL and Previous Day
by Option Whisperer
Started by Option Whisperer, Today, 09:55 AM
|
0 responses
5 views
0 likes
|
Last Post Today, 09:55 AM | ||
Started by halgo_boulder, 04-20-2024, 08:44 AM
|
2 responses
22 views
0 likes
|
Last Post Today, 09:41 AM | ||
Started by mishhh, 05-25-2010, 08:54 AM
|
19 responses
6,189 views
0 likes
|
Last Post
by rene69851
Today, 09:29 AM
|
Comment